Esempio n. 1
0
    def __init__(self, name = None, url = None, path = None, pkg_type = "plugin", data = None):
        """ Read json file of a plugin and make an object from it
            @param name : name of package
            @param url : url of file
            @param path : path of file
            @param pkg_type : package type (default : 'plugin')
                          To use only with name != None
            @param data : json data as a python object. Used by package.py when installing a zip file : the json is read from memory
        """
        json_file = None
        try:
            # load from sources repository
            if name != None:
                # get config
                cfg = Loader('domogik')
                config = cfg.load()
                conf = dict(config[1])

                if pkg_type == "plugin":
                    json_file = "{0}/{1}/{2}_{3}/info.json".format(conf['libraries_path'], PACKAGES_DIR, pkg_type, name)
                    icon_file = "{0}/{1}/{2}_{3}/design/icon.png".format(conf['libraries_path'], PACKAGES_DIR, pkg_type, name)
                # TODO : reactivate later
                #elif pkg_type == "external":
                #    if conf.has_key('package_path'):
                #        json_directory = "%s/domogik_packages/externals/" % (conf['package_path'])
                #    else:
                #        json_directory = "%s/%s" % (conf['src_prefix'], "share/domogik/externals/")
                else:
                    raise PackageException("Type '%s' doesn't exists" % pkg_type)
                #json_file = "%s/%s.json" % (json_directory, name)

                self.json = json.load(open(json_file))

            elif path != None:
                json_file = path
                icon_file = None
                self.json = json.load(open(json_file))

            elif url != None:
                json_file = url
                icon_file = None
                json_data = urllib2.urlopen(json_file)
                # TODO : there is an error here!!!!!
                self.json = json.load(xml_data)

            elif data != None:
                json_file = None
                icon_file = None
                self.json = data

            self.validate()

            # complete json
            self.json["identity"]["package_id"] = "%s-%s" % (self.json["identity"]["type"],
                                                           self.json["identity"]["name"])
            self.json["identity"]["icon_file"] = icon_file
        except PackageException as exp:
            raise PackageException(exp.value)
        except:
            raise PackageException("Error reading json file : %s : %s" % (json_file, str(traceback.format_exc())))
Esempio n. 2
0
 def __init__(self, xpl, log = None):
     '''
     Init the query system and connect it to xPL network
     :param xpl: the XplManager instance (usually self.myxpl)
     :param log: a Logger instance (usually took from self.log))
     '''
     self.log = log
     self.__myxpl = xpl
     if self.log != None : self.log.debug("Init config query instance")
     self._keys = {}
     self._listens = {}
     self._result = None
     self.parameters = ["parameter0", "parameter1"]
     self.values = ["valueon", "valueoff"]
     # Check in config file is target is forced
     cfg = Loader('domogik')
     config = cfg.load()
     conf = dict(config[1])
     if conf.has_key('query_xpl_timeout'):
         try:
             self.query_timeout = int(conf["query_xpl_timeout"])
             msg = "Set query timeout to '%s' from domogik.cfg" % self.query_timeout
             if self.log != None : self.log.debug(msg)
         except ValueError:
             #There is an error in domogik.cfg. Set it to default.
             self.query_timeout = 10
             msg = "Error in domogik.cfg. query_xpl_timeout ('%s') is not an integer." % conf["query_xpl_timeout"]
             if self.log != None : self.log.error(msg)
     else:
         #There is not option in domogik.cfg. Set it to default.
         self.query_timeout = 10
Esempio n. 3
0
 def get_data_files_directory(self):
    """
    Return the directory where a plugin developper can store data files.
    If the directory doesn't exist, try to create it.
    After that, try to create a file inside it.
    If something goes wrong, generate an explicit exception.
    """
    cfg = Loader('domogik')
    my_conf = cfg.load()
    config = dict(my_conf[1])
    path = "{0}/{1}/{2}_{3}/data/" % (self.librairies_directory, PACKAGES_DIR, "plugin", self._name)
    if os.path.exists(path):
        if not os.access(path, os.W_OK & os.X_OK):
            raise OSError("Can't write in directory %s" % path)
    else:
        try:
            os.mkdir(path, '0770')
            self.log.info(u"Create directory %s." % path)
        except:
            raise OSError("Can't create directory %s." % path)
    try:
        tmp_prefix = "write_test";
        count = 0
        filename = os.path.join(path, tmp_prefix)
        while(os.path.exists(filename)):
            filename = "{}.{}".format(os.path.join(path, tmp_prefix),count)
            count = count + 1
        f = open(filename,"w")
        f.close()
        os.remove(filename)
    except :
        raise IOError("Can't create a file in directory %s." % path)
    return path
Esempio n. 4
0
 def __init__(self, xpl, log=None):
     '''
     Init the query system and connect it to xPL network
     :param xpl: the XplManager instance (usually self.myxpl)
     :param log: a Logger instance (usually took from self.log))
     '''
     self.log = log
     self.__myxpl = xpl
     if self.log != None: self.log.debug("Init config query instance")
     self._keys = {}
     self._listens = {}
     self._result = None
     self.parameters = ["parameter0", "parameter1"]
     self.values = ["valueon", "valueoff"]
     # Check in config file is target is forced
     cfg = Loader('domogik')
     config = cfg.load()
     conf = dict(config[1])
     if conf.has_key('query_xpl_timeout'):
         try:
             self.query_timeout = int(conf["query_xpl_timeout"])
             msg = "Set query timeout to '%s' from domogik.cfg" % self.query_timeout
             if self.log != None: self.log.debug(msg)
         except ValueError:
             #There is an error in domogik.cfg. Set it to default.
             self.query_timeout = 10
             msg = "Error in domogik.cfg. query_xpl_timeout ('%s') is not an integer." % conf[
                 "query_xpl_timeout"]
             if self.log != None: self.log.error(msg)
     else:
         #There is not option in domogik.cfg. Set it to default.
         self.query_timeout = 10
Esempio n. 5
0
    def __init__(self):
        """ Init
        """
        l = logger.Logger("package")
        l.set_format_mode("messageOnly")
        self.log = l.get_logger()

        parser = ArgumentParser()
        parser.add_argument("-i", 
                          "--install", 
                          dest="install", 
                          help="Install a package from a path, a zip file or an url to a zip file or to a github repository and branch")
        parser.add_argument("-u", 
                          "--uninstall", 
                          dest="uninstall", 
                          help="Uninstall a package. Example : plugin_rfxcom")
        self.options = parser.parse_args()

        # get install path for packages
        cfg = Loader('domogik')
        config = cfg.load()
        conf = dict(config[1])
        self.pkg_path = os.path.join(conf['libraries_path'], PACKAGES_DIR)

        # install a package
        if self.options.install:
            self.install(self.options.install)
 
        # uninstall a package
        elif self.options.uninstall:
            self.uninstall(self.options.uninstall)

        # no choice : display the list of installed packages
        else:
            self.list_packages()
Esempio n. 6
0
 def get_data_files_directory(self):
    """
    Return the directory where a plugin developper can store data files.
    If the directory doesn't exist, try to create it.
    After that, try to create a file inside it.
    If something goes wrong, generate an explicit exception.
    """
    cfg = Loader('domogik')
    my_conf = cfg.load()
    config = dict(my_conf[1])
    path = "{0}/{1}/{2}_{3}/data/" % (self.librairies_directory, PACKAGES_DIR, "plugin", self._name)
    if os.path.exists(path):
        if not os.access(path, os.W_OK & os.X_OK):
            raise OSError("Can't write in directory %s" % path)
    else:
        try:
            os.mkdir(path, '0770')
            self.log.info(u"Create directory %s." % path)
        except:
            raise OSError("Can't create directory %s." % path)
    try:
        tmp_prefix = "write_test";
        count = 0
        filename = os.path.join(path, tmp_prefix)
        while(os.path.exists(filename)):
            filename = "{}.{}".format(os.path.join(path, tmp_prefix),count)
            count = count + 1
        f = open(filename,"w")
        f.close()
        os.remove(filename)
    except :
        raise IOError("Can't create a file in directory %s." % path)
    return path
