-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnaruma.py
More file actions
254 lines (198 loc) · 7.2 KB
/
Copy pathnaruma.py
File metadata and controls
254 lines (198 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import cmd
import json
import time
from pathlib import Path
from typing import Optional
from urllib.parse import urlparse
from requests import Session, codes
def url_to_profile_path(url: str) -> str:
parse_url = urlparse(url)
filename: str = f"{parse_url.netloc}.json"
return filename
class NarumaShell(cmd.Cmd):
intro: str = "Welcome to naruma. Type help or ? to list commands.\n"
prompt: str = "(naruma) "
session: Session
remote: str
cache: Optional[tuple[str, str]]
profile_path: Path
def __init__(self):
cwd = Path.cwd()
self.cache = None
self.profile_path = cwd / "profiles"
self.download_path = cwd / "notes"
super().__init__()
@property
def profile(self):
remote = self.remote if hasattr(self, "remote") else None
profile_path: str = self.profile_path.as_posix()
download_path: str = self.download_path.as_posix()
profile = {
"remote": remote,
"profile_path": profile_path,
"download_path": download_path,
}
return profile
@profile.setter
def profile(self, other: dict):
self.remote = other["remote"]
self.profile_path = Path(other["profile_path"])
self.download_path = Path(other["download_path"])
def do_connect(self, remote_url: str) -> None:
"""Connect to a remote HedgeDoc instance.
Args:
remote_url (str): root url of the instance
"""
if remote_url:
self.remote = remote_url
elif hasattr(self, "remote"):
remote_url = self.remote
else:
print("couldn't find remote")
return
self.session = Session()
response = self.session.get(remote_url)
if response.status_code != codes.ok:
response.raise_for_status()
else:
print(response)
def do_get(self, note_id: str) -> None:
"""Downloads note and saves it to the cache.
Args:
note_id (str): note id
"""
if self.cache is not None:
print("cache isn't empty, please save content to file")
return
download_url: str = f"{self.remote}/{note_id}/download"
response = self.session.get(download_url)
if response.status_code != codes.ok:
response.raise_for_status()
return
self.cache = (note_id, response.text)
print("saved note content to cache")
def do_save(self, filename: str):
"""Saves last entry of the cache.
Args:
filename (str): target path
"""
if self.cache is None:
print("Nothing to save")
return
if not self.download_path.exists():
self.download_path.mkdir()
target_path = self.download_path / Path(f"{filename}.md")
if target_path.exists():
print(f"Path {target_path} already exists.")
return
else:
target_path.touch()
note_id, content = self.cache
with target_path.open("w", encoding="UTF-8") as stream:
stream.write(content)
print(f"written {note_id} to {target_path}")
self.cache = None
def do_cache(self, arg):
"""Shows current cache."""
del arg
if self.cache:
note_id, content = self.cache
print(f"{note_id} {len(content)}")
else:
print("empty")
def do_clear(self, arg):
"""Clears the cache."""
del arg
print("cache was cleared")
self.cache = None
def do_local(self, sub_cmd: str):
"""
Manipulate the local directory path.
This is the directory where your downloaded notes will be saved.
Subcommands:
get: show the current local directory path
set: change the current local directory path
list: list notes in local directory
"""
match sub_cmd.lower():
case "list":
path: Path
for path in self.download_path.glob("*.md"):
stat = path.stat()
print(
f"{stat.st_size:>10} | {time.ctime(stat.st_ctime)} | {path.name}"
)
case "set":
new_path: Path = Path(input("Please enter new path: "))
self.download_path = new_path
print(f"set local directory to: {self.download_path}")
case "get":
print(f"current local directory: {self.download_path}")
case _:
print(self.do_local.__doc__)
def do_profile(self, sub_cmd: str):
"""Manipulates profile.
Your profile is a set of settings, which is individual for every
HedgeDoc remote.
Subcommands:
save: saves your current profile
load: loads profile from the profile path
list: lists available profiles
get: shows current profile path
set: sets profile path
show: shows your current profile
help: shows this help message
Note that the subcommands are not case-sensitive.
"""
match sub_cmd.lower():
case "save":
if hasattr(self, "remote"):
filename = url_to_profile_path(self.remote)
else:
filename = input("Please enter filename: ")
target_path: Path = self.profile_path / filename
if not self.profile_path.exists():
self.profile_path.mkdir()
with target_path.open("w", encoding="UTF-8") as stream:
json.dump(self.profile, stream)
print(f"saved profile to {target_path}")
case "load":
filename: str = input("please enter filename: ")
target_path: Path = self.profile_path / f"{filename}.json"
with target_path.open("r", encoding="UTF-8") as stream:
profile = json.load(stream)
print(f"loaded profile {profile}")
self.profile = profile
case "list":
path: Path
for path in self.profile_path.glob("*.json"):
stat = path.stat()
print(
f"{stat.st_size:>10} | {time.ctime(stat.st_ctime)} | {path.name}"
)
case "get":
print(f"profile directory: {self.profile_path}")
case "set":
new_path = Path(input("new profiles path: "))
self.profile_path = new_path
case "show":
print(self.profile)
case "help" | _:
print(self.do_profile.__doc__)
def do_bye(self, arg) -> bool:
"""Closes program."""
del arg
if hasattr(self, "session"):
self.session.close()
if self.cache is not None:
print("Cache is not empty, please save cache to file")
return False
return True
if __name__ == "__main__":
try:
import rich
except ImportError:
pass
else:
old_print, print = print, rich.print
NarumaShell().cmdloop()