Skip to content

Commit

Permalink
optimized conf parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cannatag committed Apr 30, 2017
1 parent 397a01a commit 25e56e5
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 71 deletions.
7 changes: 4 additions & 3 deletions ldap3/abstract/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def _get_attributes(self, response, attr_defs, entry):
If the 'dereference_dn' in AttrDef is a ObjectDef then the attribute values are treated as distinguished name and the relevant entry is retrieved and stored in the attribute value.
"""
conf_operational_attribute_prefix = get_config_parameter('ABSTRACTION_OPERATIONAL_ATTRIBUTE_PREFIX')
attributes = CaseInsensitiveWithAliasDict()
used_attribute_names = set()
for attr_def in attr_defs:
Expand Down Expand Up @@ -217,11 +218,11 @@ def _get_attributes(self, response, attr_defs, entry):
if attribute_name not in used_attribute_names:
if attribute_name not in attr_defs:
raise LDAPCursorError('attribute \'%s\' not in object class \'%s\' for entry %s' % (attribute_name, ', '.join(entry.entry_definition._object_class), entry.entry_dn))
attribute = OperationalAttribute(AttrDef(get_config_parameter('ABSTRACTION_OPERATIONAL_ATTRIBUTE_PREFIX') + attribute_name), entry, self)
attribute = OperationalAttribute(AttrDef(conf_operational_attribute_prefix + attribute_name), entry, self)
attribute.raw_values = response['raw_attributes'][attribute_name]
attribute.values = response['attributes'][attribute_name]
if (get_config_parameter('ABSTRACTION_OPERATIONAL_ATTRIBUTE_PREFIX') + attribute_name) not in attributes:
attributes[get_config_parameter('ABSTRACTION_OPERATIONAL_ATTRIBUTE_PREFIX') + attribute_name] = attribute
if (conf_operational_attribute_prefix + attribute_name) not in attributes:
attributes[conf_operational_attribute_prefix + attribute_name] = attribute

return attributes

Expand Down
4 changes: 2 additions & 2 deletions ldap3/abstract/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __str__(self):
return self.__repr__()

def set_status(self, status):
conf_ignored_mandatory_attributes_in_object_def = get_config_parameter('IGNORED_MANDATORY_ATTRIBUTES_IN_OBJECT_DEF')
if status not in STATUSES:
raise LDAPCursorError('invalid entry status ' + str(status))

Expand All @@ -97,7 +98,7 @@ def set_status(self, status):
self._initial_status = STATUS_WRITABLE
if self.status == STATUS_VIRTUAL or (self.status == STATUS_PENDING_CHANGES and self._initial_status == STATUS_VIRTUAL): # checks if all mandatory attributes are present in new entries
for attr in self.definition._attributes:
if self.definition._attributes[attr].mandatory and attr not in get_config_parameter('IGNORED_MANDATORY_ATTRIBUTES_IN_OBJECT_DEF'):
if self.definition._attributes[attr].mandatory and attr not in conf_ignored_mandatory_attributes_in_object_def:
if (attr not in self.attributes or self.attributes[attr].virtual) and attr not in self.changes:
self.status = STATUS_MANDATORY_MISSING
break
Expand Down Expand Up @@ -520,7 +521,6 @@ def entry_commit_changes(self, refresh=True, controls=None, clear_history=True):
if not self.entry_cursor.connection.strategy.sync: # async request
response, result, request = self.entry_cursor.connection.get_response(result, get_request=True)
else:
result = self.entry_cursor.connection.result
response = self.entry_cursor.connection.response
result = self.entry_cursor.connection.result
request = self.entry_cursor.connection.request
Expand Down
3 changes: 2 additions & 1 deletion ldap3/abstract/objectDef.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ def add_attribute(self, definition=None):
:param definition: the AttrDef object to add, can also be a string containing the name of attribute to add. Can be a list of both
"""
conf_attributes_excluded_from_object_def = get_config_parameter('ATTRIBUTES_EXCLUDED_FROM_OBJECT_DEF')
if isinstance(definition, STRING_TYPES):
self.add_from_schema(definition)
elif isinstance(definition, AttrDef):
if definition.key not in get_config_parameter('ATTRIBUTES_EXCLUDED_FROM_OBJECT_DEF'):
if definition.key not in conf_attributes_excluded_from_object_def:
if definition.key not in self._attributes:
self._attributes[definition.key] = definition
if definition.name and definition.name != definition.key:
Expand Down
27 changes: 18 additions & 9 deletions ldap3/core/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def __init__(self,
auto_escape=True,
auto_encode=True):