Esempio n. 7
0
    def __init__(self, server_interfaces, server_port):
        """ Initiate DbHelper, Logs and config
            
            Then, start HTTP server and give it initialized data
            @param server_interfaces :  interfaces of HTTP server
            @param server_port :  port of HTTP server
        """

        Plugin.__init__(self, name = 'admin')
        # logging initialization
        self.log.info(u"Admin Server initialisation...")
        self.log.debug(u"locale : %s %s" % locale.getdefaultlocale())

	try:
            try:
                # admin config
                cfg_admin = Loader('admin')
                config_admin = cfg_admin.load()
                conf_admin = dict(config_admin[1])
                self.interfaces = conf_admin['interfaces']
                self.port = conf_admin['port']
                # if use_ssl = True, set here path for ssl certificate/key
                self.use_ssl = conf_admin['use_ssl']
                self.key_file = conf_admin['ssl_certificate']
                self.cert_file = conf_admin['ssl_key']

            except KeyError:
                # default parameters
                self.interfaces = server_interfaces
                self.port = server_port
		self.use_ssl = False
		self.key_file = ""
		self.cert_file = ""
                self.clean_json = False
                self.log.error("Error while reading configuration for section [admin] : using default values instead")
            self.log.info(u"Configuration : interfaces:port = %s:%s" % (self.interfaces, self.port))
	    
	    # get all datatypes
            cli = MQSyncReq(self.zmq)
            msg = MQMessage()
            msg.set_action('datatype.get')
            res = cli.request('manager', msg.get(), timeout=10)
            if res is not None:
                self.datatypes = res.get_data()['datatypes']
            else:
                self.datatypes = {}

 	    # Launch server, stats
            self.log.info(u"Admin Initialisation OK")
            self.add_stop_cb(self.stop_http)
            self.server = None
	    self.start_http()
            # calls the tornado.ioloop.instance().start()
            
            ### Component is ready
            self.ready(0)
            IOLoop.instance().start()
        except :
            self.log.error(u"%s" % self.get_exception())
Esempio n. 8
0
    def __init__(self, component_name, domogik_prefix=True, use_filename=None, log_on_stdout = True):
        '''
        Get a logger with provided parameters and set config
        @param component_name : component name to log
        @param domogik_prefix : if logger name should be prefixed by 'domogik-'
        @param use_filename : if set tells the logger to use this file name (otherwise takes 'component_name')
        @param log_on_stdout : if set to True, allow to display logs in both stdout and log file
        '''
        if component_name not in self.logger:
            LEVELS = {
                        'debug': logging.DEBUG,
                        'info': logging.INFO,
                        'warning': logging.WARNING,
                        'error': logging.ERROR,
                        'critical': logging.CRITICAL
            }

            cfg = Loader()
            config = cfg.load()[0]
            if use_filename is None:
                filename = "{0}/{1}.log".format(config['log_dir_path'], component_name)
            else:
                filename = "{0}/{1}.log".format(config['log_dir_path'], use_filename)
            level = config['log_level']

            if 'log_when' not in config:
                config['log_when'] = 'D'
            if 'log_interval' not in config:
                config['log_interval'] = 1
            if 'log_backup_count' not in config:
                config['log_backup_count'] = 10

            if level not in LEVELS:
                raise ValueError("level must be one of 'debug','info','warning',"\
                        "'error','critical'. Check your config.")

            if domogik_prefix:
                my_logger = logging.getLogger('domogik-{0}'.format(component_name))
            else:
                my_logger = logging.getLogger(component_name)
            # log to file
            my_logger.propagate = 0
            if not my_logger.handlers:
                hdlr = TimedRotatingFileHandler(filename, \
                        when=config['log_when'], interval=int(config['log_interval']), \
                        backupCount=int(config['log_backup_count']))
                formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
                hdlr.setFormatter(formatter)
                my_logger.addHandler(hdlr)

	        # if loglevvel is set to debug (all log entries also go to stdout)
                # TODO : why looking about level=debug ? to clean ?
                if log_on_stdout and level == 'debug' and component_name.find('sqlalchemy') == -1:
                    dhdlr = logging.StreamHandler(sys.stdout)
                    dhdlr.setFormatter(formatter)
                    my_logger.addHandler(dhdlr)

            my_logger.setLevel(LEVELS[level])
            self.logger[component_name] = my_logger
Esempio n. 9
0
def get_data_files_directory_for_plugin(plugin_name):
    """ This function is already defined in the Plugin class, but this one is used by the admin in application.py
        In fact, this is not really the same function as this one takes one parameter
    """
    # global config
    cfg_global = Loader('domogik')
    config_global = cfg_global.load()
    conf_global = dict(config_global[1])
    return "{0}/{1}/plugin_{2}/data".format(conf_global['libraries_path'], "domogik_packages", plugin_name)
Esempio n. 10
0
def get_libraries_directory():
    """ This function is already defined in the Plugin class, but this one is used by the admin in application.py
        TODO : see if there is a cleaner way to do this!
    """
    # global config
    cfg_global = Loader('domogik')
    config_global = cfg_global.load()
    conf_global = dict(config_global[1])
    return conf_global['libraries_path']
Esempio n. 11
0
    def __init__(self,
                 component_name,
                 domogik_prefix=True,
                 use_filename=None,
                 log_on_stdout=True):
        '''
        Get a logger with provided parameters and set config
        @param component_name : component name to log
        @param domogik_prefix : if logger name should be prefixed by 'domogik-'
        @param use_filename : if set tells the logger to use this file name (otherwise takes 'component_name')
        @param log_on_stdout : if set to True, allow to display logs in both stdout and log file
        '''
        if component_name not in self.logger:
            LEVELS = {
                'debug': logging.DEBUG,
                'info': logging.INFO,
                'warning': logging.WARNING,
                'error': logging.ERROR,
                'critical': logging.CRITICAL
            }

            cfg = Loader()
            config = cfg.load()[0]
            if use_filename is None:
                filename = "%s/%s.log" % (config['log_dir_path'],
                                          component_name)
            else:
                filename = "%s/%s.log" % (config['log_dir_path'], use_filename)
            level = config['log_level']

            if level not in LEVELS:
                raise ValueError("level must be one of 'debug','info','warning',"\
                        "'error','critical'. Check your config.")

            if domogik_prefix:
                my_logger = logging.getLogger('domogik-%s' % component_name)
            else:
                my_logger = logging.getLogger(component_name)
            # log to file
            my_logger.propagate = 0
            if not my_logger.handlers:
                hdlr = logging.FileHandler(filename)
                formatter = logging.Formatter(
                    '%(asctime)s %(name)s %(levelname)s %(message)s')
                hdlr.setFormatter(formatter)
                my_logger.addHandler(hdlr)

                # if loglevvel is set to debug (all log entries also go to stdout)
                # TODO : why looking about level=debug ? to clean ?
                if log_on_stdout and level == 'debug' and component_name.find(
                        'sqlalchemy') == -1:
                    dhdlr = logging.StreamHandler(sys.stdout)
                    dhdlr.setFormatter(formatter)
                    my_logger.addHandler(dhdlr)

            my_logger.setLevel(LEVELS[level])
            self.logger[component_name] = my_logger
