GnuCash  5.6-150-g038405b370+
init.py
1 import sys
2 from gnucash import *
3 from gnucash import _sw_app_utils
4 from gnucash import _sw_core_utils
5 from gnucash._sw_core_utils import gnc_prefs_is_extra_enabled, gnc_prefs_is_debugging_enabled
6 from gi import require_version
7 require_version('Gtk', '3.0')
8 from gi.repository import Gtk
9 import os
10 
11 sys.path.append(os.path.dirname(__file__))
12 
13 # output file location if gnucash has been started with
14 # gnucash --extra
15 if gnc_prefs_is_extra_enabled():
16  print("Python shell init file: %s" % (__file__))
17  print("\n" + "The following string should appear translated in your preferred language:" + "\n")
18  print("\n" + _("Welcome to GnuCash") +"\n")
19 
20 # Importing the console class causes SIGTTOU to be thrown if GnuCash is
21 # started in the background. This causes a hang if it is not handled,
22 # so ignore it for the duration
23 import signal
24 old_sigttou = signal.signal(signal.SIGTTOU, signal.SIG_IGN)
25 
26 import pycons.console as cons
27 
28 # Restore the SIGTTOU handler
29 signal.signal(signal.SIGTTOU, old_sigttou)
30 
31 # output debug information if gnucash has been started with
32 # gnucash --debug --extra
33 if gnc_prefs_is_extra_enabled() and gnc_prefs_is_debugging_enabled():
34  print("Hello from python!\n")
35  print("sys.modules.keys(): ", sys.modules.keys(), "\n")
36  print("dir(_sw_app_utils): ", dir(_sw_app_utils), "\n")
37 
38  #session = app_utils.gnc_get_current_session()
39  #root account can later on be accessed by session.get_book().get_root_account()
40 
41  #print("test", dir(root), root.__class__)
42  print("dir(gnucash_core_c): ", dir(gnucash_core_c))
43 
44  #acct = Account(instance = root)
45 
46  #print("test3", dir(acct))
47  #print(acct.GetName())
48  #print(acct.GetBalance())
49  #print(acct.GetSplitList())
50  #print("test2", dir(gnucash.gnucash_core_c))
51 
52 class Console (cons.Console):
53  """ GTK python console """
54 
55  def __init__(self, argv=[], shelltype='python', banner=[],
56  filename=None, size=100, user_local_ns=None, user_global_ns=None):
57  cons.Console.__init__(self, argv, shelltype, banner, filename, size,
58  user_local_ns=user_local_ns, user_global_ns=user_global_ns)
59  self.buffer.create_tag('center',
60  justification=Gtk.Justification.CENTER,
61  font='Mono 4')
62  self.figures = []
63  self.callbacks = []
64  self.last_figure = None
65  self.active_canvas = None
66  self.view.connect ('key-press-event', self.key_press_event)
67  self.view.connect ('button-press-event', self.button_press_event)
68  self.view.connect ('scroll-event', self.scroll_event)
69 
70 
71  def key_press_event (self, widget, event):
72  """ Handle key press event """
73 
74  if self.active_canvas:
75  self.active_canvas.emit ('key-press-event', event)
76  return True
77  return cons.Console.key_press_event (self, widget, event)
78 
79  def scroll_event (self, widget, event):
80  """ Scroll event """
81  if self.active_canvas:
82  return True
83  return False
84 
85  def button_press_event (self, widget, event):
86  """ Button press event """
87  return self.refresh()
88 
89  def quit_event (self, widget, event):
90  """ Event handler for closing of console window """
91  return self.quit()
92 
93  def refresh (self):
94  """ Refresh drawing """
95  for fig in self.figures:
96  figure, canvas, anchor = fig
97  canvas.draw()
98  return False
99 
100  def quit (self):
101  """ quit """
102 
103  self.write("\n" + _("Have a nice day!") + "\n")
104  return super(Console, self).quit()
105 
106 
107 # Change this to "if True:" to switch on a python console at gnucash
108 # startup:
109 # shelltype can either be "python" or "ipython" (the latter is not yet fully functional)
110 if False:
111  shelltype = "python"
112  if shelltype=="python":
113  shelltypeName = "Python"
114  else:
115  shelltypeName = "IPython"
116  banner_style = 'title'
117  # TRANSLATORS: %s is either Python or IPython
118  banner = _("Welcome to GnuCash %s Shell") % shelltypeName
119  console = Console(argv = [], shelltype = shelltype, banner = [[banner, banner_style]], size = 100)
120 
121  window = Gtk.Window(type = Gtk.WindowType.TOPLEVEL)
122  window.set_position(Gtk.WindowPosition.CENTER)
123  window.set_default_size(800,600)
124  window.set_border_width(0)
125 
126  console = Console(argv = [], shelltype = shelltype, banner = [[banner, banner_style]],
127  size = 100, user_local_ns=locals(), user_global_ns=globals())
128 
129  window.connect('destroy-event', console.quit_event)
130  window.connect('delete-event', console.quit_event)
131  window.add (console)
132  window.show_all()
133  console.grab_focus()
def scroll_event(self, widget, event)
Definition: init.py:79
def refresh(self)
Definition: init.py:93
def key_press_event(self, widget, event)
Definition: init.py:71
def quit(self)
Definition: init.py:100
def quit_event(self, widget, event)
Definition: init.py:89
last_figure
Definition: init.py:64
active_canvas
Definition: init.py:65
def button_press_event(self, widget, event)
Definition: init.py:85