conf_default_pool_name = get_config_parameter('DEFAULT_THREADED_POOL_NAME')
self.lock = RLock() # re-entrant lock to ensure that operations in the Connection object are executed atomically in the same thread
with self.lock:
if client_strategy not in CLIENT_STRATEGIES:
Expand Down Expand Up @@ -254,7 +255,7 @@ def __init__(self,
self._bind_controls = None
self._executing_deferred = False
self.lazy = lazy
self.pool_name = pool_name if pool_name else get_config_parameter('DEFAULT_THREADED_POOL_NAME')
self.pool_name = pool_name if pool_name else conf_default_pool_name
self.pool_size = pool_size
self.pool_lifetime = pool_lifetime
self.starting_tls = False
Expand Down Expand Up @@ -361,6 +362,7 @@ def __str__(self):
return ' - '.join(s)

def __repr__(self):
conf_default_pool_name = get_config_parameter('DEFAULT_THREADED_POOL_NAME')
if self.server_pool:
r = 'Connection(server={0.server_pool!r}'.format(self)
else:
Expand All @@ -379,7 +381,7 @@ def __repr__(self):
r += '' if self.read_only is None else ', read_only={0.read_only!r}'.format(self)
r += '' if self.lazy is None else ', lazy={0.lazy!r}'.format(self)
r += '' if self.raise_exceptions is None else ', raise_exceptions={0.raise_exceptions!r}'.format(self)
r += '' if (self.pool_name is None or self.pool_name == get_config_parameter('DEFAULT_THREADED_POOL_NAME')) else ', pool_name={0.pool_name!r}'.format(self)
r += '' if (self.pool_name is None or self.pool_name == conf_default_pool_name) else ', pool_name={0.pool_name!r}'.format(self)
r += '' if self.pool_size is None else ', pool_size={0.pool_size!r}'.format(self)
r += '' if self.pool_lifetime is None else ', pool_lifetime={0.pool_lifetime!r}'.format(self)
r += '' if self.fast_decoder is None else (', fast_decoder=' + ('True' if self.fast_decoder else 'False'))
Expand All @@ -391,6 +393,7 @@ def __repr__(self):
return r

def repr_with_sensitive_data_stripped(self):
conf_default_pool_name = get_config_parameter('DEFAULT_THREADED_POOL_NAME')
if self.server_pool:
r = 'Connection(server={0.server_pool!r}'.format(self)
else:
Expand All @@ -413,7 +416,7 @@ def repr_with_sensitive_data_stripped(self):
r += '' if self.lazy is None else ', lazy={0.lazy!r}'.format(self)
r += '' if self.raise_exceptions is None else ', raise_exceptions={0.raise_exceptions!r}'.format(self)
r += '' if (
self.pool_name is None or self.pool_name == get_config_parameter('DEFAULT_THREADED_POOL_NAME')) else ', pool_name={0.pool_name!r}'.format(
self.pool_name is None or self.pool_name == conf_default_pool_name) else ', pool_name={0.pool_name!r}'.format(
self)
r += '' if self.pool_size is None else ', pool_size={0.pool_size!r}'.format(self)
r += '' if self.pool_lifetime is None else ', pool_lifetime={0.pool_lifetime!r}'.format(self)
Expand Down Expand Up @@ -706,6 +709,7 @@ def search(self,
- If mssing_attributes == True then an attribute not returned by the server is set to None
- If auto_escape is set it overrides the Connection auto_escape
"""
conf_attributes_excluded_from_check = get_config_parameter('ATTRIBUTES_EXCLUDED_FROM_CHECK')
if log_enabled(BASIC):
log(BASIC, 'start SEARCH operation via <%s>', self)

Expand Down Expand Up @@ -743,7 +747,7 @@ def search(self,
attribute_name_to_check = attribute_name.split(';')[0]
else:
attribute_name_to_check = attribute_name
if self.server.schema and attribute_name_to_check not in get_config_parameter('ATTRIBUTES_EXCLUDED_FROM_CHECK') and attribute_name_to_check not in self.server.schema.attribute_types:
if self.server.schema and attribute_name_to_check not in conf_attributes_excluded_from_check and attribute_name_to_check not in self.server.schema.attribute_types:
raise LDAPAttributeError('invalid attribute type ' + attribute_name_to_check)

request = search_operation(search_base,
Expand Down Expand Up @@ -791,6 +795,8 @@ def compare(self,
"""
Perform a compare operation
"""
conf_attributes_excluded_from_check = get_config_parameter('ATTRIBUTES_EXCLUDED_FROM_CHECK')

if log_enabled(BASIC):
log(BASIC, 'start COMPARE operation via <%s>', self)
self.last_error = None
Expand All @@ -805,7 +811,7 @@ def compare(self,
else:
attribute_name_to_check = attribute

if self.server.schema.attribute_types and attribute_name_to_check not in get_config_parameter('ATTRIBUTES_EXCLUDED_FROM_CHECK') and attribute_name_to_check not in self.server.schema.attribute_types:
if self.server.schema.attribute_types and attribute_name_to_check not in conf_attributes_excluded_from_check and attribute_name_to_check not in self.server.schema.attribute_types:
raise LDAPAttributeError('invalid attribute type ' + attribute_name_to_check)

if isinstance(value, SEQUENCE_TYPES): # value can't be a sequence
Expand Down Expand Up @@ -847,6 +853,8 @@ def add(self,
Attributes is a dictionary in the form 'attr': 'val' or 'attr':
['val1', 'val2', ...] for multivalued attributes
"""
conf_attributes_excluded_from_check = get_config_parameter('ATTRIBUTES_EXCLUDED_FROM_CHECK')
conf_classes_excluded_from_check = get_config_parameter('CLASSES_EXCLUDED_FROM_CHECK')
if log_enabled(BASIC):
log(BASIC, 'start ADD operation via <%s>', self)
self.last_error = None
Expand Down Expand Up @@ -886,7 +894,7 @@ def add(self,

if self.server and self.server.schema and self.check_names:
for object_class_name in attributes[object_class_attr_name]:
if object_class_name not in get_config_parameter('CLASSES_EXCLUDED_FROM_CHECK') and object_class_name not in self.server.schema.object_classes:
if object_class_name not in conf_classes_excluded_from_check and object_class_name not in self.server.schema.object_classes:
raise LDAPObjectClassError('invalid object class ' + str(object_class_name))

for attribute_name in attributes:
Expand All @@ -895,7 +903,7 @@ def add(self,
else:
attribute_name_to_check = attribute_name

if attribute_name_to_check not in get_config_parameter('ATTRIBUTES_EXCLUDED_FROM_CHECK') and attribute_name_to_check not in self.server.schema.attribute_types:
if attribute_name_to_check not in conf_attributes_excluded_from_check and attribute_name_to_check not in self.server.schema.attribute_types:
raise LDAPAttributeError('invalid attribute type ' + attribute_name_to_check)

request = add_operation(dn, attributes, self.auto_encode, self.server.schema if self.server else None)
Expand Down Expand Up @@ -975,6 +983,8 @@ def modify(self,
- change is (operation, [value1, value2, ...])
- operation is 0 (MODIFY_ADD), 1 (MODIFY_DELETE), 2 (MODIFY_REPLACE), 3 (MODIFY_INCREMENT)
"""
conf_attributes_excluded_from_check = get_config_parameter('ATTRIBUTES_EXCLUDED_FROM_CHECK')

if log_enabled(BASIC):
log(BASIC, 'start MODIFY operation via <%s>', self)
self.last_error = None
Expand Down Expand Up @@ -1005,13 +1015,12 @@ def modify(self,

for attribute_name in changes:
if self.server and self.server.schema and self.check_names:
tags = None
if ';' in attribute_name: # remove tags for checking
attribute_name_to_check = attribute_name.split(';')[0]
else:
attribute_name_to_check = attribute_name

if self.server.schema.attribute_types and attribute_name_to_check not in get_config_parameter('ATTRIBUTES_EXCLUDED_FROM_CHECK') and attribute_name_to_check not in self.server.schema.attribute_types:
if self.server.schema.attribute_types and attribute_name_to_check not in conf_attributes_excluded_from_check and attribute_name_to_check not in self.server.schema.attribute_types:
raise LDAPAttributeError('invalid attribute type ' + attribute_name_to_check)
change = changes[attribute_name]
if isinstance(change, SEQUENCE_TYPES) and change[0] in [MODIFY_ADD, MODIFY_DELETE, MODIFY_REPLACE, MODIFY_INCREMENT, 0, 1, 2, 3]:
Expand Down
5 changes: 3 additions & 2 deletions ldap3/core/pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def find_active_random_server(self):
raise LDAPServerPoolExhaustedError('no random active server available in server pool after maximum number of tries')

def find_active_server(self, starting):
conf_pool_timeout = get_config_parameter('POOLING_LOOP_TIMEOUT')
counter = self.server_pool.active # can be True for "forever" or the number of cycles to try
if starting >= len(self.servers):
starting = 0
Expand Down Expand Up @@ -170,8 +171,8 @@ def find_active_server(self, starting):
if not isinstance(self.server_pool.active, bool):
counter -= 1
if log_enabled(NETWORK):
log(NETWORK, 'waiting for %d seconds before retrying pool servers cycle', get_config_parameter('POOLING_LOOP_TIMEOUT'))
sleep(get_config_parameter('POOLING_LOOP_TIMEOUT'))
log(NETWORK, 'waiting for %d seconds before retrying pool servers cycle', conf_pool_timeout)
sleep(conf_pool_timeout)

if log_enabled(ERROR):
log(ERROR, 'no active server available in Server Pool <%s> after maximum number of tries', self)
Expand Down
9 changes: 6 additions & 3 deletions ldap3/core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ def __repr__(self):

@property
def address_info(self):
if not self._address_info or (datetime.now() - self._address_info_resolved_time).seconds > get_config_parameter('ADDRESS_INFO_REFRESH_TIME'):
conf_refresh_interval = get_config_parameter('ADDRESS_INFO_REFRESH_TIME')
if not self._address_info or (datetime.now() - self._address_info_resolved_time).seconds > conf_refresh_interval:
# converts addresses tuple to list and adds a 6th parameter for availability (None = not checked, True = available, False=not available) and a 7th parameter for the checking time
addresses = None
try:
Expand Down Expand Up @@ -278,6 +279,7 @@ def check_availability(self):
and port to check availability. Timeout in seconds is specified in CHECK_AVAILABITY_TIMEOUT if not specified in
the Server object
"""
conf_availability_timeout = get_config_parameter('CHECK_AVAILABILITY_TIMEOUT')
available = False
self.reset_availability()
for address in self.candidate_addresses():
Expand All @@ -287,7 +289,7 @@ def check_availability(self):
if self.connect_timeout:
temp_socket.settimeout(self.connect_timeout)
else:
temp_socket.settimeout(get_config_parameter('CHECK_AVAILABILITY_TIMEOUT')) # set timeout for checking availability to default
temp_socket.settimeout(conf_availability_timeout) # set timeout for checking availability to default
try:
temp_socket.connect(address[4])
except socket.error:
Expand Down Expand Up @@ -519,14 +521,15 @@ def from_definition(host, dsa_info, dsa_schema, port=None, use_ssl=False, format
return dummy

def candidate_addresses(self):
conf_reset_availability_timeout = get_config_parameter('RESET_AVAILABILITY_TIMEOUT')
if self.ipc:
candidates = self.address_info
if log_enabled(BASIC):
log(BASIC, 'candidate address for <%s>: <%s> with mode UNIX_SOCKET', self, self.name)
else:
# checks reset availability timeout
for address in self.address_info:
if address[6] and ((datetime.now() - address[6]).seconds > get_config_parameter('RESET_AVAILABILITY_TIMEOUT')):
if address[6] and ((datetime.now() - address[6]).seconds > conf_reset_availability_timeout):
address[5] = None
address[6] = None

Expand Down
3 changes: 2 additions & 1 deletion ldap3/extend/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def decode_response(self):
if self.result['result'] not in [RESULT_SUCCESS]:
if self.connection.raise_exceptions:
raise LDAPExtensionError('extended operation error: ' + self.result['description'] + ' - ' + self.result['message'])
else: return None
else:
return None
if not self.response_name or self.result['responseName'] == self.response_name:
if self.result['responseValue']:
if self.asn1_spec is not None:
Expand Down
2 changes: 1 addition & 1 deletion ldap3/extend/standard/PersistentSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def stop(self):
self.connection.unbind()
if self.message_id in self.connection.strategy._responses:
del self.connection.strategy._responses[self.message_id]
if hasattr(self.connection.strategy, '_requests') and self.message_id in self.connection.strategy._requests: # async strategy has a dict of request that could be returned by get_response()
if hasattr(self.connection.strategy, '_requests') and self.message_id in self.connection.strategy._requests: # async strategy has a dict of request that could be returned by get_response()
del self.connection.strategy._requests[self.message_id]
self.connection.strategy.persistent_search_message_id = None
self.message_id = None
Expand Down

0 comments on commit 25e56e5

Please sign in to comment.