Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nsheff committed Nov 7, 2023
1 parent 8cdbd55 commit f05d561
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Added in ability to specify abstract targets, which don't show up in buildable lists, [issue 19](https://github.com/databio/markmeld/issues/19)
- mm now raises an error if you try to inherit from an non-existing target, [issue 20](https://github.com/databio/markmeld/issues/20)
- overriding targets is no longer allowed, [issue 22](https://github.com/databio/markmeld/issues/22)
- Major revamps on data structures availalbe to jinja template

## [0.2.0] -- 2023-01-07

Expand Down
7 changes: 5 additions & 2 deletions markmeld/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def main(test_args=None):
if args.list:
if "targets" not in cfg:
raise TargetError(f"No targets specified in config.")

tarlist = {}
for t, k in cfg["targets"].items():
if "abstract" in cfg["targets"][t]:
Expand All @@ -189,6 +189,7 @@ def main(test_args=None):

if args.template:
from .melder import Target, load_template

tgt = Target(mm.cfg, args.target)
tpl = load_template(tgt.meta)
_LOGGER.info("Template content:")
Expand Down Expand Up @@ -226,7 +227,9 @@ def report_result(built_target):
color_code = color_red
else:
color_code = color_green
_LOGGER.info(f"{color_code}{item['status']}: {item['message']}{color_reset}")
_LOGGER.info(
f"{color_code}{item['status']}: {item['message']}{color_reset}"
)

if built_target.returncode != 0:
_LOGGER.error(f"{color_red}Building target failed.{color_reset}")
Expand Down
8 changes: 6 additions & 2 deletions markmeld/melder.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,9 @@ def build_target(self, target_name, print_only=False, vardump=False):
@return [False|tgt] if the
"""
tgt = Target(self.cfg, target_name)
_LOGGER.info(f"MM | Building target: {tgt.target_name} from file {tgt.meta['_cfg_file_path']}")
_LOGGER.info(
f"MM | Building target: {tgt.target_name} from file {tgt.meta['_cfg_file_path']}"
)

# First, run any pre-builds
prebuild_results = self.build_side_targets(tgt, "prebuild")
Expand Down Expand Up @@ -558,7 +560,9 @@ def render_template(self, melded_input, target, double=None):
if "data" not in melded_input:
melded_input["data"] = {}
if "md_template" in target.meta:
raise Exception("Please update your config! 'md_template' was renamed to 'jinja_template'.")
raise Exception(
"Please update your config! 'md_template' was renamed to 'jinja_template'."
)

if "jinja_template" in target.meta and target.meta["jinja_template"]:
tpl = load_template(target.meta)
Expand Down
35 changes: 26 additions & 9 deletions markmeld/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

_LOGGER = getLogger(PKG_NAME)


# define some useful functions
def recursive_get(dat, indices):
"""
Expand Down Expand Up @@ -69,11 +70,13 @@ def format_command(tgt):
vars_to_exp = [v[1] for v in string.Formatter().parse(cmd) if v[1] is not None]
return cmd


# There are two paths associated with each target:
# 1. the location of its definition (defpath or filepath)
# 2. the location of where it should be executed (workpath)
# 1. the location of its definition (defpath or filepath)
# 2. the location of where it should be executed (workpath)
# These are not the same thing.


def load_config_wrapper(cfg_path, workpath=None, autocomplete=True):
"""
Wrapper function that maintains a list of imported files, to prevent duplicate imports.
Expand Down Expand Up @@ -101,7 +104,7 @@ def load_config_file(filepath, workpath=None, autocomplete=True, imported_list={
return load_config_data(
cfg_data, os.path.abspath(filepath), workpath, autocomplete, imported_list
)
except FileNotFoundError as e:
except FileNotFoundError as e:
_LOGGER.error(f"Couldn't load config file: {filepath} because: {repr(e)}")
return {} # Allow continuing if file not found
except Exception as e:
Expand All @@ -115,7 +118,9 @@ def make_abspath(relpath, filepath, root=None):
return os.path.abspath(os.path.join(os.path.dirname(filepath), relpath))


def load_config_data(cfg_data, filepath=None, workpath=None, autocomplete=True, imported_list={}):
def load_config_data(
cfg_data, filepath=None, workpath=None, autocomplete=True, imported_list={}
):
"""
Recursive loader that parses a yaml string, handles imports, and runs target factories to create targets.
"""
Expand All @@ -138,7 +143,9 @@ def load_config_data(cfg_data, filepath=None, workpath=None, autocomplete=True,
if "imports" in higher_cfg and higher_cfg["imports"]:
_LOGGER.debug("Found imports")
for import_file in higher_cfg["imports"]:
import_file_abspath = make_abspath(expandpath(import_file), expandpath(filepath))
import_file_abspath = make_abspath(
expandpath(import_file), expandpath(filepath)
)
if not autocomplete:
_LOGGER.info(f"Specified config file to import: {import_file_abspath}")
deep_update(
Expand All @@ -151,7 +158,9 @@ def load_config_data(cfg_data, filepath=None, workpath=None, autocomplete=True,
if "imports_relative" in higher_cfg and higher_cfg["imports_relative"]:
_LOGGER.debug("Found relative imports")
for import_file in higher_cfg["imports_relative"]:
import_file_abspath = make_abspath(expandpath(import_file), expandpath(filepath))
import_file_abspath = make_abspath(
expandpath(import_file), expandpath(filepath)
)
if not autocomplete:
_LOGGER.info(
f"Specified relative config file to import (relative): {import_file}"
Expand Down Expand Up @@ -192,9 +201,17 @@ def warn_overriding_target(old, new):
for tgt in new["targets"]:
if tgt in old["targets"]:
_LOGGER.error(f"Overriding target: {tgt}")
_LOGGER.error("Originally defined in: ".rjust(27, " ") + f"{old['targets'][tgt]['_defpath']}")
_LOGGER.error("Redefined in: ".rjust(27, " ") + f"{new['targets'][tgt]['_defpath']}")
raise Exception("Same target name is defined in imported file. Overriding targets is not allowed.")
_LOGGER.error(
"Originally defined in: ".rjust(27, " ")
+ f"{old['targets'][tgt]['_defpath']}"
)
_LOGGER.error(
"Redefined in: ".rjust(27, " ")
+ f"{new['targets'][tgt]['_defpath']}"
)
raise Exception(
"Same target name is defined in imported file. Overriding targets is not allowed."
)


def deep_update(old, new, warn_override=True):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_markmeld.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ def test_import():
print(res.melded_output)
assert "rVEeqUQ1t5" in str(res.melded_output)

cfg2 = markmeld.load_config_wrapper("tests/test_data/_markmeld_import_relative.yaml")
cfg2 = markmeld.load_config_wrapper(
"tests/test_data/_markmeld_import_relative.yaml"
)
mm2 = markmeld.MarkdownMelder(cfg2)

res = mm2.build_target("imported_target", print_only=True)
Expand Down

0 comments on commit f05d561

Please sign in to comment.