-
Notifications
You must be signed in to change notification settings - Fork 13
Minor code quality improvements #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
53adcbd
3c9b64a
9a9fd15
d94bcb5
4a75a94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,6 @@ | |
| import copy | ||
| import subprocess | ||
| import hashlib | ||
| import logging as log | ||
| from typing import List, Tuple | ||
| import urllib | ||
| import urllib.request # needed on some platforms | ||
| from collections import OrderedDict | ||
|
|
@@ -79,35 +77,12 @@ def cp(src, dst): | |
| sh("rsync -r %s/ %s" % (src, dst)) | ||
|
|
||
|
|
||
| def pad_left(s, n) -> int: | ||
| return s if len(s) >= n else " " * (n - len(s)) + s | ||
| def pad_left(s, n): | ||
| return s.rjust(n) | ||
|
|
||
|
|
||
| def pad_right(s, n) -> int: | ||
| return s if len(s) >= n else s + " " * (n - len(s)) | ||
|
|
||
|
|
||
| def split_command(command) -> Tuple[str, List[str]]: | ||
| terms = command.split(" ") | ||
| operation, args = terms[0], terms[1:] | ||
| return operation, args | ||
|
|
||
|
|
||
| def is_valid_arg_count(args, expected): | ||
| actual = len(args) | ||
|
|
||
| if type(expected) is int: | ||
| if actual != expected: | ||
| return False | ||
|
|
||
| else: | ||
| # Only other option is a string of 1+, 2+ or similar: | ||
| assert type(expected) is str and expected.endswith("+") | ||
| expected = int(expected[0:-1]) | ||
| if actual < expected: | ||
| return False | ||
|
|
||
| return True | ||
| def pad_right(s, n): | ||
| return s.ljust(n) | ||
|
|
||
|
|
||
| def user_error(msg: str): | ||
|
|
@@ -140,12 +115,14 @@ def item_index(iterable, item, extra_at_end=True): | |
|
|
||
|
|
||
| def strip_right(string, ending): | ||
| # can be replaced with str.removesuffix from Python 3.9 onwards | ||
| if not string.endswith(ending): | ||
| return string | ||
| return string[0 : -len(ending)] | ||
|
|
||
|
|
||
| def strip_left(string, beginning): | ||
| # can be replaced with str.removeprefix from Python 3.9 onwards | ||
| if not string.startswith(beginning): | ||
| return string | ||
| return string[len(beginning) :] | ||
|
|
@@ -166,7 +143,7 @@ def save_file(path, data): | |
| f.write(data) | ||
|
|
||
|
|
||
| def read_json(path): | ||
| def read_json(path) -> OrderedDict: | ||
| try: | ||
| with open(path, "r") as f: | ||
| return json.loads(f.read(), object_pairs_hook=OrderedDict) | ||
|
|
@@ -175,7 +152,7 @@ def read_json(path): | |
| except NotADirectoryError: | ||
| return None | ||
| except json.decoder.JSONDecodeError as ex: | ||
| print("Error reading json file {} : {}".format(path, ex)) | ||
| print("Error reading json file '{}': {}".format(path, ex)) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
|
|
@@ -275,15 +252,24 @@ def is_cfbs_repo() -> bool: | |
|
|
||
|
|
||
| def immediate_subdirectories(path): | ||
| return [f.name for f in os.scandir(path) if f.is_dir()] | ||
| l = [f.name for f in os.scandir(path) if f.is_dir()] | ||
|
|
||
| # `os.scandir` returns the entries in arbitrary order, so sort for determinism | ||
| l = sorted(l) | ||
|
|
||
| return l | ||
|
|
||
|
|
||
| def immediate_files(path): | ||
| return [f.name for f in os.scandir(path) if not f.is_dir()] | ||
| l = [f.name for f in os.scandir(path) if not f.is_dir()] | ||
|
|
||
| # `os.scandir` returns the entries in arbitrary order, so sort for determinism | ||
| l = sorted(l) | ||
|
|
||
| return l | ||
|
|
||
|
|
||
| def path_append(dir, subdir): | ||
| dir = os.path.abspath(os.path.expanduser(dir)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's okay to make this change here, however we might reverse it in the future. It does seem useful that |
||
| return dir if not subdir else os.path.join(dir, subdir) | ||
|
|
||
|
|
||
|
|
@@ -299,7 +285,10 @@ def are_paths_equal(path_a, path_b) -> bool: | |
|
|
||
|
|
||
| def cfengine_dir(subdir=None): | ||
| return path_append("~/.cfengine/", subdir) | ||
| CFENGINE_DIR = "~/.cfengine/" | ||
| cfengine_dir_abspath = os.path.abspath(os.path.expanduser(CFENGINE_DIR)) | ||
|
|
||
| return path_append(cfengine_dir_abspath, subdir) | ||
|
|
||
|
|
||
| def cfbs_dir(append=None) -> str: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.