def init(self, app_path):
        """ Initialize configuration after openning the application. """
        self.app_path = app_path

        try:
            with open(os.path.join(app_path, 'version.txt')) as fptr:
                lines = fptr.read().splitlines()
                self.app_version = app_utils.extract_app_version(lines)
        except:
            pass

        parser = argparse.ArgumentParser()
        parser.add_argument('--config', help="Path to a configuration file", dest='config')
        parser.add_argument('--data-dir', help="Root directory for configuration file, cache and log dubdirs",
                            dest='data_dir')
        args = parser.parse_args()

        app_user_dir = ''
        if args.data_dir:
            if os.path.exists(args.data_dir):
                if os.path.isdir(args.data_dir):
                    app_user_dir = args.data_dir
                else:
                    WndUtils.errorMsg('--data-dir parameter doesn\'t point to a directory. Using the default '
                                      'data directory.')
            else:
                WndUtils.errorMsg('--data-dir parameter doesn\'t point to an existing directory. Using the default '
                                  'data directory.')

        if not app_user_dir:
            home_dir = expanduser('~')
            app_user_dir = os.path.join(home_dir, APP_NAME_SHORT)
            if not os.path.exists(app_user_dir):
                os.makedirs(app_user_dir)

        self.cache_dir = os.path.join(app_user_dir, 'cache')
        if not os.path.exists(self.cache_dir):
            os.makedirs(self.cache_dir)
        cache.init(self.cache_dir, self.app_version)
        self.app_last_version = cache.get_value('app_version', '', str)
        self.app_config_file_name = ''

        if args.config is not None:
            self.app_config_file_name = args.config
            if not os.path.exists(self.app_config_file_name):
                msg = 'Config file "%s" does not exist.' % self.app_config_file_name
                print(msg)
                raise Exception(msg)

        if not self.app_config_file_name:
            self.app_config_file_name = os.path.join(app_user_dir, 'config.ini')

        # setup logging
        self.log_dir = os.path.join(app_user_dir, 'logs')
        self.log_file = os.path.join(self.log_dir, 'dmt.log')
        if not os.path.exists(self.log_dir):
            os.makedirs(self.log_dir)

        self.log_level_str = 'INFO'
        log_exists = os.path.exists(self.log_file)
        handler = RotatingFileHandler(filename=self.log_file, mode='a', backupCount=30)
        logger = logging.getLogger()
        formatter = logging.Formatter(fmt='%(asctime)s %(levelname)s |%(threadName)s |%(filename)s |%(funcName)s '
                                          '|%(message)s', datefmt='%Y-%m-%d %H:%M:%S')
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        logger.setLevel(self.log_level_str)
        if log_exists:
            handler.doRollover()
        logging.info('App started')

        # database (SQLITE) cache for caching bigger datasets:
        self.db_cache_file_name = os.path.join(self.cache_dir, 'dmt_cache.db')

        try:
            self.db_intf = DBCache(self.db_cache_file_name)
        except Exception as e:
            logging.exception('SQLite initialization error')

        # directory for configuration backups:
        self.cfg_backup_dir = os.path.join(app_user_dir, 'backup')
        if not os.path.exists(self.cfg_backup_dir):
            os.makedirs(self.cfg_backup_dir)

        try:
            # read configuration from a file
            self.read_from_file()
        except:
            pass

        if not self.app_last_version or \
           app_utils.version_str_to_number(self.app_last_version) < app_utils.version_str_to_number(self.app_version):
            cache.save_data()
        self.initialized = True
Ejemplo n.º 2
0
    def __init__(self, app_path):
        self.app_path = app_path

        # List of Dash network configurations. Multiple conn configs advantage is to give the possibility to use
        # another config if particular one is not functioning (when using "public" RPC service, it could be node's
        # maintanance)
        self.dash_net_configs = []

        # to distribute the load evenly over "public" RPC services, we choose radom connection (from enabled ones)
        # if it is set to False, connections will be used accoording to its order in dash_net_configs list
        self.random_dash_net_config = True

        # list of all enabled dashd configurations (DashNetworkConnectionCfg) - they will be used accourding to
        # the order in list
        self.active_dash_net_configs = []

        # list of misbehaving dash network configurations - they will have the lowest priority during next
        # connections
        self.defective_net_configs = []

        self.hw_type = 'TREZOR'  # TREZOR or KEEPKEY

        self.check_for_updates = True
        self.backup_config_file = True

        self.masternodes = []
        self.last_bip32_base_path = ''
        self.bip32_recursive_search = True
        self.modified = False
        home_dir = expanduser('~')
        app_user_dir = os.path.join(home_dir, APP_NAME_SHORT)
        if not os.path.exists(app_user_dir):
            os.makedirs(app_user_dir)
        self.cache_dir = os.path.join(app_user_dir, 'cache')
        if not os.path.exists(self.cache_dir):
            os.makedirs(self.cache_dir)
        self.app_config_file_name = os.path.join(app_user_dir, 'config.ini')
        cache.init(self.cache_dir)

        # setup logging
        self.log_dir = os.path.join(app_user_dir, 'logs')
        self.log_file = os.path.join(self.log_dir, 'dmt.log')
        if not os.path.exists(self.log_dir):
            os.makedirs(self.log_dir)

        if getattr(sys, 'frozen', False):
            llevel = logging.INFO
        else:
            llevel = logging.DEBUG
        logging.basicConfig(
            filename=self.log_file,
            format='%(asctime)s %(levelname)s | %(funcName)s | %(message)s',
            level=llevel,
            filemode='w',
            datefmt='%Y-%m-%d %H:%M:%S')
        logging.info('App started')

        # directory for configuration backups:
        self.cfg_backup_dir = os.path.join(app_user_dir, 'backup')
        if not os.path.exists(self.cfg_backup_dir):
            os.makedirs(self.cfg_backup_dir)