<commit_before>##
# Copyright (c) 2006-2013 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Directory service interface.
"""

__all__ = [
    "DirectoryServiceError",
    "QueryNotSupportedError",
    "FieldName",
    "MatchType",
    "Operand",
    "QueryFlags",
    "DirectoryQueryMatchExpression",
    "IDirectoryService",
    "IDirectoryRecord",
]

from zope.interface import Attribute, Interface

from twisted.python.constants import Names, NamedConstant
from twisted.python.constants import Flags, FlagConstant



##
# Exceptions
##

class DirectoryServiceError(RuntimeError):
    """
    Directory service generic error.
    """

class QueryNotSupportedError(DirectoryServiceError):
    """
    Query not supported.
    """



##
# Data Types
##

class RecordType(Names):
    user  = NamedConstant()
    group = NamedConstant()

class FieldName(Names):
    """
    Constants for common field names.
    """
    recordType     = NamedConstant()
    shortNames     = NamedConstant()
    uid            = NamedConstant()
    guid           = NamedConstant()
    emailAddresses = NamedConstant()

    shortNames.multiValue     = True
    emailAddresses.multiValue = True

    @classmethod
    def isMultiValue(cls, name):
        return hasattr(name, "multiValue") and name.multiValue

class MatchType(Names):
    """
    Query match types.
    """
    equals     = NamedConstant()
    startsWith = NamedConstant()
    contains   = NamedConstant()

class Operand(Names):
    OR  = NamedConstant()
    AND = NamedConstant()

class QueryFlags(Flags):
    """
    Query flags.
    """
    caseInsensitive = FlagConstant()

class DirectoryQueryMatchExpression(object):
    """
    Directory query.

    @ivar fieldName: a L{FieldName}
    @ivar fieldValue: a text value to match
    @ivar matchType: a L{MatchType}
    @ivar flags: l{QueryFlags}
    """

    def __init__(self, fieldName, fieldValue, matchType=MatchType.equals, flags=None):
        self.fieldName  = fieldName
        self.fieldValue = fieldValue
        self.matchType  = matchType
        self.flags      = flags



##
# Interfaces
##

class IDirectoryService(Interface):
    """
    Directory service.
    """
    realmName = Attribute("The name of the authentication realm this service represents.")

    def recordTypes():
        """
        @return: a deferred iterable of strings denoting the record
            types that are kept in the directory.  For example:
            C{("users", "groups", "resources")}.
        """

    def recordFromQuery(expressions, operand=Operand.AND):
        """
        @return: a deferred iterable of matching L{IDirectoryRecord}s.
        @raises: L{QueryNotSupportedError} is the query is not
            supported by this directory service.
        """



class IDirectoryRecord(Interface):
    """
    Directory record.
    """
    service = Attribute("The L{IDirectoryService} this record exists in.")
    fields  = Attribute("A dictionary with L{FieldName} keys and text values.")<commit_msg>Note attribute access in docstring.<commit_after>##
# Copyright (c) 2006-2013 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Directory service interface.
"""

__all__ = [
    "DirectoryServiceError",
    "QueryNotSupportedError",
    "FieldName",
    "MatchType",
    "Operand",
    "QueryFlags",
    "DirectoryQueryMatchExpression",
    "IDirectoryService",
    "IDirectoryRecord",
]

from zope.interface import Attribute, Interface

from twisted.python.constants import Names, NamedConstant
from twisted.python.constants import Flags, FlagConstant



##
# Exceptions
##

class DirectoryServiceError(RuntimeError):
    """
    Directory service generic error.
    """

class QueryNotSupportedError(DirectoryServiceError):
    """
    Query not supported.
    """



##
# Data Types
##

class RecordType(Names):
    user  = NamedConstant()
    group = NamedConstant()

class FieldName(Names):
    """
    Constants for common field names.
    """
    recordType     = NamedConstant()
    shortNames     = NamedConstant()
    uid            = NamedConstant()
    guid           = NamedConstant()
    emailAddresses = NamedConstant()

    shortNames.multiValue     = True
    emailAddresses.multiValue = True

    @classmethod
    def isMultiValue(cls, name):
        return hasattr(name, "multiValue") and name.multiValue

class MatchType(Names):
    """
    Query match types.
    """
    equals     = NamedConstant()
    startsWith = NamedConstant()
    contains   = NamedConstant()

class Operand(Names):
    OR  = NamedConstant()
    AND = NamedConstant()

class QueryFlags(Flags):
    """
    Query flags.
    """
    caseInsensitive = FlagConstant()

class DirectoryQueryMatchExpression(object):
    """
    Directory query.

    @ivar fieldName: a L{FieldName}
    @ivar fieldValue: a text value to match
    @ivar matchType: a L{MatchType}
    @ivar flags: l{QueryFlags}
    """

    def __init__(self, fieldName, fieldValue, matchType=MatchType.equals, flags=None):
        self.fieldName  = fieldName
        self.fieldValue = fieldValue
        self.matchType  = matchType
        self.flags      = flags



##
# Interfaces
##

class IDirectoryService(Interface):
    """
    Directory service.
    """
    realmName = Attribute("The name of the authentication realm this service represents.")

    def recordTypes():
        """
        @return: a deferred iterable of strings denoting the record
            types that are kept in the directory.  For example:
            C{("users", "groups", "resources")}.
        """

    def recordFromQuery(expressions, operand=Operand.AND):
        """
        @return: a deferred iterable of matching L{IDirectoryRecord}s.
        @raises: L{QueryNotSupportedError} is the query is not
            supported by this directory service.
        """



class IDirectoryRecord(Interface):
    """
    Directory record.

    Fields may also be accessed as attributes of L{IDirectoryRecord} objects.
    """
    service = Attribute("The L{IDirectoryService} this record exists in.")
    fields  = Attribute("A dictionary with L{FieldName} keys and text values.")
{'fraction_non_alphanumeric': 0.09823292055099485, 'fraction_numerical': 0.0033393627382774455, 'mean_word_length': 3.074829931972789, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2097397', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1937, 'n_words': 621}