Example #1
0
 def addRepository(repositoryName, reftag):
     ridno = db.find_next_repository_gramps_id()
     repository = Repository()
     repositoryType = RepositoryType()
     repositoryType.set(RepositoryType.ARCHIVE)
     repository.set_type(repositoryType)
     repository.set_gramps_id(ridno)
     repository.set_name(repositoryName)
     repository.set_url_list(())
     repository.set_change_time(chgtime)
     if reftag != None:
         repository.add_tag(reftag.get_handle())
     with DbTxn(_("Add Repository"), db) as trans:
         rhandle = db.add_repository(repository, trans)
     return repository
Example #2
0
    def buildRepository(self, ridno, rname, tag, rtype, rhandle=None):
        if rhandle == None:
            rhandle = id.create_id()  # 26-merkkinen tunniste
        repositoryType = RepositoryType()
        repositoryType.set(rtype)

        repository = Repository()
        repository.set_type(repositoryType)
        repository.set_handle(rhandle)
        repository.set_gramps_id(ridno)
        repository.set_name(rname)
        repository.set_change_time(self.chgtime)
        #       repository.set_color("#000000")
        repository.add_tag(tag.get_handle())
        #        print ("Repository ") ; print(repository.to_struct())
        return ([repository, rhandle])
Example #3
0
 def addRepository(repositoryName, repositoryUrl, reftag):
     ridno = db.find_next_repository_gramps_id()
     repository = Repository()
     repositoryType = RepositoryType()
     repositoryType.set(RepositoryType.ARCHIVE)
     repository.set_type(repositoryType)
     repository.set_gramps_id(ridno)
     repository.set_name(repositoryName)
     urlList = []
     #        for urlPath in repositoryUrls:
     url = Url()
     url.set_path(repositoryUrl)
     url.set_description('Sshy')
     url.set_type(UrlType.WEB_HOME)
     urlList.append(url)
     repository.set_url_list(urlList)
     repository.set_change_time(chgtime)
     if reftag != None:
         repository.add_tag(reftag.get_handle())
     with DbTxn(_("Add Repository"), db) as trans:
         rhandle = db.add_repository(repository, trans)
     return repository
