Source code for accounting.gtkclient

import sys
import logging
import threading
import pkg_resources

from functools import wraps
from datetime import datetime

from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import GObject

from accounting.client import Client

_log = logging.getLogger(__name__)


[docs]def indicate_activity(func_or_str): description = None def decorator(func): @wraps(func) def wrapper(self, *args, **kw): self.status_description.set_text(description) self.activity_indicator.show() self.activity_indicator.start() return func(self, *args, **kw) return wrapper if callable(func_or_str): description = 'Working' return decorator(func_or_str) else: description = func_or_str return decorator
[docs]def indicate_activity_done(func): @wraps(func) def wrapper(self, *args, **kw): self.status_description.set_text('') self.activity_indicator.stop() self.activity_indicator.hide() return func(self, *args, **kw) return wrapper
[docs]class AccountingApplication: def __init__(self): #Gtk.Window.__init__(self, title='Accounting Client') self.client = Client() self.load_ui(pkg_resources.resource_filename( 'accounting', 'res/client-ui.glade')) self.aboutdialog.set_transient_for(self.accounting_window) self.accounting_window.connect('delete-event', Gtk.main_quit) self.accounting_window.set_border_width(0) self.accounting_window.set_default_geometry(640, 360) self.accounting_window.show_all()
[docs] def load_ui(self, path): _log.debug('Loading UI...') builder = Gtk.Builder() builder.add_from_file(path) builder.connect_signals(self) for element in builder.get_objects(): _log.debug('Loaded %s', Gtk.Buildable.get_name(element)) setattr(self, Gtk.Buildable.get_name(element), element) _log.debug('UI loaded')
[docs] def on_transaction_selected(self, widget): selection = self.transaction_view.get_selection() selection.set_mode(Gtk.SelectionMode.SINGLE) xact_store, xact_iter = selection.get_selected() xact_id = xact_store.get_value(xact_iter, 0) _log.debug('selection: %s', xact_id) for transaction in self.transaction_data: if transaction.id == xact_id: self.lbl_payee.set_text(transaction.payee) self.posting_store.clear() for posting in transaction.postings: self.posting_store.append([ posting.account, str(posting.amount.amount), posting.amount.symbol ]) self.detail_view.show() break
[docs] def on_show_about_activate(self, widget): _log.debug('Showing About') self.aboutdialog.show_all()
[docs] def on_aboutdialog_close(self, widget): _log.debug('Closing About') self.aboutdialog.hide_all()
@indicate_activity('Refreshing Transactions')
[docs] def on_refresh_transactions_activate(self, widget): def load_transactions(): transactions = self.client.get_register() GLib.idle_add(self.on_transactions_loaded, transactions) threading.Thread(target=load_transactions).start()
@indicate_activity_done
[docs] def on_transactions_loaded(self, transactions): _log.debug('transactions: %s', transactions) self.transaction_data = transactions self.transaction_store.clear() for transaction in transactions: self.transaction_store.append([ transaction.id, transaction.date.strftime('%Y-%m-%d'), transaction.payee ])
[docs]def main(argv=None): logging.basicConfig(level=logging.DEBUG) GObject.threads_init() accounting = AccountingApplication() #accounting_win.connect('delete-event', Gtk.main_quit) Gtk.main()
if __name__ == '__main__': sys.exit(main())