Skip to content
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

feat(args): Change config arguments to be a list #110

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/cijoe/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ def cli_example(args):
err = 0

resources = get_resources()

resource = resources["configs"].get(f"{args.example}.default-config", None)
if resource is None:
log.error(
Expand Down Expand Up @@ -276,7 +275,7 @@ def cli_workflow(args):
log.error(f"aborting; output({args.output}) directory already exists")
return errno.EPERM

config = Config(args.config)
config = Config([args.config])
errors = config.load()
if errors:
log_errors(errors)
Expand Down Expand Up @@ -417,9 +416,10 @@ def parse_args():
workflow_group.add_argument(
"--config",
"-c",
nargs="+",
type=Path,
default=Path(os.environ.get("CIJOE_DEFAULT_CONFIG", DEFAULT_CONFIG_FILENAME)),
help="Path to the Configuration file.",
default=[Path(os.environ.get("CIJOE_DEFAULT_CONFIG", DEFAULT_CONFIG_FILENAME))],
help="Paths to the Configuration file.",
)
workflow_group.add_argument(
"--workflow",
Expand Down Expand Up @@ -552,10 +552,17 @@ def main(args=None):

for filearg in ["config", "workflow"]:
argv = getattr(args, filearg)
path = search_for_file(argv)
if path is None:
log.error(f"{filearg}({argv}) does not exist; exiting")
return errno.EINVAL
if isinstance(argv, list):
for p in argv:
path = search_for_file(p)
if path is None:
log.error(f"{filearg}({p}) does not exist; exiting")
return errno.EINVAL
else:
path = search_for_file(argv)
if path is None:
log.error(f"{filearg}({argv}) does not exist; exiting")
return errno.EINVAL

setattr(args, filearg, path)

Expand Down
41 changes: 33 additions & 8 deletions src/cijoe/core/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import sys
from importlib.machinery import SourceFileLoader
from pathlib import Path
from typing import Any, Dict, List, Union
from typing import Any, Dict, List, Optional, Union

try:
from importlib.resources import files as importlib_files
Expand Down Expand Up @@ -156,7 +156,30 @@ def content_from_file(self):
self.content = resource_file.read()


class Config(Resource):
class ResourceMultiFiles(object):
"""Base representation of a Resource that spans over multiple files"""

def __init__(self, paths: List[Path], pkg=None):
self.paths = [path.resolve() for path in paths]
self.pkg = pkg

self.content = None

prefix = ".".join(pkg.name.split(".")[1:-1]) + "." if pkg else ""

self.ident = f"{prefix}{'_'.join([path.stem for path in self.paths])}"

@property
def path(self) -> Optional[Path]:
if len(self.paths):
return self.paths[0]
return None

def __repr__(self):
return "_".join([str(path) for path in self.paths])


class Config(ResourceMultiFiles):
"""
Encapsulation of a CIJOE config-file, e.g. 'default-config.toml'

Expand All @@ -165,16 +188,18 @@ class Config(Resource):

SUFFIX = ".toml"

def __init__(self, path: Path, pkg=None):
super().__init__(path, pkg)
def __init__(self, paths: List[Path], pkg=None):
super().__init__(paths, pkg)

self.options: Dict[str, Any] = {}

def load(self):
"""Populates self.options on success. Returns a list of errors otherwise"""

# config_dict = dict_from_yamlfile(self.path)
config_dict = dict_from_tomlfile(self.path)
config_dict = {}
for path in self.paths:
config_dict.update(dict_from_tomlfile(path))

errors = dict_substitute(config_dict, default_context())
if errors:
Expand All @@ -184,14 +209,14 @@ def load(self):
return []

@staticmethod
def from_path(path, pkg=None):
def from_path(path: Path, pkg=None):
"""Instantiate a Config from path, returning None on error"""

path = Path(path).resolve()
if not path.exists():
return None

config = Config(path, pkg)
config = Config([path], pkg)
errors = config.load()
if errors:
return None
Expand Down Expand Up @@ -461,7 +486,7 @@ def __process_candidate(self, candidate: Path, category: str, pkg):
if not resource.content_has_script_func():
category = "auxiliary"
elif category == "configs":
resource = Config(candidate, pkg)
resource = Config([candidate], pkg)
elif category == "workflows":
resource = Workflow(candidate, pkg)
else:
Expand Down
Loading