#!/usr/bin/env python # -*- coding: utf-8 -*- # import jinja2 import optparse import os import sys import importlib BASEPATH = os.path.abspath(os.path.join(os.path.dirname(__file__))) sys.path.insert(0, BASEPATH) class rspec(dict): KEY_MAIN_TITLE = "title" KEY_MAIN_ENTRIES = "entries" KEY_MAIN_SECTIONS = "sections" KEY_SEC_CHILDS = "childs" def __init__(self): super().__init__() self[self.KEY_MAIN_ENTRIES] = {} self[self.KEY_MAIN_SECTIONS] = [] def add_title(self, title): self[self.KEY_MAIN_TITLE] = title def add(self, **kwargs): # # UUID # try: uuid = kwargs.pop("itemtype") + "-" + "%04d" % kwargs.pop("id") except KeyError: raise KeyError( "Missing required argument itemtype or id in kwargs: %s" % repr(kwargs)) # # Check uuid is unique # if uuid in self[self.KEY_MAIN_ENTRIES]: raise AttributeError("UUID %s already exists in entries" % uuid) # # Parent # try: parent = kwargs.pop("parent") except KeyError: if not uuid.lower().startswith("sec"): raise KeyError( "You need to specify a parent for non sec entries") # # Create Structure # if uuid.lower().startswith("sec"): # Create section self[self.KEY_MAIN_SECTIONS].append(uuid) else: # Add item to section self[self.KEY_MAIN_ENTRIES][parent][self.KEY_SEC_CHILDS].append(uuid) # # Add item to itemlist # if uuid.lower().startswith("sec"): kwargs[self.KEY_SEC_CHILDS] = [] self[self.KEY_MAIN_ENTRIES][uuid] = kwargs # return uuid def rs_by_spec_file(spec_file): spec = importlib.util.spec_from_file_location("specification", spec_file) s = importlib.util.module_from_spec(spec) sys.modules["specification"] = s spec.loader.exec_module(s) # rs = rspec() s.specification(rs) return rs if __name__ == '__main__': parser = optparse.OptionParser("usage: %prog reqif_file") parser.add_option("-t", "--template", dest="template", default=os.path.join(BASEPATH, 'templates', 'tex', 'requirement_specification.tex'), help="path to the template") (options, args) = parser.parse_args() # # Check options and args if len(args) != 1 or not os.path.isfile(args[0]): parser.print_help() else: rs = rs_by_spec_file(args[0]) # template_path = os.path.abspath(options.template) jenv = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.dirname(template_path))) template = jenv.get_template(os.path.basename(template_path)) print(template.render(data=rs))