Odyssey
base.py
1 """
2 Usable base class for all hosting commands.
3 """
4 
5 import datetime
6 import dateutil.parser
7 import dateutil.tz
8 import humanize
9 import shellish
10 
11 VERSION = "0.8.4" # Increment this value when backwards compat is broken.
12 DEFAULT_STACK_ENV = 'ODYSSEY_DEFAULT_STACK'
13 
14 localtz = dateutil.tz.tzlocal()
15 
16 
17 def parse_ts(ts, **kwargs):
18  dt = dateutil.parser.parse(ts)
19  return localize_dt(dt, **kwargs)
20 
21 
22 def localize_dt(dt, tz=localtz):
23  return dt.astimezone(tz)
24 
25 
26 def localnow(tz=localtz):
27  return datetime.datetime.now(tz=tz)
28 
29 
30 def formatdate(dt, format='%b %d, %Y'):
31  return dt.strftime(format)
32 
33 
34 def formattime(dt, format='%I:%M %p %Z'):
35  return dt.strftime(format)
36 
37 
38 def formatdatetime(dt, timeformat=None, dateformat=None):
39  d = formatdate(dt, format=dateformat) if dateformat else formatdate(dt)
40  t = formattime(dt, format=timeformat) if timeformat else formattime(dt)
41  return '%s, %s' % (d, t)
42 
43 
44 def naturaldelta(td):
45  return humanize.naturaldelta(td)
46 
47 
48 class HostingCommand(shellish.Command):
49 
50  # Subclasses should override with a set of stack states that their command
51  # works with. Ie. {'init', 'ready', 'normal', 'partial_deploy'}
52  allowed_states = set()
53  min_version = "0"
54  max_version = VERSION
55 
56  def add_stack_argument(self, *args, env=DEFAULT_STACK_ENV,
57  help=None, metavar='STACK_NAME', **kwargs):
58  if help is None:
59  help = "Stack name to apply command to."
60  if not args:
61  args = ('stack',)
62  return self.add_argument(*args, env=env, help=help, metavar=metavar,
63  **kwargs)
64 
65  def check_requirements(self, stack):
66  """ Used to assert a stack is compatible and in a viable state. """
67  ver = stack.stack_version
68  if not (self.min_version <= ver <= self.max_version):
69  raise SystemExit("Incompatible version: %s" % ver)
70  if stack.state not in self.allowed_states:
71  raise SystemExit("Improper stack state: %s" % stack.state)
string min_version
Definition: base.py:53
def check_requirements(self, stack)
Definition: base.py:65