Skip to content

Instantly share code, notes, and snippets.

@pawelz
Created March 1, 2010 16:18
Show Gist options
  • Save pawelz/318498 to your computer and use it in GitHub Desktop.
Save pawelz/318498 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# vim:fileencoding=utf-8
"""
Copyrights (c) 2001, Paweł Zuzelski <pawelz@pld-linux.org>
Usage:
Move mode:
./timemove.py [-]HHMMSS file1 [file2 ...]
Verify mode:
./timemove.py -v file1 [file2 ...]
each line of each input file must begin with "hhmmssxxxxhhmmss" where hh, mm,
ss are respectively hours, minutes and soconds, xxxx are arbitrary digits.
In move mode script adds [-]HHMMSS to both hhmmss values and prints modified
line on stdout.
In verify mode scripts verifytime specification (first line of input) and
prints evil lines.
Return values:
0 - script executed with no error
1 - warning: some files have not been processed
2 - error: something went extremally bad
"""
# This program is free software. It comes without any warranty, to the extent
# permitted by applicable law. You can redistribute it and/or modify it under
# the terms of the Do What The Fuck You Want To Public License, Version 2, as
# published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more
# details.
import re
import sys
class WrongLine(Exception):
pass
rline=re.compile("(?P<prefix>\s*)"
"(?P<bh>[0-9]{2})"
"(?P<bm>[0-9]{2})"
"(?P<bs>[0-9]{2})"
"(?P<crap>[0-9]{5})"
"(?P<eh>[0-9]{2})"
"(?P<em>[0-9]{2})"
"(?P<es>[0-9]{2})"
"(?P<rest>[^0-9].*)$"
)
rtime=re.compile("\s*(?P<neg>-?)(?P<h>[0-9]{2})(?P<m>[0-9]{2})(?P<s>[0-9]{2})$")
class TimeSpec:
"""
This class represents daytime as Z_24 x Z_60 x Z_60 group and implements
basic arithmetic operations on such strings.
"""
h = 0
m = 0
s = 0
def __init__(self, H, M, S):
"""
Initializes TimeSpec from three arguments (hours, minutes, seconds)
convertable to integer.
"""
self.h, self.m, self.s = (int(H), int(M), int(S))
self.mod()
def equals(self, x):
"""TimeSpec equality is equality of (h, m, s)."""
if (self.h, self.m, self.s) == (x.h, x.m, x.s):
return True
return False
def negate(self):
"""returns negation"""
return TimeSpec(self.h * -1, self.m * -1, self.s * -1)
def add(self, a):
"""addition modulo Z_24 x Z_60 x Z_60 operator"""
self.h += a.h
self.m += a.m
self.s += a.s
self.mod()
def substract(self, a):
"""substraction modulo Z_24 x Z_60 x Z_60 operator"""
self.add(a.negate())
def sadd(self, neg, a):
"""signed addition modulo Z_24 x Z_60 x Z_60 operator"""
if neg:
self.substract(a)
else:
self.add(a)
def mod(self):
"""Z^3 |-> Z_24 x Z_60 x Z_60 mapping"""
self.m += self.s/60
self.h += self.m/60
self.h %= 24
self.m %= 60
self.s %= 60
def str(self):
"""TimeSpec to str conversion"""
return "%.2d%.2d%.2d" % (self.h, self.m, self.s)
def fail(str = ""):
"""Function executed on handled errors and exceptions."""
print >> sys.stderr, "\ntimemove ERROR: %s\n\ntry %s --help to get help.\n" % (str, sys.argv[0])
sys.exit(2)
try:
if sys.argv[1]=="-h" or sys.argv[1]=="--help":
print __doc__
sys.exit(0)
except IndexError:
print __doc__
sys.exit(0)
if len(sys.argv) < 3:
fail("Syntax error, need more arguments.")
if sys.argv[1]=="-v" or sys.argv[1]=="--verify":
mode="verify"
else:
av=rtime.match(sys.argv[1])
if not av:
fail("Syntax error, wrong format of the first argument, expected [-]HHMMSS.")
mode="move"
if av.group("neg") == "-":
neg = True
else:
neg = False
div = TimeSpec(av.group("h"), av.group("m"), av.group("s"))
for f in sys.argv[2:]:
try:
with open(f, "r") as input:
lineNumber = 0
for line in input:
lineNumber += 1
m = rline.match(line)
if not m:
raise WrongLine(line)
t1 = TimeSpec(m.group("bh"), m.group("bm"), m.group("bs"))
t2 = TimeSpec(m.group("eh"), m.group("em"), m.group("es"))
if mode == "move":
t1.sadd(neg, div)
t2.sadd(neg, div)
print "%s%s%s%s%s" % (m.group("prefix"), t1.str(), m.group("crap"), t2.str(), m.group("rest"))
else:
c = rtime.match(("0%s" % m.group("crap")))
t3 = TimeSpec(c.group("h"), c.group("m"), c.group("s"))
t1.substract(t3)
if not t1.equals(t2):
print "%s:%i mismatch: %s" % (f, lineNumber, line)
except IOError:
print >> sys.stderr, "Can not read %s file! Output may be incomplete."
sys.exit(1)
except WrongLine as e:
print >> sys.stderr, "Encoutered evil line: [%s], aborting. Output may be incomplete" % str(e)[:-1]
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment