forked from gramps-project/addons-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONImport.py
88 lines (81 loc) · 3.38 KB
/
JSONImport.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
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2013 Douglas S. Blank
# Copyright (C) 2016-2017 Nick Hall
#
# 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.
#
"Import from JSON data"
#-------------------------------------------------------------------------
#
# Standard Python Modules
#
#-------------------------------------------------------------------------
import logging
#-------------------------------------------------------------------------
#
# Gramps modules
#
#-------------------------------------------------------------------------
from gramps.gen.db import DbTxn
from gramps.gen.plug.utils import OpenFileOrStdin
from gramps.gen.lib import (Note, Person, Event, Family, Repository, Place,
Media, Source, Tag, Citation)
from gramps.gen.lib.serialize import from_json
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
#------------------------------------------------------------------------
#
# Set up logging
#
#------------------------------------------------------------------------
LOG = logging.getLogger(".ImportJSON")
def importData(db, filename, user):
"""Function called by Gramps to import data on persons in CSV format."""
db.disable_signals()
try:
with DbTxn(_("JSON import"), db, batch=True) as trans:
with OpenFileOrStdin(filename, encoding="utf-8") as fp:
line = fp.readline()
while line:
obj = from_json(line)
if isinstance(obj, Person):
db.add_person(obj, trans)
elif isinstance(obj, Family):
db.add_family(obj, trans)
elif isinstance(obj, Event):
db.add_event(obj, trans)
elif isinstance(obj, Media):
db.add_media(obj, trans)
elif isinstance(obj, Repository):
db.add_repository(obj, trans)
elif isinstance(obj, Tag):
db.add_tag(obj, trans)
elif isinstance(obj, Source):
db.add_source(obj, trans)
elif isinstance(obj, Citation):
db.add_citation(obj, trans)
elif isinstance(obj, Note):
db.add_note(obj, trans)
elif isinstance(obj, Place):
db.add_place(obj, trans)
else:
LOG.warn("ignored: " + data)
line = fp.readline()
except EnvironmentError as err:
user.notify_error(_("%s could not be opened\n") % filename, str(err))
db.enable_signals()
db.request_rebuild()