def test_prefill_entries_add_to_bottom(self): config = self.default_config.copy() tmp_entries_dir = tempfile.mkdtemp() os.remove(self.entries_file) self.entries_file = os.path.join(tmp_entries_dir, '%m_%Y.txt') config['default']['file'] = self.entries_file with freeze_time('2014-01-21'): self.write_entries("""20/01/2014 alias_1 2 hello world 21/01/2014 alias_1 1 foo bar """) with freeze_time('2014-02-21'): self.write_entries("""20/02/2014 alias_1 2 hello world """) self.run_command('edit', config_options=config) with open(expand_filename(self.entries_file), 'r') as f: lines = f.readlines() self.assertEqual('20/02/2014\n', lines[0]) self.assertEqual('21/02/2014\n', lines[3])
def get_files(self, filename, nb_previous_files): date_units = ['m', 'Y'] smallest_unit = None for date in date_units: if '%%%s' % date in filename: smallest_unit = date break if smallest_unit is None: return OrderedSet([filename]) files = OrderedSet() file_date = datetime.date.today() for i in xrange(0, nb_previous_files + 1): files.add(file.expand_filename(filename, file_date)) if smallest_unit == 'm': if file_date.month == 1: file_date = file_date.replace(day=1, month=12, year=file_date.year - 1) else: file_date = file_date.replace(day=1, month=file_date.month - 1) elif smallest_unit == 'Y': file_date = file_date.replace(day=1, year=file_date.year - 1) return files
def tearDown(self): ZebraRemote.send_entries = self.original_zebra_remote_send_entries entries_file = expand_filename(self.entries_file) os.remove(self.config_file) if os.path.exists(entries_file): os.remove(entries_file) os.remove(self.projects_db)
def run_command(self, command, options={}, args=[]): actions = { 'add': commands.AddCommand, 'alias': commands.AliasCommand, 'autofill': commands.AutofillCommand, 'clean-aliases': commands.CleanAliasesCommand, 'ci': commands.CommitCommand, 'commit': commands.CommitCommand, 'edit': commands.EditCommand, 'help': commands.HelpCommand, 'kitty': commands.KittyCommand, 'ohai': commands.KittyCommand, 'search': commands.SearchCommand, 'show': commands.ShowCommand, 'start': commands.StartCommand, 'stat': commands.StatusCommand, 'status': commands.StatusCommand, 'stop': commands.StopCommand, 'up': commands.UpdateCommand, 'update': commands.UpdateCommand, } options = options.copy() args = list(args) options['config'] = os.path.expanduser(options['config']) self.create_config_file(options['config']) settings = Settings(options['config']) if not os.path.exists(settings.TAXI_PATH): os.mkdir(settings.TAXI_PATH) if options.get('file', None) is None: options['forced_file'] = False try: options['file'] = settings.get('file') except ConfigParser.NoOptionError: raise Exception("Error: no file to parse. You must either " "define one in your config file with the " "'file' setting, or use the -f option") else: options['forced_file'] = True options['unparsed_file'] = os.path.expanduser(options['file']) options['file'] = expand_filename(options['file']) if options.get('date', None) is not None: date_format = '%d.%m.%Y' try: if '-' in options['date']: split_date = options['date'].split('-', 1) options['date'] = ( datetime.datetime.strptime(split_date[0], date_format).date(), datetime.datetime.strptime(split_date[1], date_format).date()) else: options['date'] = datetime.datetime.strptime( options['date'], date_format ).date() except ValueError: raise UsageError("Invalid date format (must be dd.mm.yyyy)") else: options['date'] = None if 'projects_db' not in options: options['projects_db'] = os.path.join( settings.TAXI_PATH, 'projects.db' ) projects_db = ProjectsDb(options['projects_db']) view = TtyUi( options.get('stdout', sys.stdout), settings.get('use_colors').lower() in ['1', 'yes', 'true'] ) ac = AppContainer() ac.settings = settings ac.options = options ac.projects_db = projects_db ac.arguments = args ac.view = view action = None try: if command not in actions: raise UsageError("Unknown command `%s`" % command) if command == 'help': ac.commands_mapping = actions action = actions[command](ac) action.validate() action.setup() except UsageError as ue: if (action is not None and not isinstance(action, commands.HelpCommand)): view.msg(inspect.getdoc(action)) else: raise ue else: try: action.run() except TaxiException as e: view.err(e)
def read_entries(self): with open(expand_filename(self.entries_file), 'r') as f: contents = f.read() return contents
def write_entries(self, contents): with open(expand_filename(self.entries_file), 'a') as f: f.write(contents)