Skip to content

Commit

Permalink
untested: codex-assisted first attempt at adding --file support for #…
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleibrand committed Aug 24, 2021
1 parent 839a250 commit 424473c
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions bin/get_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ def get_current_profile(nightscout, token, profile_name):
logging.debug("default profile: %s", default_profile)
profile["timezone"] = p_list[0]["store"][default_profile]["timezone"]
return profile
# sys.exit(
# """Latest 'Profile Switch' event doesn't contain profile, """ +
# """please specify profile name to use with --name flag.""")
p_list[0]["store"][default_profile]["name"] = default_profile
try:
if not p_list[0]["store"][default_profile]["units"]:
Expand All @@ -103,39 +100,50 @@ def get_current_profile(nightscout, token, profile_name):
return p_list[0]["store"][profile_name]


def profiles(nightscout, token):
def profiles(nightscout, token, file):
"""
print list of profiles available in nightscout
print list of profiles available in --nightscout or --file
"""
p_list = get_profiles(nightscout, token)
if file is not None:
with open(file, 'r') as f:
p_list = json.loads(f.read())
else:
p_list = get_profiles(nightscout, token)
default_profile = p_list[0]["defaultProfile"]
profile_list = p_list[0]["store"].keys()
print("Default profile: {}".format(default_profile))
print("Available profiles:")
for profile in profile_list:
print("\t" + profile)


def display(nightscout, token, profile_name, profile_format):
def display(nightscout, token, profile_name, profile_format, file):
"""
Display contents of a profile, in requested format
Display contents of a profile (from --nightscout or --file), in requested format
"""
if file is not None:
with open(file, 'r') as f:
p_list = json.loads(f.read())
else:
p_list = get_profiles(nightscout, token)
profile = get_current_profile(nightscout, token, profile_name)
if profile_format == "nightscout":
# display_nightscout(p_list, profile_name)
logging.debug("Displaying profile {}".format(profile["name"]))
print(json.dumps(profile, indent=4))
elif profile_format == "text":
display_text(profile)
else:
print(json.dumps(ns_to_oaps(profile), indent=4))


def write(nightscout, token, profile_name, directory):
def write(nightscout, token, profile_name, directory, file):
"""
Write profile in OpenAPS format to a directory
Write profile (from either --nightscout or --file) in OpenAPS format to a directory
"""
profile = ns_to_oaps(get_current_profile(nightscout, token, profile_name))
if file is not None:
with open(file, 'r') as f:
p_list = json.loads(f.read())
else:
p_list = get_profiles(nightscout, token)
profile = get_current_profile(nightscout, token, profile_name)
logging.debug("Checking for directory: %s", directory)
if not os.path.isdir(directory):
sys.exit(
Expand All @@ -160,7 +168,6 @@ def write(nightscout, token, profile_name, directory):
with open(os.path.join(directory, profile_file), 'w') as f:
f.write(json.dumps(profile, indent=4))


def normalize_entry(entry):
"""
Clean up an entry before further processing
Expand Down Expand Up @@ -383,18 +390,25 @@ def display_text(p_data):
#times_table.add_rows(times_list)
#print(times_table.draw() + "\n")


# support either --nightscout or --file (reading in the profile from a file).
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Get nightscout profile.")
parser.add_argument(
"--nightscout",
help="Nightscout URL",
required=True,
required=False,
nargs="?",
const="http://127.0.0.1:1337",
default="http://127.0.0.1:1337",
)
parser.add_argument("--token", help="Authenticaton token")
parser.add_argument(
"--file",
help="File containing nightscout profile",
required=False,
nargs="?",
default=None,
)

subparsers = parser.add_subparsers(help="Sub-command to run",
dest="subparser")
Expand Down Expand Up @@ -428,7 +442,5 @@ def display_text(p_data):

logging.debug(vars(parser.parse_args()))

# https://stackoverflow.com/questions/4575747/get-selected-subcommand-with-argparse/44948406#44948406
# I have no idea what it does, but it seems to do the trick
kwargs = vars(parser.parse_args())
globals()[kwargs.pop("subparser")](**kwargs)

0 comments on commit 424473c

Please sign in to comment.