Ejemplo n.º 1
0
    def __init__(self, **kwargs):
        """Initializes DropBoxInterface instance, creates dbx instance
        :param kwargs:
        """
        # try to find token from given kwargs arguments or from os environment
        self.token = kwargs.pop('dropbox_token', None)
        self.root = kwargs.pop('dropbox_root', None)

        if not self.token:
            self.token = os.environ.get('DROPBOX_TOKEN')
        if self.root is None:
            self.root = str2bool(os.environ.get('DROPBOX_ROOT'))
        if not self.root:
            self.root = False

        if not self.token:
            raise ValueError('Please specify dropbox app access key')

        self._encoding = 'utf8'
        self._mode = None
        self._current_path = None
        self._write_mode = None
        self.metadata = None
        self.path = None
        self.list_recursive = False
        self.include_folders = False

        self.dbx = dropbox.Dropbox(self.token)

        # namespace id starts from root
        if self.root:
            root_namespace_id = self.dbx.users_get_current_account().root_info.root_namespace_id
            self.dbx = self.dbx.with_path_root(PathRoot.namespace_id(root_namespace_id))
Ejemplo n.º 2
0
    def test_path_root_err(self, dbx):
        # verify invalid namespace return is_no_permission error
        dbxpr = dbx.with_path_root(PathRoot.namespace_id("1234567890"))
        with self.assertRaises(PathRootError) as cm:
            dbxpr.files_list_folder('')
        self.assertTrue(cm.exception.error.is_no_permission())

        dbxpr = dbx.with_path_root(PathRoot.root("1234567890"))
        with self.assertRaises(PathRootError) as cm:
            dbxpr.files_list_folder('')
        self.assertTrue(cm.exception.error.is_invalid_root())
Ejemplo n.º 3
0
    def test_path_root_err(self, dbx):
        # verify invalid namespace return is_no_permission error
        dbxpr = dbx.with_path_root(PathRoot.namespace_id("1234567890"))
        with self.assertRaises(PathRootError) as cm:
            dbxpr.files_list_folder('')
        self.assertTrue(cm.exception.error.is_no_permission())

        dbxpr = dbx.with_path_root(PathRoot.root("1234567890"))
        with self.assertRaises(PathRootError) as cm:
            dbxpr.files_list_folder('')
        self.assertTrue(cm.exception.error.is_invalid_root())
Ejemplo n.º 4
0
    def test_path_root_err(self, dbx_from_env):
        # verify invalid namespace return is_no_permission error
        dbxpr = dbx_from_env.with_path_root(
            PathRoot.namespace_id("1234567890"))
        with pytest.raises(PathRootError) as cm:
            dbxpr.files_list_folder('')
        assert cm.value.error.is_no_permission()

        dbxpr = dbx_from_env.with_path_root(PathRoot.root("1234567890"))
        with pytest.raises(PathRootError) as cm:
            dbxpr.files_list_folder('')
        assert cm.value.error.is_invalid_root()
Ejemplo n.º 5
0
    def test_path_root(self, dbx):
        root_info = dbx.users_get_current_account().root_info
        root_ns = root_info.root_namespace_id
        home_ns = root_info.home_namespace_id

        # verify home mode
        dbxpr = dbx.with_path_root(PathRoot.home)
        dbxpr.files_list_folder('')

        # verify root mode
        dbxpr = dbx.with_path_root(PathRoot.root(root_ns))
        dbxpr.files_list_folder('')

        # verify namespace_id mode
        dbxpr = dbx.with_path_root(PathRoot.namespace_id(home_ns))
        dbxpr.files_list_folder('')
Ejemplo n.º 6
0
    def test_with_path_root_constructor(self, dbx):
        # Verify valid mode types
        for path_root in (
            PathRoot.home,
            PathRoot.root("123"),
            PathRoot.namespace_id("123"),
        ):
            dbx_new = dbx.with_path_root(path_root)
            self.assertIsNot(dbx_new, dbx)

            expected = stone_serializers.json_encode(PathRoot_validator, path_root)
            self.assertEqual(dbx_new._headers.get(PATH_ROOT_HEADER), expected)

        # verify invalid mode raises ValueError
        with self.assertRaises(ValueError):
            dbx.with_path_root(None)
Ejemplo n.º 7
0
    def test_path_root(self, dbx):
        root_info = dbx.users_get_current_account().root_info
        root_ns = root_info.root_namespace_id
        home_ns = root_info.home_namespace_id

        # verify home mode
        dbxpr = dbx.with_path_root(PathRoot.home)
        dbxpr.files_list_folder('')

        # verify root mode
        dbxpr = dbx.with_path_root(PathRoot.root(root_ns))
        dbxpr.files_list_folder('')

        # verify namespace_id mode
        dbxpr = dbx.with_path_root(PathRoot.namespace_id(home_ns))
        dbxpr.files_list_folder('')
Ejemplo n.º 8
0
    def test_with_path_root_constructor(self, dbx):
        # Verify valid mode types
        for path_root in (
            PathRoot.home,
            PathRoot.root("123"),
            PathRoot.namespace_id("123"),
        ):
            dbx_new = dbx.with_path_root(path_root)
            self.assertIsNot(dbx_new, dbx)

            expected = stone_serializers.json_encode(PathRoot_validator, path_root)
            self.assertEqual(dbx_new._headers.get(PATH_ROOT_HEADER), expected)

        # verify invalid mode raises ValueError
        with self.assertRaises(ValueError):
            dbx.with_path_root(None)
Ejemplo n.º 9
0
    def test_with_path_root_constructor(self, dbx_from_env):
        # Verify valid mode types
        for path_root in (
                PathRoot.home,
                PathRoot.root("123"),
                PathRoot.namespace_id("123"),
        ):
            dbx_new = dbx_from_env.with_path_root(path_root)
            assert dbx_new is not dbx_from_env

            expected = stone_serializers.json_encode(PathRoot_validator,
                                                     path_root)
            assert dbx_new._headers.get(PATH_ROOT_HEADER) == expected

        # verify invalid mode raises ValueError
        with pytest.raises(ValueError):
            dbx_from_env.with_path_root(None)