Example #4
0
def importSourceHierarchies(db, filename, user):
    def findNextRidno(ridstrt):
        with DbTxn(_("Find next ridno"), db):
            prefix_save = db.get_repository_prefix()
            db.set_repository_id_prefix(ridstrt + '%05d')
            next_ridno = db.find_next_repository_gramps_id()
            LOG.debug('Next ridno = ' + next_ridno)
            db.set_repository_id_prefix(prefix_save)
        return next_ridno

    def findNextSidno(ridno):
        with DbTxn(_("Find next sidno"), db):
            prefix_save = db.get_source_prefix()
            db.set_source_id_prefix(ridno + '%05d')
            next_sidno = db.find_next_source_gramps_id()
            LOG.debug('Next sidno = ' + next_sidno)
            db.set_source_id_prefix(prefix_save)
        return next_sidno

    def checkTagExistence(otext):
        with DbTxn(_("Read Tag"), db):
            tag = db.get_tag_from_name(otext)
        if tag != None:
            LOG.debug('Tag found by name, no duplicates: ' + otext + ' ' +
                      tag.get_name())
        else:
            tag = Tag()
            tag.set_name(otext)
            tag.set_color("#EF2929")
            with DbTxn(_("Add Tag"), db) as trans:
                thandle = db.add_tag(tag, trans)
                LOG.debug('Tag added: ' + tag.get_name() + ' ' + thandle)
        return tag

    fdir = os.path.dirname(filename)

    fh = logging.FileHandler(fdir + '\\sourceimport.log')
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)
    LOG.addHandler(fh)

    LOG.info("   fdir = " + fdir)
    cout = fdir + "\\sresult.csv"
    LOG.debug('ini file handling')

    config = configman.register_manager("importsources")
    '''
    config.register("options.repositoryidrng", "1000")    
    config.register("options.repositoryincr", "1") 
    config.register("options.sourceidrng", "1000")    
    config.register("options.sourceidincr", "1") 
    '''
    config.register("options.refstring", "r")

    config.load()
    config.save()
    '''     
    repository_idrange = int(config.get('options.repositoryidrng'))
    repository_incr = int(config.get('options.repositoryincr'))
    source_idrange = int(config.get('options.sourceidrng'))
    source_idincr = int(config.get('options.sourceidincr'))
    '''
    refstr = config.get('options.refstring')
    '''
    repository_idno  = 0
    source_idno = 0
    '''

    t_count = 0
    r_count = 0
    s_count = 0
    c_count = 0
    u_count = 0

    ridno = None
    sidno = None

    reftext = 'Referenssi'
    reftag = checkTagExistence(reftext)

    chgtime = int(time.time())
    LOG.info("   chgtime = " + str(chgtime))

    try:
        with open(cout, 'w', newline='\n', encoding="utf-8-sig") as csv_out:
            #            csv_out.write(u'\uFEFF')        #   utf-8-bom
            r_writer = csv.writer(csv_out, delimiter=';')

            with open(filename, 'r', encoding="utf-8-sig") as t_in:
                rhandle = None
                t_dialect = csv.Sniffer().sniff(t_in.read(1024))
                t_in.seek(0)
                t_reader = csv.reader(t_in, t_dialect)
                LOG.info('CSV input file delimiter is ' + t_dialect.delimiter)
                for row in t_reader:

                    rectype = row[
                        0]  # Record type = Gramps object id prefix character
                    #                    LOG.debug('Row type: -' + row[0] + '-')

                    if rectype == '#':
                        LOG.debug('Comment row: ' + row[0])
                        c_count += 1
                        r_writer.writerow([
                            row[0], row[1], row[2], row[3], row[4], row[5],
                            row[6], row[7]
                        ])

                    else:
                        idno = row[
                            2]  # Possibly previously assigned Gramps object id
                        handle = row[3].strip(
                            '"'
                        )  # Possibly previously assigned Gramps object handle
                        otext = row[4].strip()
                        LOG.debug('Handle = ' + handle)
                        '''                    
                        if rectype == 'T':
                            LOG.debug('Tag row: ' + row[0])
                            thandle = ''
                            t_count += 1
                            recobj  = row[1]     # Tag related to repositories or sources
                            tag = None
                            with DbTxn(_("Read Tag"), db):
                                tag = db.get_tag_from_name(otext)
                            if tag != None: 
                                LOG.info('Tag found by name, no duplicates: ' + otext + ' ' + tag.get_name())       
                                thandle = tag.get_handle()
                            else:       
                                tag = Tag()                  
                                tag.set_name(otext)
                                tag.set_change_time(chgtime)
                                tag.set_color("#EF2929")
                                with DbTxn(_("Add Tag"), db) as trans:
                                    thandle = db.add_tag(tag, trans)
                                    LOG.info('Tag added: ' + tag.get_name() + ' ' + thandle)
                            tags[recobj] = tag
                            try: 
                                r_writer.writerow([rectype, recobj, '', '"' + thandle + '"', otext, '', '', '', ''])
                            except IOError:    
                                LOG.error('Error writing T-csv '  + IOError.strerror) 
                        '''

                        if rectype == 'R':
                            LOG.debug('Repository row: ' + row[0])
                            rhandle = ''
                            #                            source_idno = 0
                            r_count += 1
                            repotype = row[1]  # repository type number
                            if idno == '':
                                if refstr == 'r':
                                    # repository type based numbering should be applied but not supplied by Gramps
                                    ridno = findNextRidno(rectype + refstr +
                                                          '9')
