|
5 | 5 | from .md_to_json import parse_readme_to_json |
6 | 6 | import json |
7 | 7 | import os |
8 | | -import configparser |
| 8 | +#import configparser |
| 9 | +from cryptography.fernet import Fernet |
9 | 10 |
|
10 | 11 | CALTECHDATA_API = "https://data.caltech.edu/api/names?q=identifiers.identifier:{}" |
11 | 12 | ORCID_API = "https://orcid.org/" |
|
22 | 23 | funderName = "" |
23 | 24 |
|
24 | 25 |
|
25 | | -CONFIG_FILE = "caltechdata_config.ini" |
| 26 | +#CONFIG_FILE = "caltechdata_config.ini" |
26 | 27 |
|
27 | 28 |
|
28 | | -def get_or_set_token(): |
29 | | - config = configparser.ConfigParser() |
| 29 | +home_directory = os.path.expanduser("~") |
| 30 | +caltechdata_directory = os.path.join(home_directory, ".caltechdata") |
| 31 | + |
| 32 | + |
| 33 | +if not os.path.exists(caltechdata_directory): |
| 34 | + os.makedirs(caltechdata_directory) |
30 | 35 |
|
31 | | - if os.path.isfile(CONFIG_FILE): |
32 | | - config.read(CONFIG_FILE) |
33 | | - if "CaltechDATA" in config and "token" in config["CaltechDATA"]: |
34 | | - return config["CaltechDATA"]["token"] |
| 36 | +def generate_key(): |
| 37 | + return Fernet.generate_key() |
| 38 | + |
| 39 | +# Load the key from a file or generate a new one if not present |
| 40 | +def load_or_generate_key(key_file="key.key"): |
| 41 | + if os.path.exists(key_file): |
| 42 | + with open(key_file, "rb") as f: |
| 43 | + return f.read() |
35 | 44 | else: |
| 45 | + key = generate_key() |
| 46 | + with open(key_file, "wb") as f: |
| 47 | + f.write(key) |
| 48 | + return key |
| 49 | + |
| 50 | +# Encrypt the token |
| 51 | +def encrypt_token(token, key): |
| 52 | + f = Fernet(key) |
| 53 | + return f.encrypt(token.encode()) |
| 54 | + |
| 55 | +# Decrypt the token |
| 56 | +def decrypt_token(encrypted_token, key): |
| 57 | + f = Fernet(key) |
| 58 | + return f.decrypt(encrypted_token).decode() |
| 59 | + |
| 60 | +# Function to get or set token |
| 61 | +def get_or_set_token(): |
| 62 | + |
| 63 | + key = load_or_generate_key() |
| 64 | + try: |
| 65 | + with open("token.txt", "rb") as f: |
| 66 | + encrypted_token = f.read() |
| 67 | + token = decrypt_token(encrypted_token, key) |
| 68 | + return token |
| 69 | + except FileNotFoundError: |
36 | 70 | while True: |
37 | | - token = get_user_input("Enter your CaltechDATA token: ") |
38 | | - confirm_token = get_user_input("Confirm your CaltechDATA token: ") |
| 71 | + token = input("Enter your CaltechDATA token: ").strip() |
| 72 | + confirm_token = input("Confirm your CaltechDATA token: ").strip() |
39 | 73 | if token == confirm_token: |
40 | | - config.add_section("CaltechDATA") |
41 | | - config.set("CaltechDATA", "token", token) |
42 | | - with open(CONFIG_FILE, "w") as configfile: |
43 | | - config.write(configfile) |
| 74 | + encrypted_token = encrypt_token(token, key) |
| 75 | + with open("token.txt", "wb") as f: |
| 76 | + f.write(encrypted_token) |
44 | 77 | return token |
45 | 78 | else: |
46 | 79 | print("Tokens do not match. Please try again.") |
47 | | - |
48 | | - |
| 80 | + |
49 | 81 | def welcome_message(): |
50 | 82 | print("Welcome to CaltechDATA CLI") |
51 | 83 |
|
@@ -218,6 +250,7 @@ def get_names(orcid): |
218 | 250 |
|
219 | 251 | def upload_supporting_file(record_id=None): |
220 | 252 | filepath = "" |
| 253 | + filepaths = [] |
221 | 254 | file_link = "" |
222 | 255 | while True: |
223 | 256 | choice = get_user_input( |
@@ -264,34 +297,38 @@ def upload_supporting_file(record_id=None): |
264 | 297 | f for f in os.listdir() if not f.endswith(".json") and os.path.isfile(f) |
265 | 298 | ] |
266 | 299 | print("\n".join(files)) |
267 | | - filename = get_user_input( |
268 | | - "Enter the filename to upload as a supporting file: " |
269 | | - ) |
270 | | - if filename in files: |
271 | | - file_size = os.path.getsize(filename) |
272 | | - if file_size > 1024 * 1024 * 1024: |
273 | | - file_link = get_user_input( |
274 | | - "Enter the S3 link to the file (File size is more than 1GB): " |
275 | | - ) |
276 | | - if file_link: |
277 | | - return filepath, file_link |
| 300 | + while True: |
| 301 | + filename = get_user_input( |
| 302 | + "Enter the filename to upload as a supporting file (or 'n' to finish): " |
| 303 | + ) |
| 304 | + if filename == "n": |
| 305 | + break |
| 306 | + if filename in files: |
| 307 | + file_size = os.path.getsize(filename) |
| 308 | + if file_size > 1024 * 1024 * 1024: |
| 309 | + file_link = get_user_input( |
| 310 | + "Enter the S3 link to the file (File size is more than 1GB): " |
| 311 | + ) |
| 312 | + if file_link: |
| 313 | + file_links.append(file_link) |
278 | 314 | else: |
279 | | - print("Link is required for files larger than 1GB.") |
280 | | - continue |
| 315 | + filepath = os.path.abspath(filename) |
| 316 | + filepaths.append(filepath) |
281 | 317 | else: |
282 | | - filepath = os.path.abspath(filename) |
283 | | - break |
284 | | - else: |
285 | | - print( |
286 | | - f"Error: File '{filename}' not found. Please enter a valid filename." |
287 | | - ) |
| 318 | + print( |
| 319 | + f"Error: File '{filename}' not found. Please enter a valid filename." |
| 320 | + ) |
| 321 | + |
| 322 | + add_more = get_user_input("Do you want to add more files? (y/n): ").lower() |
| 323 | + if add_more != "y": |
| 324 | + break |
| 325 | + |
288 | 326 | elif choice == "n": |
289 | 327 | break |
290 | 328 | else: |
291 | 329 | print("Invalid input. Please enter 'link' or 'upload' or 'n'.") |
292 | 330 |
|
293 | | - return filepath, file_link |
294 | | - |
| 331 | + return filepaths, file_links |
295 | 332 |
|
296 | 333 | def upload_data_from_file(): |
297 | 334 | while True: |
|
0 commit comments