1
+ import os
1
2
import storage
2
3
import microcontroller
3
- import os
4
4
5
5
# Get all files in the format of .env.xxxxxxxxxx
6
6
def enumerate_env_files ():
7
- env_files = []
7
+ found_files = []
8
8
all_files = os .listdir ("/" )
9
- for file in all_files :
10
- if file [:4 ] == ".env" and len (file ) > 4 :
11
- env_files .append (file )
12
- return env_files
9
+ for current_file in all_files :
10
+ if current_file [:4 ] == ".env" and len (current_file ) > 4 :
11
+ found_files .append (current_file )
12
+ return found_files
13
+
13
14
14
15
# Compare .env to enumerated env files
15
- def get_current_env_file (env_files ):
16
- with open (' .env' ) as env :
16
+ def get_current_env_file (enumerated_files ):
17
+ with open (" .env" ) as env :
17
18
env_lines = env .readlines ()
18
- for env_file in env_files :
19
+ for env_file in enumerated_files :
19
20
with open (env_file ) as f :
20
21
lines = f .readlines ()
21
22
if len (env_lines ) != len (lines ):
@@ -30,12 +31,13 @@ def get_current_env_file(env_files):
30
31
return env_file
31
32
return None
32
33
34
+
33
35
# Erase .env then write the contents of the new env file
34
36
def change_env_file (env_file ):
35
37
try :
36
38
storage .remount ("/" , False )
37
- open (' .env' , 'w' ).close ()
38
- with open (' .env' , 'w' ) as env , open (env_file ) as f :
39
+ open (" .env" , "w" ).close ()
40
+ with open (" .env" , "w" ) as env , open (env_file ) as f :
39
41
for line in f .readlines ():
40
42
env .write (line )
41
43
env .close ()
@@ -44,6 +46,7 @@ def change_env_file(env_file):
44
46
except RuntimeError :
45
47
print ("You can't change the env file with this script while USB is mounted" )
46
48
49
+
47
50
# Return a prettier name than the env file
48
51
def pretty_name (env_file ):
49
52
name = env_file [5 :]
@@ -62,16 +65,16 @@ def pretty_name(env_file):
62
65
if len (env_files ) == 1 :
63
66
answer = input (f"Change to { pretty_name (env_files [0 ])} ? " )
64
67
answer = answer .lower ()
65
- if answer == "y" or answer == "yes" :
68
+ if answer in ( "y" , "yes" ) :
66
69
change_env_file (env_files [0 ])
67
70
else :
68
71
valid_selection = False
69
72
while not valid_selection :
70
73
print ("Select an option:" )
71
74
for index , file in enumerate (env_files ):
72
75
print (f"{ index + 1 } : { pretty_name (file )} " )
73
- answer = input (f "Which option would you like? " )
76
+ answer = input ("Which option would you like? " )
74
77
if answer .isdigit () and 0 < int (answer ) <= len (env_files ):
75
78
valid_selection = True
76
79
change_env_file (env_files [int (answer ) - 1 ])
77
- print (f"{ answer } was an invalid selection.\n " )
80
+ print (f"{ answer } was an invalid selection.\n " )
0 commit comments