Copy/paste/create a separate person via JSON

Maybe anybody is interesting to create such addon or core feature.

The feature allows copy and paste separate persons from one Gramps app (say on virtual machine) to another Gramps app (say on host machine). Or even do this inside one app.

I made a small test of the copy feature (the “Copy to Clipboard” button):

It copies the next:

{
    "gender": 1,
    "primary_name": {
        "given": "\u0421\u0435\u043c\u0435\u043d"
    },
    "alternative_names": {},
    "notes": {},
    "attributes": {},
    "events": {}
}

Then the clipboard text could be pasted into any input and save. And a new person with all necessary data will be created. Code fragment:

   def _setup_fields(self):
        ...
        self.json_button = Gtk.Button(label="Copy to clipboard")
        self.top.get_object("vbox").pack_start(self.json_button, False, False, 0)
        self.json_button.connect("clicked", self._export_to_json)
    
   def _export_to_json(self, widget):
        # Gather person data
        primary_surname = self.pname.get_primary_surname()
        person_data = {
            "gender": self.obj.get_gender(),
            "primary_name": {
                "given": self.pname.get_first_name(),
            },
            "alternative_names": {},
            "notes": {},
            "attributes": {},
            "events": {},
        }

        # Convert to JSON
        json_data = json.dumps(person_data, indent=4)

        # Copy to clipboard
        clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        clipboard.set_text(json_data, -1)
        clipboard.store()

        # Notify user
        dialog = Gtk.MessageDialog(
            transient_for=self.window,
            flags=0,
            message_type=Gtk.MessageType.INFO,
            buttons=Gtk.ButtonsType.OK,
            text="Person is copied",
        )
        dialog.format_secondary_text(
            "The current Person data has been copied to the clipboard."
        )
        dialog.run()
        dialog.destroy()

The code above demonstrates copying only, no paste.

/usr/local/lib/python3.10/dist-packages/gramps/gui/editors$ sudo vi editperson.py
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.