|
| 1 | +"""Interactive tree tool.""" |
| 2 | + |
| 3 | +from tempfile import mkstemp |
| 4 | +from threading import Thread |
| 5 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 6 | + |
| 7 | +from gramps.plugins.export.exportgedcom import GedcomWriter |
| 8 | +from gramps.gui.user import User |
| 9 | +from gramps.gui.display import display_url |
| 10 | +from gramps.gui.plug import tool |
| 11 | + |
| 12 | +# Port to start the HTTP server. |
| 13 | +HTTP_PORT = 8156 |
| 14 | + |
| 15 | +class TopolaServer(Thread): |
| 16 | + """Server serving the Gramps database in GEDCOM format. |
| 17 | +
|
| 18 | + This class is used as a singleton. |
| 19 | + """ |
| 20 | + def __init__(self): |
| 21 | + Thread.__init__(self) |
| 22 | + self.daemon = True |
| 23 | + self.path = None |
| 24 | + |
| 25 | + def set_path(self, path): |
| 26 | + """Sets a new path of the GEDCOM to be served.""" |
| 27 | + self.path = path |
| 28 | + |
| 29 | + def run(self): |
| 30 | + server = self |
| 31 | + |
| 32 | + class Handler(BaseHTTPRequestHandler): |
| 33 | + def do_GET(self): |
| 34 | + """Serves the GEDCOM file.""" |
| 35 | + self.send_response(200) |
| 36 | + self.send_header('Access-Control-Allow-Origin', '*') |
| 37 | + self.end_headers() |
| 38 | + content = open(server.path, 'rb').read() |
| 39 | + self.wfile.write(content) |
| 40 | + |
| 41 | + server_address = ('127.0.0.1', HTTP_PORT) |
| 42 | + httpd = HTTPServer(server_address, Handler) |
| 43 | + httpd.serve_forever() |
| 44 | + |
| 45 | +class Topola(tool.Tool): |
| 46 | + """Gramps tool that opens an interactive tree in the browser. |
| 47 | +
|
| 48 | + Starts a HTTP server that serves the data in GEDCOM format, then opens |
| 49 | + Topola Genealogy Viewer at https://pewu.github.io/topola-viewer pointing |
| 50 | + to the local server to get data. The online viewer does not send any data |
| 51 | + to a remote server. All data is only kept in the browser. |
| 52 | + """ |
| 53 | + server = None |
| 54 | + def __init__(self, dbstate, user, options_class, name, callback=None): |
| 55 | + tool.Tool.__init__(self, dbstate, options_class, name) |
| 56 | + if not dbstate.db: |
| 57 | + return |
| 58 | + |
| 59 | + if not Topola.server: |
| 60 | + Topola.server = TopolaServer() |
| 61 | + Topola.server.start() |
| 62 | + |
| 63 | + temp_file = mkstemp()[1] |
| 64 | + ged_writer = GedcomWriter(dbstate.db, User()) |
| 65 | + ged_writer.write_gedcom_file(temp_file) |
| 66 | + Topola.server.set_path(temp_file) |
| 67 | + display_url( |
| 68 | + 'https://pewu.github.io/topola-viewer/#/view' + |
| 69 | + '?utm_source=gramps&handleCors=false&standalone=false' + |
| 70 | + '&url=http://127.0.0.1:{}/'.format(HTTP_PORT)) |
| 71 | + |
| 72 | +class TopolaOptions(tool.ToolOptions): |
| 73 | + """Empty options class.""" |
0 commit comments