@@ -83,9 +83,6 @@ def get_current_profile(nightscout, token, profile_name):
83
83
logging .debug ("default profile: %s" , default_profile )
84
84
profile ["timezone" ] = p_list [0 ]["store" ][default_profile ]["timezone" ]
85
85
return profile
86
- # sys.exit(
87
- # """Latest 'Profile Switch' event doesn't contain profile, """ +
88
- # """please specify profile name to use with --name flag.""")
89
86
p_list [0 ]["store" ][default_profile ]["name" ] = default_profile
90
87
try :
91
88
if not p_list [0 ]["store" ][default_profile ]["units" ]:
@@ -103,39 +100,50 @@ def get_current_profile(nightscout, token, profile_name):
103
100
return p_list [0 ]["store" ][profile_name ]
104
101
105
102
106
- def profiles (nightscout , token ):
103
+ def profiles (nightscout , token , file ):
107
104
"""
108
- print list of profiles available in nightscout
105
+ print list of profiles available in -- nightscout or --file
109
106
"""
110
- p_list = get_profiles (nightscout , token )
107
+ if file is not None :
108
+ with open (file , 'r' ) as f :
109
+ p_list = json .loads (f .read ())
110
+ else :
111
+ p_list = get_profiles (nightscout , token )
111
112
default_profile = p_list [0 ]["defaultProfile" ]
112
113
profile_list = p_list [0 ]["store" ].keys ()
113
114
print ("Default profile: {}" .format (default_profile ))
114
115
print ("Available profiles:" )
115
116
for profile in profile_list :
116
117
print ("\t " + profile )
117
118
118
-
119
- def display (nightscout , token , profile_name , profile_format ):
119
+ def display (nightscout , token , profile_name , profile_format , file ):
120
120
"""
121
- Display contents of a profile, in requested format
121
+ Display contents of a profile (from --nightscout or --file) , in requested format
122
122
"""
123
+ if file is not None :
124
+ with open (file , 'r' ) as f :
125
+ p_list = json .loads (f .read ())
126
+ else :
127
+ p_list = get_profiles (nightscout , token )
123
128
profile = get_current_profile (nightscout , token , profile_name )
124
129
if profile_format == "nightscout" :
125
- # display_nightscout(p_list, profile_name)
126
130
logging .debug ("Displaying profile {}" .format (profile ["name" ]))
127
131
print (json .dumps (profile , indent = 4 ))
128
132
elif profile_format == "text" :
129
133
display_text (profile )
130
134
else :
131
135
print (json .dumps (ns_to_oaps (profile ), indent = 4 ))
132
136
133
-
134
- def write (nightscout , token , profile_name , directory ):
137
+ def write (nightscout , token , profile_name , directory , file ):
135
138
"""
136
- Write profile in OpenAPS format to a directory
139
+ Write profile (from either --nightscout or --file) in OpenAPS format to a directory
137
140
"""
138
- profile = ns_to_oaps (get_current_profile (nightscout , token , profile_name ))
141
+ if file is not None :
142
+ with open (file , 'r' ) as f :
143
+ p_list = json .loads (f .read ())
144
+ else :
145
+ p_list = get_profiles (nightscout , token )
146
+ profile = get_current_profile (nightscout , token , profile_name )
139
147
logging .debug ("Checking for directory: %s" , directory )
140
148
if not os .path .isdir (directory ):
141
149
sys .exit (
@@ -160,7 +168,6 @@ def write(nightscout, token, profile_name, directory):
160
168
with open (os .path .join (directory , profile_file ), 'w' ) as f :
161
169
f .write (json .dumps (profile , indent = 4 ))
162
170
163
-
164
171
def normalize_entry (entry ):
165
172
"""
166
173
Clean up an entry before further processing
@@ -383,18 +390,25 @@ def display_text(p_data):
383
390
#times_table.add_rows(times_list)
384
391
#print(times_table.draw() + "\n")
385
392
386
-
393
+ # support either --nightscout or --file (reading in the profile from a file).
387
394
if __name__ == "__main__" :
388
395
parser = argparse .ArgumentParser (description = "Get nightscout profile." )
389
396
parser .add_argument (
390
397
"--nightscout" ,
391
398
help = "Nightscout URL" ,
392
- required = True ,
399
+ required = False ,
393
400
nargs = "?" ,
394
401
const = "http://127.0.0.1:1337" ,
395
402
default = "http://127.0.0.1:1337" ,
396
403
)
397
404
parser .add_argument ("--token" , help = "Authenticaton token" )
405
+ parser .add_argument (
406
+ "--file" ,
407
+ help = "File containing nightscout profile" ,
408
+ required = False ,
409
+ nargs = "?" ,
410
+ default = None ,
411
+ )
398
412
399
413
subparsers = parser .add_subparsers (help = "Sub-command to run" ,
400
414
dest = "subparser" )
@@ -428,7 +442,5 @@ def display_text(p_data):
428
442
429
443
logging .debug (vars (parser .parse_args ()))
430
444
431
- # https://stackoverflow.com/questions/4575747/get-selected-subcommand-with-argparse/44948406#44948406
432
- # I have no idea what it does, but it seems to do the trick
433
445
kwargs = vars (parser .parse_args ())
434
446
globals ()[kwargs .pop ("subparser" )](** kwargs )
0 commit comments