summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkballou <kballou@devnulllabs.io>2015-05-29 18:09:18 -0600
committerkballou <kballou@devnulllabs.io>2015-05-29 18:09:18 -0600
commit24033535d56d860658959df401c9726b154fdc49 (patch)
tree013bf925fae09586f160eab33716ddd92600d378
parent8502b90fba729968915a8dbc35f17f75a3693855 (diff)
downloadxnt-24033535d56d860658959df401c9726b154fdc49.tar.gz
xnt-24033535d56d860658959df401c9726b154fdc49.tar.xz
Move parameter passing
Full parameter passing (converting `key=value` to `{'key': 'value'}`)was happening in the `xnt.core_tasks` module, and attempting to merge the dictionaries. Move the former functionality to the `xnt.xenant` module and pass the result of the first pass to the `xnt.xntcall` function, which will merge the loaded properties with the parsed properties.
-rw-r--r--xnt/tasks/core_tasks.py14
-rw-r--r--xnt/tests/xenantparamparsertests.py63
-rw-r--r--xnt/xenant.py12
3 files changed, 81 insertions, 8 deletions
diff --git a/xnt/tasks/core_tasks.py b/xnt/tasks/core_tasks.py
index 9a5e1e8..66c40c5 100644
--- a/xnt/tasks/core_tasks.py
+++ b/xnt/tasks/core_tasks.py
@@ -243,12 +243,12 @@ def __xntcall__(buildfile, targets=None, props=None):
"""Invoke Build with `targets` passing `props`"""
def call_target(target_name, props):
"""Call target on build module"""
- def process_params(params, existing_props=None):
- """Parse and separate properties"""
- properties = existing_props if existing_props else {}
- for param in params:
- name, value = param.split("=")
- properties[name] = value
+ def merge_properties(params, existing_props=None):
+ """Merge existing properties with passed properties"""
+ if not existing_props:
+ return params
+ properties = existing_props.copy()
+ properties.update(params)
return properties
def __get_properties():
"""Return the properties dictionary of the build module"""
@@ -261,7 +261,7 @@ def __xntcall__(buildfile, targets=None, props=None):
if props and len(props) > 0:
setattr(build,
"PROPERTIES",
- process_params(props, __get_properties()))
+ merge_properties(props, __get_properties()))
target = getattr(build, target_name)
error_code = target()
return error_code if error_code else SUCCESS
diff --git a/xnt/tests/xenantparamparsertests.py b/xnt/tests/xenantparamparsertests.py
new file mode 100644
index 0000000..f855dd1
--- /dev/null
+++ b/xnt/tests/xenantparamparsertests.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+'''Xenant Arg Parser Tests'''
+
+# Xnt -- A Wrapper Build Tool
+# Copyright (C) 2013 Kenny Ballou
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+from xnt.xenant import _process_params_
+import unittest
+
+# pylint: disable=R0904
+class XenantParamParserTests(unittest.TestCase):
+ '''xenant param proessing tests'''
+
+ #pylint: disable=C0103
+ def test_process_return_empty_when_none(self):
+ '''Test process_params returns empty map when nil'''
+ result = _process_params_(None)
+ self.assertIsNotNone(result)
+ self.assertEquals(result, {})
+
+ #pylint: disable=C0103
+ def test_process_return_empty_when_zero_elements(self):
+ '''Test process_params returns empty map when zero params given'''
+ result = _process_params_([])
+ self.assertIsNotNone(result)
+ self.assertEquals(result, {})
+
+ #pylint: disable=C0103
+ def test_process_returns_dictionary_when_params(self):
+ '''Test process_params returns dictionary when given one param'''
+ params = ['key=value']
+ result = _process_params_(params)
+ self.assertIsNotNone(result)
+ self.assertIn('key', result)
+ self.assertEquals('value', result['key'])
+
+ #pylint: disable=C0103
+ def test_process_returns_dictionary_when_many(self):
+ '''Test process_params returns dictionary when given many params'''
+ params = ['key1=value1',
+ 'key2=value2',
+ 'key3=value3',
+ 'key4=value4',]
+ result = _process_params_(params)
+ self.assertIsNotNone(result)
+ for i in range(len(params)):
+ key = 'key%d' % (i + 1)
+ value = 'value%d' % (i + 1)
+ self.assertIn(key, result)
+ self.assertEquals(value, result[key])
diff --git a/xnt/xenant.py b/xnt/xenant.py
index edc0226..263bf83 100644
--- a/xnt/xenant.py
+++ b/xnt/xenant.py
@@ -46,7 +46,7 @@ def main():
else:
error_code = xnt.xntcall(build_file,
args["targets"],
- args["properties"])
+ _process_params_(args["properties"]))
elapsed_time = time.time() - start_time
print("Execution time: %.3f" % elapsed_time)
if error_code != 0:
@@ -58,6 +58,16 @@ def main():
if error_code != 0:
sys.exit(error_code)
+def _process_params_(params):
+ '''Parse and separate properties'''
+ if not params:
+ return {}
+ properties = {}
+ for param in params:
+ name, value = param.split('=')
+ properties[name] = value
+ return properties
+
def parse_args(args_in):
"""Parse and group arguments"""
parser = argparse.ArgumentParser(prog="Xnt")