""" Command-line client """ import re import sys import optparse import report import watcher import filters import api import ConfigParser import glob all_watchers = [] for name in dir(watcher): value = getattr(watcher, name) if not hasattr(value, 'is_abstract'): continue if not value.is_abstract(): all_watchers.append(value()) def split_camel(name, separator='_'): def subber(match): return separator + match.group(1).lower() new_name = re.sub(r'([A-Z]+)', subber, name) if new_name.startswith(separator): new_name = new_name[len(separator):] return new_name def print_error(*msgs): sys.stderr.write(' '.join(map(str, msgs))) sys.stderr.write('\n') sys.stderr.flush() # parser = optparse.OptionParser() # parser.add_option('--local-domain', # dest='local_domains', # default=[], # action='append', # metavar='DOMAIN', # help='Local domains to be filtered') # parser.config_names = [] # for w in all_watchers: # parser.add_option('--%s' % split_camel(w.name, '-'), # dest=w.name, # action='store_true', # help="Show %s report" % w.title) # for conf in w.configurable: # parser.config_names.append(conf.name) # parser.add_option(conf.make_option()) def run(): conf = api.Config() options, args = parser.parse_args() conf.local_domains = options.local_domains use_watchers = [] for w in all_watchers: if getattr(options, w.name): use_watchers.append(w) for name in parser.config_names: setattr(conf, name, getattr(options, name)) watchers, pipeline = report.setup_stack(use_watchers) for w in watchers: w.set_config(conf) for p in pipeline: p.set_config(conf) reporter = report.Reporter() page = reporter.render_file(args[0], watchers, pipeline) print page def make_dict(conf, section): d = {} try: names = conf.options(section) except ConfigParser.NoSectionError: return {} for name in conf.options(section): if d.has_key(name): if isinstance(d[name], list): d[name].append(conf.get(section, name)) else: d[name] = [d[name], conf.get(section, name)] else: d[name] = conf.get(section, name) return d def run2(args): conf_fn = args[0] conf = ConfigParser.RawConfigParser() conf.read([conf_fn]) glob_conf = make_dict(conf, 'global') watchers = {} for section in conf.sections(): watchers[section] = make_dict(conf, section) use_watchers = [] for name, w_conf in watchers.items(): for w in all_watchers: if name == w.name: use_watchers.append(w) c = glob_conf.copy() c.update(w_conf) w.set_config(w_conf) break watchers, pipeline = report.setup_stack(use_watchers) reporter = report.Reporter() files = args[1:] if conf.has_option('files', 'read'): files.extend(glob.glob(conf.get('files', 'read'))) for file in files: print_error("Reading file %s" % file) if conf.has_option('files', 'write'): output = conf.get('files', 'write') else: output = '-' print_error("Writing to %s" % output) page = reporter.render_file(files, watchers, pipeline) if output == '-': print page else: f = open(output, 'w') f.write(page) f.close()