3 # Author: Martin Matusiak <numerodix@gmail.com>
4 # Licensed under the GNU Public License, version 3.
11 from conf
import config
21 # output: => Requesting ip...
25 "Attempted to display new pending message without closing previous"
27 msg
= "=> %s..." % msg
34 # output: (=> Requesting ip...) done
35 def result(self
, msg
):
38 "Attempted to display result of action without pending message"
42 self
.log("%s %s" % (self
.pending
, msg
))
44 msg
= "%s %s" % (self
.pending
, msg
)
45 print "\r%s" % msg
# workaround for space at the start of the line
50 def display(self
, msg
, log
=True):
51 if self
.pending
and not self
.dirty
:
58 def err(self
, msg
, log
=True):
59 msg
= "Erratum: %s" % msg
62 print >>sys
.stderr
, "\n%s" % msg
63 else: print >>sys
.stderr
, msg
71 if os
.path
.isfile(config
.logfile
) and \
72 os
.path
.getsize(config
.logfile
) > config
.logfile_size
:
73 shutil
.move(config
.logfile
, "%s.0" % config
.logfile
)
75 t
= time
.strftime("%d.%m.%y %H:%M:%S", time
.gmtime())
77 f
= open(config
.logfile
, 'a')
78 if msg
[-1:] != '\n': msg
+= '\n'
79 f
.write("[%s] %s" % (t
, msg
))
82 self
.err("Could not write to logfile %s" % config
.logfile
, log
=False)
84 def mute(self
, quiet
=False):
87 self
.display("*** Halting output until we need to obtain ip again")
88 sys
.stdin
= open("/dev/null", 'r')
89 sys
.stdout
= open("/dev/null", 'w')
90 sys
.stderr
= open("/dev/null", 'w')
93 def unmute(self
, quiet
=False):
94 if not self
.muted
: return
95 sys
.stdin
= sys
.__stdin
__
96 sys
.stdout
= sys
.__stdout
__
97 sys
.stderr
= sys
.__stderr
__
98 if not quiet
: self
.display("*** Resuming output")
106 def format_timespan(span
):
108 units
= ['s', 'm', 'h', 'd', 'w']
133 span
= int(round(span
))
134 return "%s%s%s" % (prefix
, span
, units
[unit_id
])