def create_event(self, description=_("Estimated date"),
                  type=None, date=None, source=None,
                  note_text="", modifier=None):
     event = Event()
     event.set_description(description)
     note = Note()
     note.handle = create_id()
     note.type.set(NoteType.EVENT)
     note.set(note_text)
     self.db.add_note(note, self.trans)
     event.add_note(note.handle)
     if type:
         event.set_type(EventType(type))
     if date:
         if modifier:
             date.set_modifier(modifier)
         date.set_quality(Date.QUAL_ESTIMATED)
         date.set_yr_mon_day(date.get_year(), 0, 0)
         event.set_date_object(date)
     if source:
         citation = Citation()
         citation.set_reference_handle(source.get_handle())
         self.db.add_citation(citation, self.trans)
         event.add_citation(citation.get_handle())
         self.db.commit_source(source, self.trans)
     self.db.add_event(event, self.trans)
     return event
Ejemplo n.º 2
0
    def instance_from_struct(cls, struct):
        """
        Given a struct with metadata, create a Gramps object.

        self is class when called as a classmethod.
        """
        from gramps.gen.lib import (Person, Family, Event, Source, Place,
                                    Citation, Repository, Media, Note, Tag,
                                    Date)
        if isinstance(struct, dict):
            if "_class" in struct.keys():
                if struct["_class"] == "Person":
                    return Person.create(Person.from_struct(struct))
                elif struct["_class"] == "Family":
                    return Family.create(Family.from_struct(struct))
                elif struct["_class"] == "Event":
                    return Event.create(Event.from_struct(struct))
                elif struct["_class"] == "Source":
                    return Source.create(Source.from_struct(struct))
                elif struct["_class"] == "Place":
                    return Place.create(Place.from_struct(struct))
                elif struct["_class"] == "Citation":
                    return Citation.create(Citation.from_struct(struct))
                elif struct["_class"] == "Repository":
                    return Repository.create(Repository.from_struct(struct))
                elif struct["_class"] == "Media":
                    return Media.create(Media.from_struct(struct))
                elif struct["_class"] == "Note":
                    return Note.create(Note.from_struct(struct))
                elif struct["_class"] == "Tag":
                    return Tag.create(Tag.from_struct(struct))
                elif struct["_class"] == "Date":
                    return Date().unserialize(
                        Date.from_struct(struct, full=True))
        raise AttributeError("invalid struct: %s" % struct)
Ejemplo n.º 3
0
def from_struct(struct):
    """
    Given a struct with metadata, create a Gramps object.
    """
    from  gramps.gen.lib import (Person, Family, Event, Source, Place, Citation,
                                 Repository, MediaObject, Note, Tag)
    if isinstance(struct, dict):
        if "_class" in struct.keys():
            if struct["_class"] == "Person":
                return Person.create(Person.from_struct(struct))
            elif struct["_class"] == "Family":
                return Family.create(Family.from_struct(struct))
            elif struct["_class"] == "Event":
                return Event.create(Event.from_struct(struct))
            elif struct["_class"] == "Source":
                return Source.create(Source.from_struct(struct))
            elif struct["_class"] == "Place":
                return Place.create(Place.from_struct(struct))
            elif struct["_class"] == "Citation":
                return Citation.create(Citation.from_struct(struct))
            elif struct["_class"] == "Repository":
                return Repository.create(Repository.from_struct(struct))
            elif struct["_class"] == "MediaObject":
                return MediaObject.create(MediaObject.from_struct(struct))
            elif struct["_class"] == "Note":
                return Note.create(Note.from_struct(struct))
            elif struct["_class"] == "Tag":
                return Tag.create(Tag.from_struct(struct))
    raise AttributeError("invalid struct: %s" % struct)
Ejemplo n.º 4
0
    def instance_from_struct(cls, struct):
        """
        Given a struct with metadata, create a Gramps object.

        self is class when called as a classmethod.
        """
        from  gramps.gen.lib import (Person, Family, Event, Source, Place, Citation,
                                     Repository, Media, Note, Tag, Date)
        if isinstance(struct, dict):
            if "_class" in struct.keys():
                if struct["_class"] == "Person":
                    return Person.create(Person.from_struct(struct))
                elif struct["_class"] == "Family":
                    return Family.create(Family.from_struct(struct))
                elif struct["_class"] == "Event":
                    return Event.create(Event.from_struct(struct))
                elif struct["_class"] == "Source":
                    return Source.create(Source.from_struct(struct))
                elif struct["_class"] == "Place":
                    return Place.create(Place.from_struct(struct))
                elif struct["_class"] == "Citation":
                    return Citation.create(Citation.from_struct(struct))
                elif struct["_class"] == "Repository":
                    return Repository.create(Repository.from_struct(struct))
                elif struct["_class"] == "Media":
                    return Media.create(Media.from_struct(struct))
                elif struct["_class"] == "Note":
                    return Note.create(Note.from_struct(struct))
                elif struct["_class"] == "Tag":
                    return Tag.create(Tag.from_struct(struct))
                elif struct["_class"] == "Date":
                    return Date().unserialize(Date.from_struct(struct, full=True))
        raise AttributeError("invalid struct: %s" % struct)
 def create_event(self, description=_("Estimated date"),
                  type=None, date=None, source=None,
                  note_text="", modifier=None):
     event = Event()
     event.set_description(description)
     note = Note()
     note.handle = create_id()
     note.type.set(NoteType.EVENT)
     note.set(note_text)
     self.db.add_note(note, self.trans)
     event.add_note(note.handle)
     if type:
         event.set_type(EventType(type))
     if date:
         if modifier:
             date.set_modifier(modifier)
         date.set_quality(Date.QUAL_ESTIMATED)
         date.set_yr_mon_day(date.get_year(), 0, 0)
         event.set_date_object(date)
     if source:
         citation = Citation()
         citation.set_reference_handle(source.get_handle())
         self.db.add_citation(citation, self.trans)
         event.add_citation(citation.get_handle())
         self.db.commit_source(source, self.trans)
     self.db.add_event(event, self.trans)
     return event
Ejemplo n.º 6
0
def from_struct(struct):
    """
    Given a struct with metadata, create a Gramps object.
    """
    from gramps.gen.lib import (Person, Family, Event, Source, Place, Citation,
                                Repository, MediaObject, Note, Tag)
    if isinstance(struct, dict):
        if "_class" in struct.keys():
            if struct["_class"] == "Person":
                return Person.create(Person.from_struct(struct))
            elif struct["_class"] == "Family":
                return Family.create(Family.from_struct(struct))
            elif struct["_class"] == "Event":
                return Event.create(Event.from_struct(struct))
            elif struct["_class"] == "Source":
                return Source.create(Source.from_struct(struct))
            elif struct["_class"] == "Place":
                return Place.create(Place.from_struct(struct))
            elif struct["_class"] == "Citation":
                return Citation.create(Citation.from_struct(struct))
            elif struct["_class"] == "Repository":
                return Repository.create(Repository.from_struct(struct))
            elif struct["_class"] == "MediaObject":
                return MediaObject.create(MediaObject.from_struct(struct))
            elif struct["_class"] == "Note":
                return Note.create(Note.from_struct(struct))
            elif struct["_class"] == "Tag":
                return Tag.create(Tag.from_struct(struct))
    raise AttributeError("invalid struct: %s" % struct)
Ejemplo n.º 7
0
 def object_extend(self, obj: Citation, args: Dict) -> Citation:
     """Extend citation attributes as needed."""
     if "extend" in args:
         db_handle = self.db_handle
         obj.extended = get_extended_attributes(db_handle, obj, args)
         if "all" in args["extend"] or "source" in args["extend"]:
             obj.extended["source"] = get_source_by_handle(
                 db_handle, obj.source_handle, args)
     return obj
Ejemplo n.º 8
0
 def citation_sort_date(self, data):
     if data[COLUMN_DATE]:
         citation = Citation()
         citation.unserialize(data)
         retval = "%09d" % citation.get_date_object().get_sort_value()
         if not get_date_valid(citation):
             return INVALID_DATE_FORMAT % retval
         else:
             return retval
     return ''
Ejemplo n.º 9
0
 def citation_sort_date(self, data):
     if data[COLUMN_DATE]:
         citation = Citation()
         citation.unserialize(data)
         retval = "%09d" % citation.get_date_object().get_sort_value()
         if not get_date_valid(citation):
             return INVALID_DATE_FORMAT % retval
         else:
             return retval
     return ''
Ejemplo n.º 10
0
 def citation_date(self, data):
     if data[COLUMN_DATE]:
         citation = Citation()
         citation.unserialize(data)
         date_str = get_date(citation)
         if date_str != "":
             retval = escape(date_str)
         if not get_date_valid(citation):
             return INVALID_DATE_FORMAT % retval
         else:
             return retval
     return ''
Ejemplo n.º 11
0
 def citation_date(self, data):
     if data[COLUMN_DATE]:
         citation = Citation()
         citation.unserialize(data)
         date_str =  get_date(citation)
         if date_str != "":
             retval = escape(date_str)
         if not get_date_valid(citation):
             return INVALID_DATE_FORMAT % retval
         else:
             return retval
     return ''
Ejemplo n.º 12
0
 def object_extend(self,
                   obj: Citation,
                   args: Dict,
                   locale: GrampsLocale = glocale) -> Citation:
     """Extend citation attributes as needed."""
     if "profile" in args:
         obj.profile = get_citation_profile_for_object(
             self.db_handle, obj, args["profile"])
     if "extend" in args:
         obj.extended = get_extended_attributes(self.db_handle, obj, args)
         if "all" in args["extend"] or "source_handle" in args["extend"]:
             obj.extended["source"] = get_source_by_handle(
                 self.db_handle, obj.source_handle, args)
     return obj
Ejemplo n.º 13
0
 def __new_form(self, widget):
     """
     Create a new form and invoke the editor.
     """
     sel = SelectForm(self.dbstate, self.uistate, [])
     source_handle = sel.run()
     if source_handle:
         citation = Citation()
         citation.set_reference_handle(source_handle)
         try:
             EditForm(self.gui.dbstate, self.gui.uistate, [], citation,
                      self.update)
         except WindowActiveError:
             pass
Ejemplo n.º 14
0
    def on_ok_clicked(self):
        """
        Method that is run when you click the OK button. The numbers of sources
        and citations are retrieved from the entry box and used to govern the
        amount of data generated
        """

        num_sources_text = self.sources_entry.get_text()
        try:
            num_sources = int(num_sources_text)
        except:
            return
        num_citations_text = self.citations_entry.get_text()
        num_citations = int(num_citations_text)

        self.progress = ProgressMeter(
            'Generating data', '', parent=self.uistate.window)
        self.progress.set_pass('Generating data',
                               num_sources*num_citations)
        LOG.debug("sources %04d citations %04d" % (num_sources,
                                                     num_citations))

        source = Source()
        citation = Citation()

        self.db.disable_signals()
        with DbTxn('Populate sources and citations', self.db) as trans:
            for i in range(num_sources):
                source.gramps_id = None
                source.handle = None
                source.title = "Source %04d" % (i + 1)
                source_handle = self.db.add_source(source, trans)

                for j in range(num_citations):
                    citation.gramps_id = None
                    citation.handle = None
                    citation.source_handle = source_handle
                    citation.page = "Page %04d" % (j + 1)
                    self.db.add_citation(citation, trans)
                    self.progress.step()
            LOG.debug("sources and citations added")
        self.db.enable_signals()
        self.db.request_rebuild()
        self.progress.close()

        self.options.handler.options_dict['sources'] = num_sources
        self.options.handler.options_dict['citations'] = num_citations
        # Save options
        self.options.handler.save_options()
Ejemplo n.º 15
0
 def get_or_create_source(self,source_name):
     source = None
     if source_name in self.skeys:
         source = self.db.get_source_from_handle(self.skeys[source_name])
     else:
         source = Source()
         source.set_title(source_name)
         self.db.add_source(source,self.trans)
         self.db.commit_source(source,self.trans)
         self.skeys[source_name] = source.get_handle()
     citation = Citation()
     citation.set_reference_handle(source.get_handle())
     self.db.add_citation(citation, self.trans)
     self.db.commit_citation(citation, self.trans)
     return citation
Ejemplo n.º 16
0
 def get_or_create_source(self, source_name):
     source = None
     if source_name in self.skeys:
         source = self.db.get_source_from_handle(self.skeys[source_name])
     else:
         source = Source()
         source.set_title(source_name)
         self.db.add_source(source, self.trans)
         self.db.commit_source(source, self.trans)
         self.skeys[source_name] = source.get_handle()
     citation = Citation()
     citation.set_reference_handle(source.get_handle())
     self.db.add_citation(citation, self.trans)
     self.db.commit_citation(citation, self.trans)
     return citation
Ejemplo n.º 17
0
    def empty_object(self):
        """
        Return an empty Citation object for comparison for changes.

        It is used by the base class L{EditPrimary}.
        """
        return Citation()
Ejemplo n.º 18
0
    def share_button_clicked(self, obj):
        SelectCitation = SelectorFactory('Citation')

        sel = SelectCitation(self.dbstate, self.uistate, self.track)
        objct = sel.run()
        LOG.debug("selected object: %s" % objct)
        # the object returned should either be a Source or a Citation
        if objct:
            if isinstance(objct, Source):
                try:
                    from .. import EditCitation
                    EditCitation(self.dbstate, self.uistate, self.track,
                                 Citation(), objct,
                                 callback=self.add_callback,
                                 callertitle=self.callertitle)
                except WindowActiveError:
                    from ...dialog import WarningDialog
                    WarningDialog(_("Cannot share this reference"),
                                  self.__blocked_text(),
                                  parent=self.uistate.window)
            elif isinstance(objct, Citation):
                try:
                    from .. import EditCitation
                    EditCitation(self.dbstate, self.uistate, self.track,
                                 objct, callback=self.add_callback,
                                 callertitle=self.callertitle)
                except WindowActiveError:
                    from ...dialog import WarningDialog
                    WarningDialog(_("Cannot share this reference"),
                                  self.__blocked_text(),
                                  parent=self.uistate.window)
            else:
                raise ValueError("selection must be either source or citation")
Ejemplo n.º 19
0
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:
                    data = json.loads(line)
                    if data["_class"] == "Person":
                        obj = Person.create(Person.from_struct(data))
                        db.add_person(obj, trans)
                    elif data["_class"] == "Family":
                        obj = Family.create(Family.from_struct(data))
                        db.add_family(obj, trans)
                    elif data["_class"] == "Event":
                        obj = Event.create(Event.from_struct(data))
                        db.add_event(obj, trans)
                    elif data["_class"] == "Media":
                        obj = Media.create(Media.from_struct(data))
                        db.add_media(obj, trans)
                    elif data["_class"] == "Repository":
                        obj = Repository.create(Repository.from_struct(data))
                        db.add_repository(obj, trans)
                    elif data["_class"] == "Tag":
                        obj = Tag.create(Tag.from_struct(data))
                        db.add_tag(obj, trans)
                    elif data["_class"] == "Source":
                        obj = Source.create(Source.from_struct(data))
                        db.add_source(obj, trans)
                    elif data["_class"] == "Citation":
                        obj = Citation.create(Citation.from_struct(data))
                        db.add_citation(obj, trans)
                    elif data["_class"] == "Note":
                        obj = Note.create(Note.from_struct(data))
                        db.add_note(obj, trans)
                    elif data["_class"] == "Place":
                        obj = Place.create(Place.from_struct(data))
                        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()
Ejemplo n.º 20
0
    def _add_source(self, repos=None):
        # Add a Source

        with DbTxn("Add Source and Citation", self._db) as tran:
            source = Source()
            if repos is not None:
                repo_ref = RepoRef()
                repo_ref.set_reference_handle(repos.get_handle())
                source.add_repo_reference(repo_ref)
            self._db.add_source(source, tran)
            self._db.commit_source(source, tran)
            citation = Citation()
            citation.set_reference_handle(source.get_handle())
            self._db.add_citation(citation, tran)
            self._db.commit_citation(citation, tran)

        return citation
Ejemplo n.º 21
0
    def _add_source(self,repos=None):
        # Add a Source

        with DbTxn("Add Source and Citation", self._db) as tran:
            source = Source()
            if repos is not None:
                repo_ref = RepoRef()
                repo_ref.set_reference_handle(repos.get_handle())
                source.add_repo_reference(repo_ref)
            self._db.add_source(source, tran)
            self._db.commit_source(source, tran)
            citation = Citation()
            citation.set_reference_handle(source.get_handle())
            self._db.add_citation(citation, tran)
            self._db.commit_citation(citation, tran)

        return citation
Ejemplo n.º 22
0
    def add_button_clicked(self, obj):
        """
        Create a new Citation instance and call the EditCitation editor with
        the new citation.

        Called when the Add button is clicked.
        If the window already exists (WindowActiveError), we ignore it.
        This prevents the dialog from coming up twice on the same object.
        """
        try:
            from .. import EditCitation
            EditCitation(self.dbstate, self.uistate, self.track, Citation(),
                         Source(), self.add_callback, self.callertitle)
        except WindowActiveError:
            pass
Ejemplo n.º 23
0
 def find_and_set_citation(self, obj, source):
     # look for the source in the existing citations for the object
     LOG.debug("find_and_set_citation: looking for source: %s",
               source.get_gramps_id())
     for citation_handle in obj.get_citation_list():
         citation = self.db.get_citation_from_handle(citation_handle)
         LOG.debug("find_and_set_citation: existing citation: %s",
                   citation.get_gramps_id())
         poss_source = self.db.get_source_from_handle(
                                 citation.get_reference_handle())
         LOG.debug("   compare source %s == %s", source.get_gramps_id(),
                   poss_source.get_gramps_id())
         if poss_source.get_handle() == source.get_handle():
             # The source is already cited
             LOG.debug("   source already cited")
             return
     # we couldn't find an appropriate citation, so we have to create one.
     citation = Citation()
     LOG.debug("   creating citation")
     citation.set_reference_handle(source.get_handle())
     self.db.add_citation(citation, self.trans)
     LOG.debug("   created citation, citation %s %s" %
               (citation, citation.get_gramps_id()))
     obj.add_citation(citation.get_handle())
Ejemplo n.º 24
0
 def share(self, obj):
     """
     share:      Add a new citation to an existing source (when a source is
                   selected)
     """
     for handle in self.selected_handles():
         # The handle will either be a Source handle or a Citation handle
         source, citation = self.get_source_or_citation(handle)
         if not source:
             source = self.dbstate.db.get_source_from_handle(
                 citation.get_reference_handle())
         try:
             EditCitation(self.dbstate, self.uistate, [], Citation(),
                          source)
         except WindowActiveError:
             from gramps.gui.dialog import WarningDialog
             WarningDialog(_("Cannot share this reference"),
                           self.__blocked_text(),
                           parent=self.uistate.window)
Ejemplo n.º 25
0
 def handle_extra_type(self, objtype, handle):
     """
     A SOURCE_LINK object has been dragged
     """
     if handle:
         objct = self.dbstate.db.get_source_from_handle(handle)
         if isinstance(objct, Source):
             try:
                 from .. import EditCitation
                 EditCitation(self.dbstate, self.uistate, self.track,
                              Citation(), objct,
                              callback=self.add_callback,
                              callertitle=self.callertitle)
             except WindowActiveError:
                 from ...dialog import WarningDialog
                 WarningDialog(_("Cannot share this reference"),
                               self.__blocked_text(),
                               parent=self.uistate.window)
         else:
             raise ValueError("selection must be either source or citation")
Ejemplo n.º 26
0
    def on_ok_clicked(self):
        """
        Method that is run when you click the OK button. The numbers of sources
        and citations are retrieved from the entry box and used to govern the
        amount of data generated
        """

        num_sources_text = self.sources_entry.get_text()
        try:
            num_sources = int(num_sources_text)
        except:
            return
        num_citations_text = self.citations_entry.get_text()
        num_citations = int(num_citations_text)

        self.progress = ProgressMeter(
            'Generating data', '')
        self.progress.set_pass('Generating data',
                               num_sources*num_citations)
        LOG.debug("sources %04d citations %04d" % (num_sources,
                                                     num_citations))

        source = Source()
        citation = Citation()

        self.db.disable_signals()
        with DbTxn('Populate sources and citations', self.db) as trans:
            for i in range(num_sources):
                source.gramps_id = None
                source.handle = None
                source.title = "Source %04d" % (i + 1)
                source_handle = self.db.add_source(source, trans)

                for j in range(num_citations):
                    citation.gramps_id = None
                    citation.handle = None
                    citation.source_handle = source_handle
                    citation.page = "Page %04d" % (j + 1)
                    self.db.add_citation(citation, trans)
                    self.progress.step()
            LOG.debug("sources and citations added")
        self.db.enable_signals()
        self.db.request_rebuild()
        self.progress.close()

        self.options.handler.options_dict['sources'] = num_sources
        self.options.handler.options_dict['citations'] = num_citations
        # Save options
        self.options.handler.save_options()
Ejemplo n.º 27
0
    def add(self, obj):
        """
        add:        Add a new citation and a new source (this can also be done
                      by source view add a source, then citation view add a new
                      citation to an existing source)

        Create a new Source instance and Citation instance and call the
        EditSource editor with the new source.

        Called when the Add button is clicked.
        If the window already exists (WindowActiveError), we ignore it.
        This prevents the dialog from coming up twice on the same object.

        However, since the window is identified by the Source object, and
        we have just created a new one, it seems to be impossible for the
        window to already exist, so this is just an extra safety measure.
        """
        try:
            EditCitation(self.dbstate, self.uistate, [], Citation(), Source())
        except WindowActiveError:
            pass
Ejemplo n.º 28
0
    def __init__(self, dbstate, uistate, track, event):

        self.dbstate = dbstate
        self.uistate = uistate
        self.track = track
        self.db = dbstate.db
        
        self.event = event
        self.citation = get_census_citation(self.db, self.event)
        if self.citation is None:
            self.citation = Citation()

        ManagedWindow.__init__(self, uistate, track, event)

        self.widgets = {}
        top = self.__create_gui()
        self.set_window(top, None, self.get_menu_title())

        self._config = config.get_manager('census')
        width = self._config.get('interface.census-width')
        height = self._config.get('interface.census-height')
        self.window.resize(width, height)

        self.place_field = PlaceEntry(self.dbstate, self.uistate, self.track,
                                      self.widgets['place_text'],
                                      self.event.set_place_handle,
                                      self.event.get_place_handle,
                                      self.widgets['place_add'],
                                      self.widgets['place_share'])

        self.ref_field = MonitoredEntry(
            self.widgets['ref_entry'], 
            self.citation.set_page, 
            self.citation.get_page, 
            self.db.readonly)

        if self.event.get_handle():
            self.widgets['census_combo'].set_sensitive(False)
            self.__populate_gui(event)
            self.details.populate_gui(event)
Ejemplo n.º 29
0
 def share(self, obj):
     """
     share:      Add a new citation to an existing source (when a source is
                   selected)
     """
     for handle in self.selected_handles():
         # The handle will either be a Source handle or a Citation handle
         source = self.dbstate.db.get_source_from_handle(handle)
         citation = self.dbstate.db.get_citation_from_handle(handle)
         if (not source and not citation) or (source and citation):
             raise ValueError("selection must be either source or citation")
         if source:
             try:
                 EditCitation(self.dbstate, self.uistate, [], Citation(),
                              source)
             except WindowActiveError:
                 from gramps.gui.dialog import WarningDialog
                 WarningDialog(_("Cannot share this reference"),
                               self.__blocked_text())
         else:
             msg = _("Cannot add citation.")
             msg2 = _("In order to add a citation to an existing source, "
                      " you must select a source.")
             ErrorDialog(msg, msg2)
Ejemplo n.º 30
0
 def find_and_set_citation(self, obj, source):
     # look for the source in the existing citations for the object
     LOG.debug("find_and_set_citation: looking for source: %s",
               source.get_gramps_id())
     for citation_handle in obj.get_citation_list():
         citation = self.db.get_citation_from_handle(citation_handle)
         LOG.debug("find_and_set_citation: existing citation: %s",
                   citation.get_gramps_id())
         poss_source = self.db.get_source_from_handle(
             citation.get_reference_handle())
         LOG.debug("   compare source %s == %s", source.get_gramps_id(),
                   poss_source.get_gramps_id())
         if poss_source.get_handle() == source.get_handle():
             # The source is already cited
             LOG.debug("   source already cited")
             return
     # we couldn't find an appropriate citation, so we have to create one.
     citation = Citation()
     LOG.debug("   creating citation")
     citation.set_reference_handle(source.get_handle())
     self.db.add_citation(citation, self.trans)
     LOG.debug("   created citation, citation %s %s" %
               (citation, citation.get_gramps_id()))
     obj.add_citation(citation.get_handle())
Ejemplo n.º 31
0
 def make_citation(self, citation):
     if self.use_db_cache and citation.cache:
         data = citation.from_cache()
     else:
         data = self.dji.get_citation(citation)
     return Citation.create(data)
Ejemplo n.º 32
0
 def on_res_ok_clicked(self, dummy):
     """ Accept changes displayed and commit to place.
     Also find or create a new enclosing place from parent. """
     # do the names
     namelist = []
     for row in self.alt_store:
         if row[0] == 'P':
             self.place.name = self.newplace.names[row[4]]
         elif row[0] == '\u2714':
             namelist.append(self.newplace.names[row[4]])
     self.place.alt_names = namelist
     # Lat/lon/ID/code/type
     self.place.lat = self.top.get_object('latitude').get_text()
     self.place.long = self.top.get_object('longitude').get_text()
     self.place.gramps_id = self.top.get_object('grampsid').get_text()
     self.place.code = self.top.get_object('postal').get_text()
     self.place.place_type.set(self.type_combo.get_values())
     # Add in URLs if wanted
     if self.keepweb:
         for url in self.newplace.links:
             self.place.add_url(url)
     # Enclose in the next level place
     next_place = False
     parent = None
     if not self.keep_enclosure or not self.place.placeref_list:
         if self.newplace.parent_ids:
             # we might have a parent with geo id 'GEO12345'
             parent = self.dbstate.db.get_place_from_gramps_id(
                 self.newplace.parent_ids[0])
         if not parent and self.newplace.parent_names:
             # make one, will have to be examined/cleaned later
             parent = Place()
             parent.title = ', '.join(self.newplace.parent_names)
             name = PlaceName()
             name.value = parent.title
             parent.name = name
             parent.gramps_id = self.newplace.parent_ids[0]
             with DbTxn(
                     _("Add Place (%s)") % parent.title,
                     self.dbstate.db) as trans:
                 self.dbstate.db.add_place(parent, trans)
                 next_place = True
         if parent:
             if located_in(self.dbstate.db, parent.handle,
                           self.place.handle):
                 # attempting to create a place loop, not good!
                 ErrorDialog(_('Place cycle detected'),
                             msg2=_("The place you chose is enclosed in the"
                                    " place you are workin on!\n"
                                    "Please cancel and choose another "
                                    "place."),
                             parent=self.uistate.window)
                 return
             # check to see if we already have the enclosing place
             already_there = False
             for pref in self.place.placeref_list:
                 if parent.handle == pref.ref:
                     already_there = True
                     break
             if not already_there:
                 placeref = PlaceRef()
                 placeref.set_reference_handle(parent.handle)
                 self.place.set_placeref_list([placeref])
     # Add in Citation/Source if wanted
     if self.addcitation and self.newplace.geoid:
         src_hndl = self.find_or_make_source()
         cit = Citation()
         cit.set_reference_handle(src_hndl)
         cit.set_page("GeoNames ID: %s" %
                      self.newplace.geoid.replace('GEO', ''))
         with DbTxn(_("Add Citation (%s)") % "GeoNames",
                    self.dbstate.db) as trans:
             self.dbstate.db.add_citation(cit, trans)
         self.place.add_citation(cit.handle)
     # We're finally ready to commit the updated place
     with DbTxn(_("Edit Place (%s)") % self.place.title,
                self.dbstate.db) as trans:
         self.dbstate.db.commit_place(self.place, trans)
     # Jump to enclosing place to clean it if necessary
     if next_place:
         self.set_active('Place', parent.handle)
         self.place = parent
         # if geoparse fails, leave us at main view
         if self.newplace.parent_ids and \
             self.geoparse(self.newplace.parent_ids[0],
                           _(", ").join(self.newplace.parent_names),
                           self.newplace.parent_ids,
                           self.newplace.parent_names):
             # geoparse worked, lets put up the results view
             self.gui.get_child().remove(self.mainwin)
             self.gui.get_child().add(self.results_win)
             self.res_gui()
             return
     self.reset_main()
     if self.gui.get_child().get_child() == self.results_win:
         self.gui.get_child().remove(self.results_win)
         self.gui.get_child().add(self.mainwin)
Ejemplo n.º 33
0
def exportData(database, filename,
               error_dialog=None, option_box=None, callback=None):
    if not callable(callback):
        callback = lambda percent: None # dummy

    with OpenFileOrStdout(filename, encoding="utf-8") as fp:

        total = (len(database.note_map) +
                 len(database.person_map) +
                 len(database.event_map) +
                 len(database.family_map) +
                 len(database.repository_map) +
                 len(database.place_map) +
                 len(database.media_map) +
                 len(database.citation_map) +
                 len(database.source_map) +
                 len(database.tag_map))
        count = 0.0

        # ---------------------------------
        # Notes
        # ---------------------------------
        for handle in database.note_map.keys():
            serial = database.note_map[handle]
            write_line(fp, Note.create(serial))
            count += 1
            callback(100 * count/total)

        # ---------------------------------
        # Event
        # ---------------------------------
        for handle in database.event_map.keys():
            serial = database.event_map[handle]
            write_line(fp, Event.create(serial))
            count += 1
            callback(100 * count/total)

        # ---------------------------------
        # Person
        # ---------------------------------
        for handle in database.person_map.keys():
            serial = database.person_map[handle]
            write_line(fp, Person.create(serial))
            count += 1
            callback(100 * count/total)

        # ---------------------------------
        # Family
        # ---------------------------------
        for handle in database.family_map.keys():
            serial = database.family_map[handle]
            write_line(fp, Family.create(serial))
            count += 1
            callback(100 * count/total)

        # ---------------------------------
        # Repository
        # ---------------------------------
        for handle in database.repository_map.keys():
            serial = database.repository_map[handle]
            write_line(fp, Repository.create(serial))
            count += 1
            callback(100 * count/total)

        # ---------------------------------
        # Place
        # ---------------------------------
        for handle in database.place_map.keys():
            serial = database.place_map[handle]
            write_line(fp, Place.create(serial))
            count += 1
            callback(100 * count/total)

        # ---------------------------------
        # Source
        # ---------------------------------
        for handle in database.source_map.keys():
            serial = database.source_map[handle]
            write_line(fp, Source.create(serial))
            count += 1
            callback(100 * count/total)

        # ---------------------------------
        # Citation
        # ---------------------------------
        for handle in database.citation_map.keys():
            serial = database.citation_map[handle]
            write_line(fp, Citation.create(serial))
            count += 1
            callback(100 * count/total)

        # ---------------------------------
        # Media
        # ---------------------------------
        for handle in database.media_map.keys():
            serial = database.media_map[handle]
            write_line(fp, Media.create(serial))
            count += 1
            callback(100 * count/total)

        # ---------------------------------
        # Tag
        # ---------------------------------
        for handle in database.tag_map.keys():
            serial = database.tag_map[handle]
            write_line(fp, Tag.create(serial))
            count += 1
            callback(100 * count/total)

    return True
