actualpy 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,198 @@
1
+ Metadata-Version: 2.1
2
+ Name: actualpy
3
+ Version: 0.1.0
4
+ Summary: Implementation of the Actual API to interact with Actual over Python.
5
+ Home-page: https://github.com/bvanelli/actualpy
6
+ Author: Brunno Vanelli
7
+ Author-email: brunnovanelli@gmail.com
8
+ Project-URL: Issues, https://github.com/bvanelli/actualpy/issues
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: cryptography
11
+ Requires-Dist: proto-plus
12
+ Requires-Dist: python-dateutil
13
+ Requires-Dist: requests
14
+ Requires-Dist: sqlmodel
15
+
16
+ [![codecov](https://codecov.io/github/bvanelli/actualpy/graph/badge.svg?token=N6V05MY70U)](https://codecov.io/github/bvanelli/actualpy)
17
+
18
+ # actualpy
19
+
20
+ Python API implementation for Actual server.
21
+
22
+ [Actual Budget](https://actualbudget.org/) is a superfast and privacy-focused app for managing your finances.
23
+
24
+ > **WARNING:** The [Javascript API](https://actualbudget.org/docs/api/) to interact with Actual server already exists,
25
+ > and is battle-tested as it is the core of the Actual frontend libraries. If you intend to use a reliable and well
26
+ > tested library, that is the way to go.
27
+
28
+ # Installation
29
+
30
+ Install it via Pip using the repository url:
31
+
32
+ ```bash
33
+ pip install git+https://github.com/bvanelli/actualpy.git
34
+ ```
35
+
36
+ # Basic usage
37
+
38
+ The most common usage would be downloading a budget to more easily build queries. This would you could handle the
39
+ Actual database using SQLAlchemy instead of having to retrieve the data via the export. The following script will print
40
+ every single transaction registered on the Actual budget file:
41
+
42
+ ```python
43
+ from actual import Actual
44
+ from actual.queries import get_transactions
45
+
46
+ with Actual(
47
+ base_url="http://localhost:5006", # Url of the Actual Server
48
+ password="<your_password>", # Password for authentication
49
+ encryption_password=None, # Optional: Password for the file encryption. Will not use it if set to None.
50
+ file="<file_id_or_name>", # Set the file to work with. Can be either the file id or file name, if name is unique
51
+ data_dir="<path_to_data_directory>" # Optional: Directory to store downloaded files. Will use a temporary if not provided
52
+ ) as actual:
53
+ transactions = get_transactions(actual.session)
54
+ for t in transactions:
55
+ account_name = t.account.name if t.account else None
56
+ category = t.category.name if t.category else None
57
+ print(t.date, account_name, t.notes, t.amount, category)
58
+ ```
59
+
60
+ ## Adding new transactions
61
+
62
+ After you created your first budget (or when updating an existing budget), you can add new transactions by adding them
63
+ using the `actual.session.add()` method. You cannot use the SQLAlchemy session directly because that adds the entries to your
64
+ local database, but will not sync the results back to the server (that is only possible when re-uploading the file).
65
+
66
+ The method will make sure the local database is updated, but will also send a SYNC request with the added data so that
67
+ it will be immediately available on the frontend:
68
+
69
+ ```python
70
+ import decimal
71
+ import datetime
72
+ from actual import Actual
73
+ from actual.queries import create_transaction, create_account
74
+
75
+ with Actual(base_url="http://localhost:5006", password="mypass", file="My budget") as actual:
76
+ act = create_account(actual.session, "My account")
77
+ t = create_transaction(
78
+ actual.session,
79
+ datetime.date.today(),
80
+ act,
81
+ "My payee",
82
+ notes="My first transaction",
83
+ amount=decimal.Decimal(-10.5),
84
+ )
85
+ actual.commit() # use the actual.commit() instead of session.commit()!
86
+ ```
87
+
88
+ Will produce:
89
+
90
+ ![added-transaction](./docs/static/added-transaction.png)
91
+
92
+ # Experimental features
93
+
94
+ > **WARNING:** Experimental features do not have all the testing necessary to ensure correctness in comparison to the
95
+ > files generated by the Javascript API. This means that this operations could in theory corrupt the data. Make sure
96
+ > you have backups of your data before trying any of those operations.
97
+
98
+ ## Bootstraping a new server and uploading a first file
99
+
100
+ The following script would generate a new empty budget on the Actual server, even if the server was not bootstrapped
101
+ with an initial password.
102
+
103
+ ```python
104
+ from actual import Actual
105
+
106
+ with Actual(base_url="http://localhost:5006", password="mypass", bootstrap=True) as actual:
107
+ actual.create_budget("My budget")
108
+ actual.upload_budget()
109
+ ```
110
+
111
+ You will then have a freshly created new budget to use:
112
+
113
+ ![created-budget](./docs/static/new-budget.png)
114
+
115
+ If the `encryption_password` is set, the budget will additionally also be encrypted on the upload step to the server.
116
+
117
+ ## Updating transactions using Bank Sync
118
+
119
+ If you have either [goCardless](https://actualbudget.org/docs/advanced/bank-sync/#gocardless-setup) or
120
+ [simplefin](https://actualbudget.org/docs/experimental/simplefin-sync/) integration configured, it is possible to
121
+ update the transactions using just the Python API alone. This is because the actual queries to the third-party service
122
+ are handled on the server, so the client does not have to do any custom API queries.
123
+
124
+ To sync your account, simply call the `run_bank_sync` method:
125
+
126
+ ```python
127
+ from actual import Actual
128
+
129
+ with Actual(base_url="http://localhost:5006", password="mypass") as actual:
130
+ synchronized_transactions = actual.run_bank_sync()
131
+ for transaction in synchronized_transactions:
132
+ print(f"Added of modified {transaction}")
133
+ # sync changes back to the server
134
+ actual.commit()
135
+ ```
136
+
137
+ ## Running rules
138
+
139
+ You can also automatically run rules using the library:
140
+
141
+ ```python
142
+ from actual import Actual
143
+
144
+ with Actual(base_url="http://localhost:5006", password="mypass", file="My budget") as actual:
145
+ actual.run_rules()
146
+ ```
147
+
148
+ You can also manipulate the rules individually:
149
+
150
+ ```python
151
+ from actual import Actual
152
+ from actual.queries import get_ruleset, get_transactions
153
+
154
+ with Actual(base_url="http://localhost:5006", password="mypass", file="My budget") as actual:
155
+ rs = get_ruleset(actual.session)
156
+ transactions = get_transactions(actual.session)
157
+ for rule in rs:
158
+ for t in transactions:
159
+ if rule.evaluate(t):
160
+ print(f"Rule {rule} matches for {t}")
161
+ ```
162
+
163
+ # Understanding how Actual handles changes
164
+
165
+ The Actual budget is stored in a sqlite database hosted on the user's browser. This means all your data is fully local
166
+ and can be encrypted with a local key, so that not even the server can read your statements.
167
+
168
+ The Actual Server is a way of only hosting files and changes. Since re-uploading the full database on every single
169
+ change is too heavy, Actual only stores one state of the database and everything added by the user via frontend
170
+ or via the APIs are individual changes above the base stored database on the server, stored via separate endpoint.
171
+ This means that on every change, done locally, a SYNC request is sent to the server with a list of the following string
172
+ parameters:
173
+
174
+ - `dataset`: the name of the table where the change happened.
175
+ - `row`: the row identifier for the entry that was added/update. This would be the primary key of the row (a uuid value)
176
+ - `column`: the column that had the value changed
177
+ - `value`: the new value. Since it's a string, the values are either prefixed by `S:` to denote a string, `N:` to denote
178
+ a numeric value and `0:` to denote a null value.
179
+
180
+ All individual column changes are computed on an insert, serialized with protobuf and sent to the server to be stored.
181
+ New clients can use this individual changes to then sync their local copies and add the changes executed on other users.
182
+
183
+ But this also means that new users need to download a long list of changes, possibly making the initialization slow.
184
+ Thankfully, user is also allowed to reset the sync. This would make sure all changes are actually stored in the
185
+ database. This is done on the frontend under *Settings > Reset sync*, and causes the current file to be reset (removed
186
+ from the server) and re-uploaded again, with all changes already in place. In this case, the sync list of changes is
187
+ reset because the server already has the latest version of the database.
188
+
189
+ This means that, when using this library to operate changes on the database, you have to make sure that either:
190
+
191
+ - do a sync request is made using the `actual.commit()` method. This only handles pending operations that haven't yet
192
+ been committed, generates a change list with them and posts them on the sync endpoint.
193
+ - do a full re-upload of the database is done.
194
+
195
+ # Contributing
196
+
197
+ The goal is to have more features implemented and tested on the Actual API. If you have ideas, comments, bug fixes or
198
+ requests feel free to open an issue or submit a pull request.
@@ -0,0 +1,183 @@
1
+ [![codecov](https://codecov.io/github/bvanelli/actualpy/graph/badge.svg?token=N6V05MY70U)](https://codecov.io/github/bvanelli/actualpy)
2
+
3
+ # actualpy
4
+
5
+ Python API implementation for Actual server.
6
+
7
+ [Actual Budget](https://actualbudget.org/) is a superfast and privacy-focused app for managing your finances.
8
+
9
+ > **WARNING:** The [Javascript API](https://actualbudget.org/docs/api/) to interact with Actual server already exists,
10
+ > and is battle-tested as it is the core of the Actual frontend libraries. If you intend to use a reliable and well
11
+ > tested library, that is the way to go.
12
+
13
+ # Installation
14
+
15
+ Install it via Pip using the repository url:
16
+
17
+ ```bash
18
+ pip install git+https://github.com/bvanelli/actualpy.git
19
+ ```
20
+
21
+ # Basic usage
22
+
23
+ The most common usage would be downloading a budget to more easily build queries. This would you could handle the
24
+ Actual database using SQLAlchemy instead of having to retrieve the data via the export. The following script will print
25
+ every single transaction registered on the Actual budget file:
26
+
27
+ ```python
28
+ from actual import Actual
29
+ from actual.queries import get_transactions
30
+
31
+ with Actual(
32
+ base_url="http://localhost:5006", # Url of the Actual Server
33
+ password="<your_password>", # Password for authentication
34
+ encryption_password=None, # Optional: Password for the file encryption. Will not use it if set to None.
35
+ file="<file_id_or_name>", # Set the file to work with. Can be either the file id or file name, if name is unique
36
+ data_dir="<path_to_data_directory>" # Optional: Directory to store downloaded files. Will use a temporary if not provided
37
+ ) as actual:
38
+ transactions = get_transactions(actual.session)
39
+ for t in transactions:
40
+ account_name = t.account.name if t.account else None
41
+ category = t.category.name if t.category else None
42
+ print(t.date, account_name, t.notes, t.amount, category)
43
+ ```
44
+
45
+ ## Adding new transactions
46
+
47
+ After you created your first budget (or when updating an existing budget), you can add new transactions by adding them
48
+ using the `actual.session.add()` method. You cannot use the SQLAlchemy session directly because that adds the entries to your
49
+ local database, but will not sync the results back to the server (that is only possible when re-uploading the file).
50
+
51
+ The method will make sure the local database is updated, but will also send a SYNC request with the added data so that
52
+ it will be immediately available on the frontend:
53
+
54
+ ```python
55
+ import decimal
56
+ import datetime
57
+ from actual import Actual
58
+ from actual.queries import create_transaction, create_account
59
+
60
+ with Actual(base_url="http://localhost:5006", password="mypass", file="My budget") as actual:
61
+ act = create_account(actual.session, "My account")
62
+ t = create_transaction(
63
+ actual.session,
64
+ datetime.date.today(),
65
+ act,
66
+ "My payee",
67
+ notes="My first transaction",
68
+ amount=decimal.Decimal(-10.5),
69
+ )
70
+ actual.commit() # use the actual.commit() instead of session.commit()!
71
+ ```
72
+
73
+ Will produce:
74
+
75
+ ![added-transaction](./docs/static/added-transaction.png)
76
+
77
+ # Experimental features
78
+
79
+ > **WARNING:** Experimental features do not have all the testing necessary to ensure correctness in comparison to the
80
+ > files generated by the Javascript API. This means that this operations could in theory corrupt the data. Make sure
81
+ > you have backups of your data before trying any of those operations.
82
+
83
+ ## Bootstraping a new server and uploading a first file
84
+
85
+ The following script would generate a new empty budget on the Actual server, even if the server was not bootstrapped
86
+ with an initial password.
87
+
88
+ ```python
89
+ from actual import Actual
90
+
91
+ with Actual(base_url="http://localhost:5006", password="mypass", bootstrap=True) as actual:
92
+ actual.create_budget("My budget")
93
+ actual.upload_budget()
94
+ ```
95
+
96
+ You will then have a freshly created new budget to use:
97
+
98
+ ![created-budget](./docs/static/new-budget.png)
99
+
100
+ If the `encryption_password` is set, the budget will additionally also be encrypted on the upload step to the server.
101
+
102
+ ## Updating transactions using Bank Sync
103
+
104
+ If you have either [goCardless](https://actualbudget.org/docs/advanced/bank-sync/#gocardless-setup) or
105
+ [simplefin](https://actualbudget.org/docs/experimental/simplefin-sync/) integration configured, it is possible to
106
+ update the transactions using just the Python API alone. This is because the actual queries to the third-party service
107
+ are handled on the server, so the client does not have to do any custom API queries.
108
+
109
+ To sync your account, simply call the `run_bank_sync` method:
110
+
111
+ ```python
112
+ from actual import Actual
113
+
114
+ with Actual(base_url="http://localhost:5006", password="mypass") as actual:
115
+ synchronized_transactions = actual.run_bank_sync()
116
+ for transaction in synchronized_transactions:
117
+ print(f"Added of modified {transaction}")
118
+ # sync changes back to the server
119
+ actual.commit()
120
+ ```
121
+
122
+ ## Running rules
123
+
124
+ You can also automatically run rules using the library:
125
+
126
+ ```python
127
+ from actual import Actual
128
+
129
+ with Actual(base_url="http://localhost:5006", password="mypass", file="My budget") as actual:
130
+ actual.run_rules()
131
+ ```
132
+
133
+ You can also manipulate the rules individually:
134
+
135
+ ```python
136
+ from actual import Actual
137
+ from actual.queries import get_ruleset, get_transactions
138
+
139
+ with Actual(base_url="http://localhost:5006", password="mypass", file="My budget") as actual:
140
+ rs = get_ruleset(actual.session)
141
+ transactions = get_transactions(actual.session)
142
+ for rule in rs:
143
+ for t in transactions:
144
+ if rule.evaluate(t):
145
+ print(f"Rule {rule} matches for {t}")
146
+ ```
147
+
148
+ # Understanding how Actual handles changes
149
+
150
+ The Actual budget is stored in a sqlite database hosted on the user's browser. This means all your data is fully local
151
+ and can be encrypted with a local key, so that not even the server can read your statements.
152
+
153
+ The Actual Server is a way of only hosting files and changes. Since re-uploading the full database on every single
154
+ change is too heavy, Actual only stores one state of the database and everything added by the user via frontend
155
+ or via the APIs are individual changes above the base stored database on the server, stored via separate endpoint.
156
+ This means that on every change, done locally, a SYNC request is sent to the server with a list of the following string
157
+ parameters:
158
+
159
+ - `dataset`: the name of the table where the change happened.
160
+ - `row`: the row identifier for the entry that was added/update. This would be the primary key of the row (a uuid value)
161
+ - `column`: the column that had the value changed
162
+ - `value`: the new value. Since it's a string, the values are either prefixed by `S:` to denote a string, `N:` to denote
163
+ a numeric value and `0:` to denote a null value.
164
+
165
+ All individual column changes are computed on an insert, serialized with protobuf and sent to the server to be stored.
166
+ New clients can use this individual changes to then sync their local copies and add the changes executed on other users.
167
+
168
+ But this also means that new users need to download a long list of changes, possibly making the initialization slow.
169
+ Thankfully, user is also allowed to reset the sync. This would make sure all changes are actually stored in the
170
+ database. This is done on the frontend under *Settings > Reset sync*, and causes the current file to be reset (removed
171
+ from the server) and re-uploaded again, with all changes already in place. In this case, the sync list of changes is
172
+ reset because the server already has the latest version of the database.
173
+
174
+ This means that, when using this library to operate changes on the database, you have to make sure that either:
175
+
176
+ - do a sync request is made using the `actual.commit()` method. This only handles pending operations that haven't yet
177
+ been committed, generates a change list with them and posts them on the sync endpoint.
178
+ - do a full re-upload of the database is done.
179
+
180
+ # Contributing
181
+
182
+ The goal is to have more features implemented and tested on the Actual API. If you have ideas, comments, bug fixes or
183
+ requests feel free to open an issue or submit a pull request.