def process_doc(doc, env): wrapped_env = UsedDict(env) # Expand the template strings with the command-line arguments def expand(template_string): try: return formatter.vformat(template_string, [], wrapped_env) except KeyError as ex: die("Missing value for {name} in '{template}'".format(name=str(ex), template=template_string)) def process(elem): for name, value in elem.attributes.items(): if "{" in value: elem.attributes[name] = expand(value) for child in elem.childNodes: if child.nodeType == Node.TEXT_NODE: child.data = expand(child.data) elif child.nodeType == Node.ELEMENT_NODE: process(child) def process(elem): for name, value in elem.attributes.items(): if "{" in value: elem.attributes[name] = expand(value) for child in elem.childNodes: if child.nodeType == Node.TEXT_NODE: child.data = expand(child.data) elif child.nodeType == Node.ELEMENT_NODE: process(child) result = process(doc.documentElement) for x in env: if x not in wrapped_env.used: die("Unused parameter '{name}'".format(name=x)) return result
def __getitem__(self, key): assert key != 'foo' if key in self.underlying: self.used.add(key) return self.underlying[key] else: value = os.getenv(key) if value is None: die("Missing value for '{key}'".format(key = key)) else: return value
def create(options): template = options.template if options.substitutions: die("{template} does not exist".format(template = template)) if template.endswith('.xml.template'): remote = True elif template.endswith('.xml'): remote = False else: die("'{template}' does not end with .xml.template or .xml".format(template = template)) print("'{template}' does not exist; creating new template.".format(template = template)) if not remote: print("\nAs it ends with .xml, not .xml.template, I assume you want a feed for\n" "a local project (e.g. a Git checkout). If you want a template for\n" "publishing existing releases, use {template}.template instead.".format( template = template)) doc = minidom.parse(os.path.join(mydir, "example.xml")) impls = doc.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'implementation') impls[0].parentNode.removeChild(impls[0] if remote else impls[1]) choice = get_choice("Does your program need to be compiled before it can be used?", [ (1, "Generate a source template (e.g. for compiling C source code)"), (2, "Generate a binary template (e.g. for a pre-compiled binary or script)"), ]) commands = doc.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'command') commands[0].parentNode.removeChild(commands[choice - 1]) impl, = doc.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'implementation') if choice == 1: impl.setAttribute('arch', '*-src') else: impl.setAttribute('arch', '*-*') assert not os.path.exists(template), template print("\nWriting", template) with open(template, 'wt') as stream: stream.write('<?xml version="1.0"?>\n') doc.documentElement.writexml(stream) stream.write('\n')
def expand(template_string): try: return formatter.vformat(template_string, [], wrapped_env) except KeyError as ex: die("Missing value for {name} in '{template}'".format(name=str(ex), template=template_string))