chiark / gitweb /
make-secnet-sites: pline: Break up `copyout'
[secnet.git] / make-secnet-sites
index b666351e9e05917f62367bb2048012bbf7336b05..38badbbe830f8562bf512f44aa5cabe9938c4a3a 100755 (executable)
@@ -85,6 +85,10 @@ if version_info.major == 2:  # for python2
 
 max={'rsa_bits':8200,'name':33,'dh_bits':8200}
 
+def debugrepr(*args):
+       if debug_level > 0:
+               print(repr(args), file=sys.stderr)
+
 class Tainted:
        def __init__(self,s,tline=None,tfile=None):
                self._s=s
@@ -116,7 +120,9 @@ class Tainted:
                if maxlen is None: maxlen=max[what]
                self._max_ok(what,maxlen)
                if self._ok is False: return False
-               if bad.search(self._s): return self._bad(what,'bad syntax')
+               if bad.search(self._s):
+                       #print(repr(self), file=sys.stderr)
+                       return self._bad(what,'bad syntax')
                return True
 
        def _rtnval(self, is_ok, ifgood, ifbad=''):
@@ -229,6 +235,7 @@ def parse_args():
        global of
        global prefix
        global key_prefix
+       global debug_level
 
        ap = argparse.ArgumentParser(description='process secnet sites files')
        ap.add_argument('--userv', '-u', action='store_true',
@@ -238,9 +245,11 @@ def parse_args():
                 help='prefix conf file key names derived from sites data')
        ap.add_argument('--prefix', '-P', nargs=1,
                        help='set prefix')
+       ap.add_argument('--debug', '-D', action='count', default=0)
        ap.add_argument('arg',nargs=argparse.REMAINDER)
        av = ap.parse_args()
-       #print(repr(av), file=sys.stderr)
+       debug_level = av.debug
+       debugrepr('av',av)
        service = 1 if av.userv else 0
        prefix = '' if av.prefix is None else av.prefix[0]
        key_prefix = av.conf_key_prefix
@@ -580,8 +589,15 @@ def set_property(obj,w):
        else:
                obj.properties[propname]=kw[0](w)
 
+class FilterState:
+       def __init__(self):
+               self.reset()
+       def reset(self):
+               # called when we enter a new node,
+               # in particular, at the start of each site
+               pass
 
-def pline(il,allow_include=False):
+def pline(il,filterstate,allow_include=False):
        "Process a configuration file line"
        global allow_defs, obstack, root
        w=il.rstrip('\n').split()
@@ -589,9 +605,9 @@ def pline(il,allow_include=False):
        w=list([Tainted(x) for x in w])
        keyword=w[0]
        current=obstack[len(obstack)-1]
-       copyout=lambda: ['    '*len(obstack) +
-                       ' '.join([ww.output() for ww in w]) +
-                       '\n']
+       copyout_core=lambda: ' '.join([ww.output() for ww in w])
+       indent='    '*len(obstack)
+       copyout=lambda: [indent + copyout_core() + '\n']
        if keyword=='end-definitions':
                keyword.raw_mark_ok()
                allow_defs=sitelevel.depth
@@ -638,6 +654,7 @@ def pline(il,allow_include=False):
                                sys.exit(1)
                        current.children[tname]=nl
                        current=nl
+               filterstate.reset()
                obstack.append(current)
                return copyout()
        if keyword.raw() not in current.allow_properties:
@@ -665,10 +682,11 @@ def pfile(name,lines,allow_include=False):
        file=name
        line=0
        outlines=[]
+       filterstate = FilterState()
        for i in lines:
                line=line+1
                if (i[0]=='#'): continue
-               outlines += pline(i,allow_include=allow_include)
+               outlines += pline(i,filterstate,allow_include=allow_include)
        return outlines
 
 def outputsites(w):