rspec/__init__.py

79 lines
1.9 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
try:
import jinja2
except ImportError:
jinja2 = None
import optparse
import os
import sys
import importlib
BASEPATH = os.path.abspath(os.path.join(os.path.dirname(__file__)))
sys.path.insert(0, BASEPATH)
ITYPE_SEC = "SEC"
ITYPE_REQ = "REQ"
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(ITYPE_SEC.lower()):
raise KeyError(
"You need to specify a parent for non sec entries")
#
# Create Structure
#
if uuid.lower().startswith(ITYPE_SEC.lower()):
# 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(ITYPE_SEC.lower()):
kwargs[self.KEY_SEC_CHILDS] = []
self[self.KEY_MAIN_ENTRIES][uuid] = kwargs
#
return uuid