def test_partial_file(self): tempdir = tempfile.mkdtemp() with open(os.path.join(tempdir, 'timelog'), 'w') as f: f.write('i 2010/03/16 12:20:19 misc\n') f.write('o 2010/03/16 12:22:19 out\n') now = datetime.datetime(2010, 03, 17) timelog.clock_in( reason='test', log_path=os.path.join(tempdir, 'timelog'), _now=now, ) with open(os.path.join(tempdir, 'timelog'), 'r') as f: lines = f.readlines() wanted = [ 'i 2010/03/16 12:20:19 misc\n', 'o 2010/03/16 12:22:19 out\n', ' '.join( [ 'i', now.strftime("%Y/%m/%d"), now.strftime("%H:%M:%S"), 'test\n', ] ), ] assert lines == wanted
def clock(): parser = OptionParser() parser.add_option("-l", "--log", dest="log_path", help="location of timelog file") parser.add_option("--c", "--config", dest="config_path", help="path to config file") opts, args = parser.parse_args() config_path = opts.config_path if config_path is None: config_path = CONFIG_PATH if os.path.exists(config_path): conf = ConfigParser() conf.read([config_path]) try: log_path = conf.get("dhclock", "log_path") except ConfigParser.NoOptionError: log_path = opts.log_path # None if unset if log_path is None: log_path = LOG_PATH if len(args) != 2: parser.error("use: clock.py in/out reason") if args[0] == "in": timelog.clock_in(args[1], log_path) elif args[0] == "out": timelog.clock_out(args[1], log_path) elif args[0] == "switch": timelog.clock_out("switch", log_path) timelog.clock_in(args[1], log_path) else: parser.error("use: clock.py in/out/switch reason")
def test_empty_file(self): tempdir = tempfile.mkdtemp() f = open(os.path.join(tempdir, 'timelog'), 'w') f.close() now = datetime.datetime(2010, 05, 01) timelog.clock_in(reason='test', log_path=os.path.join(tempdir, 'timelog'), _now=now) assert 'timelog' in os.listdir(tempdir) with open(os.path.join(tempdir, 'timelog')) as f: lines = f.readlines() assert len(lines) == 1 assert lines[0] == ' '.join( [ 'i', now.strftime("%Y/%m/%d"), now.strftime("%H:%M:%S"), 'test\n', ] )