Exemplo n.º 1
0
def main():
    with tempfile.NamedTemporaryFile() as fp:
        sqlite_db_name = fp.name
        SqliteAccountInfo(sqlite_db_name)
        engine = create_engine('sqlite:///' + sqlite_db_name)

        meta = MetaData()

        meta.reflect(bind=engine)

        tables = set(meta.tables.keys())

        desc = describe(
            map(lambda x: operator.getitem(meta.tables, x), sorted(tables)))
        print(getattr(render, 'dot')(desc).encode('utf-8'))
Exemplo n.º 2
0
    def __init__(self, account_info=None, cache=None, raw_api=None):
        """
        Initialize Session using given account info.

        :param account_info: an instance of :class:`~b2sdk.v1.UrlPoolAccountInfo`,
                      or any custom class derived from
                      :class:`~b2sdk.v1.AbstractAccountInfo`
                      To learn more about Account Info objects, see here
                      :class:`~b2sdk.v1.SqliteAccountInfo`

        :param cache: an instance of the one of the following classes:
                      :class:`~b2sdk.cache.DummyCache`, :class:`~b2sdk.cache.InMemoryCache`,
                      :class:`~b2sdk.cache.AuthInfoCache`,
                      or any custom class derived from :class:`~b2sdk.cache.AbstractCache`
                      It is used by B2Api to cache the mapping between bucket name and bucket ids.
                      default is :class:`~b2sdk.cache.DummyCache`

        :param raw_api: an instance of one of the following classes:
                        :class:`~b2sdk.raw_api.B2RawApi`, :class:`~b2sdk.raw_simulator.RawSimulator`,
                        or any custom class derived from :class:`~b2sdk.raw_api.AbstractRawApi`
                        It makes network-less unit testing simple by using :class:`~b2sdk.raw_simulator.RawSimulator`,
                        in tests and :class:`~b2sdk.raw_api.B2RawApi` in production.
                        default is :class:`~b2sdk.raw_api.B2RawApi`
        """

        self.raw_api = raw_api or B2RawApi(B2Http())
        if account_info is None:
            account_info = SqliteAccountInfo()
            if cache is None:
                cache = AuthInfoCache(account_info)
        if cache is None:
            cache = DummyCache()

        self.account_info = account_info
        self.cache = cache
        self._token_callbacks = {
            TokenType.API: self._api_token_callback,
            TokenType.API_TOKEN_ONLY: self._api_token_only_callback,
            TokenType.UPLOAD_SMALL: self._upload_small,
            TokenType.UPLOAD_PART: self._upload_part,
        }
Exemplo n.º 3
0
    def __init__(self, base=None, storage=None):

        super(B2StorageSystem, self).__init__(location=base, base_url=None)

        self.base = base
        self.storage = storage

        match = re.match(r'^(b2:)?(//)?(\w+)/(.+)/?$', base)
        if not match:
            return

        bucket = match.group(3)
        self.path = match.group(4)

        file_name = os.path.expanduser('~/.b2_account_info-%d' % storage.id)
        self.api = B2Api(SqliteAccountInfo(file_name=file_name))
        self.api.authorize_account(
            'production',
            storage.credential_id,
            storage.credential_key,
        )
        self.bucket = self.api.get_bucket_by_name(bucket)