Esempio n. 12
0
    def __init__(self):
        """ Init
        """
        # set logger
        l = logger.Logger("testrunner")
        l.set_format_mode("messageOnly")
        self.log = l.get_logger()

        # read the config file
        try:
            cfg = Loader('domogik')
            config = cfg.load()
            conf = dict(config[1])

            # pid dir path
            self._libraries_path = conf['libraries_path']
            self.log.debug("Libraries path is : {0}".format(self._libraries_path))

        except:
            self.log.error(u"Error while reading the configuration file '{0}' : {1}".format(CONFIG_FILE, traceback.format_exc()))
            return


        parser = ArgumentParser(description="Launch all the tests that don't need hardware.")
	parser.add_argument("directory",
                          help="What directory to run")
        parser.add_argument("-a", 
                          "--allow-alter", 
                          dest="allow_alter",
                          action="store_true",
                          help="Launch the tests that can alter the configuration of the plugin or the setup (devices, ...)")
        parser.add_argument("-c", 
                          "--criticity", 
                          dest="criticity", 
                          help="Set the minimum level of criticity to use to filter the tests to execute. low/medium/high. Default is low.")
        self.options = parser.parse_args()
	self.testcases = {}
        self.results = {}

        # options
        self.log.info("Domogik release : {0}".format(DMG_VERSION))
        self.log.info("Running test with the folowing parameters:")
	if self.options.allow_alter:
	    self.log.info("- allow to alter the configuration or setup.")
	if self.options.criticity not in (LOW, MEDIUM, HIGH):
            self.options.criticity = LOW
	self.log.info("- criticity : {0}".format(self.options.criticity))

        # check tests folder
	self.log.info("- path {0}".format(self.options.directory))
        if not self.check_dir():
            return

        # check and load the json file
        self.log.info("- json file {0}".format(self.json_file))
	if not self.load_json():
	    return
Esempio n. 13
0
def api_root():
    """
    @api {get} /rest/ Get the status of the REST server
    @apiName getStatus
    @apiGroup Status
    @apiVersion 0.4.1

    @apiSuccess {json} result A json result with the status

    @apiSuccessExample Success-Response:
        HTTTP/1.1 200 OK
        {
            "info": {
                "REST_API_version": "0.7",
                "SSL": false,
                "Host": "igor",
                "Domogik_release": "0.4.0",
                "Domogik_version": "0.4.0",
                "REST_API_release": "0.7",
                "Sources_release": "0.4.0",
                "Sources_version": "0.4.0"
            },
            "mq": {
                "sub_port": "40412",
                "ip": "127.0.0.1",
                "req_rep_port": "40410",
                "pub_port": "40411"
            }
        }
    """
    # domogik global version
    global_version = sys.modules["domogik"].__version__
    src_version = global_version

    info = {}
    info["REST_API_version"] = app.apiversion
    info["Domogik_version"] = global_version
    info["Sources_version"] = src_version
    info["SSL"] = app.use_ssl
    info["Host"] = app.hostname

    # for compatibility with Rest API < 0.6
    info["REST_API_release"] = app.apiversion
    info["Domogik_release"] = global_version
    info["Sources_release"] = src_version

    # mq part
    mqconfig = Loader('mq', 'domogik-mq.cfg')
    config = dict(mqconfig.load()[1])
    mq = {}
    mq["sub_port"] = config["sub_port"]
    mq["ip"] = config["ip"]
    mq["req_rep_port"] = config["req_rep_port"]
    mq["pub_port"] = config["pub_port"]

    data = {"info" : info, "mq": mq}
    return 200, data
Esempio n. 14
0
def get_libraries_directory():
    """ This function is already defined in the Plugin class, but this one is used by the admin in application.py
        TODO : see if there is a cleaner way to do this!
    """
    # global config
    cfg_global = Loader('domogik')
    config_global = cfg_global.load()
    conf_global = dict(config_global[1])
    return conf_global['libraries_path']
Esempio n. 15
0
    def __init__(self, server_interfaces, server_port):
        """ Initiate DbHelper, Logs and config
            Then, start HTTP server and give it initialized data
            @param server_interfaces :  interfaces of HTTP server
            @param server_port :  port of HTTP server
        """

        Plugin.__init__(self, name='admin')
        # logging initialization
        self.log.info(u"Admin Server initialisation...")
        self.log.debug(u"locale : %s %s" % locale.getdefaultlocale())

        try:
            try:
                cfg_rest = Loader('admin')
                config_rest = cfg_rest.load()
                conf_rest = dict(config_rest[1])
                self.interfaces = conf_rest['interfaces']
                self.port = conf_rest['port']
                # if rest_use_ssl = True, set here path for ssl certificate/key
                self.use_ssl = conf_rest['use_ssl']
                self.key_file = conf_rest['ssl_certificate']
                self.cert_file = conf_rest['ssl_key']
            except KeyError:
                # default parameters
                self.interfaces = server_interfaces
                self.port = server_port
                self.use_ssl = False
                self.key_file = ""
                self.cert_file = ""
                self.clean_json = False
            self.log.info(u"Configuration : interfaces:port = %s:%s" %
                          (self.interfaces, self.port))

            # get all datatypes
            cli = MQSyncReq(self.zmq)
            msg = MQMessage()
            msg.set_action('datatype.get')
            res = cli.request('manager', msg.get(), timeout=10)
            if res is not None:
                self.datatypes = res.get_data()['datatypes']
            else:
                self.datatypes = {}

# Launch server, stats
            self.log.info(u"Admin Initialisation OK")
            self.add_stop_cb(self.stop_http)
            self.server = None
            self.start_http()
            # calls the tornado.ioloop.instance().start()

            ### Component is ready
            self.ready(0)
            IOLoop.instance().start()
        except:
            self.log.error(u"%s" % self.get_exception())
Esempio n. 16
0
def api_datatype():
    """ return the datatypes json file
    """
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    json_file = "{0}/datatypes.json".format(urlHandler.resources_directory)
    data = json.load(open(json_file))

    return 200, data
Esempio n. 17
0
def get_rest_url():
    """ Return the REST server url (constructed from the configuration file of the host)
    """
    cfg = Loader('rest')
    config = cfg.load()
    conf = dict(config[1])
    # we return the url related to the first declared interface in domogik.cfg
    intf = conf['interfaces'].split(",")[0]
    ip = get_ip_for_interfaces([intf])[0]
    return "http://{0}:{1}/".format(ip, conf['port'])
Esempio n. 18
0
def get_rest_doc_path():
    """ return the REST API generated doc path
    """
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    ### get libraries path
    path = conf['libraries_path']

    return "{0}/rest_doc_generated_during_install/".format(path)
Esempio n. 19
0
def get_rest_doc_path():
    """ return the REST API generated doc path
    """
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    ### get libraries path
    path = conf['libraries_path']

    return "{0}/rest_doc_generated_during_install/".format(path)
Esempio n. 20
0
def get_data_files_directory_for_plugin(plugin_name):
    """ This function is already defined in the Plugin class, but this one is used by the admin in application.py
        In fact, this is not really the same function as this one takes one parameter
    """
    # global config
    cfg_global = Loader('domogik')
    config_global = cfg_global.load()
    conf_global = dict(config_global[1])
    return "{0}/{1}/plugin_{2}/data".format(conf_global['libraries_path'],
                                            "domogik_packages", plugin_name)
Esempio n. 21
0
def get_rest_url():
    """ Return the REST server url (constructed from the configuration file of the host)
    """
    cfg = Loader('rest')
    config = cfg.load()
    conf = dict(config[1])
    # we return the url related to the first declared interface in domogik.cfg
    intf = conf['interfaces'].split(",")[0]
    ip = get_ip_for_interfaces([intf])[0]
    return "http://{0}:{1}/".format(ip, conf['port'])
Esempio n. 22
0
def api_datatype():
    """ return the datatypes json file
    """
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    json_file = "{0}/datatypes.json".format(urlHandler.resources_directory)
    data = json.load(open(json_file))

    return 200, data
Esempio n. 23
0
def get_backup_dir():
    ### Read the configuration file
    try:
        cfg = Loader('backup')
        config = cfg.load()
        conf = dict(config[1])
        return conf['folder']
   
    except:
        print(u"Error while reading the configuration file '{0}' : {1}".format(CONFIG_FILE, traceback.format_exc()))
        return None
