# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import re
import copy
import logging
from faz.task import Task
TASK_PATTERN = r"^#[ ]*(?P<outputs>[a-zA-Z0-9, \.\$_\-\[\]\*]+)*[ ]*<-[ ]*(?P<inputs>[a-zA-Z0-9, \.\$_\-\[\]\*]+)*[ ]*[:]*[ ]*(?P<options>[a-zA-Z0-9, \.\$_\-\[\]\*]+)*"
[docs]def split_task_parameters(line):
""" Split a string of comma separated words."""
if line is None:
result = []
else:
result = [parameter.strip() for parameter in line.split(",")]
return result
[docs]def find_tasks(lines):
"""
Find task lines and corresponding line numbers in a list of lines.
"""
tasks = []
linenumbers = []
pattern = re.compile(TASK_PATTERN)
for n, line in enumerate(lines):
if "#" in line and "<-" in line:
m = pattern.match(line)
if m is not None:
groupdict = m.groupdict()
linenumbers.append(n)
for key in groupdict:
groupdict[key] = split_task_parameters(groupdict[key])
logging.debug(
"{0}: {1}".format(key, ", ".join(groupdict[key])))
tasks.append(groupdict)
linenumbers.append(len(lines))
return tasks, linenumbers
[docs]def create_environment(preamble):
"""
Create a dictionary of variables obtained from the preamble of
the task file and the environment the program is running on.
"""
environment = copy.deepcopy(os.environ)
for line in preamble:
logging.debug(line)
if "=" in line and not line.startswith("#"):
tmp = line.split("=")
key = tmp[0].strip()
value = tmp[1].strip()
logging.debug(
"Found variable {} with value {}".format(key, value))
environment.update({key: value})
logging.debug("Env {}".format(environment))
return environment