efs-cli 1.0.8__tar.gz → 1.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: efs-cli
3
- Version: 1.0.8
3
+ Version: 1.1.0
4
4
  Summary: EepyFileServer API CLI written in Python
5
5
  Author: maxie
6
6
  License-Expression: MIT
@@ -0,0 +1,2 @@
1
+ __version__ = "1.1.0"
2
+
@@ -3,6 +3,8 @@ from efs_wrapper import __version__ as wrapper_version
3
3
  from efs_wrapper.v1 import EepyFileServer, EepyFileServerPublic, DiskUsageObject
4
4
  from argparse import ArgumentParser, Namespace
5
5
  import os
6
+ import pathlib
7
+ from tempfile import NamedTemporaryFile
6
8
  import json
7
9
  import sys
8
10
 
@@ -15,10 +17,12 @@ def exception_handler(func):
15
17
  try:
16
18
  func(*args, **kwargs)
17
19
  except Exception as exc:
18
- if isinstance(exc.args[1], int):
19
- print(f"Error: {exc.args[0]}")
20
- sys.exit(1)
20
+ if len(exc.args) == 2:
21
+ if isinstance(exc.args[1], int):
22
+ print(f"Error: {exc.args[0]}")
23
+ sys.exit(1)
21
24
  print(f"Error: {exc.__class__.__name__}: {exc}")
25
+ sys.exit(1)
22
26
  return wrapper
23
27
 
24
28
  def get_conf():
@@ -33,8 +37,8 @@ def get_conf():
33
37
  def init_api():
34
38
  conf = get_conf()
35
39
 
36
- if conf is None:
37
- print("Please authorize with 'efs-cli auth <base_url> <password>'")
40
+ if conf is None or conf.get("base_url", None) is None:
41
+ print("Please authorize with 'efs-cli auth <base_url> <password> (password is optional but you will be able only to read and download public files)'")
38
42
  sys.exit(1)
39
43
 
40
44
  base_url = conf.get("base_url")
@@ -42,11 +46,55 @@ def init_api():
42
46
  efs = EepyFileServerPublic(base_url, ignore_initialization = True) if password is None else EepyFileServer(base_url, password, ignore_initialization = True)
43
47
  return efs
44
48
 
49
+ @exception_handler
50
+ def config(reset, **__):
51
+ conf = get_conf()
52
+ if conf is None or reset:
53
+ with open(CONFIG_PATH, "w") as file:
54
+ json.dump({"base_url": None, "password": None, "editor": "nano"}, file)
55
+ if reset:
56
+ print("Config reset")
57
+ sys.exit(0)
58
+ editor = conf.get("editor", "nano")
59
+
60
+ status = os.system(f"{editor} {CONFIG_PATH}")
61
+ if status:
62
+ print("using default editor: nano")
63
+ os.system(f"nano {CONFIG_PATH}")
64
+ print("Config saved successfully")
65
+
66
+ @exception_handler
67
+ def efs_config(verbose, **__):
68
+ conf = get_conf()
69
+ api = init_api()
70
+ editor = conf.get("editor", "nano")
71
+ if not hasattr(api, "get_config"):
72
+ print("Login using 'efs-cli auth <base url> <password>' to get the config")
73
+ sys.exit(1)
74
+ data = api.get_config()
75
+ with NamedTemporaryFile(suffix = ".json", mode="w+") as tmp:
76
+ json.dump(data, tmp)
77
+ tmp.seek(0)
78
+ status = os.system(f"{editor} {tmp.name}")
79
+ if status:
80
+ print("using default editor: nano")
81
+ os.system(f"nano {tmp.name}")
82
+ api.update_config(json.load(tmp), _print_warnings = verbose)
83
+ print("Config saved successfully")
84
+
45
85
  @exception_handler
46
86
  def auth(base_url, password, **__):
47
- data = {"base_url": base_url, "password": password}
48
- with open(CONFIG_PATH, "w") as file:
49
- json.dump(data, file)
87
+ conf = get_conf()
88
+ data = {"base_url": base_url, "password": password, "editor": "nano"}
89
+
90
+ if conf is not None:
91
+ conf.update(data)
92
+ with open(CONFIG_PATH, "w") as file:
93
+ json.dump(conf, file)
94
+
95
+ else:
96
+ with open(CONFIG_PATH, "w") as file:
97
+ json.dump(data, file)
50
98
  print("Saved eepyfileserver password and base url in config.json")