Esempio n. 24
0
 def get_stats_files_directory(self):
    """ Return the directory where a plugin developper can store data files
    """
    cfg = Loader('domogik')
    my_conf = cfg.load()
    config = dict(my_conf[1])
    if config.has_key('package_path'):
        path = "%s/domogik_packages/stats/%s" % (config['package_path'], self._name)
    else:
        path = "%s/share/domogik/stats/%s" % (config['src_prefix'], self._name)
    return path
Esempio n. 25
0
def get_packages_dir():
    ### Read the configuration file
    try:
        cfg = Loader('domogik')
        config = cfg.load()
        conf = dict(config[1])
        return os.path.join(conf['libraries_path'], PACKAGES_DIR)
   
    except:
        print(u"Error while reading the configuration file '{0}' : {1}".format(CONFIG_FILE, traceback.format_exc()))
        return None
Esempio n. 26
0
    def __init__(self):
        """ Init
        """
        l = logger.Logger("package")
        l.set_format_mode("messageOnly")
        self.log = l.get_logger()

        parser = ArgumentParser()
        parser.add_argument("-i", 
                          "--install", 
                          dest="install", 
                          help="Install a package from a path, a zip file or an url to a zip file or to a github repository and branch")
        parser.add_argument("-u", 
                          "--upgrade", 
                          dest="upgrade", 
                          help="Upgrade a package (zip and url only).")
        parser.add_argument("-r", 
                          "--remove", 
                          dest="uninstall", 
                          help="Remove (uninstall) a package. Example : plugin_rfxcom")
        parser.add_argument("-H", 
                          "--hash", 
                          dest="hash", 
                          help="Hash value to check the package integrity. Only for zip files!")
        self.options = parser.parse_args()

        # get install path for packages
        cfg = Loader('domogik')
        config = cfg.load()
        conf = dict(config[1])
        self.pkg_path = os.path.join(conf['libraries_path'], PACKAGES_DIR)

        # install a package
        if self.options.install:
            if self.options.hash:
                self.install(self.options.install, self.options.hash)
            else:
                self.install(self.options.install)
 
        # uninstall a package
        elif self.options.uninstall:
            self.uninstall(self.options.uninstall)

        # upgrade a package
        elif self.options.upgrade:
            if self.options.hash:
                self.install(self.options.upgrade, hash = self.options.hash, upgrade = True)
            else:
                self.install(self.options.upgrade, hash = None, upgrade = True)

        # no choice : display the list of installed packages
        else:
            self.list_packages()
Esempio n. 27
0
    def __init__(self, handler_params, xpl):
        """ 
        @param handler_params : The server params 
        @param xpl : A xPL Manager instance 
        """

        try:
            self.myxpl = xpl

            # logging initialization
            log = logger.Logger('rest-stat')
            self._log_stats = log.get_logger('rest-stat')
            self._log_stats.info("Rest Stat Manager initialisation...")
    
            # logging initialization for unkwnon devices
            log_unknown = logger.Logger('rest-stat-unknown-devices')
            self._log_stats_unknown = log_unknown.get_logger('rest-stat-unknown-devices')
    
            # config
            cfg = Loader('domogik')
            config = cfg.load()
            cfg_db = dict(config[1])
            # plugin installation path
            if cfg_db.has_key('package_path'):
                self._package_path = cfg_db['package_path']
                self._log_stats.info("Set package path to '%s' " % self._package_path)
                print("Set package path to '%s' " % self._package_path)
                self.directory = "%s/domogik_packages/stats/" % self._package_path
            else:
                self._log_stats.info("No package path defined in config file")
                self._package_path = None
                self.directory = "%s/share/domogik/stats/" % cfg_db['src_prefix']

            self._db = DbHelper()

            ### Rest data
            self.handler_params = handler_params
            self.handler_params.append(self._log_stats)
            self.handler_params.append(self._log_stats_unknown)
            self.handler_params.append(self._db)
    
            self._event_requests = self.handler_params[0]._event_requests
            self.get_exception = self.handler_params[0].get_exception

            self.stats = None

            ### list of loaded and KO xml files
            self.xml_date = None
            self.xml = []
            self.xml_ko = []

        except :
            self._log_stats.error("%s" % traceback.format_exc())
Esempio n. 28
0
def get_packages_dir():
    ### Read the configuration file
    try:
        cfg = Loader('domogik')
        config = cfg.load()
        conf = dict(config[1])
        return os.path.join(conf['libraries_path'], PACKAGES_DIR)

    except:
        print(u"Error while reading the configuration file '{0}' : {1}".format(
            CONFIG_FILE, traceback.format_exc()))
        return None
Esempio n. 29
0
def get_backup_dir():
    ### Read the configuration file
    try:
        cfg = Loader('backup')
        config = cfg.load()
        conf = dict(config[1])
        return conf['folder']

    except:
        print(u"Error while reading the configuration file '{0}' : {1}".format(
            CONFIG_FILE, traceback.format_exc()))
        return None
Esempio n. 30
0
    def __init__(self, xpl, log):
        cfg = Loader('mq')
        config = cfg.load()
        conf = dict(config[1])
        if conf.has_key('query_mq'):
            self.mq = True
        else:
            self.mq = False

        if self.mq:
            self.qry = QueryMQ(None, log)
        else:
            self.qry = QueryXPL(xpl, log)
Esempio n. 31
0
def main():
    cfg = Loader('mq')
    my_conf = cfg.load()
    config = dict(my_conf[1])

    ctx = DaemonContext()
    ctx.open()

    context = zmq.Context()
    print(("tcp://{0}:{1}".format(config['ip'], config['req_rep_port'])))
    broker = MDPBroker(context, "tcp://{0}:{1}".format(config['ip'], config['req_rep_port']))
    IOLoop.instance().start()
    broker.shutdown()
Esempio n. 32
0
    def __init__(self, xpl, log):
        cfg = Loader("mq")
        config = cfg.load()
        conf = dict(config[1])
        if conf.has_key("query_mq"):
            self.mq = True
        else:
            self.mq = False

        if self.mq:
            self.qry = QueryMQ(None, log)
        else:
            self.qry = QueryXPL(xpl, log)
Esempio n. 33
0
def rest():

    cfg = Loader('rest')
    config = cfg.load()
    conf = dict(config[1])
    port = conf['port']                          

    r = requests.get("http://localhost:{0}/map".format(port))

    return render_template('rest.html',
        mactive="rest",
        urls = r.json(),
        )
Esempio n. 34
0
 def get_stats_files_directory(self):
     """ Return the directory where a plugin developper can store data files
    """
     cfg = Loader('domogik')
     my_conf = cfg.load()
     config = dict(my_conf[1])
     if config.has_key('package_path'):
         path = "%s/domogik_packages/stats/%s" % (config['package_path'],
                                                  self._name)
     else:
         path = "%s/share/domogik/stats/%s" % (config['src_prefix'],
                                               self._name)
     return path
Esempio n. 35
0
def get_rest_url():
    """ Build and return the rest url
    """
    cfg = Loader('rest')
    config = cfg.load()
    conf = dict(config[1])
    ### get REST ip and port
    port = conf['port']
    interfaces = conf['interfaces']
    intf = interfaces.split(',')
    # get the first ip of the first interface declared
    ip = get_ip_for_interfaces(intf)[0]

    return "http://{0}:{1}".format(ip, port)
Esempio n. 36
0
def main():
    cfg = Loader('mq')
    my_conf = cfg.load()
    config = dict(my_conf[1])

    ctx = DaemonContext()
    ctx.open()

    context = zmq.Context()
    print(("tcp://{0}:{1}".format(config['ip'], config['req_rep_port'])))
    broker = MDPBroker(
        context, "tcp://{0}:{1}".format(config['ip'], config['req_rep_port']))
    IOLoop.instance().start()
    broker.shutdown()
Esempio n. 37
0
def get_rest_url():
    """ Build and return the rest url
    """
    cfg = Loader('rest')
    config = cfg.load()
    conf = dict(config[1])
    ### get REST ip and port
    port = conf['port']
    interfaces = conf['interfaces']
    intf = interfaces.split(',')
    # get the first ip of the first interface declared
    ip = get_ip_for_interfaces(intf)[0]

    return "http://{0}:{1}".format(ip, port)
