Skip to content

Commit

Permalink
use kotekan config function for parsing yaml/j2 files
Browse files Browse the repository at this point in the history
  • Loading branch information
aelanman committed Jun 16, 2022
1 parent 788fa79 commit e01e3e2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 26 deletions.
29 changes: 3 additions & 26 deletions coco/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path
from typing import List, Dict
import yaml
from kotekan.config import load_config_file

from .result import Result
from .util import Host, PersistentState, hash_dict
Expand Down Expand Up @@ -184,32 +185,8 @@ def read_from_file(self, path, file):
element, name = self._find_new(path)

file_ext = os.path.splitext(file)[1]
if not file_ext == ".j2":
with open(file, "r") as stream:
try:
new_state = yaml.safe_load(stream)
except yaml.YAMLError as exc:
logger.error(f"Failure reading YAML file {file}: {exc}")
else:
# Is a jinja file
try:
from jinja2 import FileSystemLoader, Environment, select_autoescape
from jinja2 import TemplateNotFound
except ImportError as err:
logger.error("Jinja2 required for '.j2' files.")

directory, file_name = os.path.split(file)
directory = '.' if directory == '' else directory
# Load the template
env = Environment(
loader=FileSystemLoader(directory), autoescape=select_autoescape()
)
try:
template = env.get_template(file_name)
except TemplateNotFound as err:
logger.error(f"Could not open file {file}")
yaml_raw = template.render()
new_state = yaml.safe_load(yaml_raw)

new_state = load_config_file(file, return_dict=True, dump=False, jinja_options=None)

# Don't load state parts that are excluded from reset
self._exclude_paths(path, new_state)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
comet @ git+https://github.com/chime-experiment/comet.git@ael/fixjson#egg=comet
kotekan @ git+https://github.com/kotekan/kotekan.git@develop#egg=kotekan&subdirectory=python/
aiohttp
pyyaml
requests
Expand Down

0 comments on commit e01e3e2

Please sign in to comment.