Ejemplo n.º 34
0
 def on_res_ok_clicked(self, dummy):
     """ Accept changes displayed and commit to place.
     Also find or create a new enclosing place from parent. """
     # do the names
     namelist = []
     for row in self.alt_store:
         if row[0] == 'P':
             self.place.name = self.newplace.names[row[4]]
         elif row[0] == '\u2714':
             namelist.append(self.newplace.names[row[4]])
     self.place.alt_names = namelist
     # Lat/lon/ID/code/type
     self.place.lat = self.top.get_object('latitude').get_text()
     self.place.long = self.top.get_object('longitude').get_text()
     self.place.gramps_id = self.top.get_object('grampsid').get_text()
     self.place.code = self.top.get_object('postal').get_text()
     self.place.place_type.set(self.type_combo.get_values())
     # Add in URLs if wanted
     if self.keepweb:
         for url in self.newplace.links:
             self.place.add_url(url)
     # Enclose in the next level place
     next_place = False
     parent = None
     if not self.keep_enclosure or not self.place.placeref_list:
         if self.newplace.parent_ids:
             # we might have a parent with geo id 'GEO12345'
             parent = self.dbstate.db.get_place_from_gramps_id(
                 self.newplace.parent_ids[0])
         if not parent and self.newplace.parent_names:
             # make one, will have to be examined/cleaned later
             parent = Place()
             parent.title = ', '.join(self.newplace.parent_names)
             name = PlaceName()
             name.value = parent.title
             parent.name = name
             parent.gramps_id = self.newplace.parent_ids[0]
             with DbTxn(_("Add Place (%s)") % parent.title,
                        self.dbstate.db) as trans:
                 self.dbstate.db.add_place(parent, trans)
                 next_place = True
         if parent:
             if located_in(self.dbstate.db, parent.handle,
                           self.place.handle):
                 # attempting to create a place loop, not good!
                 ErrorDialog(_('Place cycle detected'),
                             msg2=_("The place you chose is enclosed in the"
                                    " place you are workin on!\n"
                                    "Please cancel and choose another "
                                    "place."),
                             parent=self.uistate.window)
                 return
             # check to see if we already have the enclosing place
             already_there = False
             for pref in self.place.placeref_list:
                 if parent.handle == pref.ref:
                     already_there = True
                     break
             if not already_there:
                 placeref = PlaceRef()
                 placeref.set_reference_handle(parent.handle)
                 self.place.set_placeref_list([placeref])
     # Add in Citation/Source if wanted
     if self.addcitation and self.newplace.geoid:
         src_hndl = self.find_or_make_source()
         cit = Citation()
         cit.set_reference_handle(src_hndl)
         cit.set_page("GeoNames ID: %s" %
                      self.newplace.geoid.replace('GEO', ''))
         with DbTxn(_("Add Citation (%s)") % "GeoNames",
                    self.dbstate.db) as trans:
             self.dbstate.db.add_citation(cit, trans)
         self.place.add_citation(cit.handle)
     # We're finally ready to commit the updated place
     with DbTxn(_("Edit Place (%s)") % self.place.title,
                self.dbstate.db) as trans:
         self.dbstate.db.commit_place(self.place, trans)
     # Jump to enclosing place to clean it if necessary
     if next_place:
         self.set_active('Place', parent.handle)
         self.place = parent
         # if geoparse fails, leave us at main view
         if self.newplace.parent_ids and \
             self.geoparse(self.newplace.parent_ids[0],
                           _(", ").join(self.newplace.parent_names),
                           self.newplace.parent_ids,
                           self.newplace.parent_names):
             # geoparse worked, lets put up the results view
             self.gui.get_child().remove(self.mainwin)
             self.gui.get_child().add(self.results_win)
             self.res_gui()
             return
     self.reset_main()
     if self.gui.get_child().get_child() == self.results_win:
         self.gui.get_child().remove(self.results_win)
         self.gui.get_child().add(self.mainwin)
Ejemplo n.º 35
0
def exportData(database,
               filename,
               error_dialog=None,
               option_box=None,
               callback=None):
    if not callable(callback):
        callback = lambda percent: None  # dummy

    with OpenFileOrStdout(filename, encoding="utf-8") as fp:

        total = (len(database.note_map) + len(database.person_map) +
                 len(database.event_map) + len(database.family_map) +
                 len(database.repository_map) + len(database.place_map) +
                 len(database.media_map) + len(database.citation_map) +
                 len(database.source_map) + len(database.tag_map))
        count = 0.0

        # ---------------------------------
        # Notes
        # ---------------------------------
        for handle in database.note_map.keys():
            serial = database.note_map[handle]
            write_line(fp, Note.create(serial))
            count += 1
            callback(100 * count / total)

        # ---------------------------------
        # Event
        # ---------------------------------
        for handle in database.event_map.keys():
            serial = database.event_map[handle]
            write_line(fp, Event.create(serial))
            count += 1
            callback(100 * count / total)

        # ---------------------------------
        # Person
        # ---------------------------------
        for handle in database.person_map.keys():
            serial = database.person_map[handle]
            write_line(fp, Person.create(serial))
            count += 1
            callback(100 * count / total)

        # ---------------------------------
        # Family
        # ---------------------------------
        for handle in database.family_map.keys():
            serial = database.family_map[handle]
            write_line(fp, Family.create(serial))
            count += 1
            callback(100 * count / total)

        # ---------------------------------
        # Repository
        # ---------------------------------
        for handle in database.repository_map.keys():
            serial = database.repository_map[handle]
            write_line(fp, Repository.create(serial))
            count += 1
            callback(100 * count / total)

        # ---------------------------------
        # Place
        # ---------------------------------
        for handle in database.place_map.keys():
            serial = database.place_map[handle]
            write_line(fp, Place.create(serial))
            count += 1
            callback(100 * count / total)

        # ---------------------------------
        # Source
        # ---------------------------------
        for handle in database.source_map.keys():
            serial = database.source_map[handle]
            write_line(fp, Source.create(serial))
            count += 1
            callback(100 * count / total)

        # ---------------------------------
        # Citation
        # ---------------------------------
        for handle in database.citation_map.keys():
            serial = database.citation_map[handle]
            write_line(fp, Citation.create(serial))
            count += 1
            callback(100 * count / total)

        # ---------------------------------
        # Media
        # ---------------------------------
        for handle in database.media_map.keys():
            serial = database.media_map[handle]
            write_line(fp, MediaObject.create(serial))
            count += 1
            callback(100 * count / total)

        # ---------------------------------
        # Tag
        # ---------------------------------
        for handle in database.tag_map.keys():
            serial = database.tag_map[handle]
            write_line(fp, Tag.create(serial))
            count += 1
            callback(100 * count / total)

    return True