Esempio n. 38
0
    def __init__(self, component_name, domogik_prefix=True, use_filename=None):
        """
        Get a logger with provided parameters and set config
        @param component_name : component name to log
        @param domogik_prefix : if logger name should be prefixed by 'domogik-'
        @param use_filename : if set tells the logger to use this file name (otherwise takes 'component_name')
        """
        if component_name not in self.logger:
            LEVELS = {
                "debug": logging.DEBUG,
                "info": logging.INFO,
                "warning": logging.WARNING,
                "error": logging.ERROR,
                "critical": logging.CRITICAL,
            }

            cfg = Loader()
            config = cfg.load()[0]
            if use_filename is None:
                filename = "%s/%s.log" % (config["log_dir_path"], component_name)
            else:
                filename = "%s/%s.log" % (config["log_dir_path"], use_filename)
            level = config["log_level"]

            if level not in LEVELS:
                raise ValueError(
                    "level must be one of 'debug','info','warning'," "'error','critical'. Check your config."
                )

            if domogik_prefix:
                my_logger = logging.getLogger("domogik-%s" % component_name)
            else:
                my_logger = logging.getLogger(component_name)
            # log to file
            my_logger.propagate = 0
            if not my_logger.handlers:
                hdlr = logging.FileHandler(filename)
                formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s")
                hdlr.setFormatter(formatter)
                my_logger.addHandler(hdlr)

                # if loglevvel is set to debug (all log entries also go to stdout)
                if level == "debug" and component_name.find("sqlalchemy") == -1:
                    dhdlr = logging.StreamHandler(sys.stdout)
                    dhdlr.setFormatter(formatter)
                    my_logger.addHandler(dhdlr)

            my_logger.setLevel(LEVELS[level])
            self.logger[component_name] = my_logger
Esempio n. 39
0
def test_config_files():
    # TODO : /etc/default

    ### /etc/domogik/domogik.cfg
    from domogik.common.configloader import Loader, CONFIG_FILE
    try:
        cfg = Loader('domogik')
        config = cfg.load()
        conf = dict(config[1])

        ok("Configuration file '{0}' can be read".format(CONFIG_FILE))
        return True
    except:
        error(u"Error while reading the configuration file '{0}' : {1}".format(CONFIG_FILE, traceback.format_exc()))
        solution("Make sure the Domogik installation is OK")
        return False
Esempio n. 40
0
def test_config_files():
    # TODO : /etc/default

    ### /etc/domogik/domogik.cfg
    from domogik.common.configloader import Loader, CONFIG_FILE
    try:
        cfg = Loader('domogik')
        config = cfg.load()
        conf = dict(config[1])

        ok("Configuration file '{0}' can be read".format(CONFIG_FILE))
        return True
    except:
        error(u"Error while reading the configuration file '{0}' : {1}".format(CONFIG_FILE, traceback.format_exc()))
        solution("Make sure the Domogik installation is OK")
        return False
Esempio n. 41
0
def api_datatype():
    """
    @api {get} /datatype Retrieve all datatypes
    @apiName getDataTypes
    @apiGroup DataType

    @apiSuccess {json} result The json representation of the datatypes

    @apiSuccessExample Success-Response:
        HTTTP/1.1 200 OK
        {
            "DT_HVACVent": {
                "childs": [],
                "values": {
                    "0": "Auto",
                    "1": "Heat",
                    "2": "Cool",
                    "3": "Fan only",
                    "4": "Dry"
                }
            },
            "DT_DateTime": {
                "childs": [
                    "DT_Date",
                    "DT_Time"
                ],
                "format": "YYYY-MM-DDThh:mm:ss.s"
            },
            "DT_String": {
                "childs": [
                    "DT_Phone",
                    "DT_Hexa",
                    "DT_ColorRGBHexa"
                ],
                "maxLengh": null,
                "format": null
            },
            ...
        }
    """
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    json_file = "{0}/datatypes.json".format(urlHandler.resources_directory)
    data = json.load(open(json_file))

    return 200, data
Esempio n. 42
0
    def __init__(self, server_interfaces, server_port):
        """ Initiate DbHelper, Logs and config
            Then, start HTTP server and give it initialized data
            @param server_interfaces :  interfaces of HTTP server
            @param server_port :  port of HTTP server
        """

        XplPlugin.__init__(self, name = 'admin')
        # logging initialization
        self.log.info(u"Admin Server initialisation...")
        self.log.debug(u"locale : %s %s" % locale.getdefaultlocale())

	try:
            try:
                cfg_rest = Loader('admin')
                config_rest = cfg_rest.load()
                conf_rest = dict(config_rest[1])
                self.interfaces = conf_rest['interfaces']
                self.port = conf_rest['port']
                # if rest_use_ssl = True, set here path for ssl certificate/key
                self.use_ssl = conf_rest['use_ssl']
                self.key_file = conf_rest['ssl_certificate']
                self.cert_file = conf_rest['ssl_key']
            except KeyError:
                # default parameters
                self.interfaces = server_interfaces
                self.port = server_port
		self.use_ssl = False
		self.key_file = ""
		self.cert_file = ""
                self.clean_json = False
            self.log.info(u"Configuration : interfaces:port = %s:%s" % (self.interfaces, self.port))
            # TODO : delete
            #self.db = DbHelper()

 	    # Launch server, stats
            self.log.info(u"Admin Initialisation OK")
            self.add_stop_cb(self.stop_http)
            self.server = None
	    self.start_http()
            # calls the tornado.ioloop.instance().start()
            
            ### Component is ready
            self.ready(0)
            IOLoop.instance().start()
        except :
            self.log.error(u"%s" % self.get_exception())
Esempio n. 43
0
    def __init__(self, listitems, expr):
        '''
        @param listitems a dictionnary discribing items used in the condition
        @param expr : the condition
        '''
        XplPlugin.__init__(self, 'dbmgr')
        self._log = self.get_my_logger()
        self.listitems = listitems
        loader = Loader('trigger')
        config = loader.load()[1]
        self.__expr = expr

        #We should try/catch this bloc in case of undefined method
        #Anyway, if they're not defined, needed value will never be updated,
        #So we prefer raise an exception now
        for k in listitems:
            call = "self.build%slistener(listitems[k])" % k.lower()
            eval(call)
Esempio n. 44
0
    def __init__(self, log=None, params=None):
        AbstractAction.__init__(self, log)
        self.set_description("Make the butler say something.")

        ### Butler configuration elements
        try:
            cfg = Loader('butler')
            config = cfg.load()
            conf = dict(config[1])

            self.butler_name = conf['name']
            self.butler_sex = conf['sex']
        except:
            self._log.error(u"ButlerACtion init : error while reading the configuration file '{0}' : {1}".format(CONFIG_FILE, traceback.format_exc()))

        ### MQ
        self._mq_name = "butler"
        self.zmq = zmq.Context()
        self.pub = MQPub(self.zmq, self._mq_name)
Esempio n. 45
0
def rest():

    cfg = Loader('rest')
    config = cfg.load()
    conf = dict(config[1])
    ### get REST ip and port
    port = conf['port']                          
    interfaces = conf['interfaces']                          
    intf = interfaces.split(',')
    print intf
    # get the first ip of the first interface declared
    ip = get_ip_for_interfaces(intf)[0]


    r = requests.get("http://{0}:{1}/map".format(ip, port))

    return render_template('rest.html',
        mactive="rest",
        urls = r.json(),
        )
Esempio n. 46
0
def load_plugin_library(name, element):
    '''
    Function to call in the bin part of each plugin to load the appropriate library :  from the sources in development mode and from packages folder in package mode
    To do a : from foo import bar
    Do      : bar = load_plugin_library("foo", "bar")
    '''

    # Read config files to get mode
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    if conf.has_key('package_path'):
        print("Load library from packages")
        sys.path.append(conf["package_path"])
        lib_path = "packages.xpl.lib." + name
    else:
        print("Load library from sources")
        lib_path = "domogik.xpl.lib." + name
    imp = __import__(lib_path, globals(), locals(), element)
    ret = "imp." + element
    return eval(ret)
