Translate human time ranges to a machine-readable way

trange.skel 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/python
  2. """Date range
  3. usage: trange [options] WHICH WHAT [WHICH WHAT]
  4. Options:
  5. -e, --enum Enumerate all UNITs in range, not just first and last.
  6. -f, --from Display only FROM.
  7. -F, --format=FMT Use one of formats: `words`, `lines`, `json` or
  8. a UNIX date format, if FMT starts with "+" [default: words]
  9. -d, --debug Turn on debugging output.
  10. -D, --date=DATE Use DATE instead of reading system clock. DATE must
  11. be in ISO 8601 format
  12. -S, --strict Enable strict mode, e.g. do not change "this" to "next"
  13. -t, --to Display only TO.
  14. -u, --unit UNIT Unit to use. One of: `second`, `day`, `hour`,
  15. `minute`, `month`, `year` or `auto`. [default: auto]
  16. -v, --verbose Be verbose, e.g. warn about this vs. next.
  17. --version Show version
  18. -h, --help Show this screen
  19. """
  20. import docopt
  21. import imp
  22. import sys
  23. trange = imp.load_source('trange', '__PYLIB__/trange.py')
  24. class UsageError(ValueError):
  25. pass
  26. class Options(object):
  27. def __init__(self, arguments):
  28. self.enum = arguments['--enum']
  29. self.only_from = arguments['--from']
  30. self.only_to = arguments['--to']
  31. if arguments['--format'] not in ['words', 'lines', 'json']:
  32. raise UsageError("invalid format specified: %s"
  33. % arguments['--format'])
  34. self.fmt = arguments['--format']
  35. self.date = arguments['--date']
  36. self.unit = arguments['--unit']
  37. self.debug = arguments['--debug']
  38. class App:
  39. def __init__(self):
  40. self.arguments = docopt.docopt(__doc__, version="trange 0.1")
  41. self.options = Options(self.arguments)
  42. if self.options.debug:
  43. sys.stderr.write('DEBUG: options: %s\n' % self.options.__dict__)
  44. def run(self):
  45. offsets = self.arguments['WHICH']
  46. unitnames = self.arguments['WHAT']
  47. ranges = []
  48. while unitnames:
  49. ranges.append(trange.Range(offset=offsets.pop(0),
  50. unitname=unitnames.pop(0)))
  51. for r in ranges:
  52. print("%s %s" % (r.start(), r.end()))
  53. if __name__ == '__main__':
  54. app = App()
  55. app.run()