Ejemplo n.º 36
0
    def _process(self, count, total, sql):
        # ---------------------------------
        # Process note
        # ---------------------------------
        notes = sql.query("""select * from note;""")
        for note in notes:
            (handle, gid, text, _format, note_type1, note_type2, change,
             private) = note
            styled_text = [text, []]
            markups = sql.query(
                """select * from link where from_handle = ? """
                """and to_type = 'markup';""", handle)
            for markup_link in markups:
                _from_type, _from_handle, _to_type, to_handle = markup_link
                markup_detail = sql.query(
                    """select * from markup where handle = ?;""", to_handle)
                for markup in markup_detail:
                    (_mhandle, markup0, markup1, value,
                     start_stop_list) = markup
                    ss_list = eval(start_stop_list)
                    styled_text[1] += [((markup0, markup1), value, ss_list)]

            tags = self.get_links(sql, "note", handle, "tag")

            g_note = Note()
            g_note.unserialize(
                (handle, gid, styled_text, _format, (note_type1, note_type2),
                 change, tags, bool(private)))
            self.db.add_note(g_note, self.trans)
            count += 1
            self.callback(100 * count / total)

        # ---------------------------------
        # Process event
        # ---------------------------------
        events = sql.query("""select * from event;""")
        for event in events:
            (handle, gid, the_type0, the_type1, description, change,
             private) = event

            note_list = self.get_note_list(sql, "event", handle)
            citation_list = self.get_citation_list(sql, "event", handle)
            media_list = self.get_media_list(sql, "event", handle)
            attribute_list = self.get_attribute_list(sql, "event", handle)

            date_handle = self.get_link(sql, "event", handle, "date")
            date = self.get_date(sql, date_handle)

            place_handle = self.get_link(sql, "event", handle, "place")
            place = self.get_place_from_handle(sql, place_handle)

            tags = self.get_links(sql, "event", handle, "tag")
            data = (handle, gid, (the_type0, the_type1), date, description,
                    place, citation_list, note_list, media_list,
                    attribute_list, change, tags, bool(private))

            g_event = Event()
            g_event.unserialize(data)
            self.db.add_event(g_event, self.trans)

            count += 1
            self.callback(100 * count / total)

        # ---------------------------------
        # Process person
        # ---------------------------------
        people = sql.query("""select * from person;""")
        for person in people:
            if person is None:
                continue
            (
                handle,  # 0
                gid,  # 1
                gender,  # 2
                death_ref_handle,  # 5
                birth_ref_handle,  # 6
                change,  # 17
                private,  # 19
            ) = person
            primary_name = self.get_names(sql, "person", handle, True)  # one
            alternate_names = self.get_names(sql, "person", handle, False)
            event_ref_list = self.get_event_ref_list(sql, "person", handle)
            family_list = self.get_family_list(sql, "person", handle)
            parent_family_list = self.get_parent_family_list(
                sql, "person", handle)
            media_list = self.get_media_list(sql, "person", handle)
            address_list = self.get_address_list(sql,
                                                 "person",
                                                 handle,
                                                 with_parish=False)
            attribute_list = self.get_attribute_list(sql, "person", handle)
            urls = self.get_url_list(sql, "person", handle)
            lds_ord_list = self.get_lds_list(sql, "person", handle)
            pcitation_list = self.get_citation_list(sql, "person", handle)
            pnote_list = self.get_note_list(sql, "person", handle)
            person_ref_list = self.get_person_ref_list(sql, "person", handle)
            death_ref_index = lookup(death_ref_handle, event_ref_list)
            birth_ref_index = lookup(birth_ref_handle, event_ref_list)
            tags = self.get_links(sql, "person", handle, "tag")

            data = (
                handle,  # 0
                gid,  # 1
                gender,  # 2
                primary_name,  # 3
                alternate_names,  # 4
                death_ref_index,  # 5
                birth_ref_index,  # 6
                event_ref_list,  # 7
                family_list,  # 8
                parent_family_list,  # 9
                media_list,  # 10
                address_list,  # 11
                attribute_list,  # 12
                urls,  # 13
                lds_ord_list,  # 14
                pcitation_list,  # 15
                pnote_list,  # 16
                change,  # 17
                tags,
                bool(private),  # 19
                person_ref_list,
            )  # 20

            g_pers = Person()
            g_pers.unserialize(data)
            self.db.add_person(g_pers, self.trans)
            count += 1
            self.callback(100 * count / total)
        # ---------------------------------
        # Process family
        # ---------------------------------
        families = sql.query("""select * from family;""")
        for family in families:
            (handle, gid, father_handle, mother_handle, the_type0, the_type1,
             change, private) = family

            child_ref_list = self.get_child_ref_list(sql, "family", handle)
            event_ref_list = self.get_event_ref_list(sql, "family", handle)
            media_list = self.get_media_list(sql, "family", handle)
            attribute_list = self.get_attribute_list(sql, "family", handle)
            lds_seal_list = self.get_lds_list(sql, "family", handle)
            citation_list = self.get_citation_list(sql, "family", handle)
            note_list = self.get_note_list(sql, "family", handle)
            tags = self.get_links(sql, "family", handle, "tag")

            data = (handle, gid, father_handle, mother_handle, child_ref_list,
                    (the_type0, the_type1), event_ref_list, media_list,
                    attribute_list, lds_seal_list, citation_list, note_list,
                    change, tags, private)
            g_fam = Family()
            g_fam.unserialize(data)
            self.db.add_family(g_fam, self.trans)

            count += 1
            self.callback(100 * count / total)
        # ---------------------------------
        # Process repository
        # ---------------------------------
        repositories = sql.query("""select * from repository;""")
        for repo in repositories:
            (handle, gid, the_type0, the_type1, name, change, private) = repo

            note_list = self.get_note_list(sql, "repository", handle)
            address_list = self.get_address_list(sql,
                                                 "repository",
                                                 handle,
                                                 with_parish=False)
            urls = self.get_url_list(sql, "repository", handle)
            tags = self.get_links(sql, "repository", handle, "tag")
            data = (handle, gid, (the_type0, the_type1), name, note_list,
                    address_list, urls, change, tags, private)
            g_rep = Repository()
            g_rep.unserialize(data)
            self.db.add_repository(g_rep, self.trans)
            count += 1
            self.callback(100 * count / total)
        # ---------------------------------
        # Process place
        # ---------------------------------
        places = sql.query("""select * from place;""")
        for place in places:
            count += 1
            (handle, gid, title, value, the_type0, the_type1, code, long, lat,
             lang, change, private) = place

            # We could look this up by "place_main", but we have the handle:
            #main_loc = self.get_main_location(sql, handle, with_parish=True)
            alt_loc_list = self.get_location_list(sql,
                                                  "place_alt",
                                                  handle,
                                                  with_parish=True)
            urls = self.get_url_list(sql, "place", handle)
            media_list = self.get_media_list(sql, "place", handle)
            citation_list = self.get_citation_list(sql, "place", handle)
            note_list = self.get_note_list(sql, "place", handle)
            tags = self.get_links(sql, "place", handle, "tag")
            place_type = (the_type0, the_type1)
            alt_place_name_list = self.get_alt_place_name_list(sql, handle)
            place_ref_list = self.get_place_ref_list(sql, handle)
            data = (handle, gid, title, long, lat, place_ref_list,
                    PlaceName(value=value,
                              lang=lang).serialize(), alt_place_name_list,
                    place_type, code, alt_loc_list, urls, media_list,
                    citation_list, note_list, change, tags, private)
            g_plac = Place()
            g_plac.unserialize(data)
            self.db.commit_place(g_plac, self.trans)
            self.callback(100 * count / total)

        # ---------------------------------
        # Process citation
        # ---------------------------------
        citations = sql.query("""select * from citation;""")
        for citation in citations:
            (handle, gid, confidence, page, source_handle, change,
             private) = citation
            date_handle = self.get_link(sql, "citation", handle, "date")
            date = self.get_date(sql, date_handle)
            note_list = self.get_note_list(sql, "citation", handle)
            media_list = self.get_media_list(sql, "citation", handle)
            datamap = self.get_datamap_list(sql, "citation", handle)
            tags = self.get_links(sql, "citation", handle, "tag")
            data = (handle, gid, date, page, confidence, source_handle,
                    note_list, media_list, datamap, change, tags, private)
            g_cit = Citation()
            g_cit.unserialize(data)
            self.db.commit_citation(g_cit, self.trans)
            count += 1
            self.callback(100 * count / total)

        # ---------------------------------
        # Process source
        # ---------------------------------
        sources = sql.query("""select * from source;""")
        for source in sources:
            (handle, gid, title, author, pubinfo, abbrev, change,
             private) = source
            note_list = self.get_note_list(sql, "source", handle)
            media_list = self.get_media_list(sql, "source", handle)
            datamap = self.get_datamap_list(sql, "source", handle)
            reporef_list = self.get_repository_ref_list(sql, "source", handle)
            tags = self.get_links(sql, "source", handle, "tag")

            data = (handle, gid, title, author, pubinfo, note_list, media_list,
                    abbrev, change, datamap, reporef_list, tags, private)
            g_src = Source()
            g_src.unserialize(data)
            self.db.commit_source(g_src, self.trans)
            count += 1
            self.callback(100 * count / total)
        # ---------------------------------
        # Process media
        # ---------------------------------
        media = sql.query("""select * from media;""")
        for med in media:
            (handle, gid, path, mime, desc, checksum, change, private) = med

            attribute_list = self.get_attribute_list(sql, "media", handle)
            citation_list = self.get_citation_list(sql, "media", handle)
            note_list = self.get_note_list(sql, "media", handle)

            date_handle = self.get_link(sql, "media", handle, "date")
            date = self.get_date(sql, date_handle)
            tags = self.get_links(sql, "media", handle, "tag")

            data = (handle, gid, path, mime, desc, checksum, attribute_list,
                    citation_list, note_list, change, date, tags, private)
            g_med = Media()
            g_med.unserialize(data)
            self.db.commit_media(g_med, self.trans)
            count += 1
            self.callback(100 * count / total)
        # ---------------------------------
        # Process tag
        # ---------------------------------
        tags = sql.query("""select * from tag;""")
        for tag in tags:
            (handle, name, color, priority, change) = tag

            data = (handle, name, color, priority, change)
            g_tag = Tag()
            g_tag.unserialize(data)
            self.db.commit_tag(g_tag, self.trans)
            count += 1
            self.callback(100 * count / total)
