Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attrDef.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """
  2. """
  3. # Created on 2014.01.11
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2014 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. from os import linesep
  25. from .. import SEQUENCE_TYPES
  26. from ..core.exceptions import LDAPKeyError
  27. from ..utils.log import log, log_enabled, ERROR, BASIC, PROTOCOL, EXTENDED
  28. class AttrDef(object):
  29. """Hold the definition of an attribute
  30. :param name: the real attribute name
  31. :type name: string
  32. :param key: the friendly name to use in queries and when accessing the attribute, default to the real attribute name
  33. :type key: string
  34. :param validate: called to check if the value in the query is valid, the callable is called with the value parameter
  35. :type validate: callable
  36. :param pre_query: called to transform values returned by search
  37. :type pre_query: callable
  38. :param post_query: called to transform values returned by search
  39. :type post_query: callable
  40. :param default: value returned when the attribute is absent (defaults to NotImplemented to allow use of None as default)
  41. :type default: string, integer
  42. :param dereference_dn: reference to an ObjectDef instance. When the attribute value contains a dn it will be searched and substituted in the entry
  43. :type dereference_dn: ObjectDef
  44. :param description: custom attribute description
  45. :type description: string
  46. :param mandatory: specify if attribute is defined as mandatory in LDAP schema
  47. :type mandatory: boolean
  48. """
  49. def __init__(self, name, key=None, validate=None, pre_query=None, post_query=None, default=NotImplemented, dereference_dn=None, description=None, mandatory=False, single_value=None, alias=None):
  50. self.name = name
  51. self.key = ''.join(key.split()) if key else name # key set to name if not present
  52. self.validate = validate
  53. self.pre_query = pre_query
  54. self.post_query = post_query
  55. self.default = default
  56. self.dereference_dn = dereference_dn
  57. self.description = description
  58. self.mandatory = mandatory
  59. self.single_value = single_value
  60. self.oid_info = None
  61. if not alias:
  62. self.other_names = None
  63. elif isinstance(alias, SEQUENCE_TYPES): # multiple aliases
  64. self.\
  65. other_names = set(alias)
  66. else: # single alias
  67. self.other_names = set([alias]) # python 2 compatibility
  68. if log_enabled(BASIC):
  69. log(BASIC, 'instantiated AttrDef: <%r>', self)
  70. def __repr__(self):
  71. r = 'ATTR: ' + ', '.join([self.key] + list(self.other_names)) if self.other_names else self.key
  72. r += '' if self.name == self.key else ' [' + self.name + ']'
  73. r += '' if self.default is NotImplemented else ' - default: ' + str(self.default)
  74. r += '' if self.mandatory is None else ' - mandatory: ' + str(self.mandatory)
  75. r += '' if self.single_value is None else ' - single_value: ' + str(self.single_value)
  76. r += '' if not self.dereference_dn else ' - dereference_dn: ' + str(self.dereference_dn)
  77. r += '' if not self.description else ' - description: ' + str(self.description)
  78. if self.oid_info:
  79. for line in str(self.oid_info).split(linesep):
  80. r += linesep + ' ' + line
  81. return r
  82. def __str__(self):
  83. return self.__repr__()
  84. def __eq__(self, other):
  85. if isinstance(other, AttrDef):
  86. return self.key == other.key
  87. return False
  88. def __lt__(self, other):
  89. if isinstance(other, AttrDef):
  90. return self.key < other.key
  91. return False
  92. def __hash__(self):
  93. if self.key:
  94. return hash(self.key)
  95. else:
  96. return id(self) # unique for each instance
  97. def __setattr__(self, key, value):
  98. if hasattr(self, 'key') and key == 'key': # key cannot be changed because is being used for __hash__
  99. error_message = 'key \'%s\' already set' % key
  100. if log_enabled(ERROR):
  101. log(ERROR, '%s for <%s>', error_message, self)
  102. raise LDAPKeyError(error_message)
  103. else:
  104. object.__setattr__(self, key, value)