change disk format and add conversion utility

Signed-off-by: Nico Schottelius <nico@brief.schottelius.org>
This commit is contained in:
Nico Schottelius 2012-08-28 14:03:58 +02:00
parent be36db753d
commit 58a8924811
4 changed files with 48 additions and 11 deletions

View file

@ -27,9 +27,13 @@ VERSION = "0.1"
FILE_DELTA = "delta"
DATEFORMAT = "%Y-%m-%d"
DATEFORMAT_PLAIN= DATEFORMAT.replace("%","")
DATETIMEFORMAT = "%Y-%m-%d-%H:%M"
DATETIMEFORMAT = "%Y-%m-%d-%H%M"
DATETIMEFORMAT_PLAIN= DATETIMEFORMAT.replace("%","")
# Name of the folder to create - should not contain special characters
# to ensure cross-os compatibility
DISKFORMAT = DATETIMEFORMAT
class Error(Exception):
pass

View file

@ -74,15 +74,15 @@ class Report(object):
self._report_db = {}
for dirname in os.listdir(self.project_dir):
if dirname >= self.start_seconds and dirname <= self.end_seconds:
dir_datetime = datetime.datetime.strptime(dirname, ctt.DISKFORMAT)
if dir_datetime >= self.start_date and dir_datetime <= self.end_date:
filename = os.path.join(self.project_dir, dirname, ctt.FILE_DELTA)
with open(filename, "r") as fd:
self._report_db[dirname] = fd.read().rstrip('\n')
log.debug("%s: %s" % (dirname, self._report_db[dirname]))
log.debug("Recording: %s: %s" % (dirname, self._report_db[dirname]))
else:
log.debug("%s/%s" % (float(dirname) - float(self.start_seconds),
float(self.end_seconds) - float(dirname)))
log.debug("Skipping: %s" % dirname)
def report(self):
"""Show report to the user"""

View file

@ -93,11 +93,8 @@ class Tracker:
raise ctt.Error("End date must be after start date! (%s > %s)!" %
(self.start_datetime, self.end_datetime))
start_seconds = self.start_datetime.strftime("%s")
end_seconds = self.end_datetime.strftime("%s")
delta_seconds = self.delta()
time_dir = os.path.join(self.project_dir, start_seconds)
subdirname = self.start_datetime.strftime(ctt.DISKFORMAT)
time_dir = os.path.join(self.project_dir, subdirname)
if os.path.exists(time_dir):
raise ctt.Error("Already tracked time at this beginning for this project")
@ -106,7 +103,7 @@ class Tracker:
filename = os.path.join(time_dir, ctt.FILE_DELTA)
with open(filename, "w") as fd:
fd.write("%s\n" % delta_seconds)
fd.write("%s\n" % self.delta())
def delta(self, in_seconds=True):
"""Return time delta - empty (==0) if not tracked"""

36
tools/convert-0.1-to-0.2-format Executable file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env python3
import datetime
import os
import os.path
DATETIMEFORMAT = "%Y-%m-%d-%H%M"
DISKFORMAT = DATETIMEFORMAT
basedir = os.environ['HOME']
ctt_dir = os.path.join(basedir, ".ctt")
for project in os.listdir(ctt_dir):
time_dir = os.path.join(ctt_dir, project)
for time in os.listdir(time_dir):
print("Converting dir %s/%s" % (project, time))
try:
import_time = datetime.datetime.fromtimestamp(int(time))
except ValueError as e:
print("No old style directory %s from project %s - skipping" % (time, project))
continue
new_dirname = import_time.strftime(DISKFORMAT)
print("%s -> %s" % (time, new_dirname))
src = os.path.join(ctt_dir, project, time)
dst = os.path.join(ctt_dir, project, new_dirname)
print("%s -> %s" % (src, dst))
if os.path.exists(dst):
print("Cannot overwrite dst: %s, skipping %s" % (dst, src))
continue
os.rename(src, dst)