diff options
author | Luis Araujo <luis.araujo@collabora.co.uk> | 2014-11-14 23:03:01 (GMT) |
---|---|---|
committer | Luis Araujo <luis.araujo@collabora.co.uk> | 2014-11-14 23:03:01 (GMT) |
commit | 33d17b47fe50e7b709f69a71330c20c706af9d78 (patch) | |
tree | dd2f29006ecc5a6bf8befdb2e1d4b30b5b44a5b9 | |
parent | 5deb47054fb14437ffd9088007b66c552c2fec45 (diff) | |
download | singular-33d17b47fe50e7b709f69a71330c20c706af9d78.tar.gz singular-33d17b47fe50e7b709f69a71330c20c706af9d78.tar.xz |
Add the --template option to process templates
This will allow to process expressions of the form:
'field0:value0@field1:value1@...' from the command line to
replace template values.
This offers a flexible way to specify fields values and it
should help for implementing profile support.
-rwxr-xr-x | singular.py | 58 |
1 files changed, 36 insertions, 22 deletions
diff --git a/singular.py b/singular.py index cb2b94b..53458f5 100755 --- a/singular.py +++ b/singular.py @@ -80,9 +80,13 @@ class Cli(object): help="set the image date") lava_parser.add_argument('--device-type',type=str, help="set the device type") - lava_parser.add_argument('--arch',type=str, help="set the arch type") + lava_parser.add_argument('--arch', type=str, help="set the arch type") lava_parser.add_argument('-p', '--print-json', action='store_true', help="print the json job to stdout") + lava_parser.add_argument('-t', '--template', type=str, + help="receive the 'field:value[@field:value...]' " + "expression for using in the template fields " + "substitution") lava_parser.add_argument('--log-file', type=str, help="set the log file") lava_parser.set_defaults(get_command=Lava) @@ -102,25 +106,30 @@ class Lava(object): self.server = self.open_connection() def run(self): + + template = None + if self.args.template: + # '@' is used as the field:value separator + fields = self.args.template.split('@') + # The template value translates to a hash that we can easily use + # for mapping the fields -> values in the json template + template = {} + for field in fields: + k, v = field.split(':') + template[k] = v + # Submit jobs if self.args.submit_job: for job_file in self.args.submit_job: with open(job_file) as job_file_obj: json_obj = json.loads(job_file_obj.read()) - - # Template fields '{{field}}' replacement - if self.args.image_date: - json_obj = self._replace_image_date(json_obj) - - if self.args.device_type: - json_obj = self._replace_device_type(json_obj) - - if self.args.arch: - json_obj = self._replace_arch(json_obj) + # Search and replace template fields if '-t' flag specified + if template: + json_obj = replaceTemplateFields(json_obj, template) if self.args.print_json: print json.dumps(json_obj, indent=2) - return + continue job_id = self.server.scheduler.submit_job(json.dumps(json_obj)) self.settings.logger.info( @@ -129,21 +138,26 @@ class Lava(object): def open_connection(self): # Open connection with the server. - return ServerProxy("https://%s:%s@%s/RPC2" % - (self.settings.config['user'], - self.settings.config['auth-token'], - self.settings.config['server']), + return ServerProxy("https://%s:%s@%s/RPC2" % + (self.settings.config['user'], + self.settings.config['auth-token'], + self.settings.config['server']), use_datetime=True) - def _replace_image_date(self, json_obj): - return procTemplate(r'{{image-date}}', self.args.image_date, json_obj) - def _replace_device_type(self, json_obj): - return procTemplate(r'{{device-type}}', self.args.device_type, json_obj) +def replaceTemplateFields(json_obj, template): + """Iterate over the template hash and process the json_obj for each value - def _replace_arch(self, json_obj): - return procTemplate(r'{{arch}}', self.args.arch, json_obj) + :param json_obj: The json object to be processed + :param template: The dictionary containing the key -> value map for the + template fields + :returns: The processed json_obj with the templates fields replaced + """ + j = json_obj + for pattern in template: + j = procTemplate(r'{{'+pattern+'}}', template[pattern], j) + return j def procTemplate(pattern, value, json_obj): """Process the json template. |