Skip to content

Commit

Permalink
Merge pull request #111 from ENCODE-DCC/dev
Browse files Browse the repository at this point in the history
v1.5.1
  • Loading branch information
leepc12 authored Apr 8, 2021
2 parents d23bd97 + 5c8f926 commit f8c689c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
2 changes: 1 addition & 1 deletion caper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .caper_runner import CaperRunner

__all__ = ['CaperClient', 'CaperClientSubmit', 'CaperRunner']
__version__ = '1.5.0'
__version__ = '1.5.1'
2 changes: 1 addition & 1 deletion caper/caper_backend_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def create_file(

if custom_backend_conf is not None:
s = AutoURI(custom_backend_conf).read()
hocon_s.merge(s)
hocon_s.merge(s, update=True)

final_backend_conf_file = os.path.join(directory, basename)
AutoURI(final_backend_conf_file).write(str(hocon_s) + '\n')
Expand Down
32 changes: 25 additions & 7 deletions caper/hocon_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
logger = logging.getLogger(__name__)


NEW_LINE = '\n'


class HOCONString:
RE_INCLUDE_LINE = r'^\s*include\s'

Expand All @@ -18,14 +21,14 @@ def __init__(self, hocon_str):
"""
lines_include = []
lines_wo_include = []
for line in hocon_str.split('\n'):
for line in hocon_str.splitlines():
if re.findall(HOCONString.RE_INCLUDE_LINE, line):
lines_include.append(line)
else:
lines_wo_include.append(line)

self._include = '\n'.join(lines_include)
self._contents_wo_include = '\n'.join(lines_wo_include)
self._include = NEW_LINE.join(lines_include)
self._contents_wo_include = NEW_LINE.join(lines_wo_include)

def __str__(self):
return self.get_contents()
Expand All @@ -35,7 +38,7 @@ def from_dict(cls, d, include=''):
hocon = ConfigFactory.from_dict(d)
hocon_str = HOCONConverter.to_hocon(hocon)
if include:
hocon_str = include + '\n' + hocon_str
hocon_str = NEW_LINE.join([include, hocon_str])
return cls(hocon_str=hocon_str)

def to_dict(self):
Expand All @@ -45,7 +48,17 @@ def to_dict(self):
j = HOCONConverter.to_json(c)
return json.loads(j)

def merge(self, b):
def merge(self, b, update=False):
"""Merge self with b and then returns a plain string of merged.
Args:
b:
HOCONString, dict, str to be merged.
b's `include` statement will always be ignored.
update:
If `update` then replace self with a merged one.
Returns:
String of merged HOCONs.
"""
if isinstance(b, HOCONString):
d = b.to_dict()
elif isinstance(b, str):
Expand All @@ -59,7 +72,12 @@ def merge(self, b):
merge_dict(self_d, d)

hocon = ConfigFactory.from_dict(self_d)
return self._include + '\n' + HOCONConverter.to_hocon(hocon)

contents_wo_include = HOCONConverter.to_hocon(hocon)
if update:
self._contents_wo_include = contents_wo_include

return NEW_LINE.join([self._include, contents_wo_include])

def get_include(self):
return self._include
Expand All @@ -68,4 +86,4 @@ def get_contents(self, without_include=False):
if without_include:
return self._contents_wo_include
else:
return self._include + '\n' + self._contents_wo_include
return NEW_LINE.join([self._include, self._contents_wo_include])
34 changes: 33 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import os
import re
from pathlib import Path

import setuptools

META_PATH = Path('caper', '__init__.py')
HERE = os.path.abspath(os.path.dirname(__file__))


def read(*parts):
"""
Build an absolute path from *parts* and and return the contents of the
resulting file. Assume UTF-8 encoding.
"""
with Path(HERE, *parts).open(encoding='utf-8') as f:
return f.read()


META_FILE = read(META_PATH)


def find_meta(meta):
"""
Extract __*meta*__ from META_FILE.
"""
meta_match = re.search(
r"^__{meta}__ = ['\"]([^'\"]*)['\"]".format(meta=meta), META_FILE, re.M
)
if meta_match:
return meta_match.group(1)
raise


with open('README.md', 'r') as fh:
long_description = fh.read()

setuptools.setup(
name='caper',
version='1.5.0',
version=find_meta('version'),
python_requires='>=3.6',
scripts=[
'bin/caper',
Expand Down
7 changes: 7 additions & 0 deletions tests/test_hocon_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ def test_merge():
assert str(hsm) == hs1.merge(d2)
assert str(hsm) == hs1.merge(s2)

# merge with update
# item 1 should be updated with merged
hs1_original_str = str(hs1)
assert str(hsm) == hs1.merge(hs2, update=True)
assert str(hs1) == str(hsm)
assert hs1_original_str != str(hs1)


def test_get_include():
s2 = get_test_hocon_str2()
Expand Down

0 comments on commit f8c689c

Please sign in to comment.