Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions beancount_reds_plugins/effective_date/effective_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
# to enable the older transaction-level hacky plugin, now renamed to effective_date_transaction
# __plugins__ = ['effective_date', 'effective_date_transaction']

LINK_FORMAT = 'edate-{date}-{random}'
DEFAULT_FORMATS = {
'LINK_FORMAT': 'edate-{date}-{rand}',
'DATE_FORMAT': '%y%m%d',
'RANDOM_LEN': 3,
}
HEX_BASE = 16


def has_valid_effective_date(posting):
Expand Down Expand Up @@ -44,18 +49,52 @@ def cleaned(p):
return effective_date_entry


def rand_counter(n=10, repr=int):
values = [x for x in range(n)]
random.shuffle(values)
i = 0

while i < n:
value = values[i]
i += 1
yield repr(value)


def build_config(config):
# Config may contain more than just accounts, but the non-account
# configurations are not valid account names.
holding_accts = {}
formats = {}
if config:
holding_accts = literal_eval(config)
formats = {k: holding_accts.pop(k, None) or v
for k, v in DEFAULT_FORMATS.items()}

if not holding_accts:
if DEBUG:
print("effective_date: Using default config", file=sys.stderr)
holding_accts = {
'Expenses': {'earlier': 'Liabilities:Hold:Expenses', 'later': 'Assets:Hold:Expenses'},
'Income': {'earlier': 'Assets:Hold:Income', 'later': 'Liabilities:Hold:Income'},
}
return holding_accts
formats = DEFAULT_FORMATS | formats

def hexformat(i):
return hex(i)[2:].zfill(formats['RANDOM_LEN'])

return holding_accts, formats, hexformat


def make_link(entry_date, formats, hexformat, rand_gens):
date_str = entry_date.strftime(formats['DATE_FORMAT'])
# Fail collision test: rand_str = ''.join(random.choice(string.ascii_uppercase) for i in range(3))
if date_str not in rand_gens:
rand_gens[date_str] = rand_counter(
HEX_BASE**formats['RANDOM_LEN'], hexformat)
rand_str = next(rand_gens[date_str])

link = formats['LINK_FORMAT'].format(date=date_str, rand=rand_str)
return link


def effective_date(entries, options_map, config):
Expand All @@ -73,8 +112,8 @@ def effective_date(entries, options_map, config):
"""
start_time = time.time()
errors = []
holding_accts = build_config(config)

holding_accts, formats, hexformat = build_config(config)
rand_gens = {}
interesting_entries = []
filtered_entries = []
new_accounts = set()
Expand All @@ -94,9 +133,7 @@ def effective_date(entries, options_map, config):
# entries, and thus links each set of effective date entries
interesting_entries_linked = []
for entry in interesting_entries:
rand_string = ''.join(random.choice(string.ascii_lowercase) for i in range(3))
date = str(entry.date).replace('-', '')[2:]
link = LINK_FORMAT.format(date=str(date), random=rand_string)
link = make_link(entry.date, formats, hexformat, rand_gens)
new_entry = entry._replace(links=(entry.links or set()) | set([link]))
interesting_entries_linked.append(new_entry)

Expand Down Expand Up @@ -261,7 +298,7 @@ def effective_date_transaction(entries, options_map, config):

if found:
rand_string = ''.join(random.choice(string.ascii_uppercase) for i in range(5))
link = LINK_FORMAT.format(rand_string)
link = DEFAULT_FORMATS['LINK_FORMAT'].format(rand_string)
# modified_entry = data.entry_replace(entry, postings=modified_entry_postings,
# links=(entry.links or set()) | set([link]))
modified_entry = entry._replace(postings=modified_entry_postings,
Expand Down
Loading
Loading