-
Notifications
You must be signed in to change notification settings - Fork 802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
guile repl access to full api #1794
Open
christopherlam
wants to merge
7
commits into
Gnucash:stable
Choose a base branch
from
christopherlam:cli-guile-repl
base: stable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
586cb68
[engine.i] qof_session_load|save_quiet
christopherlam 7add338
[gnucash-commands.cpp] refactor session loading into load_file
christopherlam c4e8d72
[gnucash-commands.cpp] scripting & REPL for guile & python
christopherlam cf2a026
[gnucash-commands.cpp] colorize cpp get_line on all platforms
christopherlam 192205c
[book-to-hledger.scm] simple script to export book to hledger format
christopherlam 6d9e6cf
[simple-book-add-txn.scm] simple script to add txn into account
christopherlam de536c9
uri supports file: mysql: and postgres:
christopherlam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
;; this file is meant to be run via the gnucash-cli interface: --script simple-book-add-txn.scm | ||
;; | ||
;; gnucash-cli book.gnucash --script simple-book-add-txn.scm | ||
;; | ||
|
||
(use-modules (gnucash core-utils)) | ||
(use-modules (gnucash engine)) | ||
(use-modules (gnucash app-utils)) | ||
(use-modules (gnucash report)) | ||
(use-modules (ice-9 match)) | ||
|
||
(define iso-date (qof-date-format-get-string QOF-DATE-FORMAT-ISO)) | ||
(define book (gnc-get-current-book)) | ||
(define root (gnc-get-current-root-account)) | ||
(define query (qof-query-create-for-splits)) | ||
(qof-query-set-book query (gnc-get-current-book)) | ||
(xaccQueryAddAccountMatch query (gnc-account-get-descendants root) QOF-GUID-MATCH-ANY QOF-QUERY-AND) | ||
(qof-query-set-sort-order | ||
query | ||
(list SPLIT-TRANS TRANS-DATE-POSTED) | ||
'() | ||
(list QUERY-DEFAULT-SORT)) | ||
|
||
(define (dump-transaction trans) | ||
(format #t "~a ~a\n" | ||
(gnc-print-time64 (xaccTransGetDate trans) iso-date) | ||
(xaccTransGetDescription trans)) | ||
(define (split->account s) | ||
(gnc-account-get-full-name (xaccSplitGetAccount s))) | ||
(define (split->amount s) | ||
(format #f "~a ~a" | ||
(exact->inexact (xaccSplitGetAmount s)) | ||
(gnc-commodity-get-mnemonic (xaccAccountGetCommodity (xaccSplitGetAccount s))))) | ||
(define max-width | ||
(let lp ((splits (xaccTransGetSplitList trans)) (maximum 0)) | ||
(match splits | ||
(() (+ maximum 2)) | ||
((s . rest) | ||
(lp rest (max maximum (+ (string-length (split->account s)) | ||
(string-length (split->amount s))))))))) | ||
(for-each | ||
(lambda (s) | ||
(define txn (xaccSplitGetParent s)) | ||
(define acc-name (split->account s)) | ||
(define amt-str (split->amount s)) | ||
(format #t " ~a~a~a\n" | ||
acc-name | ||
(make-string (- max-width (string-length acc-name) (string-length amt-str)) #\space) | ||
amt-str)) | ||
(xaccTransGetSplitList trans))) | ||
|
||
(define split-has-no-account? (compose null? xaccSplitGetAccount)) | ||
|
||
(let lp ((splits (xaccQueryGetSplitsUniqueTrans query))) | ||
(newline) | ||
(match splits | ||
(() #f) | ||
(((? split-has-no-account?) . rest) (lp rest)) | ||
((split . rest) (dump-transaction (xaccSplitGetParent split)) (lp rest)))) | ||
|
||
(qof-query-destroy query) | ||
(gnc-clear-current-session) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
;; this file is meant to be run via the gnucash-cli interface: --script simple-book-add-txn.scm | ||
;; | ||
;; gnucash-cli book.gnucash --script simple-book-add-txn.scm | ||
;; | ||
;; the book will be considered "valid" if it has a basic hierarchy such as the following | ||
;; Assets | ||
;; |-Current | ||
;; | |- Bank | ||
;; Expenses | ||
;; |-Govt | ||
;; | |- Taxes | ||
;; |-Personal | ||
;; |-Medical | ||
|
||
(use-modules (gnucash core-utils)) | ||
(use-modules (gnucash engine)) | ||
(use-modules (gnucash app-utils)) | ||
(use-modules (gnucash report)) | ||
(use-modules (ice-9 rdelim)) | ||
(use-modules (ice-9 match)) | ||
|
||
(define (get-line prompt) | ||
(format #t "\x1b[1;33m~a:\x1b[m " prompt) | ||
(let ((rv (read-line))) | ||
(if (eof-object? rv) "" rv))) | ||
|
||
(define (get-amount prompt) | ||
(let ((amount (gnc-numeric-from-string (get-line prompt)))) | ||
(if (number? amount) | ||
amount | ||
(get-amount prompt)))) | ||
|
||
(define (get-item-from-list lst elt->string prompt) | ||
(define (get-amount-line) (get-amount prompt)) | ||
(let lp ((idx 1) (lst lst)) | ||
(unless (null? lst) | ||
(format #t "~a. ~a\n" idx (elt->string (car lst))) | ||
(lp (1+ idx) (cdr lst)))) | ||
(let lp ((idx (get-amount-line))) | ||
(cond | ||
((and (integer? idx) (positive? idx)) | ||
(let lp1 ((idx (1- idx)) (lst lst)) | ||
(cond | ||
((null? lst) (lp (get-amount-line))) | ||
((zero? idx) (car lst)) | ||
(else (lp1 (1- idx) (cdr lst)))))) | ||
(else (lp (get-amount-line)))))) | ||
|
||
(define (get-account prompt parent) | ||
(define descendants (gnc-account-get-descendants-sorted parent)) | ||
(get-item-from-list descendants gnc-account-get-full-name "Select account by index")) | ||
|
||
(define (get-binary-response prompt) | ||
(match (get-line prompt) | ||
((or "Y" "y") #t) | ||
((or "N" "n") #f) | ||
(else (get-binary-response prompt)))) | ||
|
||
(define (add-to-transaction book txn account amount memo) | ||
(let ((split (xaccMallocSplit book))) | ||
(xaccSplitSetAccount split account) | ||
(xaccSplitSetAmount split amount) | ||
(xaccSplitSetValue split amount) | ||
(xaccSplitSetMemo split memo) | ||
(xaccSplitSetParent split txn))) | ||
|
||
(define (quit-program exitlevel) | ||
(gnc-clear-current-session) | ||
(exit exitlevel)) | ||
|
||
(define (get-new-uri session) | ||
(define filepath (get-line "please input correct path, or leave blank to abort")) | ||
(gnc-clear-current-session) | ||
(cond | ||
((string-null? filepath) (quit-program 1)) | ||
((qof-session-load-quiet filepath SESSION-NORMAL-OPEN) #f) ;success | ||
(else (get-new-uri session)))) | ||
|
||
(define session (gnc-get-current-session)) | ||
(define root (gnc-get-current-root-account)) | ||
|
||
(let check-book-loop () | ||
(cond | ||
((or (null? (gnc-account-lookup-by-full-name root "Assets:Current:Bank")) | ||
(null? (gnc-account-lookup-by-full-name root "Expenses")) | ||
(null? (gnc-account-lookup-by-full-name root "Expenses:Govt:Taxes"))) | ||
(display "\n\n\nWARNING: It doesn't seem the correct book is loaded.\n") | ||
(get-new-uri session) | ||
(check-book-loop)))) | ||
|
||
(define book (gnc-get-current-book)) | ||
(define acc-BANK (gnc-account-lookup-by-full-name root "Assets:Current:Bank")) | ||
(define acc-EXP (gnc-account-lookup-by-full-name root "Expenses")) | ||
(define acc-EXP-TAX (gnc-account-lookup-by-full-name root "Expenses:Govt:Taxes")) | ||
(define acc-EXP-LEAF (get-account "Expense leaf account" acc-EXP)) | ||
|
||
(define (accounts-action action-fn) | ||
(action-fn acc-BANK) | ||
(action-fn acc-EXP-LEAF) | ||
(action-fn acc-EXP-TAX)) | ||
|
||
(define description (get-line "Description")) | ||
|
||
(let lp () | ||
(define txn (xaccMallocTransaction book)) | ||
(define net-amount (get-amount "Amount, without tax")) | ||
(define tax-amount (* net-amount 1/10)) | ||
(define total-amount (+ tax-amount net-amount)) | ||
|
||
(xaccTransBeginEdit txn) | ||
(xaccTransSetCurrency txn (xaccAccountGetCommodity acc-BANK)) | ||
(xaccTransSetDatePostedSecsNormalized txn (current-time)) | ||
(xaccTransSetDescription txn description) | ||
(add-to-transaction book txn acc-BANK (- total-amount) "from bank") | ||
(add-to-transaction book txn acc-EXP-LEAF net-amount "expense net") | ||
(add-to-transaction book txn acc-EXP-TAX tax-amount "tax paid") | ||
(newline) | ||
(gnc:dump-transaction txn) | ||
|
||
(cond | ||
((not (xaccTransIsBalanced txn)) | ||
(display "WARNING: transaction is not balanced. Try again.\n") | ||
(xaccTransRollbackEdit txn) | ||
(xaccTransDestroy txn) | ||
(lp)) | ||
((get-binary-response "Please confirm transaction [YN]") | ||
(accounts-action xaccAccountBeginEdit) | ||
(xaccTransCommitEdit txn) | ||
(accounts-action xaccAccountCommitEdit)) | ||
(else | ||
(xaccTransRollbackEdit txn) | ||
(xaccTransDestroy txn)))) | ||
|
||
;; (gnc:dump-book) | ||
(when (qof-book-session-not-saved book) | ||
(display "Saving book...\n") | ||
(qof-session-save-quiet)) | ||
|
||
(quit-program 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,7 @@ target_link_libraries (gnucash | |
gnc-bi-import gnc-customer-import gnc-report | ||
PkgConfig::GTK3 ${GUILE_LDFLAGS} PkgConfig::GLIB2 | ||
${Boost_LIBRARIES} | ||
${Python3_LIBRARIES} | ||
) | ||
|
||
set(gnucash_cli_SOURCES | ||
|
@@ -137,15 +138,20 @@ endif() | |
|
||
add_dependencies (gnucash-cli gnucash) | ||
|
||
target_compile_definitions(gnucash-cli PRIVATE -DG_LOG_DOMAIN=\"gnc.bin\") | ||
target_compile_definitions(gnucash-cli PRIVATE | ||
-DG_LOG_DOMAIN=\"gnc.bin\" | ||
$<$<BOOL:${WITH_PYTHON}>:HAVE_PYTHON_H>) | ||
|
||
target_link_libraries (gnucash-cli | ||
gnc-app-utils | ||
gnc-engine gnc-core-utils gnucash-guile gnc-report | ||
${GUILE_LDFLAGS} PkgConfig::GLIB2 | ||
${Boost_LIBRARIES} | ||
${Python3_LIBRARIES} | ||
) | ||
|
||
target_include_directories (gnucash-cli PRIVATE ${Python3_INCLUDE_DIRS}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably should be in a generator expression as well. |
||
|
||
if (BUILDING_FROM_VCS) | ||
target_compile_definitions(gnucash PRIVATE -DGNC_VCS=\"git\") | ||
target_compile_definitions(gnucash-cli PRIVATE -DGNC_VCS=\"git\") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be made conditional with a generator expression as well ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok no idea how.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same way, wrap it with
$<$<BOOL:${WITH_PYTHON}>:…>
. But it's not necessary,Python3_LIBRARIES
will be empty ifWITH_PYTHON
is false.