#!/usr/bin/python """Date range usage: trange [options] WHICH WHAT [WHICH WHAT] Options: -e, --enum Enumerate all UNITs in range, not just first and last. -f, --from Display only FROM. -F, --format=FMT Use one of formats: `words`, `lines`, `json` or a UNIX date format, if FMT starts with "+" [default: words] -d, --debug Turn on debugging output. -D, --date=DATE Use DATE instead of reading system clock. DATE must be in ISO 8601 format -S, --strict Enable strict mode, e.g. do not change "this" to "next" -t, --to Display only TO. -u, --unit UNIT Unit to use. One of: `second`, `day`, `hour`, `minute`, `month`, `year` or `auto`. [default: auto] -v, --verbose Be verbose, e.g. warn about this vs. next. --version Show version -h, --help Show this screen """ import docopt import imp import sys trange = imp.load_source('trange', '__PYLIB__/trange.py') class UsageError(ValueError): pass class Options(object): def __init__(self, arguments): self.enum = arguments['--enum'] self.only_from = arguments['--from'] self.only_to = arguments['--to'] if arguments['--format'] not in ['words', 'lines', 'json']: raise UsageError("invalid format specified: %s" % arguments['--format']) self.fmt = arguments['--format'] self.date = arguments['--date'] self.unit = arguments['--unit'] self.debug = arguments['--debug'] class App: def __init__(self): self.arguments = docopt.docopt(__doc__, version="trange 0.1") self.options = Options(self.arguments) if self.options.debug: sys.stderr.write('DEBUG: options: %s\n' % self.options.__dict__) def run(self): offsets = self.arguments['WHICH'] unitnames = self.arguments['WHAT'] ranges = [] while unitnames: ranges.append(trange.Range(offset=offsets.pop(0), unitname=unitnames.pop(0))) for r in ranges: print("%s %s" % (r.start(), r.end())) if __name__ == '__main__': app = App() app.run()