Esempio n. 47
0
def load_plugin_library(name, element):
    '''
    Function to call in the bin part of each plugin to load the appropriate library :  from the sources in development mode and from packages folder in package mode
    To do a : from foo import bar
    Do      : bar = load_plugin_library("foo", "bar")
    '''

    # Read config files to get mode
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    if conf.has_key('package_path'):
        print("Load library from packages")
        sys.path.append(conf["package_path"])
        lib_path = "packages.xpl.lib." + name
    else:
        print("Load library from sources")
        lib_path = "domogik.xpl.lib." + name
    imp = __import__(lib_path, globals(), locals(), element)
    ret = "imp." + element
    return eval(ret)
Esempio n. 48
0
 def get_data_files_directory(self):
     """
    Return the directory where a plugin developper can store data files.
    If the directory doesn't exist, try to create it.
    After that, try to create a file inside it.
    If something goes wrong, generate an explicit exception.
    """
     cfg = Loader('domogik')
     my_conf = cfg.load()
     config = dict(my_conf[1])
     if config.has_key('package_path'):
         path = "%s/domogik_packages/data/%s" % (config['package_path'],
                                                 self._name)
     else:
         path = "%s/share/domogik/data/%s" % (config['src_prefix'],
                                              self._name)
     if os.path.exists(path):
         if not os.access(path, os.W_OK & os.X_OK):
             raise OSError("Can't write in directory %s" % path)
     else:
         try:
             os.mkdir(path, 0770)
             self.log.info("Create directory %s." % path)
         except:
             raise OSError("Can't create directory %s." % path)
     try:
         tmp_prefix = "write_test"
         count = 0
         filename = os.path.join(path, tmp_prefix)
         while (os.path.exists(filename)):
             filename = "{}.{}".format(os.path.join(path, tmp_prefix),
                                       count)
             count = count + 1
         f = open(filename, "w")
         f.close()
         os.remove(filename)
     except:
         raise IOError("Can't create a file in directory %s." % path)
     return path
Esempio n. 49
0
    def __init__(self):
        """ Init
        """
        l = logger.Logger("package")
        l.set_format_mode("messageOnly")
        self.log = l.get_logger()

        parser = ArgumentParser()
        parser.add_argument(
            "-i",
            "--install",
            dest="install",
            help=
            "Install a package from a path, a zip file or an url to a zip file or to a github repository and branch"
        )
        parser.add_argument(
            "-u",
            "--uninstall",
            dest="uninstall",
            help="Uninstall a package. Example : plugin_rfxcom")
        self.options = parser.parse_args()

        # get install path for packages
        cfg = Loader('domogik')
        config = cfg.load()
        conf = dict(config[1])
        self.pkg_path = os.path.join(conf['libraries_path'], PACKAGES_DIR)

        # install a package
        if self.options.install:
            self.install(self.options.install)

        # uninstall a package
        elif self.options.uninstall:
            self.uninstall(self.options.uninstall)

        # no choice : display the list of installed packages
        else:
            self.list_packages()
Esempio n. 50
0
    def __init__(self, xpl, log):
        '''
        Init the query system and connect it to xPL network
        @param xpl : the XplManager instance (usually self.myxpl)
        @param log : a Logger instance (usually took from self.log))
        '''
        self.log = log
        self.__myxpl = xpl
        self.log.debug("Init config query instance")
        self._keys = {}
        self._l = {}
        self._result = None

        # Check in config file is target is forced
        cfg = Loader('domogik')
        config = cfg.load()
        conf = dict(config[1])
        if conf.has_key('config_provider'):
            self.target = "domogik-dbmgr.%s" % conf["config_provider"]
            msg = "Force config provider to '%s'" % self.target
            #print("Query config : %s" % msg)
            self.log.debug(msg)
        else:
            self.target = "*"
        if conf.has_key('query_xpl_timeout'):
            try:
                self.query_timeout = int(conf["query_xpl_timeout"])
                msg = "Set query timeout to '%s' from domogik.cfg" % self.query_timeout
                self.log.debug(msg)
            except ValueError:
                #There is an error in domogik.cfg. Set it to default.
                self.query_timeout = 10
                msg = "Error in domogik.cfg. query_xpl_timeout ('%s') is not an integer." % conf[
                    "query_xpl_timeout"]
                self.log.error(msg)
        else:
            #There is not option in domogik.cfg. Set it to default.
            self.query_timeout = 10
Esempio n. 51
0
    def __init__(self,
                 name=None,
                 url=None,
                 path=None,
                 pkg_type="plugin",
                 data=None):
        """ Read json file of a plugin and make an object from it
            @param name : name of package
            @param url : url of file
            @param path : path of file
            @param pkg_type : package type (default : 'plugin')
                          To use only with name != None
            @param data : json data as a python object. Used by package.py when installing a zip file : the json is read from memory
        """
        json_file = None
        try:
            # load from sources repository
            if name != None:
                # get config
                cfg = Loader('domogik')
                config = cfg.load()
                conf = dict(config[1])

                if pkg_type == "plugin":
                    json_file = "{0}/{1}/{2}_{3}/info.json".format(
                        conf['libraries_path'], PACKAGES_DIR, pkg_type, name)
                    icon_file = "{0}/{1}/{2}_{3}/design/icon.png".format(
                        conf['libraries_path'], PACKAGES_DIR, pkg_type, name)
                # TODO : reactivate later
                #elif pkg_type == "external":
                #    if conf.has_key('package_path'):
                #        json_directory = "%s/domogik_packages/externals/" % (conf['package_path'])
                #    else:
                #        json_directory = "%s/%s" % (conf['src_prefix'], "share/domogik/externals/")
                else:
                    raise PackageException("Type '%s' doesn't exists" %
                                           pkg_type)
                #json_file = "%s/%s.json" % (json_directory, name)

                self.json = json.load(open(json_file))

            elif path != None:
                json_file = path
                icon_file = None
                self.json = json.load(open(json_file))

            elif url != None:
                json_file = url
                icon_file = None
                json_data = urllib2.urlopen(json_file)
                # TODO : there is an error here!!!!!
                self.json = json.load(xml_data)

            elif data != None:
                json_file = None
                icon_file = None
                self.json = data

            self.validate()

            ### complete json
            # identity data
            self.json["identity"]["package_id"] = "%s-%s" % (
                self.json["identity"]["type"], self.json["identity"]["name"])
            self.json["identity"]["icon_file"] = icon_file

            # common configuration items
            auto_startup = {
                "default": False,
                "description":
                "Automatically start the plugin at Domogik startup",
                "key": "auto_startup",
                "name": "Start the plugin with Domogik",
                "required": True,
                "type": "boolean"
            }
            # check that auto_startup key is not already defined in the json
            for config_elt in self.json["configuration"]:
                if config_elt["key"] == "auto_startup":
                    raise PackageException(
                        "Configuration parameter 'auto_startup' has not to be defined in the json file. Please remove it"
                    )
            self.json["configuration"].insert(0, auto_startup)

        except PackageException as exp:
            raise PackageException(exp.value)
        except:
            raise PackageException("Error reading json file : %s : %s" %
                                   (json_file, str(traceback.format_exc())))
