当前位置: 首页>>代码示例>>Python>>正文


Python StoqConfig.get_config_directory方法代码示例

本文整理汇总了Python中stoqlib.lib.configparser.StoqConfig.get_config_directory方法的典型用法代码示例。如果您正苦于以下问题:Python StoqConfig.get_config_directory方法的具体用法?Python StoqConfig.get_config_directory怎么用?Python StoqConfig.get_config_directory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stoqlib.lib.configparser.StoqConfig的用法示例。


在下文中一共展示了StoqConfig.get_config_directory方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ShellDatabaseConnection

# 需要导入模块: from stoqlib.lib.configparser import StoqConfig [as 别名]
# 或者: from stoqlib.lib.configparser.StoqConfig import get_config_directory [as 别名]
class ShellDatabaseConnection(object):
    """Sets up a database connection

      - Connects to a database
      - Telling why if it failed
      - Runs database wizard if needed
      - Runs schema migration
      - Activates plugins
      - Sets up main branch
    """

    def __init__(self, options):
        self._options = options
        self._config = None
        self._ran_wizard = False

    def connect(self):
        self._load_configuration()
        self._maybe_run_first_time_wizard()
        self._try_connect()
        self._post_connect()

    def _load_configuration(self):
        from stoqlib.lib.configparser import StoqConfig
        log.debug('reading configuration')
        self._config = StoqConfig()
        if self._options.filename:
            self._config.load(self._options.filename)
        else:
            self._config.load_default()

    def _maybe_run_first_time_wizard(self):
        from stoqlib.gui.base.dialogs import run_dialog
        from stoq.gui.config import FirstTimeConfigWizard

        config_file = self._config.get_filename()
        if self._options.wizard or not os.path.exists(config_file):
            run_dialog(FirstTimeConfigWizard, None, self._options)
            self._ran_wizard = True

        if self._config.get('Database', 'enable_production') == 'True':
            run_dialog(FirstTimeConfigWizard, None, self._options, self._config)
            self._ran_wizard = True

    def _get_password(self):
        import binascii
        configdir = self._config.get_config_directory()
        filename = os.path.join(configdir, 'data')
        if not os.path.exists(filename):
            return

        with open(filename, 'rb') as f:
            return binascii.a2b_base64(f.read()).decode()

    def _try_connect(self):
        from stoqlib.lib.message import error
        try:
            store_uri = self._config.get_settings().get_store_uri()
        except Exception:
            type, value, trace = sys.exc_info()
            error(_("Could not open the database config file"),
                  _("Invalid config file settings, got error '%s', "
                    "of type '%s'") % (value, type))

        from stoqlib.database.exceptions import PostgreSQLError
        from stoqlib.database.runtime import get_default_store
        from stoqlib.exceptions import DatabaseError
        from stoqlib.lib.pgpass import write_pg_pass
        from stoq.lib.startup import setup

        # XXX: progress dialog for connecting (if it takes more than
        # 2 seconds) or creating the database
        log.debug('calling setup()')
        try:
            setup(self._config, self._options, register_station=False,
                  check_schema=False, load_plugins=False)
            # the setup call above is not really trying to connect (since
            # register_station, check_schema and load_plugins are all False).
            # Try to really connect here.
            get_default_store()
        except (StoqlibError, PostgreSQLError) as e:
            log.debug('Connection failed.')
            error(_('Could not connect to the database'),
                  'error=%s uri=%s' % (str(e), store_uri))
        except DatabaseError:
            log.debug('Connection failed. Tring to setup .pgpass')
            # This is probably a missing password configuration. Setup the
            # pgpass file and try again.
            try:
                password = self._get_password()
                if not password:
                    # There is no password stored in data file. Abort
                    raise
                from stoqlib.database.settings import db_settings
                write_pg_pass(db_settings.dbname, db_settings.address,
                              db_settings.port, db_settings.username, password)
                # Now that there is a pg_pass file, try to connect again
                get_default_store()
            except DatabaseError as e:
                log.debug('Connection failed again.')
#.........这里部分代码省略.........
开发者ID:hackedbellini,项目名称:stoq,代码行数:103,代码来源:shell.py


注:本文中的stoqlib.lib.configparser.StoqConfig.get_config_directory方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。