Created
June 26, 2018 07:26
-
-
Save chbndrhnns/807be55ebaec64b2c76e069c02b7344a to your computer and use it in GitHub Desktop.
YNAB: Clone Budget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"categories": { | |
"Pflicht": { | |
"Miete": 500.00, | |
"Krankenversicherung": 0.00, | |
"Lebensmittel": 0.00 | |
}, | |
"Projekte": { | |
"Notfall": 2000 | |
}, | |
"Realität": { | |
"Tanken": 0.00, | |
"Drogerie/Kosmetik": 0.00, | |
"Essen gehen": 0.00, | |
"Ausgehen": 0.00, | |
"Sport": 0.00, | |
"Gesundheit": 0.00, | |
"Kleidung": 0.00, | |
"Wartung Auto": 0.00, | |
"Küche/Bad": 0.00, | |
"Haushalt": 0.00 | |
}, | |
"Luxus": { | |
"2018 Urlaub": 0.00, | |
"2020 Auto [5000 €]": 0.00, | |
"Spielgeld": 0.00 | |
} | |
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
import io | |
from ynabclient.YnabClient import YnabClient | |
from pynYNAB.schema.budget import Transaction, Payee, MasterCategory, SubCategory | |
import settings | |
logger = logging.getLogger(__name__) | |
def remove_master_categories(y, categories, expected_delta=0): | |
for c in categories: | |
if categories[c].internal_name is None: | |
logging.info('Removing {}'.format(c)) | |
y.client.budget.be_master_categories.remove(categories[c]) | |
expected_delta += 1 | |
y.client.push(expected_delta) | |
def remove_sub_categories(y, categories, expected_delta=0): | |
for c in categories: | |
if categories[c].internal_name is None: | |
logging.info('Removing {}'.format(c)) | |
y.client.budget.be_subcategories.remove(categories[c]) | |
expected_delta += 1 | |
y.client.push(expected_delta) | |
def add_categories(y, categories, existing_master_categories=None, existing_sub_categories=None, expected_delta=0): | |
if existing_sub_categories is None: | |
existing_sub_categories = [] | |
if existing_master_categories is None: | |
existing_master_categories = [] | |
for master_cat in categories: | |
# master | |
if master_cat in existing_master_categories: | |
if existing_master_categories[master_cat].is_tombstone is True: | |
logger.info('{} exists as tombstone. Reactivating...'.format(master_cat)) | |
master_idx = y.client.budget.be_master_categories.index(existing_master_categories[master_cat]) | |
y.client.budget.be_master_categories[master_idx].is_tombstone = False | |
expected_delta += 1 | |
else: | |
logger.info('{} exists. Ignoring...'.format(master_cat)) | |
m = [c for c in y.client.budget.be_master_categories if c.name == master_cat and c.is_tombstone == False][0] | |
else: | |
logger.info('adding {}'.format(master_cat)) | |
m = MasterCategory(name=master_cat, parent_id=y.client.budget.id, parent=y.client.budget, | |
sortable_index=-10) | |
y.client.budget.be_master_categories.append(m) | |
expected_delta += 1 | |
# sub-categories | |
for sub_cat in categories[master_cat]: | |
if sub_cat in existing_sub_categories: | |
if existing_sub_categories[sub_cat].is_tombstone is True: | |
logger.info('{}/{} exists as tombstone. Reactivating...'.format(master_cat, sub_cat)) | |
sub_idx = y.client.budget.be_subcategories.index(existing_sub_categories[sub_cat]) | |
y.client.budget.be_subcategories[sub_idx].is_tombstone = False | |
expected_delta += 1 | |
else: | |
logger.info('{}/{} exists. Ignoring...'.format(master_cat, sub_cat)) | |
else: | |
logger.info('adding {}/{}'.format(master_cat, sub_cat)) | |
y.client.budget.be_subcategories.append( | |
SubCategory(name=sub_cat, parent_id=y.client.budget.id, parent=y.client.budget, target_balance=0, | |
sortable_index=-10, | |
monthly_funding=0, entities_master_category=m, entities_master_category_id=m.id)) | |
expected_delta += 1 | |
y.client.push(expected_delta) | |
def add_accounts(): | |
pass | |
def main(): | |
with io.open('../categories.json', 'r', encoding='utf8') as f: | |
categories = json.loads(f.read())['categories'] | |
logger.info('{}'.format(categories)) | |
y = YnabClient() | |
y.sync() | |
existing_master_categories = {p.name: p for p in y.client.budget.be_master_categories} | |
existing_sub_categories = {p.name: p for p in y.client.budget.be_subcategories} | |
remove_sub_categories(y, existing_sub_categories) | |
remove_master_categories(y, existing_master_categories) | |
existing_master_categories = {p.name: p for p in y.client.budget.be_master_categories} | |
existing_sub_categories = {p.name: p for p in y.client.budget.be_subcategories} | |
add_categories(y, categories, existing_master_categories, existing_sub_categories, expected_delta=0) | |
add_accounts(y) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment