Skip to content

Commit

Permalink
Reformatted everything with black
Browse files Browse the repository at this point in the history
Signed-off-by: Ole Herman Schumacher Elgesem <[email protected]>
  • Loading branch information
olehermanse committed Oct 5, 2021
1 parent c906820 commit a128218
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
1 change: 1 addition & 0 deletions cfbs/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
sys.path.insert(0, abspath)

from cfbs.main import main

main()
60 changes: 40 additions & 20 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
sh,
)

from cfbs.pretty import (pretty_file, pretty)
from cfbs.pretty import pretty_file, pretty


def cfbs_filename() -> str:
Expand Down Expand Up @@ -80,12 +80,15 @@ def get_index(prefer_offline=False) -> dict:
sys.exit("Empty or invalid module index")
return index["modules"]


def pretty_command(filenames: list) -> int:
if not filenames:
user_error("Filenames missing for cfbs pretty command")
for f in filenames:
if not f or not f.endswith(".json"):
user_error(f"cfbs pretty command can only be used with .json files, not '{os.path.basename(f)}'")
user_error(
f"cfbs pretty command can only be used with .json files, not '{os.path.basename(f)}'"
)
try:
pretty_file(f)
except FileNotFoundError:
Expand Down Expand Up @@ -157,14 +160,17 @@ def search_command(terms: list) -> int:
def module_exists(module_name):
return os.path.exists(module_name) or (module_name in get_index())


def local_module_name(module_path):
assert os.path.exists(module_path)
module = module_path

if module.endswith((".cf", ".json", "/")) and not module.startswith("./"):
module = "./" + module
if not module.startswith("./"):
user_error(f"Please prepend local files or folders with './' to avoid ambiguity")
user_error(
f"Please prepend local files or folders with './' to avoid ambiguity"
)

for illegal in ["//", "..", " ", "\n", "\t", " "]:
if illegal in module:
Expand All @@ -185,33 +191,37 @@ def local_module_name(module_path):

return module


def local_module_data_cf_file(module):
target = os.path.basename(module)
return {
"description": "Local policy file added using cfbs command line",
"tags": ["local"],
"dependencies": [ "autorun" ],
"steps": [f"copy {module} services/autorun/{target}"],
"added_by": "cfbs add"
"description": "Local policy file added using cfbs command line",
"tags": ["local"],
"dependencies": ["autorun"],
"steps": [f"copy {module} services/autorun/{target}"],
"added_by": "cfbs add",
}


def local_module_data_json_file(module):
return {
"description": "Local augments file added using cfbs command line",
"tags": ["local"],
"steps": [f"json {module} def.json"],
"added_by": "cfbs add"
"description": "Local augments file added using cfbs command line",
"tags": ["local"],
"steps": [f"json {module} def.json"],
"added_by": "cfbs add",
}


def local_module_data_subdir(module):
return {
"description": "Local subdirectory added using cfbs command line",
"tags": ["local"],
"dependencies": [ "autorun" ],
"steps": [f"copy {module} services/autorun/"],
"added_by": "cfbs add"
"description": "Local subdirectory added using cfbs command line",
"tags": ["local"],
"dependencies": ["autorun"],
"steps": [f"copy {module} services/autorun/"],
"added_by": "cfbs add",
}


def local_module_data(module):
assert module.startswith("./")
assert module.endswith((".cf", ".json", "/"))
Expand All @@ -224,6 +234,7 @@ def local_module_data(module):
if module.endswith(".json"):
return local_module_data_json_file(module)


def prettify_name(name):
if "/" not in name:
return name
Expand All @@ -234,6 +245,7 @@ def prettify_name(name):
assert name
return name


def local_module_copy(module, counter, max_length):
name = module["name"]
assert name.startswith("./")
Expand All @@ -243,10 +255,18 @@ def local_module_copy(module, counter, max_length):
module["_directory"] = target
module["_counter"] = counter
cp(name, target + name)
print(f"{counter:03d} {pad_right(name, max_length)} @ local (Copied)")
print(
f"{counter:03d} {pad_right(name, max_length)} @ local (Copied)"
)


def get_build_step(module):
return get_index()[module] if not module.startswith("./") else local_module_data(module)
return (
get_index()[module]
if not module.startswith("./")
else local_module_data(module)
)


def add_command(to_add: list, added_by="cfbs add") -> int:
if not to_add:
Expand All @@ -257,7 +277,7 @@ def add_command(to_add: list, added_by="cfbs add") -> int:
for module in to_add:
if not module_exists(module):
user_error(f"Module '{module}' does not exist")
if (not module in get_index() and os.path.exists(module)):
if not module in get_index() and os.path.exists(module):
translated.append(local_module_name(module))
continue
data = get_index()[module]
Expand Down
1 change: 1 addition & 0 deletions cfbs/pretty.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from collections import OrderedDict


def pretty_file(filename):
with open(filename) as f:
data = f.read()
Expand Down
2 changes: 2 additions & 0 deletions cfbs/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os


def string():
try:
with open(os.path.dirname(__file__) + "/VERSION", "r", encoding="utf-8") as fh:
Expand Down

0 comments on commit a128218

Please sign in to comment.