-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsubscription_functions.py
43 lines (30 loc) · 1.3 KB
/
subscription_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import json
import config as cfg
import gui_functions as gui
def add_subscription(subscription):
if subscription:
cfg.subscriptions = read_subscriptions()
cfg.subscriptions.append(subscription)
with open(cfg.subs_file_path, "w") as file:
json.dump(cfg.subscriptions, file, indent=2)
gui.refresh_gui()
def read_subscriptions():
if not os.path.exists(cfg.subs_file_path):
return []
with open(cfg.subs_file_path, "r") as file:
cfg.subscriptions = json.load(file)
# Sort subscriptions by days_per_billing_cycle
cfg.subscriptions.sort(key=lambda x: x['days_per_billing_cycle'])
return cfg.subscriptions
def find_matching_subscription_index(subscriptions, custom_label, amount, days_per_billing_cycle):
for index, subscription in enumerate(subscriptions):
if (subscription['custom_label'] == custom_label and
subscription['amount'] == amount and
subscription['days_per_billing_cycle'] == days_per_billing_cycle):
return index
return None
def remove_subscription(subscriptions_list):
cfg.subscriptions = subscriptions_list
with open(cfg.subs_file_path, "w") as file:
json.dump(cfg.subscriptions, file, indent=2)