diff --git a/doc.md b/doc.md index c5092bf..87c4cc3 100644 --- a/doc.md +++ b/doc.md @@ -49,9 +49,9 @@ ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') ``` **Return Type** @@ -69,9 +69,9 @@ A Client Object ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') ``` @@ -93,9 +93,9 @@ None ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') is_readonly = repo.is_readonly() ``` @@ -114,18 +114,18 @@ None ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo_list = client.repos.list_repos() print repo_list - Out >>> [, - , - , - , - , - ] + Out >>> [, + , + , + , + , + ] print [repo.name for repo in repo_list] Out >>> ['alphabox', @@ -151,9 +151,9 @@ A list of Libraries Object ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.create_repo('test_repo') ``` @@ -172,9 +172,9 @@ None ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') repo.delete() ``` @@ -194,9 +194,9 @@ None ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') seafdir = repo.get_dir('/root') print seafdir.__dict__ @@ -204,7 +204,7 @@ None 'entries': [], 'id': 'c3742dd86004d51c358845fa3178c87e4ab3aa60', 'path': '/root', - 'repo': , + 'repo': , 'size': 0} ``` @@ -225,9 +225,9 @@ A Directory Object ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') seafdir = repo.get_dir('/root') @@ -265,9 +265,9 @@ List of Directory and File ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') seafdir = repo.get_dir('/root') @@ -287,9 +287,9 @@ None ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') seafdir = repo.get_dir('/root') @@ -312,9 +312,9 @@ A Response Instance ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') seaffile = repo.get_file('/root/test.md') @@ -322,7 +322,7 @@ A Response Instance Out >>> {'client': SeafileApiClient[server=http://127.0.0.1:8000, user=admin@admin.com], 'id': '0000000000000000000000000000000000000000', 'path': '/root/test.md', - 'repo': , + 'repo': , 'size': 0} ``` @@ -344,9 +344,9 @@ None ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') seaffile = repo.get_file('/root/test.md') @@ -366,9 +366,9 @@ File Content ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') seafdir = repo.get_dir('/root') @@ -390,9 +390,9 @@ A File Object of new empty file ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') seafdir = repo.get_dir('/root') @@ -417,9 +417,9 @@ None ```python - import seafileapi + import python_seafile - client = seafileapi.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') + client = python_seafile.connect('http://127.0.0.1:8000', 'test@admin.com', 'password') repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507') seaffile = repo.get_file('/root/test.md') diff --git a/pytest.ini b/pytest.ini index bcac882..2fed149 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,2 +1,2 @@ [pytest] -addopts = -s -v --doctest-modules --ignore=build --ignore=dist --ignore=seafileapi.egg-info --ignore=setup.py +addopts = -s -v --doctest-modules --ignore=build --ignore=dist --ignore=python_seafile.egg-info --ignore=setup.py diff --git a/seafileapi/__init__.py b/python_seafile/__init__.py similarity index 54% rename from seafileapi/__init__.py rename to python_seafile/__init__.py index d6c3b8d..a0b5f37 100644 --- a/seafileapi/__init__.py +++ b/python_seafile/__init__.py @@ -1,4 +1,5 @@ -from seafileapi.client import SeafileApiClient +from python_seafile.client import SeafileApiClient +from python_seafile._version import __version__ def connect(server, username, password): client = SeafileApiClient(server, username, password) diff --git a/python_seafile/_version.py b/python_seafile/_version.py new file mode 100644 index 0000000..48fef32 --- /dev/null +++ b/python_seafile/_version.py @@ -0,0 +1 @@ +__version__ = "0.1.2" \ No newline at end of file diff --git a/seafileapi/admin.py b/python_seafile/admin.py similarity index 100% rename from seafileapi/admin.py rename to python_seafile/admin.py diff --git a/seafileapi/client.py b/python_seafile/client.py similarity index 72% rename from seafileapi/client.py rename to python_seafile/client.py index 52a6ea6..1f4bb88 100644 --- a/seafileapi/client.py +++ b/python_seafile/client.py @@ -1,7 +1,12 @@ import requests -from seafileapi.utils import urljoin -from seafileapi.exceptions import ClientHttpError -from seafileapi.repos import Repos +from python_seafile.utils import urljoin +from python_seafile.exceptions import ClientHttpError +from python_seafile.repos import Repos +from os.path import getsize + +from requests_toolbelt import MultipartEncoder + +MAX_SIZE = 2147483647 #https://github.com/psf/requests/issues/2717 class SeafileApiClient(object): """Wraps seafile web api""" @@ -60,6 +65,15 @@ def _send_request(self, method, url, *args, **kwargs): expected = kwargs.pop('expected', 200) if not hasattr(expected, '__iter__'): expected = (expected, ) + if 'files' in kwargs and hasattr(kwargs['files']['file'][1], 'name') and getsize(kwargs['files']['file'][1].name) > MAX_SIZE: + #see https://github.com/psf/requests/issues/2717#issuecomment-724725392 + m = MultipartEncoder( + fields={'file': (kwargs['files']['file'][1].name, open(kwargs['files']['file'][1].name, 'rb'), 'text/plain'), + 'parent_dir': kwargs['files']['parent_dir']} + ) + del kwargs['files'] + kwargs['data'] = m + kwargs['headers']['Content-Type'] = m.content_type resp = requests.request(method, url, *args, **kwargs) if resp.status_code not in expected: msg = 'Expected %s, but get %s' % \ diff --git a/seafileapi/exceptions.py b/python_seafile/exceptions.py similarity index 100% rename from seafileapi/exceptions.py rename to python_seafile/exceptions.py diff --git a/seafileapi/files.py b/python_seafile/files.py similarity index 99% rename from seafileapi/files.py rename to python_seafile/files.py index ed01e64..4fda80f 100644 --- a/seafileapi/files.py +++ b/python_seafile/files.py @@ -2,7 +2,7 @@ import os import posixpath import re -from seafileapi.utils import querystr +from python_seafile.utils import querystr ZERO_OBJ_ID = '0000000000000000000000000000000000000000' @@ -185,7 +185,7 @@ def upload_local_file(self, filepath, name=None): Return a :class:`SeafFile` object of the newly uploaded file. """ name = name or os.path.basename(filepath) - with open(filepath, 'r') as fp: + with open(filepath, 'rb') as fp: return self.upload(fp, name) def _get_upload_link(self): diff --git a/seafileapi/group.py b/python_seafile/group.py similarity index 100% rename from seafileapi/group.py rename to python_seafile/group.py diff --git a/seafileapi/repo.py b/python_seafile/repo.py similarity index 96% rename from seafileapi/repo.py rename to python_seafile/repo.py index 01811a2..8541c52 100644 --- a/seafileapi/repo.py +++ b/python_seafile/repo.py @@ -1,6 +1,6 @@ from urllib.parse import urlencode -from seafileapi.files import SeafDir, SeafFile -from seafileapi.utils import raise_does_not_exist +from python_seafile.files import SeafDir, SeafFile +from python_seafile.utils import raise_does_not_exist class Repo(object): """ diff --git a/seafileapi/repos.py b/python_seafile/repos.py similarity index 67% rename from seafileapi/repos.py rename to python_seafile/repos.py index 70a8fa7..7bf9822 100644 --- a/seafileapi/repos.py +++ b/python_seafile/repos.py @@ -1,5 +1,5 @@ -from seafileapi.repo import Repo -from seafileapi.utils import raise_does_not_exist +from python_seafile.repo import Repo +from python_seafile.utils import raise_does_not_exist class Repos(object): def __init__(self, client): @@ -21,6 +21,15 @@ def get_repo(self, repo_id): repo_json = self.client.get('/api2/repos/' + repo_id).json() return Repo.from_json(self.client, repo_json) + @raise_does_not_exist('The requested library does not exist') + def get_default_repo(self): + """Get the default repo. + + Raises :exc:`DoesNotExist` if no default repo exists. + """ + repo_json = self.client.get('/api2/default-repo').json() + return self.get_repo(repo_json['repo_id']) + def list_repos(self): repos_json = self.client.get('/api2/repos/').json() return [Repo.from_json(self.client, j) for j in repos_json] diff --git a/seafileapi/utils.py b/python_seafile/utils.py similarity index 95% rename from seafileapi/utils.py rename to python_seafile/utils.py index 7903414..ce05468 100644 --- a/seafileapi/utils.py +++ b/python_seafile/utils.py @@ -2,7 +2,7 @@ import random from functools import wraps from urllib.parse import urlencode -from seafileapi.exceptions import ClientHttpError, DoesNotExist +from python_seafile.exceptions import ClientHttpError, DoesNotExist def randstring(length=0): if length == 0: diff --git a/requirements.txt b/requirements.txt index f229360..d78f2d4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ requests +requests-toolbelt diff --git a/setup.py b/setup.py index d2165df..af1c9fb 100644 --- a/setup.py +++ b/setup.py @@ -1,20 +1,31 @@ from setuptools import setup, find_packages +import re -__version__ = '0.1.1' +def get_version(): + #https://stackoverflow.com/a/7071358/5122790 + VERSIONFILE="python_seafile/_version.py" + verstrline = open(VERSIONFILE, "rt").read() + VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]" + mo = re.search(VSRE, verstrline, re.M) + if mo: + return mo.group(1) + else: + raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,)) -setup(name='seafileapi', - version=__version__, + +setup(name='python_seafile', + version=get_version(), license='BSD', description='Client interface for Seafile Web API', author='Shuai Lin', - author_email='linshuai2012@gmail.com', + author_email='linshuai2012@gmail.com, cstenkamp@uos.de', url='http://seafile.com', platforms=['Any'], packages=find_packages(), - install_requires=['requests'], + install_requires=['requests', 'requests-toolbelt'], classifiers=['Development Status :: 4 - Beta', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', - 'Programming Language :: Python'], + 'Programming Language :: Python 3'], ) diff --git a/tests/fixtures.py b/tests/fixtures.py index 9247e32..b57ae83 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -3,7 +3,7 @@ import os import pytest -import seafileapi +import python_seafile from tests.utils import randstring SERVER = os.environ.get('SEAFILE_TEST_SERVER_ADDRESS', 'http://127.0.0.1:8000') @@ -14,7 +14,7 @@ @pytest.fixture(scope='session') def client(): - return seafileapi.connect(SERVER, USER, PASSWORD) + return python_seafile.connect(SERVER, USER, PASSWORD) @pytest.yield_fixture(scope='function') def repo(client): diff --git a/tests/test_repos.py b/tests/test_repos.py index d1ab642..f8b07ae 100644 --- a/tests/test_repos.py +++ b/tests/test_repos.py @@ -2,7 +2,7 @@ import pytest -from seafileapi.exceptions import DoesNotExist +from python_seafile.exceptions import DoesNotExist from tests.utils import randstring def test_create_delete_repo(client):