Esempio n. 52
0
 def reload_config(self):
     '''
     Load configuration an start proxy server
     '''
     if self.server != None:
         self.stop_http()
         self.server = None
         self.server_process = None
     try:
         cfg_rest = Loader('rest')
         config_rest = cfg_rest.load()
         conf_rest = dict(config_rest[1])
         self.server_ip = conf_rest['rest_server_ip']
         self.server_port = conf_rest['rest_server_port']
     except KeyError:
         self.log.error(
             "Can't retrieve REST configuration from domogik.cfg. Leave plugin."
         )
         self.force_leave()
     try:
         self.proxy_ip = self._config.query('proxy', 'proxy-ip')
         self.proxy_port = self._config.query('proxy', 'proxy-port')
     except:
         self.log.warning(
             "Can't get proxy address configuration from XPL. Use default value."
         )
     finally:
         if self.proxy_ip == None:
             self.proxy_ip = self.server_ip
         try:
             self.proxy_port = int(self.proxy_port)
         except:
             self.proxy_port = str(int(self.server_port) + 1)
     try:
         self.auth_method = self._config.query('proxy', 'auth-method')
     except:
         self.log.warning(
             "Can't get authentification method from XPL. Use basic by default."
         )
     finally:
         if self.auth_method == None:
             self.auth_method = "basic"
     try:
         self.username = self._config.query('proxy', 'username')
         self.password = self._config.query('proxy', 'password')
     except:
         self.log.warning(
             "Can't get username/password from XPL. Use defaults.")
     finally:
         if self.username == None or self.username == "None":
             self.username = "******"
         if self.password == None or self.password == "None":
             self.password = "******"
     try:
         boo = self._config.query('proxy', 'use-ssl')
         #print boo
         self.use_ssl = True if boo == "True" else False
         if self.use_ssl:
             cfg_rest = Loader('rest')
             config_rest = cfg_rest.load()
             conf_rest = dict(config_rest[1])
             self.ssl_certificate = conf_rest['rest_ssl_certificate']
     except KeyError:
         self.log.warning(
             "Can't get ssl configuration from XPL. Do not use it.")
         self.use_ssl = False
         self.ssl_certificate = None
     self.log.info("Proxy configuration  : ip:port = %s:%s" %
                   (self.proxy_ip, self.proxy_port))
     if self.use_ssl == True:
         self.log.info(
             "Proxy configuration : SSL support activated (certificate : %s)"
             % self.ssl_certificate)
     else:
         self.log.info("Proxy configuration : SSL support not activated")
     self.log.info("Proxy authentification  : %s" % (self.auth_method))
     self.log.info("Rest configuration : ip:port = %s:%s" %
                   (self.server_ip, self.server_port))
     try:
         self.start_http()
     except:
         self.log.error("Can't start proxy. Leave plugin.")
         self.log.error("%s" %
                        str(traceback.format_exc()).replace('"', "'"))
         self.force_leave()
Esempio n. 53
0
    def __init__(self, server_ip, server_port):
        """ Initiate DbHelper, Logs and config
            Then, start HTTP server and give it initialized data
            @param server_ip :  ip of HTTP server
            @param server_port :  port of HTTP server
        """

        XplPlugin.__init__(self, name='rest')
        # logging initialization
        self.log.info("Rest Server initialisation...")
        self.log.debug("locale : %s %s" % locale.getdefaultlocale())

        # logging Queue activities
        log_queue = logger.Logger('rest-queues')
        self.log_queue = log_queue.get_logger('rest-queues')
        self.log_queue.info("Rest's queues activities...")

        # logging data manipulation initialization
        log_dm = logger.Logger('rest-dm')
        self.log_dm = log_dm.get_logger('rest-dm')
        self.log_dm.info("Rest Server Data Manipulation...")

        # API version
        self._rest_api_version = REST_API_VERSION

        # Hosts list
        self._hosts_list = {
            self.get_sanitized_hostname(): {
                "id": self.get_sanitized_hostname(),
                "status": "on",
                "primary": True,
                "last_seen": time.time(),
                "ip": "",
                "interval": "1"
            }
        }

        try:

            ### Config

            # directory data
            cfg = Loader('domogik')
            config = cfg.load()
            conf = dict(config[1])
            self.log_dir_path = conf['log_dir_path']

            # plugin installation path
            if conf.has_key('package_path'):
                self._package_path = conf['package_path']
                self._src_prefix = None
                self.log.info("Set package path to '%s' " % self._package_path)
                print("Set package path to '%s' " % self._package_path)
                self._design_dir = "%s/domogik_packages/design/" % self._package_path
                self.package_mode = True
            else:
                self.log.info("No package path defined in config file")
                self._package_path = None
                self._src_prefix = conf['src_prefix']
                self._design_dir = "%s/share/domogik/design/" % conf[
                    'src_prefix']
                self.package_mode = False

            # HTTP server ip and port
            try:
                cfg_rest = Loader('rest')
                config_rest = cfg_rest.load()
                conf_rest = dict(config_rest[1])
                self.server_ip = conf_rest['rest_server_ip']
                self.server_port = conf_rest['rest_server_port']
            except KeyError:
                # default parameters
                self.server_ip = server_ip
                self.server_port = server_port
            self.log.info("Configuration : ip:port = %s:%s" %
                          (self.server_ip, self.server_port))

            # SSL configuration
            try:
                cfg_rest = Loader('rest')
                config_rest = cfg_rest.load()
                conf_rest = dict(config_rest[1])
                self.use_ssl = conf_rest['rest_use_ssl']
                if self.use_ssl == "True":
                    self.use_ssl = True
                else:
                    self.use_ssl = False
                self.ssl_certificate = conf_rest['rest_ssl_certificate']
            except KeyError:
                # default parameters
                self.use_ssl = USE_SSL
                self.ssl_certificate = SSL_CERTIFICATE
            if self.use_ssl == True:
                self.log.info(
                    "Configuration : SSL support activated (certificate : %s)"
                    % self.ssl_certificate)
            else:
                self.log.info("Configuration : SSL support not activated")

            # File repository
            try:
                cfg_rest = Loader('rest')
                config_rest = cfg_rest.load()
                conf_rest = dict(config_rest[1])
                self.repo_dir = conf_rest['rest_repository']
            except KeyError:
                # default parameters
                self.repo_dir = DEFAULT_REPO_DIR

            # Gloal Queues config
            self.log.debug("Get queues configuration")
            self._config = Query(self.myxpl, self.log)

            self._queue_timeout = self._config.query('rest', 'q-timeout')
            if self._queue_timeout == None:
                self._queue_timeout = QUEUE_TIMEOUT
            self._queue_timeout = float(self._queue_timeout)

            self._queue_package_size = self._config.query('rest', 'q-pkg-size')
            if self._queue_package_size == None:
                self._queue_package_size = QUEUE_PACKAGE_SIZE
            self._queue_package_size = float(self._queue_package_size)

            self._queue_size = self._config.query('rest', 'q-size')
            if self._queue_size == None:
                self._queue_size = QUEUE_SIZE
            self._queue_size = float(self._queue_size)

            self._queue_life_expectancy = self._config.query(
                'rest', 'q-life-exp')
            if self._queue_life_expectancy == None:
                self._queue_life_expectancy = QUEUE_LIFE_EXPECTANCY
            self._queue_life_expectancy = float(self._queue_life_expectancy)

            self._queue_sleep = self._config.query('rest', 'q-sleep')
            if self._queue_sleep == None:
                self._queue_sleep = QUEUE_SLEEP
            self._queue_sleep = float(self._queue_sleep)

            # /command Queues config
            self._queue_command_size = self._config.query('rest', 'q-cmd-size')
            if self._queue_command_size == None:
                self._queue_command_size = QUEUE_COMMAND_SIZE
            self._queue_command_size = float(self._queue_command_size)

            # /event Queues config
            self._event_timeout = self._config.query('rest', 'evt-timeout')
            if self._event_timeout == None:
                self._event_timeout = EVENT_TIMEOUT
            self._event_timeout = float(self._event_timeout)

            self._queue_event_size = self._config.query('rest', 'q-evt-size')
            if self._queue_event_size == None:
                self._queue_event_size = QUEUE_EVENT_SIZE
            self._queue_event_size = float(self._queue_event_size)

            self._queue_event_timeout = self._config.query(
                'rest', 'q-evt-timeout')
            if self._queue_event_timeout == None:
                self._queue_event_timeout = QUEUE_EVENT_TIMEOUT
            self._queue_event_timeout = float(self._queue_event_timeout)

            self._queue_event_life_expectancy = self._config.query(
                'rest', 'q-evt-life-exp')
            if self._queue_event_life_expectancy == None:
                self._queue_event_life_expectancy = QUEUE_EVENT_LIFE_EXPECTANCY
            self._queue_event_life_expectancy = float(
                self._queue_event_life_expectancy)

            # Queues for xPL
            # Queues for packages management
            self._queue_package = Queue(self._queue_package_size)

            # Queues for domogik system actions
            self._queue_system_list = Queue(self._queue_size)
            self._queue_system_detail = Queue(self._queue_size)
            self._queue_system_start = Queue(self._queue_size)
            self._queue_system_stop = Queue(self._queue_size)

            # Queues for /command
            self._queue_command = Queue(self._queue_command_size)

            # Queues for /events/domogik
            self._queue_event_dmg = Queue(self._queue_event_size)

            # Queues for /events/request
            # this queue will be fill by stat manager
            self._event_requests = RequestEvents(
                self.get_stop, self.log, self._event_timeout,
                self._queue_event_size, self._queue_event_timeout,
                self._queue_event_life_expectancy)
            self.add_stop_cb(self._event_requests.set_stop_clean)

            # Queues for /events/domogik
            # this queue will be fill by stat manager
            self._event_dmg = DmgEvents(self.get_stop, self.log,
                                        self._event_timeout,
                                        self._queue_event_size,
                                        self._queue_event_timeout,
                                        self._queue_event_life_expectancy)
            # notice : adding data in queue is made in _add_to_queue_system_list
            self.add_stop_cb(self._event_dmg.set_stop_clean)

            # define listeners for queues
            self.log.debug("Create listeners")
            if self.package_mode == True:
                Listener(self._list_installed_packages, self.myxpl, \
                         {'schema': 'domogik.package',
                          'xpltype': 'xpl-trig',
                          'command' : 'installed-packages-list'})
            Listener(self._add_to_queue_package, self.myxpl, \
                     {'schema': 'domogik.package',
                      'xpltype': 'xpl-trig'})
            Listener(self._add_to_queue_system_list, self.myxpl, \
                     {'schema': 'domogik.system',
                      'xpltype': 'xpl-trig',
                      'command' : 'list'})
            Listener(self._add_to_queue_system_list, self.myxpl, \
                     {'schema': 'domogik.system',
                      'xpltype': 'xpl-trig',
                      'command' : 'enable'})
            Listener(self._add_to_queue_system_list, self.myxpl, \
                     {'schema': 'domogik.system',
                      'xpltype': 'xpl-trig',
                      'command' : 'disable'})
            Listener(self._add_to_queue_system_detail, self.myxpl, \
                     {'schema': 'domogik.system',
                      'xpltype': 'xpl-trig',
                      'command' : 'detail'})
            Listener(self._add_to_queue_system_start, self.myxpl, \
                     {'schema': 'domogik.system',
                      'xpltype': 'xpl-trig',
                      'command' : 'start'})
            Listener(self._add_to_queue_system_stop, self.myxpl, \
                     {'schema': 'domogik.system',
                      'xpltype': 'xpl-trig',
                      'command' : 'stop'})
            Listener(self._add_to_queue_command, self.myxpl, \
                     {'xpltype': 'xpl-trig'})

            # Listener for hosts list
            Listener(self._list_hosts, self.myxpl, \
                     {'schema': 'hbeat.app',
                      'xpltype': 'xpl-stat'})

            # Background process to check if hosts has disappeared
            thr_hbeat = XplTimer(10, \
                                 self._refresh_status_for_list_hosts, \
                                 self.myxpl)
            thr_hbeat.start()

            self._discover_hosts()

            # Enable hbeat
            self.enable_hbeat()

            # Ask for installed packages on all hosts
            # Semaphore init for installed package list update
            self.sema_installed = Semaphore(value=1)
            self._installed_packages = {}
            if self.package_mode == True:
                self._get_installed_packages_from_manager()

            # Launch server, stats
            self.log.info("REST Initialisation OK")
            self.add_stop_cb(self.stop_http)
            self.server = None
            self.start_stats()

            self.start_http()
        except:
            self.log.error("%s" % self.get_exception())