Exemplo n.º 4
0
 def _createAccountInfoCallable(self, opts: BackblazeB2StorageOptions) -> Callable[[], AbstractAccountInfo]:
     if (
         not isinstance(opts["accountInfo"], dict)
         or "type" not in opts["accountInfo"]
         or opts["accountInfo"]["type"] not in ["memory", "sqlite", "django-cache"]
     ):
         raise ImproperlyConfigured(
             (f"accountInfo property must be a dict with type found in options.py, was {opts['accountInfo']}")
         )
     if opts["accountInfo"]["type"] == "django-cache":
         logger.debug(f"{self.__class__.__name__} will use {DjangoCacheAccountInfo.__name__}")
         return lambda: DjangoCacheAccountInfo(
             cacheName=cast(DjangoCacheAccountInfoConfig, opts["accountInfo"]).get("cache", "django-backblaze-b2")
         )
     elif opts["accountInfo"]["type"] == "memory":
         logger.debug(f"{self.__class__.__name__} will use {InMemoryAccountInfo.__name__}")
         return lambda: InMemoryAccountInfo()
     elif opts["accountInfo"]["type"] == "sqlite":
         logger.debug(f"{self.__class__.__name__} will use {SqliteAccountInfo.__name__}")
         return lambda: SqliteAccountInfo(
             file_name=cast(SqliteAccountInfoConfig, opts["accountInfo"])["databasePath"]
         )
     raise ImproperlyConfigured()
Exemplo n.º 5
0
 def _make_sqlite_account_info(self, last_upgrade_to_run=None):
     """
     Returns a new StoredAccountInfo that has just read the data from the file.
     """
     return SqliteAccountInfo(file_name=self.db_path,
                              last_upgrade_to_run=None)
Exemplo n.º 6
0
    def __init__(self, *, config: Config, name: str,
                 module_configuration: ConfigDict):
        super().__init__(config=config,
                         name=name,
                         module_configuration=module_configuration)

        account_id = Config.get_from_dict(module_configuration,
                                          'accountId',
                                          None,
                                          types=str)
        if account_id is None:
            account_id_file = Config.get_from_dict(module_configuration,
                                                   'accountIdFile',
                                                   types=str)
            with open(account_id_file, 'r') as f:
                account_id = f.read().rstrip()
        application_key = Config.get_from_dict(module_configuration,
                                               'applicationKey',
                                               None,
                                               types=str)
        if application_key is None:
            application_key_file = Config.get_from_dict(module_configuration,
                                                        'applicationKeyFile',
                                                        types=str)
            with open(application_key_file, 'r') as f:
                application_key = f.read().rstrip()

        bucket_name = Config.get_from_dict(module_configuration,
                                           'bucketName',
                                           types=str)

        account_info_file = Config.get_from_dict(module_configuration,
                                                 'accountInfoFile',
                                                 None,
                                                 types=str)
        if account_info_file is not None:
            account_info = SqliteAccountInfo(file_name=account_info_file)
        else:
            account_info = InMemoryAccountInfo()

        b2sdk.bucket.Bucket.MAX_UPLOAD_ATTEMPTS = Config.get_from_dict(
            module_configuration, 'uploadAttempts', types=int)

        self._write_object_attempts = Config.get_from_dict(
            module_configuration, 'writeObjectAttempts', types=int)

        self._read_object_attempts = Config.get_from_dict(module_configuration,
                                                          'readObjectAttempts',
                                                          types=int)

        self.service = b2sdk.api.B2Api(account_info)
        if account_info_file is not None:
            try:
                # This temporarily disables all logging as the b2 library does some very verbose logging
                # of the exception we're trying to catch here...
                logging.disable(logging.ERROR)
                _ = self.service.get_account_id()
                logging.disable(logging.NOTSET)
            except MissingAccountData:
                self.service.authorize_account('production', account_id,
                                               application_key)
        else:
            self.service.authorize_account('production', account_id,
                                           application_key)

        self.bucket = self.service.get_bucket_by_name(bucket_name)

        # Check bucket configuration
        bucket_type = self.bucket.type_
        if bucket_type != 'allPrivate':
            logger.warning(
                f'The type of bucket {bucket_name} is {bucket_type}. '
                'It is strongly recommended to set it to allPrivate.')
Exemplo n.º 7
0
 def __init__(self, arg):
     """Initialize B2 connection and credentials."""
     info = SqliteAccountInfo()  # creds and tokens in ~/.b2_account_info
     b2_api = B2Api(info)
     self.arg = arg