#                                   repository_idno = repository_idno + repository_incr
#                                   ridno = rectype + refstr + str(int(repotype) * repository_idrange + repository_idno)
                                else:
                                    ridno = db.find_next_repository_gramps_id()

                            else:
                                ridno = idno
                            LOG.debug('Ridno = ' + str(ridno))
                            repository = Repository()
                            if handle != '':
                                with DbTxn(_("Read Repository"), db) as trans:
                                    repository = db.get_repository_from_handle(
                                        handle)
                                    if repository == None:
                                        LOG.error(
                                            'Repository NOT found by handle: '
                                            + handle + ' ' + otext)
                                        raise GrampsImportError(
                                            'Repository NOT found by handle: ',
                                            handle + '/' + otext)
                            repositoryType = RepositoryType()
                            repositoryType.set(int(repotype))
                            repository.set_type(repositoryType)
                            repository.set_gramps_id(ridno)
                            repository.set_name(otext)
                            repository.set_change_time(chgtime)
                            if reftag != None:
                                repository.add_tag(reftag.get_handle())
                            if handle == '':
                                with DbTxn(_("Add Repository"), db) as trans:
                                    rhandle = db.add_repository(
                                        repository, trans)
                            else:
                                with DbTxn(_("Update Repository"),
                                           db) as trans:
                                    db.commit_repository(repository, trans)
                                    rhandle = handle
                            try:
                                r_writer.writerow([
                                    rectype, repotype, ridno,
                                    '"' + rhandle + '"', otext, '', '', '', ''
                                ])
                            except IOError:
                                LOG.error('Error writing R-csv ' +
                                          IOError.strerror)

                        elif rectype == 'S':
                            LOG.debug('Source row: ' + row[0])
                            shandle = ''
                            sidno = ''
                            s_count += 1
                            attribs = (row[5], row[6], row[7])

                            if idno == '':
                                LOG.debug('Ridno for sidno = ' + str(ridno))
                                if refstr == 'r':
                                    # repository type based numbering should be applied but not supplied by Gramps
                                    sidno = findNextSidno(rectype + refstr +
                                                          '9')
#                                   source_idno = source_idno + source_idincr
#                                   sidno = rectype + refstr + str((int(repotype) * repository_idrange + repository_idno) * source_idrange + source_idno)
                                else:
                                    sidno = db.find_next_source_gramps_id()
                            else:
                                sidno = idno
                            LOG.debug('Sidno = ' + str(sidno))
                            source = Source()
                            if handle != '':
                                with DbTxn(_("Read Source"), db) as trans:
                                    source = db.get_source_from_handle(handle)
                                    if source == None:
                                        LOG.error(
                                            'Source NOT found by handle: ' +
                                            handle + ' ' + otext)
                                        raise GrampsImportError(
                                            'Source NOT found by handle: ',
                                            handle + '/' + otext)
                            source.set_gramps_id(sidno)
                            source.set_title(otext)
                            source.set_author(attribs[0])
                            source.set_publication_info(attribs[1])
                            source.set_abbreviation(attribs[2])
                            if reftag != None:
                                source.add_tag(reftag.get_handle())
                            repoRef = RepoRef()
                            repoRef.set_reference_handle(rhandle)
                            source.add_repo_reference(repoRef)
                            source.set_change_time(chgtime)
                            if handle == '':
                                with DbTxn(_("Add Source"), db) as trans:
                                    shandle = db.add_source(source, trans)
                            else:
                                with DbTxn(_("Update Source"), db) as trans:
                                    db.commit_source(source, trans)
                                    shandle = handle
                            try:
                                r_writer.writerow([
                                    rectype, '', sidno, '"' + shandle + '"',
                                    otext, attribs[0], attribs[1], attribs[2],
                                    ''
                                ])
                            except IOError:
                                LOG.error('Error writing S-csv ' +
                                          IOError.strerror)

                        else:
                            u_count += 1
                            LOG.debug('Unknown rectype: ' + rectype)
                            raise GrampsImportError('Unknown record type ' +
                                                    rectype)

    except:
        exc = sys.exc_info()[0]
        LOG.error('*** Something went really wrong! ', exc)

        return ImportInfo({_('Results'): _('Something went really wrong  ')})

    results = {
        _('Results'):
        _('Input file handled.'),
        _('    Repositories   '):
        str(r_count),
        _('    Sources        '):
        str(s_count),
        _('    Comments       '):
        str(c_count),
        _('    Unknown types  '):
        str(u_count),
        _('  Total            '):
        str(t_count + r_count + s_count + c_count + u_count)
    }

    LOG.info('Input file handled.')
    LOG.info('    Repositories   ' + str(r_count))
    LOG.info('    Sources        ' + str(s_count))
    LOG.info('    Comments       ' + str(c_count))
    LOG.info('    Unknown types  ' + str(u_count))
    LOG.info('  Total            ' +
             str(t_count + r_count + s_count + c_count + u_count))

    db.enable_signals()
    db.request_rebuild()

    return ImportInfo(results)