pre-commit: run black with string normalization

This commit is contained in:
Erik Sundell
2021-11-03 23:55:34 +01:00
parent 2ba942ba76
commit e0aaa4f995
31 changed files with 700 additions and 692 deletions

View File

@@ -25,12 +25,12 @@ import requests
from .yaml import yaml
INSTALL_PREFIX = os.environ.get('TLJH_INSTALL_PREFIX', '/opt/tljh')
HUB_ENV_PREFIX = os.path.join(INSTALL_PREFIX, 'hub')
USER_ENV_PREFIX = os.path.join(INSTALL_PREFIX, 'user')
STATE_DIR = os.path.join(INSTALL_PREFIX, 'state')
CONFIG_DIR = os.path.join(INSTALL_PREFIX, 'config')
CONFIG_FILE = os.path.join(CONFIG_DIR, 'config.yaml')
INSTALL_PREFIX = os.environ.get("TLJH_INSTALL_PREFIX", "/opt/tljh")
HUB_ENV_PREFIX = os.path.join(INSTALL_PREFIX, "hub")
USER_ENV_PREFIX = os.path.join(INSTALL_PREFIX, "user")
STATE_DIR = os.path.join(INSTALL_PREFIX, "state")
CONFIG_DIR = os.path.join(INSTALL_PREFIX, "config")
CONFIG_FILE = os.path.join(CONFIG_DIR, "config.yaml")
def set_item_in_config(config, property_path, value):
@@ -42,7 +42,7 @@ def set_item_in_config(config, property_path, value):
property_path is a series of dot separated values. Any part of the path
that does not exist is created.
"""
path_components = property_path.split('.')
path_components = property_path.split(".")
# Mutate a copy of the config, not config itself
cur_part = config_copy = deepcopy(config)
@@ -69,7 +69,7 @@ def unset_item_from_config(config, property_path):
property_path is a series of dot separated values.
"""
path_components = property_path.split('.')
path_components = property_path.split(".")
# Mutate a copy of the config, not config itself
cur_part = config_copy = deepcopy(config)
@@ -94,13 +94,13 @@ def unset_item_from_config(config, property_path):
for i, cur_path in enumerate(path_components):
if i == len(path_components) - 1:
if cur_path not in cur_part:
raise ValueError(f'{property_path} does not exist in config!')
raise ValueError(f"{property_path} does not exist in config!")
del cur_part[cur_path]
remove_empty_configs(config_copy, path_components[:-1])
break
else:
if cur_path not in cur_part:
raise ValueError(f'{property_path} does not exist in config!')
raise ValueError(f"{property_path} does not exist in config!")
cur_part = cur_part[cur_path]
return config_copy
@@ -110,7 +110,7 @@ def add_item_to_config(config, property_path, value):
"""
Add an item to a list in config.
"""
path_components = property_path.split('.')
path_components = property_path.split(".")
# Mutate a copy of the config, not config itself
cur_part = config_copy = deepcopy(config)
@@ -136,7 +136,7 @@ def remove_item_from_config(config, property_path, value):
"""
Remove an item from a list in config.
"""
path_components = property_path.split('.')
path_components = property_path.split(".")
# Mutate a copy of the config, not config itself
cur_part = config_copy = deepcopy(config)
@@ -144,12 +144,12 @@ def remove_item_from_config(config, property_path, value):
if i == len(path_components) - 1:
# Final component, it must be a list and we delete from it
if cur_path not in cur_part or not _is_list(cur_part[cur_path]):
raise ValueError(f'{property_path} is not a list')
raise ValueError(f"{property_path} is not a list")
cur_part = cur_part[cur_path]
cur_part.remove(value)
else:
if cur_path not in cur_part or not _is_dict(cur_part[cur_path]):
raise ValueError(f'{property_path} does not exist in config!')
raise ValueError(f"{property_path} does not exist in config!")
cur_part = cur_part[cur_path]
return config_copy
@@ -182,7 +182,7 @@ def set_config_value(config_path, key_path, value):
config = set_item_in_config(config, key_path, value)
with open(config_path, 'w') as f:
with open(config_path, "w") as f:
yaml.dump(config, f)
@@ -200,7 +200,7 @@ def unset_config_value(config_path, key_path):
config = unset_item_from_config(config, key_path)
with open(config_path, 'w') as f:
with open(config_path, "w") as f:
yaml.dump(config, f)
@@ -218,7 +218,7 @@ def add_config_value(config_path, key_path, value):
config = add_item_to_config(config, key_path, value)
with open(config_path, 'w') as f:
with open(config_path, "w") as f:
yaml.dump(config, f)
@@ -236,19 +236,19 @@ def remove_config_value(config_path, key_path, value):
config = remove_item_from_config(config, key_path, value)
with open(config_path, 'w') as f:
with open(config_path, "w") as f:
yaml.dump(config, f)
def check_hub_ready():
from .configurer import load_config
base_url = load_config()['base_url']
base_url = base_url[:-1] if base_url[-1] == '/' else base_url
http_port = load_config()['http']['port']
base_url = load_config()["base_url"]
base_url = base_url[:-1] if base_url[-1] == "/" else base_url
http_port = load_config()["http"]["port"]
try:
r = requests.get(
'http://127.0.0.1:%d%s/hub/api' % (http_port, base_url), verify=False
"http://127.0.0.1:%d%s/hub/api" % (http_port, base_url), verify=False
)
return r.status_code == 200
except:
@@ -264,33 +264,33 @@ def reload_component(component):
# import here to avoid circular imports
from tljh import systemd, traefik
if component == 'hub':
systemd.restart_service('jupyterhub')
if component == "hub":
systemd.restart_service("jupyterhub")
# Ensure hub is back up
while not systemd.check_service_active('jupyterhub'):
while not systemd.check_service_active("jupyterhub"):
time.sleep(1)
while not check_hub_ready():
time.sleep(1)
print('Hub reload with new configuration complete')
elif component == 'proxy':
print("Hub reload with new configuration complete")
elif component == "proxy":
traefik.ensure_traefik_config(STATE_DIR)
systemd.restart_service('traefik')
while not systemd.check_service_active('traefik'):
systemd.restart_service("traefik")
while not systemd.check_service_active("traefik"):
time.sleep(1)
print('Proxy reload with new configuration complete')
print("Proxy reload with new configuration complete")
def parse_value(value_str):
"""Parse a value string"""
if value_str is None:
return value_str
if re.match(r'^\d+$', value_str):
if re.match(r"^\d+$", value_str):
return int(value_str)
elif re.match(r'^\d+\.\d*$', value_str):
elif re.match(r"^\d+\.\d*$", value_str):
return float(value_str)
elif value_str.lower() == 'true':
elif value_str.lower() == "true":
return True
elif value_str.lower() == 'false':
elif value_str.lower() == "false":
return False
else:
# it's a string
@@ -327,67 +327,67 @@ def main(argv=None):
argparser = argparse.ArgumentParser()
argparser.add_argument(
'--config-path', default=CONFIG_FILE, help='Path to TLJH config.yaml file'
"--config-path", default=CONFIG_FILE, help="Path to TLJH config.yaml file"
)
subparsers = argparser.add_subparsers(dest='action')
subparsers = argparser.add_subparsers(dest="action")
show_parser = subparsers.add_parser('show', help='Show current configuration')
show_parser = subparsers.add_parser("show", help="Show current configuration")
unset_parser = subparsers.add_parser('unset', help='Unset a configuration property')
unset_parser = subparsers.add_parser("unset", help="Unset a configuration property")
unset_parser.add_argument(
'key_path', help='Dot separated path to configuration key to unset'
"key_path", help="Dot separated path to configuration key to unset"
)
set_parser = subparsers.add_parser('set', help='Set a configuration property')
set_parser = subparsers.add_parser("set", help="Set a configuration property")
set_parser.add_argument(
'key_path', help='Dot separated path to configuration key to set'
"key_path", help="Dot separated path to configuration key to set"
)
set_parser.add_argument('value', help='Value to set the configuration key to')
set_parser.add_argument("value", help="Value to set the configuration key to")
add_item_parser = subparsers.add_parser(
'add-item', help='Add a value to a list for a configuration property'
"add-item", help="Add a value to a list for a configuration property"
)
add_item_parser.add_argument(
'key_path', help='Dot separated path to configuration key to add value to'
"key_path", help="Dot separated path to configuration key to add value to"
)
add_item_parser.add_argument('value', help='Value to add to the configuration key')
add_item_parser.add_argument("value", help="Value to add to the configuration key")
remove_item_parser = subparsers.add_parser(
'remove-item', help='Remove a value from a list for a configuration property'
"remove-item", help="Remove a value from a list for a configuration property"
)
remove_item_parser.add_argument(
'key_path', help='Dot separated path to configuration key to remove value from'
"key_path", help="Dot separated path to configuration key to remove value from"
)
remove_item_parser.add_argument('value', help='Value to remove from key_path')
remove_item_parser.add_argument("value", help="Value to remove from key_path")
reload_parser = subparsers.add_parser(
'reload', help='Reload a component to apply configuration change'
"reload", help="Reload a component to apply configuration change"
)
reload_parser.add_argument(
'component',
choices=('hub', 'proxy'),
help='Which component to reload',
default='hub',
nargs='?',
"component",
choices=("hub", "proxy"),
help="Which component to reload",
default="hub",
nargs="?",
)
args = argparser.parse_args(argv)
if args.action == 'show':
if args.action == "show":
show_config(args.config_path)
elif args.action == 'set':
elif args.action == "set":
set_config_value(args.config_path, args.key_path, parse_value(args.value))
elif args.action == 'unset':
elif args.action == "unset":
unset_config_value(args.config_path, args.key_path)
elif args.action == 'add-item':
elif args.action == "add-item":
add_config_value(args.config_path, args.key_path, parse_value(args.value))
elif args.action == 'remove-item':
elif args.action == "remove-item":
remove_config_value(args.config_path, args.key_path, parse_value(args.value))
elif args.action == 'reload':
elif args.action == "reload":
reload_component(args.component)
else:
argparser.print_help()
if __name__ == '__main__':
if __name__ == "__main__":
main()