def __init__(self, message, code=None, data={}, traceback=None): DeltaException.__init__(self, message) self._code = self.__class__.__name__ if code is not None: self._code = code self._data = data self._traceback = traceback
def dict_to_object(self, d): if '__JSON_TYPE__' not in d: return d json_type = d.pop('__JSON_TYPE__') if json_type == 'datetime': value = d['value'] if value is None: return None dt = datetime.datetime.strptime(value[0:-6], '%Y%m%d%H%M%S') microsecond = int(value[-6:]) return datetime.datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, microsecond) if json_type == 'byte_array': if d['value'] is None: return None return array.array.fromlist(d['value']) if json_type == 'decimal': if d['value'] is None: return None return Decimal(d['value']) raise DeltaException('Unknown json type [{0}]'.format(json_type))
def execute(self, *args, **kargs): ''' Executes the target function of the command and returns target function execution result. ''' if self.__manager is None: raise DeltaException("The command[%s] is not connected to command manager." % self) self.__check_permissions() before_execution_funcs = self.get_before_execution_funcs() if before_execution_funcs is not None: for before_func in before_execution_funcs: before_func(args, kargs) try: result = self.__func(*args, **kargs) except Exception as error: execution_failed_funcs = self.get_on_execution_failed_funcs() if execution_failed_funcs is not None: for failed_func in execution_failed_funcs: failed_func(args, kargs, error) raise # If there wasn't any exception. after_execution_funcs = self.get_after_execution_funcs() if after_execution_funcs is not None: for after_func in after_execution_funcs: after_func(result, args, kargs) return result
def remove_config_store(self, name): ''' Removes the configuration store by it's name. @param name: configuration store name ''' if name not in self.__config_stores: raise DeltaException('Configuration[%s] not found.' % name) del self.__config_stores[name]
def _get_free_process(self): counter = 1 while True: process = self._get_process_() if process.get_thread_count() < self._max_threads: return process #if counter >= len(self._processes): # return self._add_worker_() if counter > 2 * len(self._processes): raise DeltaException('There is no worker to serve request.') counter += 1
def add_command(self, command, **options): ''' Adds a command. @param command: command instance ''' replace = options.get('replace', False) if not replace and command.get_key() in self._commands: raise DeltaException("Command[%s] already exists." % command) command.attach(self) self._commands[command.get_key()] = command
def execute(self, *args, **kargs): ''' Executes the target function of the command And returns target function execution result. ''' results = [] if self.__manager: for cmd_data in self._commands: cmd_key, cmd_args, cmd_kargs, = cmd_data result = self.__manager.execute(cmd_key, cmd_args, cmd_kargs) results.append(result) return results else: raise DeltaException("The command[%s] is not connected to command manager." % self)
def reload(self): ''' Reloads configuration. ''' # Creating an instance of ConfigParser class parser = ConfigParser() #Make config parser case sensitive. parser.optionxform = str # Parsing the configuration file if len(parser.read(self.get_file_name())) == 0: # Raising an error, If configuration file is invalid raise DeltaException('Configuration file[%s] not found.' % self.get_file_name()) self.__parser = parser
def execute(self, *args, **kargs): ''' Executes the target function of the command And returns target function execution result. ''' threads = [] if self.__manager: for cmd_data in self._commands: cmd_key, callback, cmd_args, cmd_kargs, = cmd_data thread = self.__manager.execute_async(cmd_key, callback, cmd_args, cmd_kargs) threads.append(thread) for thread in threads: thread.join() return None else: raise DeltaException( "The command[%s] is not connected to command manager." % self)