Esempio n. 54
0
    def __init__(self):

        ### Option parser
        parser = ArgumentParser()
        parser.add_argument("-i",
                          action="store_true",
                          dest="interactive",
                          default=False, \
                          help="Butler interactive mode (must be used WITH -f).")

        Plugin.__init__(self, name='butler', parser=parser, log_prefix='core_')

        ### MQ
        # MQ publisher
        #self._mq_name = "interface-{0}.{1}".format(self._name, self.get_sanitized_hostname())
        self._mq_name = "butler"
        #self.zmq = zmq.Context()
        self.pub = MQPub(self.zmq, self._mq_name)

        # subscribe the MQ for interfaces inputs
        self.add_mq_sub('interface.input')
        # devices updates
        self.add_mq_sub('device.update')

        ### Configuration elements
        try:
            cfg = Loader('butler')
            config = cfg.load()
            conf = dict(config[1])

            self.lang = conf['lang']
            self.butler_name = conf['name']
            self.log.debug(u"The butler configured name is '{0}'".format(
                self.butler_name))
            self.butler_name_cleaned = clean_input(conf['name'])
            self.log.debug(u"The butler cleaned name is '{0}'".format(
                self.butler_name_cleaned))
            self.butler_sex = conf['sex']
            self.butler_mood = None
            if self.butler_sex not in SEX_ALLOWED:
                self.log.error(
                    u"Exiting : the butler sex configured is not valid : '{0}'. Expecting : {1}"
                    .format(self.butler_sex, SEX_ALLOWED))
                self.force_leave()
                return

        except:
            self.log.error(
                u"Exiting : error while reading the configuration file '{0}' : {1}"
                .format(CONFIG_FILE, traceback.format_exc()))
            self.force_leave()
            return
        # user name (default is 'localuser')
        # this is not used for now on Domogik side
        self.user_name = "localuser"

        ### Prepare the brain
        # - validate packages

        # Start the brain :)
        self.brain = RiveScript(utf8=True)

        # set rivescript variables

        # Configure bot variables
        # all must be lower case....
        self.log.info(u"Configuring name and sex : {0}, {1}".format(
            self.butler_name_cleaned.lower(), self.butler_sex.lower()))
        self.brain.set_variable(u"name", self.butler_name_cleaned.lower())
        self.brain.set_variable(u"fullname", self.butler_name.lower())
        self.brain.set_variable(u"sex", self.butler_sex.lower())

        # set the PYTHONPATH
        sys.path.append(self.get_libraries_directory())

        # load the brain
        self.brain_content = None
        self.learn_content = None
        self.not_understood_content = None
        self.load_all_brain()

        # shortcut to allow the core brain package to reload the brain for learning
        self.brain.reload_butler = self.reload

        # shortcut to allow the core brain package to do logging and access the devices in memory
        self.brain.log = self.log
        self.brain.devices = []  # will be loaded in self.reload_devices()

        # history
        self.history = []

        # load all known devices
        self.reload_devices()

        self.log.info(
            u"*** Welcome in {0} world, your digital assistant! ***".format(
                self.butler_name))

        # for chat more only
        #self.log.info(u"You may type /quit to let {0} have a break".format(self.butler_name))

        ### Interactive mode
        if self.options.interactive:
            self.log.info(u"Launched in interactive mode : running the chat!")
            # TODO : run as a thread
            #self.run_chat()
            thr_run_chat = Thread(None, self.run_chat, "run_chat", (), {})
            thr_run_chat.start()
        else:
            self.log.info(u"Not launched in interactive mode")

        ### TODO
        #self.add_stop_cb(self.shutdown)

        self.log.info(u"Butler initialized")
        self.ready()