51
99
 
52
100
  @exception_handler
@@ -63,10 +111,37 @@ def download(from_path, to_path, public, **__):
63
111
  print(f"Downloaded {from_path} to {to_path}")
64
112
 
65
113
  @exception_handler
66
- def read(file_path, **__):
114
+ def read(file_path, no_less, **__):
67
115
  api = init_api()
68
116
  file = api.get_file(file_path)
69
- print(file.fetch_file().load().decode("utf-8"))
117
+ _file = file.fetch_file()
118
+ if sys.platform in ["linux", "darwin"] and not no_less:
119
+ with NamedTemporaryFile() as tmp:
120
+ tmp.write(_file.load())
121
+ tmp.seek(0)
122
+ os.system(f"less {tmp.name}")
123
+ else:
124
+ print(_file.load().decode("utf-8"))
125
+
126
+ @exception_handler
127
+ def edit(file_path, **__):
128
+ api = init_api()
129
+ conf = get_conf()
130
+ editor = conf.get("editor", "nano")
131
+ if not hasattr(api, "overwrite_file"):
132
+ print("Login using 'efs-cli auth <base url> <password>' to overwrite files")
133
+ sys.exit(1)
134
+ file = api.get_file(file_path)
135
+ _file = file.fetch_file()
136
+ with NamedTemporaryFile(suffix = "".join(pathlib.Path(file_path).suffixes)) as tmp:
137
+ tmp.write(_file.load())
138
+ tmp.seek(0)
139
+ status = os.system(f"{editor} {tmp.name}")
140
+ if status:
141
+ print("using default editor: nano")
142
+ os.system(f"nano {tmp.name}")
143
+ api.overwrite_file(file_path, tmp)
144
+ print(f"{file_path} overwritten")
70
145
 
71
146
  @exception_handler
72
147
  def upload(from_path, to_path, **__):
@@ -172,6 +247,7 @@ def main():
172
247
 
173
248
  read_parser = subparser.add_parser("read", help="read a file", aliases=["cat"])
174
249
  read_parser.add_argument("file_path", help="the efs path of the file to read")
250
+ read_parser.add_argument("-n", "--no-less", action="store_true", help="just print out the file contents if set")
175
251
  read_parser.set_defaults(func=read)
176
252
 
177
253
  upload_parser = subparser.add_parser("upload", help="upload a file", aliases=["post", "up", "put"])
@@ -208,6 +284,18 @@ def main():
208
284
  urlfor_parser.add_argument("-c", "--check-if-exists", action="store_true", help="check if the file exists before returning the url")
209
285
  urlfor_parser.set_defaults(func=url_for)
210
286
 
287
+ config_parser = subparser.add_parser("cli-config", help="view or change your cli config", aliases=["conf"])
288
+ config_parser.add_argument("--reset", action="store_true", help="if set, resets your ENTIRE config")
289
+ config_parser.set_defaults(func=config)
290
+
291
+ efs_config_parser = subparser.add_parser("efs-config", help="view or change your eepyfileserver config", aliases=["efs-conf"])
292
+ efs_config_parser.add_argument("-v", "--verbose", action="store_true", help="prints info out if a key is not changed")
293
+ efs_config_parser.set_defaults(func=efs_config)
294
+
295
+ edit_parser = subparser.add_parser("edit", help="edit your files on efs remotely")
296
+ edit_parser.add_argument("file_path", help="the efs file path to edit")
297
+ edit_parser.set_defaults(func=edit)
298
+
211
299
  args = parser.parse_args()
212
300
  if args == Namespace():
213
301
  args = parser.parse_args(["-h"])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: efs-cli
3
- Version: 1.0.8
3
+ Version: 1.1.0
4
4
  Summary: EepyFileServer API CLI written in Python
5
5
  Author: maxie
6
6
  License-Expression: MIT
@@ -1,2 +0,0 @@
1
- __version__ = "1.0.8"
2
-
File without changes
File without changes
File without changes
File without changes