Ejemplo n.º 37
0
class CensusEditor(ManagedWindow):
    """
    Census Editor.
    """
    def __init__(self, dbstate, uistate, track, event):

        self.dbstate = dbstate
        self.uistate = uistate
        self.track = track
        self.db = dbstate.db
        
        self.event = event
        self.citation = get_census_citation(self.db, self.event)
        if self.citation is None:
            self.citation = Citation()

        ManagedWindow.__init__(self, uistate, track, event)

        self.widgets = {}
        top = self.__create_gui()
        self.set_window(top, None, self.get_menu_title())

        self._config = config.get_manager('census')
        width = self._config.get('interface.census-width')
        height = self._config.get('interface.census-height')
        self.window.resize(width, height)

        self.place_field = PlaceEntry(self.dbstate, self.uistate, self.track,
                                      self.widgets['place_text'],
                                      self.event.set_place_handle,
                                      self.event.get_place_handle,
                                      self.widgets['place_add'],
                                      self.widgets['place_share'])

        self.ref_field = MonitoredEntry(
            self.widgets['ref_entry'], 
            self.citation.set_page, 
            self.citation.get_page, 
            self.db.readonly)

        if self.event.get_handle():
            self.widgets['census_combo'].set_sensitive(False)
            self.__populate_gui(event)
            self.details.populate_gui(event)

    def _add_tab(self, notebook, page):
        notebook.append_page(page, page.get_tab_widget())
        page.label.set_use_underline(True)
        return page

    def get_menu_title(self):
        """
        Get the menu title.
        """
        if self.event.get_handle():
            date = get_date(self.event)
            if not date:
                date = 'unknown'
            dialog_title = _('Census: %s')  % date
        else:
            dialog_title = _('New Census')
        return dialog_title

    def build_menu_names(self, event):
        """
        Build menu names. Overrides method in ManagedWindow.
        """
        return (_('Edit Census'), self.get_menu_title())

    def __create_gui(self):
        """
        Create and display the GUI components of the editor.
        """
        root = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
        root.set_transient_for(self.uistate.window)

        vbox = Gtk.VBox()

        tab = Gtk.Table(4, 4, False)
        tab.set_row_spacings(10)
        tab.set_col_spacings(10)

        census_label = Gtk.Label(_("Source:"))
        census_label.set_alignment(0.0, 0.5)
        tab.attach(census_label, 0, 1, 0, 1,
                   xoptions=Gtk.AttachOptions.FILL, xpadding=10)
        
        liststore = Gtk.ListStore(str, str, str)
        for row in get_census_sources(self.db):
            liststore.append([row[0].decode(), row[1], row[2]])

        census_combo = Gtk.ComboBox()
        census_combo.set_model(liststore)
        cell = Gtk.CellRendererText()
        census_combo.pack_start(cell, True)
        census_combo.add_attribute(cell, 'text', 1)
        #cell = Gtk.CellRendererText()
        #census_combo.pack_start(cell, True)
        #census_combo.add_attribute(cell, 'text', 2)
        census_combo.connect('changed', self.__census_changed)
        self.widgets['census_combo'] = census_combo
        
        hbox = Gtk.HBox()
        hbox.pack_start(census_combo, False, True, 0)
        tab.attach(hbox, 1, 2, 0, 1)

        date_label = Gtk.Label(_("Date:"))
        date_label.set_alignment(0.0, 0.5)
        tab.attach(date_label, 0, 1, 1, 2,
                   xoptions=Gtk.AttachOptions.FILL, xpadding=10)
        
        date_text = Gtk.Label()
        date_text.set_alignment(0.0, 0.5)
        tab.attach(date_text, 1, 2, 1, 2)
        self.widgets['date_text'] = date_text
        
        ref_label = Gtk.Label(_("Reference:"))
        ref_label.set_alignment(0.0, 0.5)
        tab.attach(ref_label, 0, 1, 2, 3,
                   xoptions=Gtk.AttachOptions.FILL, xpadding=10)
        
        ref_entry = Gtk.Entry()
        tab.attach(ref_entry, 1, 2, 2, 3)
        self.widgets['ref_entry'] = ref_entry

        place_label = Gtk.Label(_("Place:"))
        place_label.set_alignment(0.0, 0.5)
        tab.attach(place_label, 0, 1, 3, 4,
                   xoptions=Gtk.AttachOptions.FILL, xpadding=10)
        
        place_text = Gtk.Label()
        place_text.set_alignment(0.0, 0.5)
        tab.attach(place_text, 1, 2, 3, 4)
        self.widgets['place_text'] = place_text

        image = Gtk.Image()
        image.set_from_stock(Gtk.STOCK_INDEX, Gtk.IconSize.BUTTON)
        place_share = Gtk.Button()
        place_share.set_relief(Gtk.ReliefStyle.NONE)
        place_share.add(image)
        tab.attach(place_share, 2, 3, 3, 4, xoptions=0)
        self.widgets['place_share'] = place_share

        image = Gtk.Image()
        image.set_from_stock(Gtk.STOCK_ADD, Gtk.IconSize.BUTTON)
        place_add = Gtk.Button()
        place_add.set_relief(Gtk.ReliefStyle.NONE)
        place_add.add(image)
        tab.attach(place_add, 3, 4, 3, 4, xoptions=0)
        self.widgets['place_add'] = place_add
 
        button_box = Gtk.HButtonBox()
        button_box.set_layout(Gtk.ButtonBoxStyle.END)
        
        help_btn = Gtk.Button(stock=Gtk.STOCK_HELP)
        help_btn.connect('clicked', self.help_clicked)
        button_box.add(help_btn)
        button_box.set_child_secondary(help_btn, True)

        cancel_btn = Gtk.Button(stock=Gtk.STOCK_CANCEL)
        cancel_btn.connect('clicked', self.close)
        button_box.add(cancel_btn)
        
        ok_btn = Gtk.Button(stock=Gtk.STOCK_OK)
        ok_btn.connect('clicked', self.save)
        button_box.add(ok_btn)

        notebook = Gtk.Notebook()

        self.details = DetailsTab(self.dbstate,
                                       self.uistate,
                                       self.track,
                                       self.event,
                                       census_combo)
        self._add_tab(notebook, self.details)

        self.headings = HeadingsTab(self.dbstate,
                                       self.uistate,
                                       self.track,
                                       self.event,
                                       census_combo)
        self._add_tab(notebook, self.headings)

        self.gallery_list = GalleryTab(self.dbstate,
                                       self.uistate,
                                       self.track,
                                       self.citation.get_media_list())
        self._add_tab(notebook, self.gallery_list)

        vbox.pack_start(tab, expand=False, fill=True, padding=10)
        vbox.pack_start(notebook, expand=True, fill=True, padding=0)
        vbox.pack_end(button_box, expand=False, fill=True, padding=10)
        
        root.add(vbox)
        root.show_all()

        notebook.set_current_page(0)

        return root

    def __populate_gui(self, event):
        """
        Populate the GUI for a given census event.
        """
        census_combo = self.widgets['census_combo']
        for pos, row in enumerate(census_combo.get_model()):
            if row[0] == self.citation.get_reference_handle():
                census_combo.set_active(pos)
                
        date_text = self.widgets['date_text']
        date_text.set_text(get_date(event))

    def __census_changed(self, combo):
        """
        Called when the user selects a new census from the combo box.
        """
        model = combo.get_model()
        index = combo.get_active()
        census_id = model[index][2]

        # Set date
        census_date = get_census_date(census_id)

        date_text = self.widgets['date_text']
        date_text.set_text(displayer.display(census_date))
        self.event.set_date_object(census_date)
        self.citation.set_date_object(census_date)

        # Set source
        self.citation.set_reference_handle(model[index][0])

        # Set new columns
        columns = get_census_columns(census_id)
        report_columns = get_report_columns(census_id)
        self.details.create_table(columns, report_columns)
        heading_list = get_census_headings(census_id)
        self.headings.create_table(heading_list)

    def save(self, button):
        """
        Called when the user clicks the OK button.
        """
        if self.widgets['census_combo'].get_active() == -1:
            ErrorDialog(_('Census Editor'),
                        _('Cannot save this census.  First select '
                          'a census from the drop-down list.'))
            return

        with DbTxn(self.get_menu_title(), self.db) as trans:
            if not self.event.get_handle():
                self.db.add_event(self.event, trans)

            self.headings.save()
            self.details.save(trans)

            citation_handle = self.citation.get_handle()
            if not citation_handle:
                citation_handle = self.db.add_citation(self.citation, trans)
                self.event.add_citation(citation_handle)
            else:
                self.db.commit_citation(self.citation, trans)

            self.db.commit_event(self.event, trans)
        self.close()

    def close(self, *args):
        """
        Close the editor window.
        """
        (width, height) = self.window.get_size()
        self._config.set('interface.census-width', width)
        self._config.set('interface.census-height', height)
        self._config.save()
        self.details.entry_grid.clean_up()
        self.details.clean_up()
        self.gallery_list.clean_up()
        ManagedWindow.close(self)

    def help_clicked(self, obj):
        """
        Display the relevant portion of GRAMPS manual
        """
        display_help(webpage='Census_Addons')
