solid-file-python
solid-file-python copied to clipboard
High-level file operation methods
Support high-level methods (in the file system context), so it will be more convenient for users. rename, move, delete_folder, copy, copy_folder, copy_file, for example. Search 'Not implemented' in https://github.com/twonote/solid-file-python/blob/master/src/solid/solid_api.py,
We don't have to implement all of these in one shot. Just part of them is also welcomed.
❣
請問修改後的檔案該如何上傳或merge?
可能得花時間消化一下,今天來不及有具體成果。
不好意思,前幾天我的團隊PM告知馬上有新project要開始,所以大概沒有時間幫忙做這個開源專案了。感謝主持人給予機會,有負所托,非常抱歉。
以下是09-26那天我練習做的code,只是幫忙改了些錯字,加一兩個註解,以及試寫patch_file(),但不知對不對。
from enum import Enum from typing import Optional, Union, Dict, Callable, Iterable, AsyncIterable, List
import httpx from httpx import Response, HTTPStatusError
from solid.auth import Auth from solid.utils.api_util import get_root_url, LINK, get_parent_url, get_item_name from solid.utils.folder_utils import parse_folder_response
class MERGE(Enum): REPLACE = 'replace' KEEP_SOURCE = 'keep_source' KEEP_TARGET = 'keep_target'
class LINKS(Enum): EXCLUDE = 'exclude' INCLUDE = 'include' INCLUDE_POSSIBLE = 'include_possible'
class AGENT(Enum): NO_MODIFY = 'no_modify' TO_TARGET = 'to_target' TO_SOURCE = 'to_source'
class WriteOptions: def init(self, create_path: bool = True, with_acl: bool = True, agent: AGENT = AGENT.NO_MODIFY, with_meta: bool = True, merge: MERGE = MERGE.REPLACE): self.create_path: bool = create_path self.with_acl: bool = with_acl self.agent: AGENT = agent self.with_meta: bool = with_meta self.merge: MERGE = merge
class ReadFolderOptions: def init(self): self.links: LINKS = LINKS.EXCLUDE.value
class SolidAPIOptions: def init(self): self.enable_logging: bool = False
class Links: def init(self): self.acl = None self.meta = None
class Item: def init(self): self.url = None self.name = None self.parent = None self.itemType = None # "Container" | "Resource" self.links: Optional[Links] = None
class FolderData: def init(self): self.url = None self.name = None self.parent = None self.links: Links = None self.type = 'folder' self.folders: List[Item] = None self.files: List[Item] = None
RequestContent = Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]]
class SolidAPI: def init(self, auth=None): if not auth: auth = Auth() self.auth = auth
def fetch(self, method, url, options: Dict = None) -> Response:
if not options:
options = {}
# options['verify'] = False
r = self.auth.client.request(method, url, **options)
# r= httpx.request(method, url, **options)
r.raise_for_status()
return r
def get(self, url, options: Dict = None) -> Response:
return self.fetch('GET', url, options)
def delete(self, url, options: Dict = None) -> Response:
return self.fetch('DELETE', url, options)
def post(self, url, options: Dict = None) -> Response:
return self.fetch('POST', url, options)
def put(self, url, options: Dict = None) -> Response:
return self.fetch('PUT', url, options)
def patch(self, url, options: Dict = None) -> Response:
return self.fetch('PATCH', url, options)
def head(self, url, options: Dict = None) -> Response:
return self.fetch('HEAD', url, options)
def option(self, url, options: Dict = None) -> Response:
return self.fetch('OPTION', url, options)
def item_exists(self, url) -> bool:
try:
self.head(url)
return True
except HTTPStatusError as e:
if e.response.status_code == 404:
return False
else:
raise e
def post_item(self, url, content: RequestContent, content_type, link: LINK,
options: WriteOptions = WriteOptions(create_path=True)) -> Response:
parent_url = get_parent_url(url)
if options.create_path:
self.create_folder(parent_url)
request_options = {
'headers': {
'Link': link.value,
'Slug': get_item_name(url),
'Content-Type': content_type,
},
'content': content
}
return self.post(parent_url, request_options)
def create_folder(self, url, options: WriteOptions = WriteOptions(merge=MERGE.KEEP_TARGET)) -> Response:
if url[-1] != '/':
raise Exception(
f'Cannot use createFolder to create a file : {url}')
try:
res = self.head(url)
if options.merge != MERGE.REPLACE:
return res
self.delete_folder(url, recursive=True)
except HTTPStatusError as e:
if e.response.status_code == 404:
pass
else:
raise e
return self.post_item(url, '', 'text/turtle', LINK.CONTAINER, options)
def post_file(self, url, content: RequestContent, content_type, options: WriteOptions = None) -> Response:
if url[-1] == '/':
raise Exception(f'Cannot use postFile to create a folder : {url}')
return self.post_item(url, content, content_type, LINK.RESOURCE, options)
def create_file(self, url, content: RequestContent, content_type, options: WriteOptions = None) -> Response:
return self.post_file(url, content, content_type, options)
"""
files
Support upload file, get/delete in higher level api
"""
def put_file(self, url, content: RequestContent, content_type, options: WriteOptions = WriteOptions()) -> Response:
if url[-1] == '/':
raise Exception(f'Cannot use putFile to create a folder : {url}')
if options.merge == MERGE.KEEP_TARGET and self.item_exists(url):
raise Exception(f'File already exists: {url}')
request_options = {
'headers': {
'Link': LINK.RESOURCE.value,
'Content-Type': content_type,
},
'content': content
}
return self.put(url, request_options)
def patch_file(self, url, patch_content, patch_content_type) -> Response:
# raise Exception('Not implemented')
if url[-1] == '/':
raise Exception(f'Cannot use patchFile to create a folder : {url}')
# if options.merge == MERGE.KEEP_TARGET and self.item_exists(url):
# raise Exception(f'File already exists: {url}')
request_options = {
'headers': {
'Link': LINK.RESOURCE.value,
'Content-Type': patch_content_type,
},
'content': patch_content
}
return self.patch(url, request_options)
def read_folder(self, url, options: ReadFolderOptions = ReadFolderOptions()) -> FolderData:
if url[-1] != '/':
url += '/' # 保證folder字串的最末是'/'。
folder_res = self.get(url, {'headers': {'Accept': 'text/turtle'}})
parsed_folder = parse_folder_response(folder_res, url)
if options.links in (LINKS.INCLUDE_POSSIBLE, LINKS.INCLUDE):
raise Exception('Not implemented')
return parsed_folder
def get_item_links(self, url, options: Dict = None) -> Response:
raise Exception('Not implemented')
def copy_file(self, _from, to, options: WriteOptions = None) -> Response:
raise Exception('Not implemented')
def copy_meta_file_for_item(self, old_target_file, new_target_file, options: WriteOptions = None) -> Response:
raise Exception('Not implemented')
def copy_acl_file_for_item(self, old_target_file, new_target_file, options: WriteOptions = None) -> Response:
raise Exception('Not implemented')
def copy_links_for_item(self, old_target_file, new_target_file, options: WriteOptions = None) -> List[Response]:
raise Exception('Not implemented')
def copy_folder(self, _from, to, options: WriteOptions = None) -> List[Response]:
raise Exception('Not implemented')
def copy(self, _from, to, options: WriteOptions = None) -> List[Response]:
raise Exception('Not implemented')
def delete_folder(self, url, recursive=False) -> List[Response]:
if recursive:
raise Exception('Not implemented')
if url == get_root_url(url):
raise Exception('405 Pod cannot be deleted')
return [self.delete(url)]
def move(self, _from, to, copy_options: WriteOptions = None) -> List[Response]:
raise Exception('Not implemented')
def rename(self, url, new_name, move_options: WriteOptions = None) -> List[Response]:
raise Exception('Not implemented')
@alexhtwen thank you join us this time!
Never mind, feel free to come back any time while you have free time available.
BTW, could you submit your patch via merge request? Your contribution will be record in your porfile this way.
You can learn how to submit a merge request here: https://docs.github.com/en/github/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request
It's totally ok if you dont's have time to do this. I will include your contribution manually by myself instead.
Thank you very much. I really appreciate all your effort and teaching.
Alex Van
hrchu @.***> 於 2021年10月7日 週四 下午1:34寫道:
@alexhtwen https://github.com/alexhtwen thank you join us this time!
Never mind, feel free to come back any time while you have free time available.
BTW, could you submit your patch via merge request? Your contribution will be record in your porfile this way.
You can learn how to submit a merge request here: https://docs.github.com/en/github/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request
It's totally ok if you dont's have time to do this. I will include your contribution manually by myself instead.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/twonote/solid-file-python/issues/7#issuecomment-937464744, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHXPI6SIYFKUKMHEXFLBOKDUFUWNPANCNFSM5ETD7R6A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
-- Though this be madness, yet there is method in ’t.
:+1:
https://github.com/solid/solid-spec/blob/master/api-rest.md
您好:
請問您是去年PyCon一個線上活動的solid-file-python project的負責人嗎?我是當天參與project討論的團員Alex。
不好意思,打擾一下請您幫個小忙。是這樣的:我最近向VS Code開發團隊提出一個Feature request,已列入backlog candidate。但要獲得20個以上的upvotes👍(猜想越多越好吧),才能正式成為backlog待辦事項。如果您覺得我的Request還有點用處,是否可以登入github給個讚?非常感謝。
Request內容見下列網址: https://github.com/microsoft/vscode/issues/147243
Alex Van
[image: Would you upvote me.png]
-- Though this be madness, yet there is method in ’t.
@alexhtwen 已upvote 👍
@.*** https://github.com/alexhtwen 已upvote 👍
謝謝。不過您是否upvote錯地方(票數沒有增加)?是進下列網址upvote喔。不好意思。 https://github.com/microsoft/vscode/issues/147243
Alex Van
[image: Would you upvote me.png]
Peter Lin @.***> 於 2022年4月29日 週五 下午4:27寫道:
@alexhtwen https://github.com/alexhtwen 已upvote 👍
— Reply to this email directly, view it on GitHub https://github.com/twonote/solid-file-python/issues/7#issuecomment-1113018561, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHXPI6UGFHHK6ZQS55EUA3LVHOMPNANCNFSM5ETD7R6A . You are receiving this because you were mentioned.Message ID: @.***>
-- Though this be madness, yet there is method in ’t.
我把upvote加在
- https://github.com/microsoft/vscode/issues/147243#issue-1200282107 和
- https://github.com/microsoft/vscode/issues/147243#issuecomment-1095554313 這樣對嗎?
我把upvote加在
- microsoft/vscode#147243 (comment) https://github.com/microsoft/vscode/issues/147243#issue-1200282107 和
- microsoft/vscode#147243 (comment) https://github.com/microsoft/vscode/issues/147243#issuecomment-1095554313 這樣對嗎?
票數有加1了。非常感謝(銘謝賜票😜)。 Alex Van
Peter Lin @.***> 於 2022年4月29日 週五 下午9:07寫道:
我把upvote加在
- microsoft/vscode#147243 (comment) https://github.com/microsoft/vscode/issues/147243#issue-1200282107 和
- microsoft/vscode#147243 (comment) https://github.com/microsoft/vscode/issues/147243#issuecomment-1095554313 這樣對嗎?
— Reply to this email directly, view it on GitHub https://github.com/twonote/solid-file-python/issues/7#issuecomment-1113289973, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHXPI6WYQ3IQZDYA5XAASV3VHPNIXANCNFSM5ETD7R6A . You are receiving this because you were mentioned.Message ID: @.***>
-- Though this be madness, yet there is method in ’t.
等等居然把 issue 變成 group chat 的xddd
BTW 很久沒機會跟大家聊,不知道今年大家有規劃怎麼參加技術社群活動呢? 比較可惜的是這個 project 暫時沒有照 roadmap 讓大家發做出一些 significant difference yet, 可能下半年在看怎麼樣重開機一下
anyway 我目前在籌劃的 coscup 2022 有提供一個新進講者育成計畫,徵求到明天!提供機會給大家參考,歡迎大家或推坑身邊朋友利用連假時間申請一下xd https://www.facebook.com/coscup/posts/10160194773247249
等等居然把 issue 變成 group chat 的xddd
不好意思,沒有您的email,只好將issue搞成個人拉票地方😜,抱歉。
如果方便,請團長upvote我一個👍吧。 https://github.com/microsoft/vscode/issues/147243
Alex
[image: Would you upvote me.png]
hrchu @.***> 於 2022年4月30日 週六 下午2:46寫道:
等等居然把 issue 變成 group chat 的xddd
BTW 很久沒機會跟大家聊,不知道今年大家有規劃怎麼參加技術社群活動呢? 比較可惜的是這個 project 暫時沒有照 roadmap 讓大家發做出一些 significant difference yet, 可能下半年在看怎麼樣重開機一下
anyway 我目前在籌劃的 coscup 2022 有提供一個新進講者育成計畫,徵求到明天!提供機會給大家參考,歡迎大家或推坑身邊朋友利用連假時間申請一下xd https://www.facebook.com/coscup/posts/10160194773247249
— Reply to this email directly, view it on GitHub https://github.com/twonote/solid-file-python/issues/7#issuecomment-1113933983, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHXPI6QP6N44QKB7IPAVTW3VHTJMPANCNFSM5ETD7R6A . You are receiving this because you were mentioned.Message ID: @.***>
-- Though this be madness, yet there is method in ’t.
按囉~雖然我是 pycharm 派的xd
Haha, thanks!
Alex
hrchu @.***> 於 2022年4月30日 週六 下午5:40寫道:
按囉~雖然我是 pycharm 派的xd
— Reply to this email directly, view it on GitHub https://github.com/twonote/solid-file-python/issues/7#issuecomment-1113958446, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHXPI6TQLNBUHX6CFTUIBPTVHT5W7ANCNFSM5ETD7R6A . You are receiving this because you were mentioned.Message ID: @.***>
-- Though this be madness, yet there is method in ’t.