signal.signal(signal.SIGINT, signal.SIG_DFL)
import sys
+import os
from zope.interface import implementer
class DBG(twisted.python.constants.Names):
INIT = NamedConstant()
+ CONFIG = NamedConstant()
ROUTE = NamedConstant()
DROP = NamedConstant()
FLOW = NamedConstant()
'''
# these need to be defined here so that they can be imported by import *
-cfg = ConfigParser()
+cfg = ConfigParser(strict=False)
optparser = OptionParser()
_mimetrans = bytes.maketrans(b'-'+slip.esc, slip.esc+b'-')
#---------- startup ----------
def common_startup():
- optparser.add_option('-c', '--config', dest='configfile',
- default='/etc/hippotat/config')
+ re = regexp.compile('#.*')
+ cfg.read_string(re.sub('', defcfg))
+ need_defcfg = True
+
+ def readconfig(pathname, mandatory=True):
+ def log(m, p=pathname):
+ if not DBG.CONFIG in debug_set: return
+ print('DBG.CONFIG: %s: %s' % (m, pathname))
+
+ try:
+ files = os.listdir(pathname)
+
+ except FileNotFoundError:
+ if mandatory: raise
+ log('skipped')
+ return
+
+ except NotADirectoryError:
+ cfg.read(pathname)
+ log('read file')
+ return
+
+ # is a directory
+ log('directory')
+ re = regexp.compile('[^-A-Za-z0-9_]')
+ for f in os.listdir(cdir):
+ if re.search(f): continue
+ subpath = pathname + '/' + f
+ try:
+ os.stat(subpath)
+ except FileNotFoundError:
+ log('entry skipped', subpath)
+ continue
+ cfg.read(subpath)
+ log('entry read', subpath)
+
+ def oc_config(od,os, value, op):
+ nonlocal need_defcfg
+ need_defcfg = False
+ readconfig(value)
def dfs_less_detailed(dl):
return [df for df in DBG.iterconstants() if df <= dl]
debug_set = set(dfs_less_detailed(debug_def_detail))
def ds_select(od,os, spec, op):
- last_df = next(DBG.iterconstants())
- mutator = debug_set.add
-
for it in spec.split(','):
- if not len(it):
- for df in dfs_less_detailed(last_df):
- mutator(df)
- continue
-
if it.startswith('-'):
mutator = debug_set.discard
it = it[1:]
else:
mutator = debug_set.add
-
- try:
- df = DBG.lookupByName(it)
- except ValueError:
- optparser.error('unknown debug flag %s in --debug-select' % it)
- mutator(df)
- last_df = df
+
+ if it == '+':
+ dfs = DBG.iterconstants()
+
+ else:
+ if it.endswith('+'):
+ mapper = dfs_less_detailed
+ it = it[0:len(it)-1]
+ else:
+ mapper = lambda x: [x]
+
+ try:
+ dfspec = DBG.lookupByName(it)
+ except ValueError:
+ optparser.error('unknown debug flag %s in --debug-select' % it)
+
+ dfs = mapper(dfspec)
+
+ for df in dfs:
+ mutator(df)
optparser.add_option('-D', '--debug',
nargs=0,
optparser.add_option('--debug-select',
nargs=1,
type='string',
- metavar='[-]DFLAG[,],...',
+ metavar='[-]DFLAG[+]|[-]+,...',
help=
-'''enable (or with -, disable) each specified DFLAG;
-empty entry means do the same for all DFLAGS "more interesting"
-than the last (or, all DFLAGs)''',
+'''enable (`-': disable) each specified DFLAG;
+`+': do same for all "more interesting" DFLAGSs;
+just `+': all DFLAGs.
+ DFLAGS: ''' + ' '.join([df.name for df in DBG.iterconstants()]),
action='callback',
callback= ds_select)
+ optparser.add_option('-c', '--config',
+ nargs=1,
+ type='string',
+ metavar='CONFIGFILE',
+ dest='configfile',
+ action='callback',
+ callback= oc_config)
+
(opts, args) = optparser.parse_args()
if len(args): optparser.error('no non-option arguments please')
- print(repr(debug_set), file=sys.stderr)
+ if need_defcfg:
+ readconfig('/etc/hippotat/config', False)
+ readconfig('/etc/hippotat/config.d', False)
- re = regexp.compile('#.*')
- cfg.read_string(re.sub('', defcfg))
- cfg.read(opts.configfile)
+ #print(repr(debug_set), file=sys.stderr)
log_formatter = twisted.logger.formatEventAsClassicLogText
stdout_obs = twisted.logger.FileLogObserver(sys.stdout, log_formatter)