/
grampsapp.py
696 lines (592 loc) · 21.6 KB
/
grampsapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006 Donald N. Allingham
# Copyright (C) 2009 Benny Malengier
# Copyright (C) 2009-2010 Stephen George
# Copyright (C) 2010 Doug Blank <doug.blank@gmail.com>
# Copyright (C) 2011 Paul Franklin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# -------------------------------------------------------------------------
#
# Python modules
#
# -------------------------------------------------------------------------
import sys
import os
import signal
import logging
LOG = logging.getLogger(".")
from subprocess import Popen, PIPE
# -------------------------------------------------------------------------
# process 'safe mode'; set up for a temp directory for user data
# actual directory paths set up in const module
if "-S" in sys.argv or "--safe" in sys.argv:
from tempfile import TemporaryDirectory
tempdir = TemporaryDirectory(prefix="gramps_")
os.environ["SAFEMODE"] = tempdir.name
# -------------------------------------------------------------------------
#
# Gramps modules
#
# -------------------------------------------------------------------------
from .gen.const import APP_GRAMPS, USER_DIRLIST, USER_DATA, ORIG_HOME_DIR
from .gen.constfunc import mac
from .version import VERSION_TUPLE
from .gen.constfunc import win, get_env_var
from .gen.config import config
from .gen.errors import HandleError
# -------------------------------------------------------------------------
#
# Instantiate Localization
#
# -------------------------------------------------------------------------
from .gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
# -------------------------------------------------------------------------
#
# Ensure that output is encoded correctly to stdout and
# stderr. This is much less cumbersome and error-prone than
# encoding individual outputs:
#
# -------------------------------------------------------------------------
try:
# On Darwin sys.getdefaultencoding() is correct, on Win32 it's
# sys.stdout.encoding, and on Linux they're both right.
if mac():
_encoding = sys.getdefaultencoding()
else:
_encoding = sys.stdout.encoding
except:
_encoding = "UTF-8"
try:
sys.stdout = open(
sys.stdout.fileno(),
mode="w",
encoding=_encoding,
buffering=1,
errors="backslashreplace",
)
sys.stderr = open(
sys.stderr.fileno(),
mode="w",
encoding=_encoding,
buffering=1,
errors="backslashreplace",
)
except:
pass
# -------------------------------------------------------------------------
#
# Setup logging
#
# Ideally, this needs to be done before any Gramps modules are
# imported, so that any code that is executed as the modules are
# imported can log errors or warnings. const and constfunc have to be
# imported before this code is executed because they are used in this
# code. That unfortunately initializes GrampsLocale, so it has its own
# logging setup during initialization.
# -------------------------------------------------------------------------
"""Set up basic logging support."""
# Setup a formatter
form = logging.Formatter(
fmt="%(asctime)s.%(msecs).03d: %(levelname)s: "
"%(filename)s: line %(lineno)d: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
# Create the log handlers
if win():
# If running in GUI mode redirect stdout and stderr to log file
if not sys.stdout:
logfile = os.path.join(USER_DATA, "Gramps%s%s.log") % (
VERSION_TUPLE[0],
VERSION_TUPLE[1],
)
# We now carry out the first step in build_user_paths(), to make sure
# that the user home directory is available to store the log file. When
# build_user_paths() is called, the call is protected by a try...except
# block, and any failure will be logged. However, if the creation of the
# user directory fails here, there is no way to report the failure,
# because stdout/stderr are not available, and neither is the logfile.
if os.path.islink(USER_DATA):
pass # ok
elif not os.path.isdir(USER_DATA):
os.makedirs(USER_DATA)
sys.stdout = sys.stderr = open(logfile, "w", encoding="utf-8")
# macOS sets stderr to /dev/null when running without a terminal,
# e.g. if Gramps.app is lauched by double-clicking on it in
# finder. Write to a file instead.
if mac() and not sys.stdin.isatty():
from tempfile import gettempdir
log_file_name = "gramps-" + str(os.getpid()) + ".log"
log_file_path = os.path.join(gettempdir(), log_file_name)
log_file_handler = logging.FileHandler(log_file_path, mode="a", encoding="utf-8")
log_file_handler.setFormatter(form)
log_file_handler.setLevel(logging.DEBUG)
logger = logging.getLogger()
logger.setLevel(logging.WARNING)
logger.addHandler(log_file_handler)
else:
stderrh = logging.StreamHandler(sys.stderr)
stderrh.setFormatter(form)
stderrh.setLevel(logging.DEBUG)
# Setup the base level logger, this one gets
# everything.
l = logging.getLogger()
l.setLevel(logging.WARNING)
l.addHandler(stderrh)
def exc_hook(err_type, value, t_b):
"""put a hook on to catch any completely unhandled exceptions."""
if err_type == KeyboardInterrupt:
# Ctrl-C is not a bug.
return
if err_type == IOError:
# strange Windows logging error on close
return
if err_type == HandleError and "not found" in value.value:
# tell Gramps to run check & repair on next start
config.set("behavior.runcheck", True)
config.save()
# Use this to show variables in each frame:
# from gramps.gen.utils.debug import format_exception
import traceback
LOG.error(
"Unhandled exception\n"
+ "".join(traceback.format_exception(err_type, value, t_b))
)
sys.excepthook = exc_hook
from .gen.mime import mime_type_is_defined
# -------------------------------------------------------------------------
#
# Minimum version check
#
# -------------------------------------------------------------------------
MIN_PYTHON_VERSION = (3, 8, 0, "", 0)
if not sys.version_info >= MIN_PYTHON_VERSION:
logging.warning(
_(
"Your Python version does not meet the "
"requirements. At least Python %(v1)d.%(v2)d.%(v3)d is needed to"
" start Gramps.\n\n"
"Gramps will terminate now."
)
% {
"v1": MIN_PYTHON_VERSION[0],
"v2": MIN_PYTHON_VERSION[1],
"v3": MIN_PYTHON_VERSION[2],
}
)
sys.exit(1)
# -------------------------------------------------------------------------
#
# Gramps libraries
#
# -------------------------------------------------------------------------
try:
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
except:
pass
args = sys.argv
def build_user_paths():
"""check/make user-dirs on each Gramps session"""
for path in USER_DIRLIST:
if os.path.islink(path):
pass # ok
elif not os.path.isdir(path):
os.makedirs(path)
def show_settings():
"""
Shows settings of all of the major components.
"""
py_str = "%d.%d.%d" % sys.version_info[:3]
try:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
try:
gtkver_str = "%d.%d.%d" % (
Gtk.get_major_version(),
Gtk.get_minor_version(),
Gtk.get_micro_version(),
)
except: # any failure to 'get' the version
gtkver_str = "unknown version"
except (ImportError, ValueError):
gtkver_str = "not found"
# no DISPLAY is a RuntimeError in an older pygtk (e.g. 2.17 in Fedora 14)
except RuntimeError:
gtkver_str = "DISPLAY not set"
# exept TypeError: To handle back formatting on version split
try:
from gi.repository import GObject
try:
pygobjectver_str = "%d.%d.%d" % GObject.pygobject_version
except: # any failure to 'get' the version
pygobjectver_str = "unknown version"
except ImportError:
pygobjectver_str = "not found"
try:
from gi.repository import Pango
try:
pangover_str = Pango.version_string()
except: # any failure to 'get' the version
pangover_str = "unknown version"
except ImportError:
pangover_str = "not found"
try:
import cairo
try:
pycairover_str = "%d.%d.%d" % cairo.version_info
cairover_str = cairo.cairo_version_string()
except: # any failure to 'get' the version
pycairover_str = "unknown version"
cairover_str = "unknown version"
except ImportError:
pycairover_str = "not found"
cairover_str = "not found"
try:
gi.require_version("PangoCairo", "1.0")
from gi.repository import PangoCairo
pangocairo_str = PangoCairo._version
except ImportError:
pangocairo_str = _("not found")
try:
from gi import Repository
repository = Repository.get_default()
if repository.enumerate_versions("OsmGpsMap"):
import gi
gi.require_version("OsmGpsMap", "1.0")
from gi.repository import OsmGpsMap as osmgpsmap
try:
osmgpsmap_str = osmgpsmap._version
except: # any failure to 'get' the version
osmgpsmap_str = "unknown version"
else:
osmgpsmap_str = "not found"
except ImportError:
osmgpsmap_str = "not found"
try:
import gi
repository = gi.Repository.get_default()
if repository.enumerate_versions("GExiv2"):
gi.require_version("GExiv2", "0.10")
from gi.repository import GExiv2
try:
gexiv2_str = GExiv2._version
except: # any failure to 'get' the version
gexiv2_str = "unknown version"
else:
gexiv2_str = "not found"
except ImportError:
gexiv2_str = "not found"
except ValueError:
gexiv2_str = "not new enough"
try:
vers_str = Popen(["exiv2", "-V"], stdout=PIPE).communicate(input=None)[0]
try:
if isinstance(vers_str, bytes) and sys.stdin.encoding:
vers_str = vers_str.decode(sys.stdin.encoding)
indx = vers_str.find("exiv2 ") + 6
vers_str = vers_str[indx : indx + 4]
except Exception:
vers_str = _("not found")
except Exception:
vers_str = _("not found because exiv2 is not installed")
try:
import PyICU
try:
pyicu_str = PyICU.VERSION
icu_str = PyICU.ICU_VERSION
except: # any failure to 'get' the version
pyicu_str = "unknown version"
icu_str = "unknown version"
except ImportError:
pyicu_str = "not found"
icu_str = "not found"
try:
import icu
icu_str = icu.PY_VERSION
except Exception:
icu_str = _("not found")
try:
import PIL
try:
pil_ver = PIL.__version__
except Exception:
try:
pil_ver = str(PIL.PILLOW_VERSION)
except Exception:
try:
# print(dir(PIL))
pil_ver = str(PIL.VERSION)
except Exception:
pil_ver = _("Installed but does not supply version")
except ImportError:
pil_ver = _("not found")
def verstr(nums):
return ".".join(str(num) for num in nums)
try:
gi.require_version("Gspell", "1")
from gi.repository import Gspell
gspell_ver = str(Gspell._version)
except Exception:
gspell_ver = _("not found")
# RCS_MIN_VER = (5, 9, 4)
# rcs_ver_str = verstr(RCS_MIN_VER)
# print("os.environ: %s " % os.environ)
try:
if win():
_RCS_FOUND = os.system("rcs -V >nul 2>nul") == 0
rcs_ver = _("installed")
# print("rcs -V : " + os.system("rcs -V"))
if _RCS_FOUND and "TZ" not in os.environ:
# RCS requires the "TZ" variable be set.
os.environ["TZ"] = str(time.timezone)
else:
import subprocess
import re
# print("xx rcs -V : " + os.system("rcs --version"))
_RCS_FOUND = str(subprocess.check_output(["rcs", "--version"]))
version = re.compile(".*GNU RCS\) ([0-9]+\.[0-9]+\.[0-9]+).*")
rcs_ver = version.findall(_RCS_FOUND)[0]
except Exception:
rcs_ver = _("not found")
try:
gi.require_version("GeocodeGlib", "1.0")
from gi.repository import GeocodeGlib
geocodeglib_ver = str(GeocodeGlib._version)
except Exception:
geocodeglib_ver = _("not found")
try:
import bsddb3 as bsddb
bsddb_str = bsddb.__version__
bsddb_db_str = (
str(bsddb.db.version()).replace(", ", ".").replace("(", "").replace(")", "")
)
bsddb_location_str = bsddb.__file__
except:
try:
import berkeleydb as bsddb
bsddb_str = bsddb.__version__
bsddb_db_str = (
str(bsddb.db.version())
.replace(", ", ".")
.replace("(", "")
.replace(")", "")
)
bsddb_location_str = bsddb.__file__
except:
bsddb_str = "not found"
bsddb_db_str = "not found"
bsddb_location_str = "not found"
try:
import sqlite3
sqlite3_py_version_str = sqlite3.version
sqlite3_version_str = sqlite3.sqlite_version
sqlite3_location_str = sqlite3.__file__
except:
sqlite3_version_str = "not found"
sqlite3_py_version_str = "not found"
sqlite3_location_str = "not found"
try:
from .gen.const import VERSION
gramps_str = VERSION
except:
gramps_str = "not found"
if hasattr(os, "uname"):
kernel = os.uname()[2]
else:
kernel = None
lang_str = get_env_var("LANG", "not set")
language_str = get_env_var("LANGUAGE", "not set")
grampsi18n_str = get_env_var("GRAMPSI18N", "not set")
grampshome_str = get_env_var("GRAMPSHOME", "not set")
grampsdir_str = get_env_var("GRAMPSDIR", "not set")
gramps_resources_str = get_env_var("GRAMPS_RESOURCES", "not set")
try:
dotversion_str = Popen(["dot", "-V"], stderr=PIPE).communicate(input=None)[1]
if isinstance(dotversion_str, bytes) and sys.stdin.encoding:
dotversion_str = dotversion_str.decode(sys.stdin.encoding)
if dotversion_str:
dotversion_str = dotversion_str.replace("\n", "")[23:27]
except:
dotversion_str = "Graphviz not in system PATH"
try:
if win():
try:
gsversion_str = Popen(
["gswin32c", "--version"], stdout=PIPE
).communicate(input=None)[0]
except:
gsversion_str = Popen(
["gswin64c", "--version"], stdout=PIPE
).communicate(input=None)[0]
else:
gsversion_str = Popen(["gs", "--version"], stdout=PIPE).communicate(
input=None
)[0]
if isinstance(gsversion_str, bytes) and sys.stdin.encoding:
gsversion_str = gsversion_str.decode(sys.stdin.encoding)
if gsversion_str:
gsversion_str = gsversion_str.replace("\n", "")
except:
gsversion_str = "Ghostscript not in system PATH"
os_path = get_env_var("PATH", "not set")
os_path = os_path.split(os.pathsep)
print("Gramps Settings:")
print("----------------")
print(" gramps : %s" % gramps_str)
print(" o.s. : %s" % sys.platform)
if kernel:
print(" kernel : %s" % kernel)
print("")
print("Required:")
print("---------")
print(" Python : %s" % py_str)
print(" Gtk++ : %s" % gtkver_str)
print(" pygobject : %s" % pygobjectver_str)
print(" Cairo : %s" % cairover_str)
print(" pycairo : %s" % pycairover_str)
print(" Pango : %s" % pangover_str)
print(" PangoCairo: %s" % pangocairo_str)
print("")
print("Recommended:")
print("------------")
print(" osmgpsmap : %s" % osmgpsmap_str)
print(" Graphviz : %s" % dotversion_str)
print(" Ghostscr. : %s" % gsversion_str)
print(" ICU : %s" % icu_str)
print(" PyICU : %s" % pyicu_str)
print("")
print("Optional:")
print("---------")
print(" Gspell :", gspell_ver)
print(" RCS :", rcs_ver)
print(" PILLOW :", pil_ver)
print(" GExiv2 : %s" % gexiv2_str)
print(" Exiv2 lib. : %s" % vers_str)
print(" geocodeglib: %s" % geocodeglib_ver)
print("")
print("Environment settings:")
print("---------------------")
print(" LANG : %s" % lang_str)
print(" LANGUAGE : %s" % language_str)
print(" GRAMPSI18N: %s" % grampsi18n_str)
print(" GRAMPSHOME: %s" % grampshome_str)
print(" GRAMPSDIR : %s" % grampsdir_str)
if __debug__:
print(" GRAMPS_RESOURCES : %s" % gramps_resources_str)
print(" PYTHONPATH:")
for folder in sys.path:
print(" ", folder)
print("")
print("System PATH env variable:")
print("-------------------------")
for folder in os_path:
print(" ", folder)
print("")
print("Databases:")
print("-------------------------")
print(" bsddb :")
print(" version : %s" % bsddb_str)
print(" db version : %s" % bsddb_db_str)
print(" location : %s" % bsddb_location_str)
print(" sqlite3 :")
print(" version : %s" % sqlite3_version_str)
print(" py version : %s" % sqlite3_py_version_str)
print(" location : %s" % sqlite3_location_str)
print("")
def run():
error = []
try:
build_user_paths()
except OSError as msg:
error += [(_("Configuration error:"), str(msg))]
return error
except msg:
LOG.error("Could not read configuration.", exc_info=True)
return [(_("Could not read configuration"), str(msg))]
if not mime_type_is_defined(APP_GRAMPS):
error += [
(
_("Configuration error:"),
_(
"A definition for the media type %s could not "
"be found \n\n Possibly the installation of Gramps "
"was incomplete. Make sure something to handle the "
"media types of Gramps is installed."
)
% APP_GRAMPS,
)
]
# we start with parsing the arguments to determine if we have a cli or a
# gui session
if "-v" in sys.argv or "--version" in sys.argv:
show_settings()
return error
from .cli.argparser import ArgParser
argv_copy = sys.argv[:]
argpars = ArgParser(argv_copy)
# if in safe mode we should point the db dir back to the original dir.
# It is ok to import config here, 'Defaults' command had its chance...
if "SAFEMODE" in os.environ:
config.set("database.path", os.path.join(ORIG_HOME_DIR, "grampsdb"))
# On windows the fontconfig handler is a better choice
if win() and ("PANGOCAIRO_BACKEND" not in os.environ):
os.environ["PANGOCAIRO_BACKEND"] = "fontconfig"
# Calls to LOG must be after setup_logging() and ArgParser()
LOG = logging.getLogger(".locale")
LOG.debug("Encoding: %s", glocale.encoding)
LOG.debug("Translating Gramps to %s", glocale.language[0])
LOG.debug("Collation Locale: %s", glocale.collation)
LOG.debug("Date/Time Locale: %s", glocale.calendar)
if "LANG" in os.environ:
LOG.debug("Using LANG: %s" % get_env_var("LANG"))
else:
LOG.debug("environment: LANG is not defined")
if "LANGUAGE" in os.environ:
LOG.debug("Using LANGUAGE: %s" % get_env_var("LANGUAGE"))
else:
LOG.debug("environment: LANGUAGE is not defined")
if argpars.need_gui():
LOG.debug("A GUI is needed. Set one up.")
try:
from .gui.grampsgui import startgramps
# no DISPLAY is a RuntimeError in an older pygtk (e.g. 2.17 in Fedora 14)
except RuntimeError as msg:
error += [(_("Configuration error:"), str(msg))]
return error
startgramps(error, argpars)
else:
# CLI use of Gramps
argpars.print_help()
argpars.print_usage()
from .cli.grampscli import startcli
startcli(error, argpars)
def main():
if "GRAMPS_RESOURCES" not in os.environ:
resource_path, filename = os.path.split(os.path.abspath(__file__))
resource_path, dirname = os.path.split(resource_path)
os.environ["GRAMPS_RESOURCES"] = resource_path
errors = run()
if errors and isinstance(errors, list):
for error in errors:
logging.warning(error[0] + error[1])
sys.stdout.close()
sys.stderr.close()
if __name__ == "__main__":
main()