From 770a84a13e30f5e5d8218ae495b430d0c3002f56 Mon Sep 17 00:00:00 2001 From: Shaun McCance Date: Fri, 1 Nov 2013 10:14:42 -0400 Subject: itstool.in: Allow users to set ITS params --- its/its.its | 1 + itstool.in | 62 +++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/its/its.its b/its/its.its index 3cd76c7..8f17c36 100644 --- a/its/its.its +++ b/its/its.its @@ -1,5 +1,6 @@ + diff --git a/itstool.in b/itstool.in index 671254c..150b395 100755 --- a/itstool.in +++ b/itstool.in @@ -528,11 +528,14 @@ class Document (object): if self._xml_err: raise libxml2.parserError(self._xml_err) - def register_its_params(self, xpath, rules): + def register_its_params(self, xpath, rules, params={}): for child in xml_child_iter(rules): if xml_is_ns_name(child, NS_ITS, 'param'): name = child.nsProp('name', None) - value = child.getContent() + if params.has_key(name): + value = params[name] + else: + value = child.getContent() xpath.xpathRegisterVariable(name, None, value) def apply_its_rule(self, rule, xpath): @@ -697,7 +700,7 @@ class Document (object): self._its_externals[node] = res[0].content xpath.setContextNode(oldnode) - def apply_its_rules (self, builtins): + def apply_its_rules(self, builtins, params={}): if builtins: dirs = [] ddir = os.getenv('XDG_DATA_HOME', '') @@ -718,11 +721,11 @@ class Document (object): for dfile in os.listdir(itsdir): if dfile.endswith('.its'): if not ddone.get(dfile, False): - self.apply_its_file(os.path.join(itsdir, dfile)) + self.apply_its_file(os.path.join(itsdir, dfile), params=params) ddone[dfile] = True - self.apply_local_its_rules() + self.apply_local_its_rules(params=params) - def apply_its_file (self, filename): + def apply_its_file(self, filename, params={}): doc = libxml2.parseFile(filename) root = doc.getRootElement() if not xml_is_ns_name(root, NS_ITS, 'rules'): @@ -772,10 +775,10 @@ class Document (object): xpath.xpathRegisterNs(nsdef.name, nsdef.content) nsdef = nsdef.next par = par.parent - self.register_its_params(xpath, root) + self.register_its_params(xpath, root, params=params) self.apply_its_rule(rule, xpath) - def apply_local_its_rules (self): + def apply_local_its_rules(self, params={}): for rules in self._localrules: def reg_ns(xpath, node): if node.parent is not None: @@ -787,14 +790,14 @@ class Document (object): nsdef = nsdef.next xpath = self._doc.xpathNewContext() reg_ns(xpath, rules) - self.register_its_params(xpath, rules) + self.register_its_params(xpath, rules, params=params) for rule in xml_child_iter(rules): if rule.type != 'element': continue if rule.nsDefs() is not None: rule_xpath = self._doc.xpathNewContext() reg_ns(rule_xpath, rule) - self.register_its_params(rule_xpath, rules) + self.register_its_params(rule_xpath, rules, params=params) else: rule_xpath = xpath self.apply_its_rule(rule, rule_xpath) @@ -1398,30 +1401,30 @@ if __name__ == '__main__': action='append', dest='itsfile', metavar='ITS', - help='load the ITS rules in the file ITS (can specify multiple times)') + help='Load the ITS rules in the file ITS (can specify multiple times)') options.add_option('-l', '--lang', dest='lang', default=None, metavar='LANGUAGE', - help='explicitly set the language code for output file') + help='Explicitly set the language code for output file') options.add_option('-j', '--join', dest='join', metavar='FILE', - help='join multiple MO files with the XML file FILE and output XML file') + help='Join multiple MO files with the XML file FILE and output XML file') options.add_option('-m', '--merge', dest='merge', metavar='FILE', - help='merge from a PO or MO file FILE and output XML files') + help='Merge from a PO or MO file FILE and output XML files') options.add_option('-n', '--no-builtins', action='store_true', dest='nobuiltins', default=False, - help='do not apply the built-in ITS rules') + help='Do not apply the built-in ITS rules') options.add_option('-o', '--output', dest='output', default=None, metavar='OUT', - help='output PO files to file OUT or XML files in directory OUT') + help='Output PO files to file OUT or XML files in directory OUT') options.add_option('-s', '--strict', action='store_true', dest='strict', @@ -1437,30 +1440,41 @@ if __name__ == '__main__': dest='keep_entities', default=False, help='Keep entity reference unexpanded') + options.add_option('-p', '--param', + action='append', + dest='params', + default=[], + nargs=2, + metavar='NAME VALUE', + help='Define the ITS parameter NAME to the value VALUE (can specify multiple times)') options.add_option('-t', '--test', dest='test', default=None, metavar='CATEGORY', - help='generate conformance test output for CATEGORY') + help='Generate conformance test output for CATEGORY') options.add_option('-v', '--version', action='store_true', dest='version', default=False, - help='print itstool version and exit') + help='Print itstool version and exit') (opts, args) = options.parse_args(sys.argv) if opts.version: print('itstool %s' % VERSION) sys.exit(0) + params = {} + for name, value in opts.params: + params[name] = value + if opts.merge is None and opts.join is None: messages = MessageList() for filename in args[1:]: doc = Document(filename, messages, load_dtd=opts.load_dtd, keep_entities=opts.keep_entities) - doc.apply_its_rules(not(opts.nobuiltins)) + doc.apply_its_rules(not(opts.nobuiltins), params=params) if opts.itsfile is not None: for itsfile in opts.itsfile: - doc.apply_its_file(itsfile) + doc.apply_its_file(itsfile, params=params) if opts.test is None: doc.generate_messages() if opts.output is None or opts.output == '-': @@ -1499,10 +1513,10 @@ if __name__ == '__main__': for filename in args[1:]: messages = MessageList() doc = Document(filename, messages, load_dtd=opts.load_dtd, keep_entities=opts.keep_entities) - doc.apply_its_rules(not(opts.nobuiltins)) + doc.apply_its_rules(not(opts.nobuiltins), params=params) if opts.itsfile is not None: for itsfile in opts.itsfile: - doc.apply_its_file(itsfile) + doc.apply_its_file(itsfile, params=params) try: doc.merge_translations(translations, opts.lang, strict=opts.strict) except Exception as e: @@ -1531,13 +1545,13 @@ if __name__ == '__main__': out = file(opts.output, 'w') messages = MessageList() doc = Document(opts.join, messages) - doc.apply_its_rules(not(opts.nobuiltins)) + doc.apply_its_rules(not(opts.nobuiltins), params=params) doc.join_translations(translations, strict=opts.strict) out.write(doc._doc.serialize('utf-8')) if False: if opts.itsfile is not None: for itsfile in opts.itsfile: - doc.apply_its_file(itsfile) + doc.apply_its_file(itsfile, params=params) try: doc.merge_translations(translations, opts.lang, strict=opts.strict) except Exception as e: -- cgit v1.2.1