├── .gitignore
├── CHANGES
├── COPYING
├── MANIFEST.in
├── README.rst
├── bin
├── lt-chart
├── lt-stmtproc
└── lt-transact
├── doc
└── .ltconfig.sample
├── ltlib
├── __init__.py
├── balance.py
├── chart.py
├── config.py
├── parse.py
├── reader.py
├── readers
│ ├── CSV.py
│ └── __init__.py
├── rule.py
├── score.py
├── test_config.py
├── ui.py
├── util.py
└── xn.py
└── setup.py
/.gitignore:
--------------------------------------------------------------------------------
1 | MANIFEST
2 | *.pyc
3 | /build/
4 | /dist/
5 |
--------------------------------------------------------------------------------
/CHANGES:
--------------------------------------------------------------------------------
1 | v0.3
2 | ====
3 |
4 | New features
5 | ------------
6 |
7 | - ``lt-transact`` is now more configurable. The global config
8 | ``transact-default-account`` specifies the default account.
9 | ``transact-default-src`` and ``transact-default-dst`` specify the
10 | default source and destination accounts and can be set on a
11 | per-account basis.
12 | - ``lt-transact`` now reads rule files and processes transactions
13 | against rulesets.
14 | - ``lt-transact`` now asks if the user would like to enter another
15 | transaction before exiting.
16 | - ``lt-transact`` now prints the account it is working with before
17 | beginning transaction entry.
18 | - Output patterns now offer the "year", "month" and "fy" (financial
19 | year) fields, in addition to "date". These all return strings,
20 | and "month" will have a leading zero for single-digit months.
21 | - The CSV reader will now ignore non-transaction "metadata" in CSV
22 | files, e.g. "Opening Balance" or "Closing Balance", instead of
23 | crashing.
24 |
25 | Bug fixes
26 | ---------
27 |
28 | - Take the absolute value of the CSV "Debit" field in order to work
29 | if these values happen to be negative.
30 | - Better config file handling (do not try to read config file during
31 | import of ``ltlib.config``).
32 |
33 |
34 | v0.2.1
35 | ======
36 |
37 | Bug fixes
38 | ---------
39 |
40 | - Chase the pygtkchart -> gtkchartlib package name change in README.
41 | No code changed.
42 |
43 |
44 | v0.2
45 | ====
46 |
47 | New features
48 | ------------
49 |
50 | - Introducing ``lt-chart``: a program for visualising expenditure as a
51 | hierarchical pie chart.
52 | - New ``readerargs`` config can be used to supply arguments to the
53 | reader constructor.
54 | - CSV reader now accepts a ``fieldnames`` argument (via the
55 | ``readerargs`` facility) which can be used to specify which
56 | fields are present and in what order. A single "amount" field
57 | is now supported as an alternative to "credit" and "debit".
58 |
59 |
60 | v0.1
61 | ====
62 |
63 | Initial release
64 | ---------------
65 |
66 | - ``lt-stmtproc``: process bank statements into a Ledger database, using
67 | rules or prompting user to determine account.
68 | - ``lt-transact``: manually enter transactions into a Ledger database.
69 |
--------------------------------------------------------------------------------
/COPYING:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/MANIFEST.in:
--------------------------------------------------------------------------------
1 | include CHANGES
2 | include COPYING
3 | include README.rst
4 | include MANIFEST.in
5 |
--------------------------------------------------------------------------------
/README.rst:
--------------------------------------------------------------------------------
1 | ledgertools is a collection of utility programs for working with and
2 | visualising data in the Ledger_ accounting system.
3 |
4 | ``lt-stmtproc``
5 | Convert a bank statement into transactions in a Ledger database.
6 |
7 | ``lt-transact``
8 | Command line program for entering transactions.
9 |
10 | ``lt-chart``
11 | Visualise income or expenditure as a multi-level pie chart.
12 | Requires Ledger_, PyGTK_ (2.12 or higher) and gtkchartlib_.
13 |
14 | .. _Ledger: https://github.com/ledger/ledger
15 | .. _PyGTK: http://www.pygtk.org/
16 | .. _gtkchartlib: http://pypi.python.org/pypi/gtkchartlib
17 |
18 |
19 | Installation
20 | ------------
21 |
22 | ::
23 |
24 | pip install ledgertools
25 |
26 |
27 | License
28 | -------
29 |
30 | ledgertools is free software: you can redistribute it and/or modify
31 | it under the terms of the GNU General Public License as published by
32 | the Free Software Foundation, either version 3 of the License, or
33 | (at your option) any later version.
34 |
35 |
36 | Contributing
37 | ------------
38 |
39 | The ledgertools source code is available from
40 | https://github.com/frasertweedale/ledgertools.
41 |
42 | Bug reports, patches, feature requests, code review and
43 | documentation are welcomed.
44 |
45 | To submit a patch, please use ``git send-email`` or generate a pull
46 | request. Write a `well formed commit message`_. If your patch is
47 | nontrivial, update the copyright notice at the top of each changed
48 | file.
49 |
50 | .. _well formed commit message: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
51 |
--------------------------------------------------------------------------------
/bin/lt-chart:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | # lt-chart - chart statistics from a Ledger database
4 | # Copyright (C) 2011 Fraser Tweedale
5 | #
6 | # This program is free software: you can redistribute it and/or modify
7 | # it under the terms of the GNU General Public License as published by
8 | # the Free Software Foundation, either version 3 of the License, or
9 | # (at your option) any later version.
10 | #
11 | # This program is distributed in the hope that it will be useful,
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | # GNU General Public License for more details.
15 | #
16 | # You should have received a copy of the GNU General Public License
17 | # along with this program. If not, see .
18 |
19 | # TODO: date ranges, amount-vs-time charts, proper GUI
20 |
21 | import argparse
22 | import glob
23 | import subprocess
24 |
25 | import gtk
26 | import gtkchartlib.ringchart
27 |
28 | import ltlib.balance
29 | import ltlib.chart
30 | import ltlib.config
31 | import ltlib.util
32 |
33 |
34 | parser = argparse.ArgumentParser(
35 | description="Chart statistics from a Ledger database"
36 | );
37 | parser.add_argument(
38 | '--account',
39 | action='append',
40 | required=True,
41 | help="Load transactions from these accounts' Ledger files."
42 | )
43 | parser.add_argument(
44 | '--filter',
45 | action='append',
46 | help="Filter transactions by account slug."
47 | )
48 | parser.add_argument(
49 | '--show',
50 | default='credit',
51 | choices=['all', 'credit', 'debit'],
52 | help="Show accounts in credit, debit, or all accounts."
53 | )
54 | args = parser.parse_args()
55 |
56 | # create a config object
57 | config = ltlib.config.Config()
58 |
59 | win = gtk.Window()
60 | win.connect('delete-event', gtk.main_quit)
61 | win.set_size_request(384, 384)
62 |
63 | # Ledger files for specified account(s)
64 | files = ltlib.util.flatten(map(
65 | lambda x: glob.glob(x + '/*'),
66 | map(config.outdir, args.account)
67 | ))
68 |
69 | # run ledger
70 | cat = subprocess.Popen(['cat'] + list(files), stdout=subprocess.PIPE)
71 | ledger = subprocess.Popen(
72 | ['ledger', '-f', '-', '-s', 'balance'] + (args.filter or []),
73 | stdin=cat.stdout,
74 | stdout=subprocess.PIPE
75 | )
76 | balance = ltlib.balance.balance(ledger.communicate()[0])
77 |
78 | # create ringchart
79 | show = {
80 | 'all': ltlib.chart.SHOW_ALL,
81 | 'credit': ltlib.chart.SHOW_CREDIT,
82 | 'debit': ltlib.chart.SHOW_DEBIT,
83 | }
84 | rcis = ltlib.chart.balance_to_ringchart_items(
85 | balance,
86 | show=show[args.show]
87 | )
88 | rc = gtkchartlib.ringchart.RingChart(rcis)
89 | event_box = gtk.EventBox()
90 | event_box.add(rc)
91 | win.add(event_box)
92 |
93 | win.show_all()
94 | gtk.main()
95 |
--------------------------------------------------------------------------------
/bin/lt-stmtproc:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | # lt-stmtproc - process transactions into Ledger files
4 | # Copyright (C) 2011 Fraser Tweedale
5 | #
6 | # This program is free software: you can redistribute it and/or modify
7 | # it under the terms of the GNU General Public License as published by
8 | # the Free Software Foundation, either version 3 of the License, or
9 | # (at your option) any later version.
10 | #
11 | # This program is distributed in the hope that it will be useful,
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | # GNU General Public License for more details.
15 | #
16 | # You should have received a copy of the GNU General Public License
17 | # along with this program. If not, see .
18 |
19 | import argparse
20 |
21 | import ltlib.config
22 | import ltlib.parse
23 | import ltlib.readers
24 | import ltlib.ui
25 | import ltlib.util
26 |
27 |
28 | parser = argparse.ArgumentParser(
29 | description="Convert transactions to Ledger format"
30 | );
31 | parser.add_argument(
32 | '--in',
33 | dest='infile',
34 | type=argparse.FileType('r'),
35 | required=True,
36 | help="the transaction input file"
37 | )
38 | parser.add_argument(
39 | '--out',
40 | dest='outfile',
41 | type=argparse.FileType('a'),
42 | help="the Ledger transaction output file (transactions are appended)"
43 | )
44 | parser.add_argument(
45 | '--reader',
46 | help='specify or override the reader to use'
47 | )
48 | parser.add_argument(
49 | '--account',
50 | required=True, # for now
51 | help="the account to which the given transactions pertain"
52 | )
53 | parser.add_argument(
54 | '--rules',
55 | type=argparse.FileType('r'),
56 | action='append',
57 | default=[],
58 | help='specify additional rules files to read'
59 | )
60 | args = parser.parse_args()
61 |
62 | # create user interface object
63 | uio = ltlib.ui.UI()
64 |
65 | # create a config object
66 | config = ltlib.config.Config()
67 |
68 | # make sure we have an outfile or outpat
69 | if not args.outfile:
70 | outpat = config.outpat(args.account)
71 | if not outpat:
72 | uio.bail('No outfile or output pattern provied')
73 |
74 | # read rules files
75 | #
76 | # first get rulefiles from config
77 | rules = ltlib.util.flatten(map(
78 | ltlib.parse.file2rules,
79 | args.rules + map(open, config.rulefiles(args.account))
80 | ))
81 | rules = list(rules)
82 |
83 | # read transactions
84 | readerclass = args.reader or config.get('reader', acc=args.account)
85 | readerargs = config.get('readerargs', acc=args.account, default={})
86 | xns = getattr(ltlib.readers, readerclass).Reader(
87 | file=args.infile,
88 | account=args.account,
89 | **readerargs
90 | )
91 | # TODO catch AttributeError for unknown reader
92 | xns = list(xns)
93 | if readerargs.get('reverse', False):
94 | xns = list(reversed(xns))
95 |
96 | # process transactions
97 | prevxn = None
98 | for xn in xns:
99 | xn.process(rules, uio, prevxn=prevxn)
100 | xn.complete(uio)
101 | if not xn.dropped:
102 | xn.balance()
103 | prevxn = xn
104 |
105 | # print transactions
106 | for xn in filter(lambda x: not x.dropped, xns):
107 | if args.outfile:
108 | print >> args.outfile, xn.ledger()
109 | else:
110 | with open(ltlib.config.format_outpat(outpat, xn), 'a') as f:
111 | print >> f, xn.ledger()
112 |
--------------------------------------------------------------------------------
/bin/lt-transact:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | # lt-transact - enter new transactions into Ledger files
4 | # Copyright (C) 2011 Fraser Tweedale
5 | #
6 | # This program is free software: you can redistribute it and/or modify
7 | # it under the terms of the GNU General Public License as published by
8 | # the Free Software Foundation, either version 3 of the License, or
9 | # (at your option) any later version.
10 | #
11 | # This program is distributed in the hope that it will be useful,
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | # GNU General Public License for more details.
15 | #
16 | # You should have received a copy of the GNU General Public License
17 | # along with this program. If not, see .
18 |
19 | import argparse
20 | import datetime
21 | import sys
22 |
23 | import ltlib.config
24 | import ltlib.parse
25 | import ltlib.ui
26 | import ltlib.util
27 | import ltlib.xn
28 |
29 |
30 | # create a config object
31 | config = ltlib.config.Config()
32 |
33 | # parse args
34 | parser = argparse.ArgumentParser(
35 | description='Manually enter transactions and write them to Ledger.'
36 | )
37 | parser.add_argument(
38 | '--account',
39 | default=config.get('transact-default-account'),
40 | help='The account to which transactions to be entered pertain.'
41 | )
42 | parser.add_argument(
43 | '--out',
44 | dest='outfile',
45 | type=argparse.FileType('a'),
46 | help='The Ledger file to which to appended transactions.'
47 | )
48 | parser.add_argument(
49 | '--rules',
50 | type=argparse.FileType('r'),
51 | action='append',
52 | default=[],
53 | metavar='RULEFILE',
54 | help='An additional rule file to read (may be used multiple times).'
55 | )
56 | args = parser.parse_args()
57 |
58 | # get a user interface
59 | uio = ltlib.ui.UI()
60 | uio.show('Entering transactions for account {}'.format(args.account))
61 |
62 | # we must have an outfile or an outpat
63 | if not args.outfile:
64 | outpat = config.outpat(args.account)
65 | if not outpat:
66 | uio.show('BAIL OUT: No outfile or outpat provided')
67 | sys.exit(1)
68 |
69 | # read rules files
70 | rule_generator = ltlib.util.flatten(map(
71 | ltlib.parse.file2rules,
72 | args.rules + map(open, config.rulefiles(args.account))
73 | ))
74 | rules = list(rule_generator)
75 |
76 |
77 | def enter_transaction():
78 | """Enter a transaction, using rules to determine values when possible."""
79 | # ask the date, description, source account and amount
80 | default_src = config.get('transact-default-src', args.account)
81 | xn_dict = {
82 | 'date': uio.pastdate("Enter date", datetime.date.today()),
83 | 'desc': uio.text("Enter description"),
84 | 'src': [ltlib.xn.Endpoint(
85 | uio.account("Enter source account", default=default_src),
86 | -uio.decimal("Enter transaction amount")
87 | )],
88 | 'dst': []
89 | }
90 | xn_dict['amount'] = -xn_dict['src'][0].amount
91 |
92 | # create a Xn instance
93 | xn = ltlib.xn.Xn(**xn_dict)
94 |
95 | # process the transaction against rules
96 | xn.process(rules, uio)
97 |
98 | # complete the transaction
99 | xn.complete(uio)
100 | xn.balance()
101 |
102 | # write transaction to ledger
103 | uio.show('')
104 | uio.show(xn.summary())
105 | if args.outfile:
106 | print >> args.outfile, xn.ledger()
107 | else:
108 | with open(ltlib.config.format_outpat(outpat, xn), 'a') as f:
109 | print >> f, xn.ledger()
110 | uio.show('Wrote ledger.')
111 |
112 | try:
113 | keep_going = True
114 | while keep_going:
115 | enter_transaction()
116 | keep_going = uio.yn('Enter another transaction?')
117 | except ltlib.ui.RejectWarning:
118 | # bail out
119 | uio.show('')
120 | uio.show('BAIL OUT')
121 | sys.exit(1)
122 |
--------------------------------------------------------------------------------
/doc/.ltconfig.sample:
--------------------------------------------------------------------------------
1 | {
2 | "rootdir": "~/doc/fin",
3 | "outpat": "{date.year}_{date.month:02}.dat",
4 | "rulesdir": "rules",
5 |
6 | "rules": [ "common_rules" ],
7 |
8 | "transact-default-account": "Expenses:Cash",
9 |
10 | "accounts": {
11 | "Assets:Bank:SomeBank:ProductA": {
12 | "reader": "CSV",
13 | "rules": [ "ProductA_rules" ],
14 | "outdir": "ledger/SomeBank_ProductA"
15 | },
16 | "Assets:Bank:SomeBank:ProductB": {
17 | "reader": "CSV",
18 | "rules": [ "ProductB_rules" ],
19 | "outdir": "ledger/SomeBank_ProductB"
20 | },
21 | "Assets:Bank:OtherBank:Product": {
22 | "reader": "CSV",
23 | "readerargs": {
24 | "fieldnames": ["Date", "Amount", "Description", "Balance"]
25 | },
26 | "rules": [ "OtherBank_rules" ],
27 | "outdir": "ledger/OtherBank_Product"
28 | },
29 | "Liabilities:SomeBank:CreditCardType": {
30 | "reader": "CSV",
31 | "rules": [ "SomeBankCredit_rules" ],
32 | "outdir": "ledger/SomeBank_CC"
33 | },
34 | "Expenses:Cash": {
35 | "outdir": "ledger/Cash",
36 | "transact-default-src": "Expenses:Cash"
37 | },
38 | "Special": {
39 | "outdir": "ledger/Special",
40 | "outpat": "special.dat",
41 | "transact-default-dst": "SpecialExpenses"
42 | }
43 | },
44 |
45 | "graph": {
46 | "accounts": [
47 | "Assets:Bank:SomeBank:ProductA"
48 | ]
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/ltlib/__init__.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
--------------------------------------------------------------------------------
/ltlib/balance.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | import decimal
18 | import itertools
19 | import re
20 |
21 | pattern = re.compile(r'([-\d\.]+)(\s+)(.*)')
22 |
23 |
24 | def match_to_dict(match):
25 | """Convert a match object into a dict.
26 |
27 | Values are:
28 | indent: amount of indentation of this [sub]account
29 | parent: the parent dict (None)
30 | account_fragment: account name fragment
31 | balance: decimal.Decimal balance
32 | children: sub-accounts ([])
33 | """
34 | balance, indent, account_fragment = match.group(1, 2, 3)
35 | return {
36 | 'balance': decimal.Decimal(balance),
37 | 'indent': len(indent),
38 | 'account_fragment': account_fragment,
39 | 'parent': None,
40 | 'children': [],
41 | }
42 |
43 |
44 | def balance(output):
45 | """Convert `ledger balance` output into an hierarchical data structure."""
46 | lines = map(pattern.search, output.splitlines())
47 |
48 | stack = []
49 | top = []
50 | for item in map(match_to_dict, itertools.takewhile(lambda x: x, lines)):
51 | # pop items off stack while current item has indent <=
52 | while stack and item['indent'] <= stack[-1]['indent']:
53 | stack.pop()
54 |
55 | # check if this is a top-level item
56 | if not stack:
57 | stack.append(item)
58 | top.append(item)
59 | else:
60 | item['parent'] = stack[-1]
61 | stack[-1]['children'].append(item)
62 | stack.append(item)
63 |
64 | return top
65 |
--------------------------------------------------------------------------------
/ltlib/chart.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | import gtkchartlib.ringchart
18 |
19 | RCI = gtkchartlib.ringchart.RingChartItem
20 |
21 | # show only accounts in credit or debit, or both
22 | SHOW_ALL = 0
23 | SHOW_CREDIT = 1
24 | SHOW_DEBIT = 2
25 |
26 |
27 | def balance_to_ringchart_items(balance, account='', show=SHOW_CREDIT):
28 | """Convert a balance data structure into RingChartItem objects."""
29 | show = show if show else SHOW_CREDIT # cannot show all in ring chart
30 | rcis = []
31 | for item in balance:
32 | subaccount = item['account_fragment'] if not account \
33 | else ':'.join((account, item['account_fragment']))
34 | ch = balance_to_ringchart_items(item['children'], subaccount, show)
35 | amount = item['balance'] if show == SHOW_CREDIT else -item['balance']
36 | if amount < 0:
37 | continue # omit negative amounts
38 | wedge_amount = max(amount, sum(map(float, ch)))
39 | rci = gtkchartlib.ringchart.RingChartItem(
40 | wedge_amount,
41 | tooltip='{}\n{}'.format(subaccount, wedge_amount),
42 | items=ch
43 | )
44 | rcis.append(rci)
45 | return rcis
46 |
--------------------------------------------------------------------------------
/ltlib/config.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011, 2012 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | import json
18 | import os
19 | import StringIO
20 |
21 |
22 | def apply(filter):
23 | """Manufacture decorator that filters return value with given function.
24 |
25 | ``filter``:
26 | Callable that takes a single parameter.
27 | """
28 | def decorator(callable):
29 | return lambda *args, **kwargs: filter(callable(*args, **kwargs))
30 | return decorator
31 |
32 |
33 | def format_outpat(outpat, xn):
34 | """
35 | Format an outpat for the given transaction.
36 |
37 | Format the given output filename pattern. The pattern should
38 | be a format string with any combination of the following named
39 | fields:
40 |
41 | ``year``
42 | The year of the transaction.
43 | ``month``
44 | The month of the transaction, with leading zero for
45 | single-digit months.
46 | ``fy``
47 | The financial year of the transaction (being the year in
48 | which the financial year of the transaction *ends*).
49 | A financial year runs from 1 July to 30 June.
50 | ``date``
51 | The date object itself. The format string may specify
52 | any attribute of the date object, e.g. ``{date.day}``.
53 | This field is deprecated.
54 | """
55 | return outpat.format(
56 | year=str(xn.date.year),
57 | month='{:02}'.format(xn.date.month),
58 | fy=str(xn.date.year if xn.date.month < 7 else xn.date.year + 1),
59 | date=xn.date
60 | )
61 |
62 |
63 | class Config(object):
64 | def __init__(self, path='~/.ltconfig', text=None):
65 | """Initialise a Config object.
66 |
67 | ``path``
68 | The path to the config file. Defaults to ``'~/.ltconfig'``.
69 | User expansion is performed on the value.
70 | ``text``
71 | JSON string that will be used for configuration. If supplied,
72 | takes precedence over ``path``.
73 | """
74 | if text is None:
75 | path = os.path.expanduser(path)
76 | if os.path.isfile(path):
77 | with open(path) as fh:
78 | self.data = json.load(fh)
79 | else:
80 | self.data = {} # file doesn't exist; empty config
81 | else:
82 | self.data = json.load(StringIO.StringIO(text))
83 |
84 | def get(self, name, acc=None, default=None):
85 | """Return the named config for the given account.
86 |
87 | If an account is given, first checks the account space for the name.
88 | If no account given, or if the name not found in the account space,
89 | look for the name in the global config space. If still not found,
90 | return the default, if given, otherwise ``None``.
91 | """
92 | if acc in self.data['accounts'] and name in self.data['accounts'][acc]:
93 | return self.data['accounts'][acc][name]
94 | if name in self.data:
95 | return self.data[name]
96 | return default
97 |
98 | @apply(os.path.normpath)
99 | @apply(os.path.expanduser)
100 | def rootdir(self):
101 | return self.get('rootdir')
102 |
103 | @apply(os.path.normpath)
104 | def outdir(self, acc=None):
105 | """Return the outdir for the given account.
106 |
107 | Attempts to create the directory if it does not exist.
108 | """
109 | rootdir = self.rootdir()
110 | outdir = self.get('outdir', acc=acc)
111 | dir = os.path.join(rootdir, outdir) if rootdir and outdir else None
112 | if not os.path.exists(dir):
113 | os.makedirs(dir)
114 | return dir
115 |
116 | def outpat(self, acc=None):
117 | """
118 | Determine the full outfile pattern for the given account.
119 |
120 | Return None if not specified.
121 | """
122 | outdir = self.outdir(acc)
123 | outpat = self.get('outpat', acc=acc)
124 | return os.path.join(outdir, outpat) if outdir and outpat else None
125 |
126 | @apply(os.path.normpath)
127 | def rulesdir(self, acc=None):
128 | """
129 | Determine the rulesdir for the given account.
130 |
131 | Return None if not specified.
132 | """
133 | rootdir = self.rootdir()
134 | rulesdir = self.get('rulesdir', acc=acc, default=[])
135 | return os.path.join(rootdir, rulesdir) \
136 | if rootdir and rulesdir else None
137 |
138 | def rulefiles(self, acc=None):
139 | """Return a list of rulefiles for the given account.
140 |
141 | Returns an empty list if none specified.
142 | """
143 | rulesdir = self.rulesdir(acc)
144 | rules = [os.path.join(rulesdir, x) for x in self.get('rules', acc, [])]
145 | if acc is not None:
146 | rules += self.rulefiles(acc=None)
147 | return rules
148 |
--------------------------------------------------------------------------------
/ltlib/parse.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | import datetime
18 | import decimal
19 | import functools
20 | import operator
21 | import re
22 | import shlex
23 |
24 | from . import rule
25 |
26 |
27 | class Parser(object):
28 | def __init__(self):
29 | self.state = ConditionState() # start in ConditionState
30 |
31 | def eatwords(self, words):
32 | if words[0] == 'then':
33 | words.pop(0)
34 | self.state = OutcomeState() # switch to OutcomeState
35 | return self.state.eatwords(words)
36 |
37 |
38 | class TypeState(object):
39 | def __init__(self, cls):
40 | self.cls = cls
41 |
42 | def eatwords(self, words):
43 | return functools.partial(self.cls, value=self.cast(words.pop(0)))
44 |
45 |
46 | class NoneState(TypeState):
47 | def eatwords(self, words):
48 | return self.cls
49 |
50 |
51 | class AccountState(TypeState):
52 | def cast(self, value):
53 | return value # TODO cast to valid account
54 |
55 |
56 | class AmountState(TypeState):
57 | def cast(self, value):
58 | return decimal.Decimal(value)
59 |
60 |
61 | class DescriptionState(TypeState):
62 | def cast(self, value):
63 | return re.compile(value, re.IGNORECASE)
64 |
65 |
66 | class DateState(TypeState):
67 | def cast(self, value):
68 | # TODO: cast to datetime.Date object
69 | date = None
70 | return date
71 |
72 |
73 | class ConditionState(object):
74 | partial = functools.partial
75 | dispatch = {
76 | 'from': AccountState(rule.SourceCondition),
77 | 'to': AccountState(rule.DestinationCondition),
78 | 'lt': AmountState(partial(rule.AmountCondition, op=operator.lt)),
79 | 'le': AmountState(partial(rule.AmountCondition, op=operator.le)),
80 | 'eq': AmountState(partial(rule.AmountCondition, op=operator.eq)),
81 | 'ne': AmountState(partial(rule.AmountCondition, op=operator.ne)),
82 | 'ge': AmountState(partial(rule.AmountCondition, op=operator.ge)),
83 | 'gt': AmountState(partial(rule.AmountCondition, op=operator.gt)),
84 | 'desc': DescriptionState(rule.DescriptionCondition),
85 | 'before': DateState(partial(rule.DateCondition, op=operator.lt)),
86 | 'notafter': DateState(partial(rule.DateCondition, op=operator.le)),
87 | 'on': DateState(partial(rule.DateCondition, op=operator.eq)),
88 | 'noton': DateState(partial(rule.DateCondition, op=operator.ne)),
89 | 'notbefore': DateState(partial(rule.DateCondition, op=operator.ge)),
90 | 'after': DateState(partial(rule.DateCondition, op=operator.gt))
91 | }
92 |
93 | def eatwords(self, words):
94 | word = words.pop(0)
95 | return self.dispatch[word].eatwords(words)()
96 |
97 |
98 | class OutcomeState(object):
99 | partial = functools.partial
100 | dispatch = {
101 | 'from': AccountState(rule.SourceOutcome),
102 | 'to': AccountState(rule.DestinationOutcome),
103 | 'desc': DescriptionState(rule.DescriptionOutcome),
104 | 'drop': NoneState(rule.DropOutcome),
105 | 'rebate': NoneState(rule.RebateOutcome),
106 | }
107 |
108 | def eatwords(self, words):
109 | word = words.pop(0)
110 | cls = self.dispatch[word].eatwords(words)
111 | return cls(score=int(words.pop(0)))
112 |
113 |
114 | def line2rule(line):
115 | parser = Parser()
116 | words = shlex.split(line)
117 | acc = []
118 | try:
119 | while words:
120 | acc.append(parser.eatwords(words))
121 | return rule.Rule(*acc)
122 | except:
123 | if words:
124 | print "error on line: '" + line + "' at '" + words[0]
125 | else:
126 | print "error on line: '" + line
127 | raise
128 |
129 |
130 | def file2rules(file):
131 | # filter out comments
132 | stripcomments = functools.partial(re.compile('\s*(?:#.*|$)').sub, '')
133 | return map(line2rule, filter(lambda x: x, map(stripcomments, file)))
134 |
--------------------------------------------------------------------------------
/ltlib/reader.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 |
18 | class ReadError(Exception):
19 | """Unable to read the file"""
20 | pass
21 |
22 |
23 | class DataError(Exception):
24 | """Unexpected or malformed data in input file"""
25 | pass
26 |
27 |
28 | class Reader(object):
29 | """Base class for transaction file readers.
30 |
31 | A Reader is an iterator that takes a transaction file of some kind
32 | and provides transaction objects via its next() method.
33 | """
34 | def __init__(self, **kwargs):
35 | super(Reader, self).__init__()
36 |
37 | if 'file' not in kwargs:
38 | raise ReadError("No file provided")
39 | self.file = kwargs['file']
40 |
41 | def __iter__(self):
42 | """Return an iterator for the Reader"""
43 | return self # override in subclass if not appropriate
44 |
45 | def next(self):
46 | """Return the next item.
47 |
48 | Must raise StopIteration when the file has no more transactions
49 | """
50 | raise NotImplementedError
51 |
--------------------------------------------------------------------------------
/ltlib/readers/CSV.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | import csv
18 | import datetime
19 | import decimal
20 | import re
21 |
22 | from .. import reader
23 | from .. import xn
24 |
25 |
26 | date_delim = re.compile('-|/')
27 |
28 |
29 | class MetadataException(Exception):
30 | """Exception to indicate metadata in the CSV file.
31 |
32 | ``Reader.dict_to_xn`` raises this exception when it encounters
33 | metadata in the CSV file; the exception can be caught and
34 | ``next`` can advance to the next row in the file.
35 | """
36 |
37 |
38 | def mkdecimal(s):
39 | if len(s) >= 1 and s[0] == '$':
40 | s = s[1:]
41 | elif s.startswith('-$') or s.startswith('+$'):
42 | s = s[0] + s[2:]
43 | return decimal.Decimal(s.replace(',', ''))
44 |
45 |
46 | class Reader(reader.Reader):
47 | """CSV statement reader.
48 |
49 | This reader expects the field names to be provided as the first line
50 | of the file (TODO: make configurable).
51 |
52 | The file must provide the "Date" and "Description" fields, and either
53 | an "Amount" field (which contains a positive or negative value) or
54 | both "Debit" and "Credit" fields (which only contain positive values).
55 | The heuristic for interpreting the "Date" field is described in the
56 | parse_date() documentation.
57 |
58 | If the field names do not match those above, use the
59 | ``fieldremap`` argument to supply a mapping of actual field
60 | names keyed by the aforementioned expected field names.
61 | Matching fields may be omitted from this mapping.
62 |
63 | Since the assumption is that all transactions in a CSV file pertain to a
64 | particular account, this account must be supplied to the constructor.
65 | For a given transaction, the "to" or "from" field of the transaction object
66 | will be set to the given account according to whether the "Debit" or
67 | "Credit" field is used.
68 | """
69 |
70 | def __init__(
71 | self,
72 | fieldnames=None,
73 | fieldremap=None,
74 | date_format=None,
75 | **kwargs):
76 | """
77 | Takes an account argument which indicates the account that was
78 | transacted upon.
79 |
80 | The fieldnames argument, if supplied, is passed to the underlying
81 | csv.DictReader object, and is required if the first line of the
82 | CSV file does not specify the field names.
83 | """
84 | if 'account' not in kwargs:
85 | raise reader.DataError('Required account field was not provided')
86 | self.account = kwargs.pop('account')
87 | super(Reader, self).__init__(**kwargs)
88 | self.csvreader = csv.DictReader(self.file, fieldnames=fieldnames)
89 | self.remap = fieldremap
90 | self.date_format = date_format
91 |
92 | def next(self):
93 | """Return the next transaction object.
94 |
95 | StopIteration will be propagated from self.csvreader.next()
96 | """
97 | try:
98 | return self.dict_to_xn(self.csvreader.next())
99 | except MetadataException:
100 | # row was metadata; proceed to next row
101 | return next(self)
102 |
103 | def parse_date(self, date):
104 | """Parse the date and return a datetime object
105 |
106 | The heuristic for determining the date is:
107 | - if ``date_format`` is set, parse using strptime
108 | - if one field of 8 digits, YYYYMMDD
109 | - split by '-' or '/'
110 | - (TODO: substitute string months with their numbers)
111 | - if (2, 2, 4), DD-MM-YYYY (not the peculiar US order)
112 | - if (4, 2, 2), YYYY-MM-DD
113 | - ka-boom!
114 |
115 | The issue of reliably discerning between DD-MM-YYYY (sane) vs.
116 | MM-DD-YYYY (absurd, but Big In America), without being told what's
117 | being used, is intractable.
118 |
119 | Return a datetime.date object.
120 |
121 | """
122 | if self.date_format is not None:
123 | return datetime.datetime.strptime(date, self.date_format).date()
124 |
125 | if re.match('\d{8}$', date):
126 | # assume YYYYMMDD
127 | return datetime.date(*map(int, (date[:4], date[4:6], date[6:])))
128 | try:
129 | # split by '-' or '/'
130 | parts = date_delim.split(date, 2) # maxsplit=2
131 | if len(parts) == 3:
132 | if len(parts[0]) == 4:
133 | # YYYY, MM, DD
134 | return datetime.date(*map(int, parts))
135 | elif len(parts[2]) == 4:
136 | # DD, MM, YYYY
137 | return datetime.date(*map(int, reversed(parts)))
138 | # fail
139 | except TypeError, ValueError:
140 | raise reader.DataError('Bad date format: "{}"'.format(date))
141 |
142 | def _fieldname(self, k):
143 | if self.remap is None:
144 | return k
145 | return self.remap.get(k, k)
146 |
147 | def dict_to_xn(self, fields):
148 | # normalise field names (strip whitespace)
149 | fields = dict(
150 | map(
151 | lambda (x, y): (x.strip(), y),
152 | fields.viewitems()
153 | )
154 | )
155 |
156 | xn_dict = {} # dict that will be passed to Xn constructor
157 |
158 | # date
159 | xn_dict['date'] = self.parse_date(fields[self._fieldname('Date')])
160 |
161 | # description
162 | xn_dict['desc'] = fields[self._fieldname('Description')]
163 |
164 | # amount
165 | fieldname_amount = self._fieldname('Amount')
166 | if fieldname_amount in fields:
167 | amount = mkdecimal(fields[fieldname_amount])
168 | xn_dict['amount'] = abs(amount)
169 | if amount > 0:
170 | xn_dict['dst'] = [xn.Endpoint(self.account, amount)] # credit
171 | else:
172 | xn_dict['src'] = [xn.Endpoint(self.account, amount)] # debit
173 | else:
174 | fieldname_credit = self._fieldname('Credit')
175 | fieldname_debit = self._fieldname('Debit')
176 | if fields[fieldname_credit] and fields[fieldname_debit]:
177 | # this doesn't seem right...
178 | raise reader.DataError('Credit and Debit field used; dubious.')
179 | elif not fields[fieldname_credit] and not fields[fieldname_debit]:
180 | # neither field is supplied; is it metadata?
181 | if re.match(
182 | r'\s*(?:open|clos)ing\s*balance\s*$',
183 | fields[self._fieldname('Description')],
184 | flags=re.IGNORECASE
185 | ):
186 | # yep, looks like metadata
187 | raise MetadataException
188 | else:
189 | raise reader.DataError(
190 | 'unable to process fields: {!r}'.format(fields)
191 | )
192 | amount_raw = fields[fieldname_credit] or fields[fieldname_debit]
193 | amount = abs(decimal.Decimal(amount_raw))
194 | xn_dict['amount'] = amount
195 | if fields[fieldname_credit]:
196 | xn_dict['dst'] = [xn.Endpoint(self.account, amount)] # credit
197 | else:
198 | xn_dict['src'] = [xn.Endpoint(self.account, -amount)] # debit
199 |
200 | return xn.Xn(**xn_dict)
201 |
--------------------------------------------------------------------------------
/ltlib/readers/__init__.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools.
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | from . import CSV
18 |
--------------------------------------------------------------------------------
/ltlib/rule.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | import re
18 |
19 |
20 | class Condition(object):
21 | """A rule condition.
22 |
23 | Provides Condition.match(xn) which returns True if the rule
24 | matches the condition, otherwise False.
25 | """
26 | def __init__(self, *args, **kwargs):
27 | self.value = kwargs.pop('value')
28 | super(Condition, self).__init__(*args, **kwargs)
29 |
30 | def match(self, xn):
31 | raise NotImplementedError # subclasses must implement
32 |
33 | def __repr__(self):
34 | return "{}(value={!r})".format(self.__class__.__name__, self.value)
35 |
36 |
37 | class OperatorCondition(Condition):
38 | def __init__(self, *args, **kwargs):
39 | self.op = kwargs.pop('op')
40 | super(OperatorCondition, self).__init__(*args, **kwargs)
41 |
42 |
43 | class AccountCondition(Condition):
44 | def __init__(self, *args, **kwargs):
45 | """
46 | Any '::' expands to 1+ intermediate fragments.
47 | No ':' at start anchors fragment at beginning.
48 | No ':' at end anchors fragment at end.
49 | """
50 | super(AccountCondition, self).__init__(*args, **kwargs)
51 | pattern = self.value.replace('::', ':[\w ]+(?::[\w ]+)*:')
52 | if pattern[0] != ':':
53 | pattern = '^' + pattern
54 | if pattern[-1] != ':':
55 | pattern = pattern + '$'
56 | self.re = re.compile(pattern)
57 |
58 |
59 | class SourceCondition(AccountCondition):
60 | def match(self, xn):
61 | if xn.src is None:
62 | return False
63 | return filter(lambda src: self.re.search(src.account), xn.src)
64 |
65 |
66 | class DestinationCondition(AccountCondition):
67 | def match(self, xn):
68 | if xn.dst is None:
69 | return False
70 | return filter(lambda dst: self.re.search(dst.account), xn.dst)
71 |
72 |
73 | class DescriptionCondition(Condition):
74 | def match(self, xn):
75 | if xn.desc is None:
76 | return False
77 | return self.value.search(xn.desc)
78 |
79 |
80 | class AmountCondition(OperatorCondition):
81 | def match(self, xn):
82 | if xn.amount is None:
83 | return False
84 | return self.op(xn.amount, self.value)
85 |
86 |
87 | class DateCondition(OperatorCondition):
88 | def match(self, xn):
89 | if xn.date is None:
90 | return False
91 | return self.op(xn.date, self.value)
92 |
93 |
94 | class Outcome(object):
95 | """Specifies a rule outcome.
96 |
97 | A rule outcome consists of a value, and a numeric score associated
98 | with that value.
99 | """
100 | def __init__(self, *args, **kwargs):
101 | self.value = kwargs.pop('value')
102 | self.score = kwargs.pop('score')
103 | super(Outcome, self).__init__(*args, **kwargs)
104 |
105 |
106 | class DropOutcome(Outcome):
107 | def __init__(self, *args, **kwargs):
108 | super(DropOutcome, self).__init__(*args, value=None, **kwargs)
109 |
110 |
111 | class RebateOutcome(Outcome):
112 | def __init__(self, *args, **kwargs):
113 | super(RebateOutcome, self).__init__(*args, value=None, **kwargs)
114 |
115 |
116 | class SourceOutcome(Outcome):
117 | pass
118 |
119 |
120 | class DestinationOutcome(Outcome):
121 | pass
122 |
123 |
124 | class DescriptionOutcome(Outcome):
125 | pass
126 |
127 |
128 | class Rule(object):
129 | """Rule providing match conditions and probabilistic outcomes
130 |
131 | When a transaction satisfies all conditions of a rule, the rule
132 | returns to the transaction a set of outcomes with probabilities.
133 |
134 | How these outcomes are used by the Xn is outside the scope of this
135 | class.
136 | """
137 | def __init__(self, *args):
138 | """Initialise the rule"""
139 | super(Rule, self).__init__()
140 |
141 | self.conditions = []
142 | self.outcomes = []
143 |
144 | for condition_or_outcome in args:
145 | if isinstance(condition_or_outcome, Condition):
146 | self.conditions.append(condition_or_outcome)
147 | elif isinstance(condition_or_outcome, Outcome):
148 | self.outcomes.append(condition_or_outcome)
149 | elif condition_or_outcome is not None:
150 | raise Exception # TODO specialise
151 |
152 | def match(self, xn):
153 | """Processes a transaction against this rule
154 |
155 | If all conditions are satisfied, a list of outcomes is returned.
156 | If any condition is unsatisifed, None is returned.
157 | """
158 | if all(map(lambda x: x.match(xn), self.conditions)):
159 | return self.outcomes
160 | return None
161 |
--------------------------------------------------------------------------------
/ltlib/score.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 |
18 | def value(item):
19 | return item[0]
20 |
21 |
22 | def score(item):
23 | return item[1]
24 |
25 |
26 | class ScoreSet(object):
27 | def __init__(self, items=None, **kwargs):
28 | self.items = items or {}
29 |
30 | def __contains__(self, item):
31 | key = item[0] if isinstance(item, tuple) else item
32 | return key in self.items
33 |
34 | def append(self, item):
35 | """Append an item to the score set.
36 |
37 | item is a pair tuple, the first element of which is a valid dict
38 | key and the second of which is a numeric value.
39 | """
40 | if item in self:
41 | self.items[item[0]].append(item[1])
42 | else:
43 | self.items[item[0]] = [item[1]]
44 |
45 | def scores(self):
46 | """Return a list of the items with their final scores.
47 |
48 | The final score of each item is its average score multiplied by the
49 | square root of its length. This reduces to sum * len^(-1/2).
50 | """
51 | return map(
52 | lambda x: (x[0], sum(x[1]) * len(x[1]) ** -.5),
53 | iter(self.items.viewitems())
54 | )
55 |
56 | def highest(self):
57 | """Return the items with the higest score.
58 |
59 | If this ScoreSet is empty, returns None.
60 | """
61 | scores = self.scores()
62 | if not scores:
63 | return None
64 | maxscore = max(map(score, scores))
65 | return filter(lambda x: score(x) == maxscore, scores)
66 |
--------------------------------------------------------------------------------
/ltlib/test_config.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011, 2012 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | import datetime
18 | import os
19 | import unittest
20 |
21 | from . import config
22 |
23 | text = r"""
24 | {
25 | "rootdir": "~/ledger",
26 | "outdir": "General",
27 | "outpat": "out.dat",
28 | "rulesdir": "rules",
29 | "rules": [ "General.rules" ],
30 |
31 | "transact-default-account": "Expenses:Cash",
32 |
33 | "accounts": {
34 | "Assets:AccountA": {
35 | },
36 | "Assets:AccountB": {
37 | "transact-default-account": "Foo:Bar",
38 | "outdir": "AccountB",
39 | "outpat": "AccountB.dat",
40 | "rulesdir": "rules/AccountB",
41 | "rules": [ "AccountB.rules" ]
42 | }
43 | }
44 | }
45 | """
46 |
47 |
48 | class ConfigTestCase(unittest.TestCase):
49 | def setUp(self):
50 | self.config = config.Config(text=text)
51 |
52 | def test_get(self):
53 | # no account
54 | self.assertEqual(
55 | self.config.get('transact-default-account'),
56 | 'Expenses:Cash'
57 | )
58 | # not overridden by account
59 | self.assertEqual(
60 | self.config.get(
61 | 'transact-default-account',
62 | acc='Assets:AccountA'
63 | ),
64 | 'Expenses:Cash'
65 | )
66 | # overriden by account
67 | self.assertEqual(
68 | self.config.get(
69 | 'transact-default-account',
70 | acc='Assets:AccountB'
71 | ),
72 | 'Foo:Bar'
73 | )
74 | # nonexistant config (no default)
75 | self.assertIsNone(self.config.get('fake'))
76 | # nonexistant config (use default)
77 | self.assertTrue(self.config.get('fake', acc=None, default=True))
78 | # nonexistant account (no default)
79 | self.assertIsNone(self.config.get('fake', acc='Foo'))
80 | # nonexistant account (use default)
81 | self.assertTrue(self.config.get('fake', acc='Foo', default=True))
82 |
83 | def test_rootdir(self):
84 | self.assertEqual(
85 | self.config.rootdir(),
86 | os.path.normpath(os.path.expanduser('~/ledger'))
87 | )
88 |
89 | def test_outdir(self):
90 | # no account
91 | self.assertEqual(
92 | self.config.outdir(),
93 | os.path.expanduser('~/ledger/General')
94 | )
95 | # not defined by account
96 | self.assertEqual(
97 | self.config.outdir('Assets:AccountA'),
98 | os.path.expanduser('~/ledger/General')
99 | )
100 | # defined by account
101 | self.assertEqual(
102 | self.config.outdir('Assets:AccountB'),
103 | os.path.expanduser('~/ledger/AccountB')
104 | )
105 |
106 | def test_outpat(self):
107 | # no account
108 | self.assertEqual(
109 | self.config.outpat(),
110 | os.path.expanduser('~/ledger/General/out.dat')
111 | )
112 | # not defined by account
113 | self.assertEqual(
114 | self.config.outpat('Assets:AccountA'),
115 | os.path.expanduser('~/ledger/General/out.dat')
116 | )
117 | # defined by account
118 | self.assertEqual(
119 | self.config.outpat('Assets:AccountB'),
120 | os.path.expanduser('~/ledger/AccountB/AccountB.dat')
121 | )
122 |
123 | def test_rulesdir(self):
124 | # no account
125 | self.assertEqual(
126 | self.config.rulesdir(),
127 | os.path.expanduser('~/ledger/rules')
128 | )
129 | # not defined by account
130 | self.assertEqual(
131 | self.config.rulesdir('Assets:AccountA'),
132 | os.path.expanduser('~/ledger/rules')
133 | )
134 | # defined by account
135 | self.assertEqual(
136 | self.config.rulesdir('Assets:AccountB'),
137 | os.path.expanduser('~/ledger/rules/AccountB')
138 | )
139 |
140 | def test_rulefiles(self):
141 | # no account
142 | self.assertSetEqual(
143 | set(self.config.rulefiles()),
144 | set([os.path.expanduser('~/ledger/rules/General.rules')])
145 | )
146 | # not defined by account
147 | self.assertSetEqual(
148 | set(self.config.rulefiles('Assets:AccountA')),
149 | set([os.path.expanduser('~/ledger/rules/General.rules')])
150 | )
151 | # defined by account
152 | self.assertSetEqual(
153 | set(self.config.rulefiles('Assets:AccountB')),
154 | set((
155 | os.path.expanduser('~/ledger/rules/' + x)
156 | for x in ['General.rules', 'AccountB/AccountB.rules']
157 | ))
158 | )
159 |
160 | class FormatOutpatTestCase(unittest.TestCase):
161 | def setUp(self):
162 | class BogoXn(object):
163 | __slots__ = ['date']
164 | self.xn = BogoXn()
165 | self.xn.date = datetime.date(2012, 7, 1)
166 |
167 | def test_year(self):
168 | self.assertEqual(config.format_outpat('{year}', self.xn), '2012')
169 |
170 | def test_month(self):
171 | self.assertEqual(config.format_outpat('{month}', self.xn), '07')
172 | self.xn.date = datetime.date(2012, 12, 31)
173 | self.assertEqual(config.format_outpat('{month}', self.xn), '12')
174 |
175 | def test_fy(self):
176 | self.assertEqual(config.format_outpat('{fy}', self.xn), '2013')
177 | self.xn.date = datetime.date(2012, 6, 30)
178 | self.assertEqual(config.format_outpat('{fy}', self.xn), '2012')
179 |
180 | def test_date(self):
181 | self.assertEqual(
182 | config.format_outpat(
183 | '{date.year}{date.month:02}{date.day:02}',
184 | self.xn
185 | ),
186 | '20120701'
187 | )
188 |
--------------------------------------------------------------------------------
/ltlib/ui.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | import datetime
18 | import decimal
19 | import functools
20 | import math
21 | import re
22 | import sys
23 |
24 | curry = functools.partial
25 |
26 |
27 | class InvalidInputError(Exception):
28 | pass
29 |
30 |
31 | class RejectWarning(Warning):
32 | pass
33 |
34 |
35 | def number(items):
36 | """Maps numbering onto given values"""
37 | n = len(items)
38 | if n == 0:
39 | return items
40 | places = str(int(math.log10(n) // 1 + 1))
41 | format = '[{0[0]:' + str(int(places)) + 'd}] {0[1]}'
42 | return map(
43 | lambda x: format.format(x),
44 | enumerate(items)
45 | )
46 |
47 |
48 | def filter_yn(string, default=None):
49 | """Return True if yes, False if no, or the default."""
50 | if string.startswith(('Y', 'y')):
51 | return True
52 | elif string.startswith(('N', 'n')):
53 | return False
54 | elif not string and default is not None:
55 | return True if default else False
56 | raise InvalidInputError
57 |
58 |
59 | def filter_int(string, default=None, start=None, stop=None):
60 | """Return the input integer, or the default."""
61 | try:
62 | i = int(string)
63 | if start is not None and i < start:
64 | raise InvalidInputError("value too small")
65 | if stop is not None and i >= stop:
66 | raise InvalidInputError("value too large")
67 | return i
68 | except ValueError:
69 | if not string and default is not None:
70 | # empty string, default was given
71 | return default
72 | else:
73 | raise InvalidInputError
74 |
75 |
76 | def filter_decimal(string, default=None, lower=None, upper=None):
77 | """Return the input decimal number, or the default."""
78 | try:
79 | d = decimal.Decimal(string)
80 | if lower is not None and d < lower:
81 | raise InvalidInputError("value too small")
82 | if upper is not None and d >= upper:
83 | raise InvalidInputError("value too large")
84 | return d
85 | except decimal.InvalidOperation:
86 | if not string and default is not None:
87 | # empty string, default was given
88 | return default
89 | else:
90 | raise InvalidInputError("invalid decimal number")
91 |
92 |
93 | def filter_text(string, default=None):
94 | if string:
95 | return string
96 | elif default is not None:
97 | return default
98 | else:
99 | raise InvalidInputError
100 |
101 |
102 | def filter_pastdate(string, default=None):
103 | """Coerce to a date not beyond the current date
104 |
105 | If only a day is given, assumes the current month if that day has
106 | passed or is the current day, otherwise assumes the previous month.
107 | If a day and month are given, but no year, assumes the current year
108 | if the given date has passed (or is today), otherwise the previous
109 | year.
110 | """
111 | if not string and default is not None:
112 | return default
113 |
114 | today = datetime.date.today()
115 |
116 | # split the string
117 | try:
118 | parts = map(int, re.split('\D+', string)) # split the string
119 | except ValueError:
120 | raise InvalidInputError("invalid date; use format: DD [MM [YYYY]]")
121 |
122 | if len(parts) < 1 or len(parts) > 3:
123 | raise InvalidInputError("invalid date; use format: DD [MM [YYYY]]")
124 |
125 | if len(parts) == 1:
126 | # no month or year given; append month
127 | parts.append(today.month - 1 if parts[0] > today.day else today.month)
128 | if parts[1] < 1:
129 | parts[1] = 12
130 |
131 | if len(parts) == 2:
132 | # no year given; append year
133 | if parts[1] > today.month \
134 | or parts[1] == today.month and parts[0] > today.day:
135 | parts.append(today.year - 1)
136 | else:
137 | parts.append(today.year)
138 |
139 | parts.reverse()
140 |
141 | try:
142 | date = datetime.date(*parts)
143 | if date > today:
144 | raise InvalidInputError("cannot choose a date in the future")
145 | return date
146 | except ValueError:
147 | print parts
148 | raise InvalidInputError("invalid date; use format: DD [MM [YYYY]]")
149 |
150 |
151 | class UI(object):
152 | def show(self, msg):
153 | print msg
154 |
155 | def bail(self, msg=None):
156 | """Exit uncleanly with an optional message"""
157 | if msg:
158 | self.show('BAIL OUT: ' + msg)
159 | sys.exit(1)
160 |
161 | def input(self, filter_fn, prompt):
162 | """Prompt user until valid input is received.
163 |
164 | RejectWarning is raised if a KeyboardInterrupt is caught.
165 | """
166 | while True:
167 | try:
168 | return filter_fn(raw_input(prompt))
169 | except InvalidInputError as e:
170 | if e.message:
171 | self.show('ERROR: ' + e.message)
172 | except KeyboardInterrupt:
173 | raise RejectWarning
174 |
175 | def text(self, prompt, default=None):
176 | """Prompts the user for some text, with optional default"""
177 | prompt = prompt if prompt is not None else 'Enter some text'
178 | prompt += " [{0}]: ".format(default) if default is not None else ': '
179 | return self.input(curry(filter_text, default=default), prompt)
180 |
181 | def account(self, prompt, default=None):
182 | """Prompts the user for an account, with optional default
183 |
184 | TODO: for now, this just wraps text, but conformity to account name
185 | style should be checked
186 | """
187 | return self.text(prompt, default)
188 |
189 | def decimal(self, prompt, default=None, lower=None, upper=None):
190 | """Prompts user to input decimal, with optional default and bounds."""
191 | prompt = prompt if prompt is not None else "Enter a decimal number"
192 | prompt += " [{0}]: ".format(default) if default is not None else ': '
193 | return self.input(
194 | curry(filter_decimal, default=default, lower=lower, upper=upper),
195 | prompt
196 | )
197 |
198 | def pastdate(self, prompt, default=None):
199 | """Prompts user to input a date in the past."""
200 | prompt = prompt if prompt is not None else "Enter a past date"
201 | if default is not None:
202 | prompt += " [" + default.strftime('%d %m %Y') + "]"
203 | prompt += ': '
204 | return self.input(curry(filter_pastdate, default=default), prompt)
205 |
206 | def yn(self, prompt, default=None):
207 | """Prompts the user for yes/no confirmation, with optional default"""
208 | if default is True:
209 | opts = " [Y/n]: "
210 | elif default is False:
211 | opts = " [y/N]: "
212 | else:
213 | opts = " [y/n]: "
214 | prompt += opts
215 | return self.input(curry(filter_yn, default=default), prompt)
216 |
217 | def choose(self, prompt, items, default=None):
218 | """Prompts the user to choose one item from a list.
219 |
220 | The default, if provided, is an index; the item of that index will
221 | be returned.
222 | """
223 | if default is not None and (default >= len(items) or default < 0):
224 | raise IndexError
225 | prompt = prompt if prompt is not None else "Choose from following:"
226 | self.show(prompt + '\n')
227 | self.show("\n".join(number(items))) # show the items
228 | prompt = "Enter number of chosen item"
229 | prompt += " [{0}]: ".format(default) if default is not None else ': '
230 | return items[self.input(
231 | curry(filter_int, default=default, start=0, stop=len(items)),
232 | prompt
233 | )]
234 |
--------------------------------------------------------------------------------
/ltlib/util.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | import collections
18 | import os
19 |
20 |
21 | def flatten(xs):
22 | for x in xs:
23 | if isinstance(x, collections.Iterable) \
24 | and not isinstance(x, basestring):
25 | for y in flatten(x):
26 | yield y
27 | else:
28 | yield x
29 |
--------------------------------------------------------------------------------
/ltlib/xn.py:
--------------------------------------------------------------------------------
1 | # This file is part of ledgertools
2 | # Copyright (C) 2011 Fraser Tweedale
3 | #
4 | # ledgertools is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | # TODO use ui.bail, not sys.exit
18 | import sys
19 |
20 | from . import rule
21 | from . import score
22 | from . import ui
23 |
24 |
25 | threshold = {
26 | 'y': 8000,
27 | 'y?': 6000,
28 | '?': 4000,
29 | 'n?': 2000,
30 | 'n': 0,
31 | }
32 |
33 |
34 | class XnDataError(Exception):
35 | """Missing or bogus data"""
36 | pass
37 |
38 |
39 | class XnBalanceError(Exception):
40 | """Transaction does not balance"""
41 | pass
42 |
43 |
44 | class Endpoint(object):
45 | def __init__(self, account, amount):
46 | self.account = account
47 | self.amount = amount
48 |
49 | def __repr__(self):
50 | return 'Endpoint({!r}, {!r})'.format(self.account, self.amount)
51 |
52 |
53 | class Xn(object):
54 | def __init__(self, **kwargs):
55 | """Initialise the transaction object"""
56 | self.dropped = kwargs['dropped'] if 'dropped' in kwargs else False
57 | self.date = kwargs['date'] if 'date' in kwargs else None
58 | self.desc = kwargs['desc'] if 'desc' in kwargs else None
59 | self.amount = kwargs['amount'] if 'amount' in kwargs else None
60 | self.dst = kwargs['dst'] if 'dst' in kwargs else None
61 | self.src = kwargs['src'] if 'src' in kwargs else None
62 |
63 | def __repr__(self):
64 | return "Xn(\n" + '\n'.join(map(
65 | lambda x: ' {!r}: {!r}'.format(x, getattr(self, x)),
66 | ['date', 'desc', 'amount', 'src', 'dst', 'dropped']
67 | )) + ')\n'
68 |
69 | def __str__(self):
70 | return self.summary()
71 |
72 | def ledger(self):
73 | """Convert to a Ledger transaction (no trailing blank line)"""
74 | self.balance() # make sure the transaction balances
75 |
76 | s = "{0}/{1:02}/{2:02} {3}\n".format(
77 | self.date.year,
78 | self.date.month,
79 | self.date.day,
80 | self.desc.replace('\n', ' ')
81 | )
82 | for src in self.src:
83 | s += " {0.account} ${0.amount}\n".format(src)
84 | for dst in self.dst:
85 | s += " {0.account} ${0.amount}\n".format(dst)
86 | return s
87 |
88 | def summary(self):
89 | """Return a string summary of transaction"""
90 | return "\n".join([
91 | "Transaction:",
92 | " When: " + self.date.strftime("%a %d %b %Y"),
93 | " Description: " + self.desc.replace('\n', ' '),
94 | " For amount: {}".format(self.amount),
95 | " From: {}".format(
96 | ", ".join(map(lambda x: x.account, self.src)) if self.src \
97 | else "UNKNOWN"
98 | ),
99 | " To: {}".format(
100 | ", ".join(map(lambda x: x.account, self.dst)) if self.dst \
101 | else "UNKNOWN"
102 | ),
103 | ""
104 | ])
105 |
106 | def check(self):
107 | """Check this transaction for completeness"""
108 | if not self.date:
109 | raise XnDataError("Missing date")
110 | if not self.desc:
111 | raise XnDataError("Missing description")
112 | if not self.dst:
113 | raise XnDataError("No destination accounts")
114 | if not self.src:
115 | raise XnDataError("No source accounts")
116 | if not self.amount:
117 | raise XnDataError("No transaction amount")
118 |
119 | def balance(self):
120 | """Check this transaction for correctness"""
121 | self.check()
122 | if not sum(map(lambda x: x.amount, self.src)) == -self.amount:
123 | raise XnBalanceError("Sum of source amounts "
124 | "not equal to transaction amount")
125 | if not sum(map(lambda x: x.amount, self.dst)) == self.amount:
126 | raise XnBalanceError("Sum of destination amounts "
127 | "not equal to transaction amount")
128 | return True
129 |
130 | def match_rules(self, rules):
131 | """Process this transaction against the given ruleset
132 |
133 | Returns a dict of fields with ScoreSet values, which may be empty.
134 | Notably, the rule processing will be shortcircuited if the Xn is
135 | already complete - in this case, None is returned.
136 | """
137 | try:
138 | self.check()
139 | return None
140 | except XnDataError:
141 | pass
142 |
143 | scores = {}
144 |
145 | for r in rules:
146 | outcomes = r.match(self)
147 | if not outcomes:
148 | continue
149 | for outcome in outcomes:
150 | if isinstance(outcome, rule.SourceOutcome):
151 | key = 'src'
152 | elif isinstance(outcome, rule.DestinationOutcome):
153 | key = 'dst'
154 | elif isinstance(outcome, rule.DescriptionOutcome):
155 | key = 'desc'
156 | elif isinstance(outcome, rule.DropOutcome):
157 | key = 'drop'
158 | elif isinstance(outcome, rule.RebateOutcome):
159 | key = 'rebate'
160 | else:
161 | raise KeyError
162 | if key not in scores:
163 | scores[key] = score.ScoreSet() # initialise ScoreSet
164 | scores[key].append((outcome.value, outcome.score))
165 |
166 | return scores
167 |
168 | def apply_outcomes(self, outcomes, uio, dropped=False, prevxn=None):
169 | """Apply the given outcomes to this rule.
170 |
171 | If user intervention is required, outcomes are not applied
172 | unless a ui.UI is supplied.
173 | """
174 | if self.dropped and not dropped:
175 | # do nothing for dropped xn, unless specifically told to
176 | return
177 |
178 | if 'drop' in outcomes:
179 | highscore = score.score(outcomes['drop'].highest()[0])
180 | if highscore >= threshold['y']:
181 | # drop without prompting
182 | self.dropped = True
183 | elif highscore < threshold['n?']:
184 | # do NOT drop, and don't even ask
185 | pass
186 | else:
187 | uio.show('DROP was determined for transaction:')
188 | uio.show('')
189 | uio.show(self.summary())
190 | if highscore >= threshold['y?']:
191 | default = True
192 | elif highscore >= threshold['?']:
193 | default = None
194 | else:
195 | default = False
196 | try:
197 | self.dropped = uio.yn('DROP this transaction?', default)
198 | except ui.RejectWarning:
199 | # we assume they mean "no"
200 | pass
201 |
202 | if self.dropped and not dropped:
203 | # do nothing further for dropped xn, unless specifically told to
204 | return
205 |
206 | # rebate outcomes
207 | #
208 | # A rebate is a rebate of the previous transaction.
209 | # The proportions of credits in the prev xn are kept,
210 | # inverted (i.e. made debits) and scaled to the rebate
211 | # amount credit amount.
212 | if 'rebate' in outcomes and not self.src and prevxn is not None:
213 | ratio = self.amount / prevxn.amount
214 | def scale(dst_ep):
215 | amount = (dst_ep.amount * ratio).quantize(dst_ep.amount)
216 | return Endpoint(dst_ep.account, -amount)
217 | self.src = map(scale, prevxn.dst)
218 | # handle rounding errors
219 | self.src[0].amount -= self.amount + sum(x.amount for x in self.src)
220 |
221 | # account outcomes
222 | for outcome in ['src', 'dst']:
223 | if outcome not in outcomes or getattr(self, outcome):
224 | # no outcome, or the attribute was already set
225 | continue
226 |
227 | endpoints = []
228 | highest = outcomes[outcome].highest()
229 | try:
230 | highscore = score.score(highest[0])
231 | if len(highest) == 1:
232 | if highscore >= threshold['y']:
233 | # do it
234 | endpoints = [
235 | Endpoint(score.value(highest[0]), self.amount)
236 | ]
237 | else:
238 | uio.show('Choose ' + outcome + ' for transaction:')
239 | uio.show('')
240 | uio.show(self.summary())
241 |
242 | prompt = 'Is the account {0}?'.format(
243 | score.value(highest[0])
244 | )
245 | if highscore >= threshold['y?']:
246 | default = True
247 | elif highscore >= threshold['?']:
248 | default = None
249 | else:
250 | default = False
251 | if uio.yn(prompt, default):
252 | endpoints = [
253 | Endpoint(
254 | score.value(highest[0]),
255 | self.amount
256 | )
257 | ]
258 | else:
259 | raise ui.RejectWarning('top score declined')
260 | else:
261 | # tied highest score, let user pick
262 | uio.show('Choose ' + outcome + ' for transaction:')
263 | uio.show('')
264 | uio.show(self.summary())
265 |
266 | prompt = 'Choose an account'
267 | endpoints = [
268 | Endpoint(
269 | uio.choose(prompt, map(score.value, highest)),
270 | self.amount
271 | )
272 | ]
273 |
274 | except ui.RejectWarning:
275 | # user has rejected our offer(s)
276 | uio.show("\n")
277 | uio.show('Enter ' + outcome + ' endpoints:')
278 | try:
279 | endpoints = []
280 | remaining = self.amount
281 | while remaining:
282 | uio.show('\n${0} remaining'.format(remaining))
283 | account = uio.text(
284 | ' Enter account',
285 | score.value(highest[0]) if highest else None
286 | )
287 | amount = uio.decimal(
288 | ' Enter amount',
289 | default=remaining,
290 | lower=0,
291 | upper=remaining
292 | )
293 | endpoints.append(Endpoint(account, amount))
294 | remaining = self.amount \
295 | - sum(map(lambda x: x.amount, endpoints))
296 | except ui.RejectWarning:
297 | # bail out
298 | sys.exit("bye!")
299 |
300 | # flip amounts if it was a src outcome
301 | if outcome == 'src':
302 | endpoints = map(
303 | lambda x: Endpoint(x.account, -x.amount),
304 | endpoints
305 | )
306 |
307 | # set endpoints
308 | setattr(self, outcome, endpoints)
309 |
310 | # TODO desc outcomes
311 |
312 | def complete(self, uio, dropped=False):
313 | """Query for all missing information in the transaction"""
314 | if self.dropped and not dropped:
315 | # do nothing for dropped xn, unless specifically told to
316 | return
317 |
318 | for end in ['src', 'dst']:
319 | if getattr(self, end):
320 | continue # we have this information
321 |
322 | uio.show('\nEnter ' + end + ' for transaction:')
323 | uio.show('')
324 | uio.show(self.summary())
325 | try:
326 | endpoints = []
327 | remaining = self.amount
328 | while remaining:
329 | account = uio.text(' Enter account', None)
330 | amount = uio.decimal(
331 | ' Enter amount',
332 | default=remaining,
333 | lower=0,
334 | upper=remaining
335 | )
336 | endpoints.append(Endpoint(account, amount))
337 | remaining = self.amount \
338 | - sum(map(lambda x: x.amount, endpoints))
339 | except ui.RejectWarning:
340 | # bail out
341 | sys.exit("bye!")
342 |
343 | # flip amounts if it was a src outcome
344 | if end == 'src':
345 | endpoints = map(
346 | lambda x: Endpoint(x.account, -x.amount),
347 | endpoints
348 | )
349 |
350 | # set endpoints
351 | setattr(self, end, endpoints)
352 |
353 | def process(self, rules, uio, prevxn=None):
354 | """Matches rules and applies outcomes"""
355 | self.apply_outcomes(self.match_rules(rules), uio, prevxn=prevxn)
356 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | from distutils.core import setup
2 |
3 | with open('README.rst') as file:
4 | long_description = file.read()
5 |
6 | setup(
7 | name='ledgertools',
8 | version='0.3',
9 | description='Ledger accounting system utilities',
10 | author='Fraser Tweedale',
11 | author_email='frase@frase.id.au',
12 | url='https://github.com/frasertweedale/ledgertools',
13 | packages=['ltlib', 'ltlib.readers'],
14 | scripts=['bin/lt-stmtproc', 'bin/lt-transact', 'bin/lt-chart'],
15 | data_files=[
16 | ('doc/ledgertools', ['doc/.ltconfig.sample']),
17 | ],
18 | classifiers=[
19 | 'Development Status :: 3 - Alpha',
20 | 'Environment :: Console',
21 | 'Environment :: X11 Applications :: GTK',
22 | 'Intended Audience :: End Users/Desktop',
23 | 'Intended Audience :: Financial and Insurance Industry',
24 | 'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
25 | 'Operating System :: OS Independent',
26 | 'Programming Language :: Python :: 2.7',
27 | 'Topic :: Office/Business :: Financial :: Accounting',
28 | ],
29 | long_description=long_description,
30 | )
31 |
--------------------------------------------------------------------------------