class ConnectionConfig(): def __init__(self, file_name): if not exists(file_name): raise StandardError("The file that contain the action list [%s] was not created" % file_name) self.file_config = Config(file_name=file_name) # Prepare the connection(s) self.connection_list = {} # Inform that need call the method to load the connections self.connection_are_loaded = False def _load_connection(self): """ Due the module.settings can't be imported (circular reference) we need call after from a method. The import is use by check_arguments. """ # For each connection we will check if the configuration is ok. for item in self.file_config._config.sections(): # Load the backends to run the process backends = self.file_config.get_config_value(item, "backend", default_value="procsync.modules.connection.backends.mysql") try: # Plus one to jump the dot and get the module name backends = __import__(backends, fromlist=[backends[backends.rfind(".") + 1:], ]) except Exception, e: raise ReferenceError("The module [%s] threw a error when tried import [%s]: [%s]" % (backends, item, e)) # Check if all arguments necessary for the thread was declared attrib = backends.check_arguments(self.file_config, item) self.connection_list[item] = { "backend" : backends, "attrib" : attrib } self.connection_are_loaded = True
def __init__(self, file_name): if not exists(file_name): raise StandardError("The file that contain the action list [%s] was not created" % file_name) self.file_config = Config(file_name=file_name) # Prepare the connection(s) self.connection_list = {} # Inform that need call the method to load the connections self.connection_are_loaded = False
def __init__(self, file_name): self.file_name = file_name if not exists(file_name): raise StandardError("The file that contain the action list [%s] was not created" % file_name) self.file_config = Config(file_name=file_name) # Initialize thread list (run_list) self.run_list = [ item for item in self.file_config._config.sections() if self.file_config.get_config_value(item, "instances", default_value=self.DEFAULT_START_INSTANCE, empty_become_none=True) > 0 ] # Prepare to load the thread(s) self.thread_list = {}
def __init__(self, file_name): self.file_name = file_name if not exists(file_name): raise StandardError("The file that contain the action list [%s] was not created" % file_name) self.file_config = Config(file_name=file_name) # Initialize thread list (run_list) self.run_list = self.file_config.get_config_value("application", "run_list", default_value=None, empty_become_none=True) session_list = [ item for item in self.file_config._config.sections() if item != "application" ] if self.run_list is None: self.run_list = session_list else: self.run_list = [ item.strip() for item in self.run_list.split(",") if item.strip() != "" ] # alert for wrong thread for item in self.run_list: if item not in session_list: raise ValueError("The item in the run_list [%s] not exist in the thread file session" % item) # Prepare to load the thread(s) self.thread_list = {}
class ThreadConfig(): DEFAULT_START_INSTANCE = 1 def __init__(self, file_name): self.file_name = file_name if not exists(file_name): raise StandardError("The file that contain the action list [%s] was not created" % file_name) self.file_config = Config(file_name=file_name) # Initialize thread list (run_list) self.run_list = [ item for item in self.file_config._config.sections() if self.file_config.get_config_value(item, "instances", default_value=self.DEFAULT_START_INSTANCE, empty_become_none=True) > 0 ] # Prepare to load the thread(s) self.thread_list = {} def _load_pool(self): """ Due the procsync.module.settings can't be imported (circular reference) we need call after from a method. """ from procsync.modules import settings load_thread_list = list(self.run_list) for item in load_thread_list: # Load the backends to run the process backends = self.file_config.get_config_value(item, "backend", default_value="procsync.modules.thread.backends.mysql") try: # Plus one to jump the dot and get the module name backends = __import__(backends, fromlist=[backends[backends.rfind(".") + 1:], ]) except Exception, e: raise ReferenceError("The module [%s] threw a error when tried import [%s]: [%s]" % (backends, item, e)) # Check if all arguments necessary for the thread was declared attrib = backends.check_arguments(self.file_config, item) attrib["backend"] = backends # Load the basic information common for all thread backends complement = self.file_config.get_config_value(item, "complement", default_value=None, empty_become_none=True) attrib["complement"] = complement if complement is None else [ item_complement.strip() for item_complement in complement.split(",") ] attrib["sleep_sum"] = self.file_config.get_config_value(item, "sleep_sum", default_value=0.5) attrib["sleep_limit"] = self.file_config.get_config_value(item, "sleep_limit", default_value=5.0) action_file = self.file_config.get_config_value(item, "action_file", default_value=None, empty_become_none=True) attrib["action_file"] = settings.ACTION_DICT if action_file is None else ActionDict(action_file) self.thread_list[item] = attrib # start more instances if necessary instances = self.file_config.get_config_value(item, "instances", default_value=self.DEFAULT_START_INSTANCE, empty_become_none=True) if instances > 1: for thread_item in range(1, instances): thread_name = item + ("_%s" % (thread_item,)) self.thread_list[thread_name] = copy(attrib) self.run_list.append(thread_name)
class ThreadConfig(): def __init__(self, file_name): self.file_name = file_name if not exists(file_name): raise StandardError("The file that contain the action list [%s] was not created" % file_name) self.file_config = Config(file_name=file_name) # Initialize thread list (run_list) self.run_list = self.file_config.get_config_value("application", "run_list", default_value=None, empty_become_none=True) session_list = [ item for item in self.file_config._config.sections() if item != "application" ] if self.run_list is None: self.run_list = session_list else: self.run_list = [ item.strip() for item in self.run_list.split(",") if item.strip() != "" ] # alert for wrong thread for item in self.run_list: if item not in session_list: raise ValueError("The item in the run_list [%s] not exist in the thread file session" % item) # Prepare to load the thread(s) self.thread_list = {} def _load_pool(self): """ Due the module.settings can't be imported (circular reference) we need call after from a method. """ from procsync.modules import settings for item in self.run_list: # Load the backends to run the process backends = self.file_config.get_config_value(item, "backend", default_value="procsync.modules.thread.backends.mysql") try: # Plus one to jump the dot and get the module name backends = __import__(backends, fromlist=[backends[backends.rfind(".") + 1:], ]) except Exception, e: raise ReferenceError("The module [%s] threw a error when tried import [%s]: [%s]" % (backends, item, e)) # Check if all arguments necessary for the thread was declared attrib = backends.check_arguments(self.file_config, item) attrib["backend"] = backends # Load the basic information common for all thread backends complement = self.file_config.get_config_value(item, "complement", default_value=None, empty_become_none=True) attrib["complement"] = complement if complement is None else [ item_complement.strip() for item_complement in complement.split(",") ] attrib["sleep_sum"] = self.file_config.get_config_value(item, "sleep_sum", default_value=0.5) attrib["sleep_limit"] = self.file_config.get_config_value(item, "sleep_limit", default_value=5.0) action_file = self.file_config.get_config_value(item, "action_file", default_value=None, empty_become_none=True) attrib["action_file"] = settings.ACTION_DICT if action_file is None else ActionDict(action_file) self.thread_list[item] = attrib
from procsync.modules.tools import Config from procsync.modules.dxml import ActionDict from procsync.modules.configuration import ThreadConfig, ConnectionConfig from os import getcwd from os.path import join PROCESS_SUCCESS, CONFIG_ERROR, ACTION_ERROR, SYSTEM_ERROR = range(4) APP_CONFIG = Config() INSTANCE_NAME = APP_CONFIG.get_config_value("application", "name", default_value="Not defined") MANAGER_SLEEP = APP_CONFIG.get_config_value("application", "manager_sleep", default_value=5.0) RETRY_SLEEP = APP_CONFIG.get_config_value("application", "retry_sleep", default_value=5.0) SCRIPT_PATH = APP_CONFIG.get_config_value("application", "script_path", default_value=join(getcwd(), "scripts")) MYSQL_UNIX_SOCKET = APP_CONFIG.get_config_value("mysql", "unix_socket", default_value=None) PYTHON_PATH = APP_CONFIG.get_config_value("python", "path", default_value=join(getcwd(), "scripts", "python")) LOG_FORMAT = APP_CONFIG.get_config_value("logger", "format", default_value="[%(process)d/%(thread)d/%(name)s] %(levelname)s: %(message)s") LOG_LEVEL = APP_CONFIG.get_config_value("logger", "log_level", default_value="ERROR").upper() ACTION_FILE_NAME = APP_CONFIG.get_config_value("files", "actions", default_value="ActionList.xml") ACTION_DICT = ActionDict(ACTION_FILE_NAME) CONNECTION_FILE_NAME = APP_CONFIG.get_config_value("files", "connections", default_value="connection.conf") CONNECTION_CONFIG = ConnectionConfig(CONNECTION_FILE_NAME) THREAD_FILE_NAME = APP_CONFIG.get_config_value("files", "threads", default_value="thread.conf") THREAD_CONFIG = ThreadConfig(THREAD_FILE_NAME)