def __init__(self, path, settings, database_manager, pushd_client, **kwargs): self.settings = settings self.path = path self.id = path.split('.')[-1] self.logger = get_logger(self.id, self.settings) self.timeout = self.settings['network']['timeout'] self.database_manager = database_manager self.pushd_client = pushd_client self.db_collection = '.'.join( path.split('.')[:-1] ) self.db_query = { } for (key, value) in self.db_query_format.iteritems(): if not hasattr(self, value): self.logger.error('Missing "%s" field defined in db_query_format' % value) sys.exit(1) self.db_query[key] = getattr(self, value) # Load and update database document (if necessary) self.document = self.load() if not self.document: if hasattr(self, 'document_prototype'): self.logger.info('No document found in database. Using prototype') self.document = self.document_prototype self.save() else: self.logger.error('No document_prototype is available!') return
def load_settings(self): self.logger.info('Loading configuration from file...') try: with open(CONFIG_FILE, 'r') as json_file: self.settings = json.load(json_file, encoding = 'utf-8') self._fix_permissions() except IOError as e: self.logger.warn( "Cannot load config file [{0}]! (Reason: {1})".format(CONFIG_FILE, e.strerror)) self.save_settings() self.logger.warn("Default settings saved to file!") except: self.logger.error("Unexpected error:", sys.exc_info()[0]) sys.exit(1) if 'log_dir' in self.settings: log_dir = self.settings['log_dir'] log_dir = os.path.abspath(log_dir) # convert to absolute path self.settings['log_dir'] = log_dir else: self.logger.warn('No log_dir specified. Using defualt') self.settings['log_dir'] = self.default_settings['log_dir'] # Update our logger based on loaded settings self.logger = get_logger('configure', self.settings) self.logger.info('Loaded new settings')
def main(): #Handle SIGINT signal.signal(signal.SIGINT, signal_handler) global uth_portal, socket_server uth_portal = UthPortal() uth_portal.start() uth_portal.logger.info('Uthportal started successfully!') #uth_portal._force_update() get_logger('py.warnings', uth_portal.settings) socket_server = ThreadedSocketServer(uth_portal.settings) socket_server.set_handle_function(handle_command) socket_server.set_auth_function(auth_function) socket_server.listen() while True: sleep(2)
def __init__(self): self.default_settings['log_dir'] = os.path.dirname(__file__) + '/logs' self.settings = self.default_settings self.logger = get_logger('init', self.settings) self.load_settings() # Check if self.settings contains all required keys if not all( key in self.settings for key in self.required_keys ): self.logger.error('Missing required key(s): "%s"' % ', '.join(self.required_keys)) sys.exit(1)
def setUp(self): self.settings = Configuration().get_settings() self.settings['logger']['levels']['notifier'] = 'DEBUG' self.db_manager = MemoryDatabaseManager() templates = {'very_real_event': { 'title' : 'test', 'msg' : 'this is a message, for real' } } self.user_manager = UserManager(self.settings, self.db_manager) self.logger = get_logger('test_users_manager', self.settings)
def setUp(self): self.settings = Configuration().get_settings() self.settings['log_dir'] = os.path.abspath("test_logs") self.settings['logger']['levels']['notifier'] = 'DEBUG' self.db_manager = MemoryDatabaseManager() templates = {'very_real_event': { 'title.gr' : 'test', 'msg.gr' : 'this is a message, for real' } } self.pushd = PushdClient(self.settings, self.db_manager, templates) self.logger = get_logger('test', self.settings)
def main(): global db_manager, settings, logger, app, user_manager,pushd_client settings = Configuration().get_settings() logger = get_logger('server', settings) logger.info('Connecting to database...') db_manager = MongoDatabaseManager(settings) db_manager.connect() user_manager = UserManager(settings, db_manager) pushd_client = PushdClient(settings, db_manager, event_templates) server_settings = settings['server'] app.run(host = server_settings['host'], port = server_settings['port'])
def __init__(self, settings, **kwargs): """ Neccessary keys are: host, port & db_name . TODO: Maybe use named arguments or get with default values? """ self.settings = settings self.logger = get_logger('mongo', self.settings) self.client = None self.info = self.settings['database'] if (not self.info['host'] or not self.info['port'] or not self.info['db_name']): self.logger.error('An argument is missing [host|port|db_name]')
def __init__(self): self.configuration = Configuration() self.settings = self.configuration.get_settings() self.logger = get_logger('uthportal', self.settings) self.db_manager = MongoDatabaseManager(self.settings) if not self.db_manager.connect(): self.logger.info('Exiting...') sys.exit(1) self.pushd_client = PushdClient( self.settings, self.db_manager, event_templates ) self.load_tasks() self.scheduler = Scheduler( self.tasks, self.settings)
def __init__(self, tasks, settings): self.settings = settings self.logger = get_logger('scheduler', self.settings) self.intervals = self.settings['scheduler']['intervals'] self.apscheduler = { 'apscheduler': self.settings['scheduler']['apscheduler'] } if not isinstance(tasks, dict): self.logger.error('tasks is not a dictionary') return if not isinstance(self.intervals, dict): self.logger.error('intervals is not a dictionary') return self.tasks = self._flatten_dict(tasks, '') self.logger.debug('Tasks found:') self.logger.debug(LINE_SPLITTER) for key in self.tasks: self.logger.debug('%45s %30s' % (key, self.tasks[key].task_type)) self.logger.debug(LINE_SPLITTER)
def init(self): """ Initializes the queue, and adds the tasks """ self.logger.info('Initilizing APScheduler...') apsched_kwargs = self._flatten_dict(self.apscheduler, '') apsched_kwargs['apscheduler.logger'] = get_logger('apscheduler', self.settings) self.sched = BackgroundScheduler(apsched_kwargs) for (id, task) in self.tasks.items(): task_type = task.task_type self.logger.debug('Adding task "%s" [%s]' % (id, task_type)) if not task_type in self.intervals: self.logger.info('Interval not defined for "%s" class. Assuming it is an once-time task' % task_type) self.add_task(id, task) continue self.add_task(id, task, self.intervals[task_type]) self.logger.info('APScheduler initialized!')
def __init__(self, settings): self.logger = get_logger('grades', settings) if not self._check_vpn(): self.logger.error("VPN not connected") self._session = requests.Session() self._logged_in = False
def setUp(self): self.settings = Configuration().get_settings() self.settings['logger']['levels']['grades'] = 'DEBUG' self.db_manager = MemoryDatabaseManager() self.grades_provider = GradesProvider(self.settings) self.logger = get_logger('test_users_manager', self.settings)