def _init_(logger=None, settings=None): """ Module init function, called by the application framework during the weblets discovery process.""" global _logger, broker, cfg, _settings, _local_debug #pylint: disable=W0603 _logger = logger if logger else log.getLogger('wblt-devcfg') # get access to the configuration broker for notification sending _settings = settings _logger.info("settings=%s", settings) _local_debug = settings and str_2_bool(settings.get('debug', 'false')) if settings and str_2_bool(settings.get('use_cfgbroker', 'true')): try: _logger.info('getting access to configuration broker') broker = cfgbroker.get_object() except Exception as e: # pylint: disable=W0702 _logger.error('cfgbroker access failed : %s', str(e)) else: _logger.warning('configuration broker usage disabled by settings') if not broker: _logger.warning('configuration broker not available. Notifications will not be sent') # creates a global singleton acting as a cache of the live configuration cfg = devcfg.DeviceNetworkConfiguration(autoload=True, logger=_logger)
def _init_(logger=None, settings=None): """ Module init function, called by the application framework during the weblets discovery process.""" global _logger _logger = logger if logger else log.getLogger('wblt-evtbrw') _logger.info('init complete')
def __init__(self, cfg, debug=False, **kwargs): if cfg.status_monitoring_period <= 0: raise ValueError('period should be a positive number of seconds') self._debug = debug self._cfg = cfg self._log = log.getLogger(self.__class__.__name__) if debug: self._log.setLevel(log.DEBUG) self._terminated = False
def _init_(logger=None, settings=None): """ Module init function, called by the application framework during the services discovery process. settings expected content: - config_path : service configuration file path, containing the definitions of the pushed variables """ # inject the (provided or created) logger in handlers default initialize parameters _handlers_initparms['logger'] = logger if logger else log.getLogger('svc.wsfeed') # same for services configuration cfg, var_defs = load_configuration(path=settings.get('config_path', '/etc/cstbox/wsfeed.cfg')) update_handlers_initparms(cfg, var_defs)
def __init__(self, url_base="/api/", port=8888, debug=False): """ Constructor :Parameters: port : int the port the server will listen to (default: 8888) """ self._app_url_base = url_base self._port = port self._debug = debug self._logger = log.getLogger(self.APP_NAME) self._services = None self._ioloop = None if self._debug: self._logger.setLevel(log.DEBUG) self._logger.warn( "AppServer instantiated with debug mode activated")
def _init_(logger=None, settings=None): """ Module init function, called by the application framework during the services discovery process. settings expected content: - dao : the name of the DAO to be used (defaulted to "fsys") """ # inject the (provided or created) logger in handlers default initialize parameters _handlers_initparms['logger'] = logger if logger else log.getLogger('svc.hello') # same for the DAO, based on the configuration from settings if available dao_name = settings.get('dao', 'fsys') try: dao = evtdao.get_dao(dao_name) except Exception as e: raise ValueError("cannot access to DAO '%s' (%s)" % (dao_name, e)) else: _handlers_initparms['dao'] = dao
def _init_(logger=None, settings=None): """ Module init function, called by the application framework during the services discovery process. settings expected content: - config_path : automation scenarios configuration file """ if not logger: logger = log.getLogger('wsapi.homeautomation') _handlers_initparms['logger'] = logger _handlers_initparms['settings'] = settings logger.info('connecting to Event Manager...') evt_mgr = evtmgr.get_object(evtmgr.CONTROL_EVENT_CHANNEL) if not evt_mgr: raise Exception('cannot get event manager access for channel %s' % evtmgr.CONTROL_EVENT_CHANNEL) _handlers_initparms['events_mgr'] = evt_mgr logger.info('success')
def __init__(self, app_name, port=8080, debug=False): """ Constructor :Parameters: app_name : str the name of the application port : int the port the server will listen to (default: 8080) """ self._app_name = app_name self._port = port self._debug = debug self._logger = log.getLogger('web_' + app_name) self.ui_modules = {} if self._debug: self._logger.setLevel(log.DEBUG) self._logger.warn( "AppServer instantiated with debug mode activated")
class UIRequestHandler(TornadoBabelMixin, tornado.web.RequestHandler): """ Base class for UI request handlers. Adds convenience methods """ _home = '.' _logger = log.getLogger('UIRequestHandler') _i18nStrs_ = {} _lang = 'en' def initialize(self): tornado.web.RequestHandler.initialize(self) if self.application.settings['debug']: self._logger.setLevel(log.DEBUG) def render(self, tmpl_name, **kwargs): self._logger.debug('rendering template') tmpl_path = os.path.join(self._home, tmpl_name) tornado.web.RequestHandler.render(self, tmpl_path, settings=self.settings, _i18nStrs_=self._i18nStrs_, _lang_=self._lang, **kwargs)
def _init_(logger=None, settings=None): """ Module init function, called by the application framework during the services discovery process.""" # inject the logger in handlers default initialize parameters _handlers_initparms['logger'] = logger if logger else log.getLogger('svc.hello')
import tornado.web import pycstbox.log as log import pycstbox.webui as webui import pycstbox.config as config import pycstbox.sysutils as sysutils from tornadobabel.mixin import TornadoBabelMixin __author__ = 'Eric PASCUAL - CSTB ([email protected])' # allows catching Exception instances #pylint: disable=W0703 _logger = log.getLogger('wblt-systools') _tools_list = None class ToolDescriptor(object): def __init__(self, icon, label, descr): self.icon = icon self._label = label self._descr = descr self._i18n_ = None def set_translator(self, translator): self._i18n_ = translator def _translate(self, s): if self._i18n_:
def _discover_weblets(self, home=None): #pylint: disable=R0912 """ Discover the weblets stored in the directory which path is provided. A weblet must provide the following mandatory items: - a __init__.py, which can contains the weblet request handler and auxiliary definitions. Although it is valid to have several modules implementing the weblet, most of the time the __init__ module can be the only one. If not, it must expose the weblet request handler so that the webapp class will be able to instantiate it. - MANIFEST file, in the form of a config file with the following minimal content : [weblet] label=<display label> icon=<icon path> mapping=<module attribute containing the list of URL mapping rules> Weblet developers are free to use this file to store additional properties they would need. If the weblet module requires some global initialization, it can be placed in a global reserved function named _init_. Altough simple variables initialization statements can live as module level statements, all other processing should be placed in this _init_ method, especially if it involves runtime dependencies, such as the connection with other services. This way, there will be no problem when importing the weblet module in contexts other than standard runtime, for instance during unit tests or automatic documentation generation. The _init_ function cannot have parameters. Parameters: home : src the path of the weblets home directory Result: a list of weblets descriptors, each one being a tuple containing in sequence : - the weblet symbolic name - the label to be displayed under the icon and to be used as the title - the path of the icon to be displayed on the desktop - the base URL of the weblet - a list of (url pattern, handler) mapping tuples """ if not home: home = self._weblets_home self._logger.debug("discovering weblets stored in %s" % home) weblets = [] # We build the weblets list by scanning sub-directories of the home # one, keeping only the ones containing the mandatory files. They are # then sorted by weblet names, so that weblet icons will be displayed # in this sequence on the desktop for weblet_name in sorted([ d for d in os.listdir(home) if os.path.isdir(os.path.join(home, d)) and os.path.exists(os.path.join(home, d, MANIFEST_FILE_NAME)) and os.path.exists(os.path.join(home, d, '__init__.py')) ]): weblet_path = os.path.join(home, weblet_name) mfpath = os.path.join(weblet_path, MANIFEST_FILE_NAME) mf = ConfigParser.SafeConfigParser(self._weblet_cfg_defaults) mf.read(mfpath) label = mf.get(MANIFEST_MAIN_SECTION, 'label') icon = mf.get(MANIFEST_MAIN_SECTION, 'icon') mapping_attr = mf.get(MANIFEST_MAIN_SECTION, 'mapping') modname = '.'.join(['pycstbox.webui.weblets', weblet_name]) self._logger.info("loading weblet '%s' from module '%s'...", weblet_name, modname) try: module = importlib.import_module(modname) # run the module initialization code if any if hasattr(module, '_init_'): init_func = getattr(module, '_init_') if callable(init_func): self._logger.info('invoking module _init_ function') wblt_logger = log.getLogger('wblt-' + weblet_name) wblt_logger.setLevel(self._logger.getEffectiveLevel()) # load settings if any settings = None cfgpath = config.make_config_file_path( os.path.join('weblets', weblet_name + '.cfg')) if os.path.exists(cfgpath): wblt_logger.info('loading weblet settings from %s', cfgpath) parser = ConfigParser.SafeConfigParser() try: parser.read(cfgpath) settings = dict(parser.items('settings')) except ConfigParser.Error as e: wblt_logger.error( 'invalid configuration file (%s)', e) init_func(logger=wblt_logger, settings=settings) self._logger.info('module _init_ ok') url_base = '/' + self._app_name + '/' + weblet_name mapping = getattr(module, mapping_attr) # expand the URL in mappings if needed handlers = [] for rule in mapping: handlers.append(((url_base + rule[0], ) + rule[1:])) weblets.append((weblet_name, label, icon, handlers)) self._logger.info("--> success") if hasattr(module, 'ui_modules'): self._logger.info("--> has attr ui_modules") self.ui_modules.update(getattr(module, 'ui_modules')) # adds weblet resources (html, js, css,...) to the autoreload # watched files if debug is active if self._debug: for _root, _dirs, files in os.walk(weblet_path): for path in [ os.path.join(_root, f) for f in files if f.endswith(('.html', '.mo')) ]: tornado.autoreload.watch(path) except (ImportError, AttributeError) as e: msg = '[%s] %s' % (e.__class__.__name__, str(e)) self._logger.exception(msg) raise except Exception as e: self._logger.error("Cannot load weblet : %s", str(e)) self._logger.exception(e) return weblets
def __init__(self, config_path, evt_mgr, log_level=log.INFO, alt_query_gateway=None): """ :param str config_path: path of the configuration file :param evt_mgr: CSTBox event manager proxy object :param log_level: logging level :param alt_query_gateway: an alternate version of :py:meth:`_query_gateway` for unit tests """ self._worker_thread = None self._terminate = False log.Loggable.__init__(self) self.log_setLevel(log_level) log.getLogger('requests').setLevel( log.INFO if self.logger.isEnabledFor(log.DEBUG) else log.WARN) def die(msg): self.log_critical(msg) raise OBIXConnectorError(msg) if not evt_mgr: die('evt_mgr parameter is mandatory') self._evt_mgr = evt_mgr with open(config_path) as fp: self.log_info("loading configuration from %s", config_path) cfg_dict = json.load(fp) gw_cfg = cfg_dict['gateway'] self.log_info("gateway configuration :") for k, v in gw_cfg.iteritems(): self.log_info("- %-20s : %s", k, v) self._gateway_cfg = GatewayConfiguration(**gw_cfg) for attr in GATEWAY_CONFIG_ATTRIBUTES.split(): if not getattr(self._gateway_cfg, attr): die('gateway "%s" parameter is mandatory' % attr) # loads the {obix_id: (var_name, var_type)} mapping self._mapping = cfg_dict['mapping'] self.log_info("mapping configuration :") for k, v in self._mapping.iteritems(): self.log_info("- %-20s -> %s", k, v) # loads the configured filters self._filters = cfg_dict['filters'] self.log_info("filters configuration :") for k, v in self._filters.iteritems(): self.log_info("- %-20s -> %s", k, v) # creates the reverse lookup {var_name: obix_id} self._reverse_mapping = { var_def[0]: obix_id for obix_id, var_def in self._mapping.iteritems() } self.log_info("global configuration :") try: global_cfg = cfg_dict["global"] except KeyError: self._events_ttl = self.DEFAULT_EVENTS_TTL self._polling_period = self.DEFAULT_POLLING_PERIOD else: try: self._events_ttl = parse_period(global_cfg['events_ttl']) except KeyError: self._events_ttl = self.DEFAULT_EVENTS_TTL try: self._polling_period = parse_period( global_cfg['polling_period']) except KeyError: self._polling_period = self.DEFAULT_POLLING_PERIOD for k, v in (("events_ttl", self._events_ttl), ("polling_period", self._polling_period)): self.log_info("- %-20s : %s", k, v) # patch the default gateway query process by the provided one if any if alt_query_gateway: self._query_gateway = alt_query_gateway
import os import gettext import json import pycstbox.log as log import pycstbox.flags as flags #pylint: disable=W0703 _here = os.path.dirname(__file__) js_i18n = {} messages_i18n = {} # logger for module level methods _logger = log.getLogger('webui_lib') def set_logLevel(level): _logger.setLevel(level) def _get_i18n_catalog(wbl_name, wbl_home, lang, filename, cache, convert): """"get_i18n_catalog(wbl_name, wbl_home, lang, fn, c, convert) -> dict loads fn.po and uses c as cache with convert()->""" if lang.startswith('en'): return '{}' key = wbl_name + '|' + lang if key not in cache: try: t = gettext.translation(
import os import re import subprocess from collections import namedtuple import datetime import pytz import socket import json import time import pycstbox.log as log __author__ = 'Eric PASCUAL - CSTB ([email protected])' _logger = log.getLogger('sysutils') _logger.setLevel(log.INFO) ISO_DATE_FORMAT = "%Y-%m-%d" CSTBOX_HOME = os.environ.get('CSTBOX_HOME', '/opt/cstbox') CSTBOX_BIN_DIR = os.path.join(CSTBOX_HOME, 'bin') CSTBOX_LIB_DIR = os.path.join(CSTBOX_HOME, 'lib/python') CSTBOX_DEPS_DIR = os.path.join(CSTBOX_HOME, 'deps/python') CSTBOX_VERSION_DIR = os.path.join(CSTBOX_HOME, 'version') CSTBOX_HOSTNAME = socket.getfqdn() CSTBOX_LOG_DIR = os.environ.get('CSTBOX_LOG_DIR', '/var/log/cstbox') CSTBOX_PYTHONPATH_ENV = 'PYTHONPATH=' + ':'.join([CSTBOX_LIB_DIR, CSTBOX_DEPS_DIR]) tz_UTC = pytz.UTC tz_PARIS = pytz.timezone('Europe/Paris')
def __init__(self): self.emitted_events = [] self._log = getLogger('evtMgr')
from datetime import datetime import os.path import importlib from collections import namedtuple from copy import deepcopy import pycstbox.evtmgr as evtmgr import pycstbox.log as log from pycstbox.config import GlobalSettings __author__ = 'Eric PASCUAL - CSTB ([email protected])' log.setup_logging() _logger = log.getLogger('evtdao') def log_setLevel(level): _logger.setLevel(level) DriverSpecs = namedtuple('DriverSpecs', 'modname cfg') CFGKEY_EVTS_DB_HOME_DIR = 'evts_db_home_dir' CFGKEY_FLASH_MEM_SUPPORT = 'flash_memory' # # The dictionary of the supported DAOs, together with their configuration # parameters # # TODO replace static dictionary by a discovery mechanism
import os import re import subprocess from collections import namedtuple import datetime import pytz import socket import json import time import pycstbox.log as log __author__ = 'Eric PASCUAL - CSTB ([email protected])' _logger = log.getLogger('sysutils') _logger.setLevel(log.INFO) ISO_DATE_FORMAT = "%Y-%m-%d" CSTBOX_HOME = os.environ.get('CSTBOX_HOME', '/opt/cstbox') CSTBOX_BIN_DIR = os.path.join(CSTBOX_HOME, 'bin') CSTBOX_LIB_DIR = os.path.join(CSTBOX_HOME, 'lib/python') CSTBOX_DEPS_DIR = os.path.join(CSTBOX_HOME, 'deps/python') CSTBOX_VERSION_DIR = os.path.join(CSTBOX_HOME, 'version') CSTBOX_HOSTNAME = socket.getfqdn() CSTBOX_LOG_DIR = os.environ.get('CSTBOX_LOG_DIR', '/var/log/cstbox') CSTBOX_PYTHONPATH_ENV = 'PYTHONPATH=' + ':'.join( [CSTBOX_LIB_DIR, CSTBOX_DEPS_DIR]) tz_UTC = pytz.UTC
import os import gettext import json import pycstbox.log as log import pycstbox.flags as flags #pylint: disable=W0703 _here = os.path.dirname(__file__) js_i18n = {} messages_i18n = {} # logger for module level methods _logger = log.getLogger('webui_lib') def set_logLevel(level): _logger.setLevel(level) def _get_i18n_catalog(wbl_name, wbl_home, lang, filename, cache, convert): """"get_i18n_catalog(wbl_name, wbl_home, lang, fn, c, convert) -> dict loads fn.po and uses c as cache with convert()->""" if lang.startswith('en'): return '{}' key = wbl_name + '|' + lang if key not in cache: try: t = gettext.translation(filename,