From 6c303c6cce0168b478d3febfa9a446ed0684d7ef Mon Sep 17 00:00:00 2001 From: Jin Lee Date: Wed, 7 Apr 2021 11:41:42 -0700 Subject: [PATCH 1/4] added update option for HOCONString.merge() --- caper/caper_backend_conf.py | 2 +- caper/hocon_string.py | 32 +++++++++++++++++++++++++------- tests/test_hocon_string.py | 7 +++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/caper/caper_backend_conf.py b/caper/caper_backend_conf.py index ce9f1c81..10f93c6c 100644 --- a/caper/caper_backend_conf.py +++ b/caper/caper_backend_conf.py @@ -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') diff --git a/caper/hocon_string.py b/caper/hocon_string.py index 42a41ea0..d566fb51 100644 --- a/caper/hocon_string.py +++ b/caper/hocon_string.py @@ -9,6 +9,9 @@ logger = logging.getLogger(__name__) +NEW_LINE = '\n' + + class HOCONString: RE_INCLUDE_LINE = r'^\s*include\s' @@ -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.split(NEW_LINE): 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() @@ -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): @@ -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): @@ -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 @@ -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]) diff --git a/tests/test_hocon_string.py b/tests/test_hocon_string.py index 7d26b397..cf3ca18e 100644 --- a/tests/test_hocon_string.py +++ b/tests/test_hocon_string.py @@ -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() From 32ad0ef8fc60e2d276a7b8774868650b30a740a6 Mon Sep 17 00:00:00 2001 From: Jin Lee Date: Wed, 7 Apr 2021 11:42:20 -0700 Subject: [PATCH 2/4] bump ver --- caper/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/caper/__init__.py b/caper/__init__.py index 62d11167..c2155f5c 100644 --- a/caper/__init__.py +++ b/caper/__init__.py @@ -2,4 +2,4 @@ from .caper_runner import CaperRunner __all__ = ['CaperClient', 'CaperClientSubmit', 'CaperRunner'] -__version__ = '1.5.0' +__version__ = '1.5.1' diff --git a/setup.py b/setup.py index d9b9b92f..451cb9f7 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name='caper', - version='1.5.0', + version='1.5.1', python_requires='>=3.6', scripts=[ 'bin/caper', From 2d1b4922e7e20ddee1ec71706f61f5baffe8b0b2 Mon Sep 17 00:00:00 2001 From: Jin Lee Date: Wed, 7 Apr 2021 11:50:17 -0700 Subject: [PATCH 3/4] get version from __init__ --- setup.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 451cb9f7..ce4b41e6 100644 --- a/setup.py +++ b/setup.py @@ -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.1', + version=find_meta('version'), python_requires='>=3.6', scripts=[ 'bin/caper', From 3a3ef5ce05d3e971410aad265f3f0efb8817cdbe Mon Sep 17 00:00:00 2001 From: Jin Lee Date: Wed, 7 Apr 2021 13:18:55 -0700 Subject: [PATCH 4/4] better way of split lines --- caper/hocon_string.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caper/hocon_string.py b/caper/hocon_string.py index d566fb51..b7f8127f 100644 --- a/caper/hocon_string.py +++ b/caper/hocon_string.py @@ -21,7 +21,7 @@ def __init__(self, hocon_str): """ lines_include = [] lines_wo_include = [] - for line in hocon_str.split(NEW_LINE): + for line in hocon_str.splitlines(): if re.findall(HOCONString.RE_INCLUDE_LINE, line): lines_include.append(line) else: