コード例 #1
0
ファイル: client.py プロジェクト: leoj3n/blockstack-cli
def session(conf=None, config_path=CONFIG_PATH, server_host=None, server_port=None,
            storage_drivers=None, metadata_dir=None, spv_headers_path=None, set_global=False):

    """
    Create a blockstack session:
    * validate the configuration
    * load all storage drivers
    * initialize all storage drivers
    * load an API proxy to blockstack

    conf's fields override specific keyword arguments.

    Returns the API proxy object.
    """

    if conf is None and config_path is not None:
        conf = get_config(config_path)

    if conf is not None:
        if server_host is None:
            server_host = conf['server']
        if server_port is None:
            server_port = conf['port']
        if storage_drivers is None:
            storage_drivers = conf['storage_drivers']
        if metadata_dir is None:
            metadata_dir = conf['metadata']
        if spv_headers_path is None:
            spv_headers_path = conf['blockchain_headers']

    if storage_drivers is None:
        log.error("No storage driver(s) defined in the config file.  Please set 'storage=' to a comma-separated list of drivers")
        sys.exit(1)

    # create proxy
    log.debug('Connect to {}:{}'.format(server_host, server_port))
    proxy = BlockstackRPCClient(server_host, server_port)

    # load all storage drivers
    for storage_driver in storage_drivers.split(","):
        storage_impl = load_storage(storage_driver)
        if storage_impl is None:
            log.error("Failed to load storage driver '%s'" % (storage_driver))
            sys.exit(1)

        rc = register_storage(storage_impl, conf)
        if not rc:
            log.error("Failed to initialize storage driver '%s' (%s)" % (storage_driver, rc))
            sys.exit(1)

    # initialize SPV
    SPVClient.init(spv_headers_path)
    proxy.spv_headers_path = spv_headers_path
    proxy.conf = conf

    if set_global:
        set_default_proxy( proxy )

    return proxy
コード例 #2
0
def session(conf=None, config_path=CONFIG_PATH, server_host=None, server_port=None, wallet_password=None,
            storage_drivers=None, metadata_dir=None, spv_headers_path=None, set_global=False):
    """
    Create a blockstack session:
    * validate the configuration
    * load all storage drivers
    * initialize all storage drivers
    * load an API proxy to blockstack

    conf's fields override specific keyword arguments.

    Returns the API proxy object.
    """

    if set_global:
        if server_host is not None:
            os.environ['BLOCKSTACK_CLI_SERVER_HOST'] = server_host

        if server_port is not None:
            os.environ['BLOCKSTACK_CLI_SERVER_PORT'] = str(server_port)

    if conf is None:
        conf = get_config(config_path)
        if conf is None:
            log.error("Failed to read configuration file {}".format(config_path))
            return None 

        conf_version = conf.get('client_version', '')
        if not semver_match(conf_version, VERSION):
            log.error("Failed to use legacy configuration file {}".format(config_path))
            return None

    if conf is not None:
        if server_host is None:
            server_host = conf['server']
        if server_port is None:
            server_port = conf['port']
        if storage_drivers is None:
            storage_drivers = conf['storage_drivers']
        if metadata_dir is None:
            metadata_dir = conf['metadata']
        if spv_headers_path is None:
            spv_headers_path = conf['bitcoind_spv_path']

    if storage_drivers is None:
        msg = ('No storage driver(s) defined in the config file. '
               'Please set "storage=" to a comma-separated list of drivers')
        log.error(msg)
        sys.exit(1)

    # create proxy
    log.debug('Connect to {}:{}'.format(server_host, server_port))
    proxy = BlockstackRPCClient(server_host, server_port)

    # load all storage drivers
    loaded = []
    for storage_driver in storage_drivers.split(','):
        storage_impl = load_storage(storage_driver)
        if storage_impl is None:
            log.error('Failed to load storage driver "{}"'.format(storage_driver))
            sys.exit(1)
        loaded.append(storage_driver)
        rc = register_storage(storage_impl, conf)
        if not rc:
            log.error('Failed to initialize storage driver "{}" ({})'.format(storage_driver, rc))
            sys.exit(1)
    log.debug('Loaded storage drivers {}'.format(loaded))
    # initialize SPV
    SPVClient.init(spv_headers_path)
    proxy.spv_headers_path = spv_headers_path
    proxy.conf = conf

    if set_global:
        set_default_proxy(proxy)

    return proxy