def parse_ledger_bal(self, text): """Demands '--balance-format=++ %(account)\n%(amount)\n' format. Demands '--date-format=%Y-%m-%d' date format.""" lines = [x.strip() for x in text.splitlines() if x.strip()] account = None for line in lines: if line.startswith("++ "): account = line[3:] else: amount = ledger.Amount(line) date = re.findall(r'\[\d\d\d\d-\d\d-\d\d]', line) assert len(date) < 2 if date: date = common.parse_date(date[0][1:-1]) else: date = None try: lot = Lot(self.nextnum(), date, amount, account) self.lots.append(lot) except TypeError: # At this point, we know the commodity does not have a price. # So we ignore this. pass
def clear(f): changed = False lines = file(f).readlines() for n, line in enumerate(lines): m = date_re.match(line) if not m: continue if m.group(3).strip().startswith("*"): continue lines_to_write = [line] originaln = n while True: n = n + 1 try: nextline = lines[n] except IndexError: break if nextline.startswith(" ") or nextline.startswith("\t"): lines_to_write.append(nextline) else: break initial_unparsed = m.group(2)[1:] if m.group(2) else m.group(1) initial = common.parse_date(initial_unparsed) if initial > datetime.date.today(): continue for line in lines_to_write: sys.stdout.write(line) sys.stdout.flush() choice = common.prompt_for_date_optional( sys.stdin, sys.stdout, "Mark cleared at this date?", initial, ) if choice is not None: choice_formatted = common.format_date(choice, initial_unparsed) if m.group(1) == choice_formatted: lines[originaln] = "%s * %s" % (m.group(1), m.group(3)) else: lines[originaln] = "%s=%s * %s" % ( m.group(1), choice_formatted, m.group(3)) for number in range(originaln + 1, n): # remove cleared bits on legs of the transaction lines[number] = re.sub("^(\\s+)\\*\\s+", "\\1", lines[number]) changed = True else: pass if changed: y = file(f + ".new", "w") y.write("".join(lines)) y.flush() try: os.rename(f + ".new", f) except Exception: os.unlink(f + ".new") raise
def get_date(self): """Get the selected date :returns: the date. :rtype: datetime.date or None """ try: date = self.entry.get_text() date = parse_date(date) except ValueError: date = None return date
def clear(f): changed = False lines = file(f).readlines() for n, line in enumerate(lines): m = date_re.match(line) if not m: continue if m.group(3).strip().startswith("*"): continue lines_to_write = [line] originaln = n while True: n = n + 1 try: nextline = lines[n] except IndexError: break if nextline.startswith(" ") or nextline.startswith("\t"): lines_to_write.append(nextline) else: break initial_unparsed = m.group(2)[1:] if m.group(2) else m.group(1) initial = common.parse_date(initial_unparsed) if initial > datetime.date.today(): continue for line in lines_to_write: sys.stdout.write(line) sys.stdout.flush() choice = common.prompt_for_date_optional( sys.stdin, sys.stdout, "Mark cleared at this date?", initial, ) if choice is not None: choice_formatted = common.format_date(choice, initial_unparsed) if m.group(1) == choice_formatted: lines[originaln] = "%s * %s" % ( m.group(1), m.group(3) ) else: lines[originaln] = "%s=%s * %s" % ( m.group(1), choice_formatted, m.group(3) ) for number in range(originaln + 1, n): # remove cleared bits on legs of the transaction lines[number] = re.sub("^(\\s+)\\*\\s+", "\\1", lines[number]) changed = True else: pass if changed: y = file(f + ".new", "w") y.write("".join(lines)) y.flush() try: os.rename(f + ".new", f) except Exception: os.unlink(f + ".new") raise
def parse_date_from_transaction_contents(contents): return ledgerhelpers.parse_date(u"".join(contents))