Ejemplo n.º 38
0
    def run(self):
        objects = []
        skipped = 0
        total_objects = 0
        total_notes = 0
        matching_notes = 0
        citations_added = 0
        sources_added = 0
        repos_added = 0
        with self.user.progress(_("Generating citations"), '',
                                self.db.get_total()) as step:

            repos = self.get_repos()
            sources = self.get_sources()
            notes_to_remove = {}
            for classname, obj in self.yield_objects(step):
                total_objects += 1
                obj.classname = classname
                obj.notes = []
                obj.citations = []
                current_citations = None
                for note, m, notehandle in self.find_matching_notes(obj):
                    matching_notes += 1

                    # get a list of current citations; this is in the loop to avoid computing the set for all objects
                    if current_citations is None:
                        current_citations = set()
                        for type, h in obj.get_referenced_citation_handles(
                        ):  # type = 'Citation'
                            citation = self.db.get_citation_from_handle(h)
                            page = citation.get_page()
                            current_citations.add(
                                (page, citation.source_handle))

                    source = sources[m.sourcetitle]
                    source.set_title(m.sourcetitle)

                    if (m.citationpage, source.handle) in current_citations:
                        skipped += 1
                        continue  # already has this citation, skip
                    repo = repos[m.reponame]
                    repo.set_name(m.reponame)

                    source.repo = repo

                    citation = Citation()
                    citation.set_page(m.citationpage)
                    citation.source = source
                    citation.note = m.details

                    obj.citations.append(citation)
                    obj.notes.append(notehandle)

                    notelines = note.get().splitlines()
                    if len(notelines) == 1:
                        notes_to_remove[notehandle] = m
                if obj.notes: objects.append(obj)

            with DbTxn(_("Add citations"), self.db) as trans:
                for obj in objects:
                    self.log("Object {}".format(obj))
                    for citation in obj.citations:
                        source = citation.source
                        repo = source.repo

                        citation_handle = self.db.add_citation(citation, trans)
                        source_handle = self.db.add_source(source, trans)
                        repo_handle = self.db.add_repository(repo, trans)

                        self.log("- Adding citation: {}".format(
                            citation.get_page()))

                        if not hasattr(repo, "in_db"):
                            repos_added += 1
                            self.log("- Adding repo: {}".format(
                                repo.get_name()))
                            repo.in_db = True
                        if not hasattr(source, "in_db"):
                            sources_added += 1
                            self.log("- Adding source: {}".format(
                                source.get_title()))
                            source.in_db = True

                        note = Note()
                        note.set(citation.note)
                        note.set_type(NoteType.LINK)
                        note_handle = self.db.add_note(note, trans)
                        citation.add_note(note_handle)
                        citation.newnote = note

                        citation.set_reference_handle(source_handle)
                        if not source.has_repo_reference(repo_handle):
                            reporef = RepoRef()
                            reporef.set_reference_handle(repo_handle)
                            source.add_repo_reference(reporef)
                        obj.add_citation(citation_handle)
                        citations_added += 1

                    for notehandle in obj.notes:
                        if notehandle in notes_to_remove:
                            obj.remove_note(notehandle)

                    commit_func = self.primary_objects[
                        obj.primary_object_class]['commit_func']
                    commit_func(obj.primary_object, trans)
                    for citation in obj.citations:
                        self.db.commit_citation(citation, trans)
                        self.db.commit_source(citation.source, trans)
                        self.db.commit_repository(source.repo, trans)
                        self.db.commit_note(citation.newnote, trans)

                for notehandle, m in notes_to_remove.items():
                    self.log("Removing note: {} {}".format(
                        repr(notehandle), m.line))
                    self.db.remove_note(notehandle, trans)

            log = []
            log.append(
                _("Total objects processed: {total_objects}").format(
                    total_objects=total_objects))
            log.append("- " + _("Total notes: {total_notes}").format(
                total_notes=self.total_notes))
            log.append("- " + _("Matching notes: {matching_notes}").format(
                matching_notes=matching_notes))
            log.append("- " +
                       _("Unique matching notes: {unique_matching_notes}"
                         ).format(unique_matching_notes=len(notes_to_remove)))
            log.append("- " +
                       _("Skipped notes: {skipped}").format(skipped=skipped))
            log.append("- " + _("Citations added: {citations_added}").format(
                citations_added=citations_added))
            log.append("- " + _("Sources added: {sources_added}").format(
                sources_added=sources_added))
            log.append("- " + _("Repositories added: {repos_added}").format(
                repos_added=repos_added))
            msg = "\n".join(log)
            msg += "\n\n" + _("You can undo all changes with Ctrl-Z.")
            self.log(msg)
            OkDialog(_("All events processed"),
                     msg,
                     parent=self.uistate.window)