|
6 | 6 | import storage
|
7 | 7 | import microcontroller
|
8 | 8 |
|
9 |
| -# Get all files in the format of .env.xxxxxxxxxx |
10 |
| -def enumerate_env_files(): |
| 9 | +SETTINGS_FOLDER = "/" |
| 10 | + |
| 11 | +# Get all files in the format of xxxxxxxxxx.toml except settings.toml |
| 12 | +def enumerate_toml_files(): |
11 | 13 | found_files = []
|
12 |
| - all_files = os.listdir("/") |
| 14 | + all_files = os.listdir(SETTINGS_FOLDER) |
13 | 15 | for current_file in all_files:
|
14 |
| - if current_file[:4] == ".env" and len(current_file) > 4: |
15 |
| - found_files.append(current_file) |
| 16 | + if ( |
| 17 | + not current_file.startswith("._") |
| 18 | + and current_file.endswith(".toml") |
| 19 | + and current_file != "settings.toml" |
| 20 | + ): |
| 21 | + found_files.append(SETTINGS_FOLDER + current_file) |
16 | 22 | return found_files
|
17 | 23 |
|
18 | 24 |
|
19 |
| -# Compare .env to enumerated env files |
20 |
| -def get_current_env_file(enumerated_files): |
21 |
| - with open(".env") as env: |
22 |
| - env_lines = env.readlines() |
23 |
| - for env_file in enumerated_files: |
24 |
| - with open(env_file) as f: |
| 25 | +# Compare settings.toml to enumerated toml files |
| 26 | +def get_current_toml_file(enumerated_files): |
| 27 | + with open("settings.toml") as settings: |
| 28 | + settings_lines = settings.readlines() |
| 29 | + for toml_file in enumerated_files: |
| 30 | + with open(toml_file) as f: |
25 | 31 | lines = f.readlines()
|
26 |
| - if len(env_lines) != len(lines): |
| 32 | + if len(settings_lines) != len(lines): |
27 | 33 | continue
|
28 | 34 | file_may_match = True
|
29 |
| - for line_no, env_line in enumerate(env_lines): |
30 |
| - if env_line != lines[line_no]: |
| 35 | + for line_no, settings_line in enumerate(settings_lines): |
| 36 | + if settings_line != lines[line_no]: |
31 | 37 | file_may_match = False
|
32 | 38 | break
|
33 | 39 | if not file_may_match:
|
34 | 40 | continue
|
35 |
| - return env_file |
| 41 | + return toml_file |
36 | 42 | return None
|
37 | 43 |
|
38 | 44 |
|
39 |
| -# Erase .env then write the contents of the new env file |
40 |
| -def change_env_file(env_file): |
| 45 | +# Erase settings.toml then write the contents of the new settings.toml file |
| 46 | +def change_toml_file(toml_file): |
41 | 47 | try:
|
42 | 48 | storage.remount("/", False)
|
43 |
| - open(".env", "w").close() |
44 |
| - with open(".env", "w") as env, open(env_file) as f: |
| 49 | + with open("settings.toml", "w") as settings: |
| 50 | + settings.write("") |
| 51 | + with open("settings.toml", "w") as settings, open(toml_file) as f: |
45 | 52 | for line in f.readlines():
|
46 |
| - env.write(line) |
47 |
| - env.close() |
| 53 | + settings.write(line) |
48 | 54 | print("Done. Hard resetting board...")
|
49 | 55 | microcontroller.reset()
|
50 | 56 | except RuntimeError:
|
51 | 57 | print("You can't change the env file with this script while USB is mounted")
|
52 | 58 |
|
53 | 59 |
|
54 |
| -# Return a prettier name than the env file |
55 |
| -def pretty_name(env_file): |
56 |
| - name = env_file[5:] |
| 60 | +# Return a prettier name than the toml filename |
| 61 | +def pretty_name(toml_file): |
| 62 | + name = toml_file.rsplit("/", 1)[1] |
| 63 | + name = name[:-5] |
57 | 64 | name = name[0].upper() + name[1:]
|
58 |
| - return f"{name} .env file" |
| 65 | + return f"{name} toml file" |
| 66 | + |
59 | 67 |
|
60 |
| -env_files = enumerate_env_files() |
| 68 | +toml_files = enumerate_toml_files() |
61 | 69 |
|
62 |
| -if len(env_files) < 2: |
63 |
| - print("You need to have at least 2 env files to change") |
| 70 | +if len(toml_files) < 2: |
| 71 | + print("You need to have at least 2 .toml files to change") |
64 | 72 |
|
65 |
| -result = get_current_env_file(env_files) |
| 73 | +result = get_current_toml_file(toml_files) |
66 | 74 | if result:
|
67 |
| - env_files.remove(result) |
68 |
| -print("WARNING: This will overwrite all of your current .env file settings.") |
69 |
| -if len(env_files) == 1: |
70 |
| - answer = input(f"Change to {pretty_name(env_files[0])}? ") |
| 75 | + toml_files.remove(result) |
| 76 | +print("WARNING: This will overwrite all of your current settings.toml file settings.") |
| 77 | +if len(toml_files) == 1: |
| 78 | + answer = input(f"Change to {pretty_name(toml_files[0])}? ") |
71 | 79 | answer = answer.lower()
|
72 | 80 | if answer in ("y", "yes"):
|
73 |
| - change_env_file(env_files[0]) |
| 81 | + change_toml_file(toml_files[0]) |
74 | 82 | else:
|
75 | 83 | valid_selection = False
|
76 | 84 | while not valid_selection:
|
77 | 85 | print("Select an option:")
|
78 |
| - for index, file in enumerate(env_files): |
| 86 | + for index, file in enumerate(toml_files): |
79 | 87 | print(f"{index + 1}: {pretty_name(file)}")
|
80 | 88 | answer = input("Which option would you like? ")
|
81 |
| - if answer.isdigit() and 0 < int(answer) <= len(env_files): |
| 89 | + if answer.isdigit() and 0 < int(answer) <= len(toml_files): |
82 | 90 | valid_selection = True
|
83 |
| - change_env_file(env_files[int(answer) - 1]) |
| 91 | + change_toml_file(toml_files[int(answer) - 1]) |
84 | 92 | print(f"{answer} was an invalid selection.\n")
|
0 commit comments