GeRod V.β.01 (code source)
De Rodovid FR
Version pour Rodovid.FR
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wikipedia as pywikibot
import urllib
nm_ur = pywikibot.getSite().loggedInAs()
nm_ur = nm_ur.encode('utf-8')
print "Connexion faite pour : ", nm_ur
lis_bio1 = ["BIRT", "BAPM", "CHR", "OCCU", "DEAT", "BURI", "CREM", "NCHI", "NMR"]
lis_bio2 = ["CENS", "DSCR", "EDUC", "GRAD", "CAST", "RESI", "RETI", "PROP", "TITL", "ADOP", "EMIG", "IMMI", "NATI", "NATU"]
lis_bio3 = ["RELI", "ORDN", "BLES", "CHRA", "CONF", "FCOM"]
lis_bio4 = ["WILL", "PROB"]
lis_bio5 = ["BARM", "BASM"]
lis_bio6 = ["BAPL" "CONL" "ENDL" "SLGC"]
lis_bio7 = ["EVEN"]
lis_bio8 = ["MARR", "MARB", "MARC", "ENGA", "DIV", "MARL", "MARS", "ANUL", "DIVF", "SLGS", "CENS", "EVEN"]
lis_bio = lis_bio1 + lis_bio2 + lis_bio3 + lis_bio4 + lis_bio5 + lis_bio6 + lis_bio7 + lis_bio8[0:-2]
#CENS et EVEN en double.
#IDNO (dédoublé en Y et note n°) et SSN (dédoublé en REFN n° et TYPE SSN)
lis_bis = []
for lis_bix in lis_bio:
lis_bis.append("1 " + lis_bix)
#Préparation des messages :
o_n_q = "Entrer o (pour oui) / n (pour non) ci-dessous : "
utf_q = "Votre fichier gedcom est-il enregistr\xc3\xa9 en codage utf-8 ? "
prf_q = "Avez-vous chang\xc3\xa9 vos pr\xc3\xa9f\xc3\xa9rences (onglet g\xc3\xa9n\xc3\xa9alogie) pour traiter les fiches Rodovid en mode Gedcom ? "
utf_c = unicode(utf_q, "utf-8")
prf_c = unicode(prf_q, "utf-8")
utf_r = ""
prf_r = ""
ged_q = "Entrer le nom complet du fichier gedcom (sans accents ni lettres d'autres alphabets) : "
qustn_fx = "Entrez l'identifiant de la fiche que vous recherchez (du type I007 ou F005) : "
#Pré-traitement d'une fiche pour remplacer l'identifiant par 0 :
def rectif_x(fc):
if "@ INDI\n" in fc:
in_cod = fc.index("@ INDI\n")
if "@ FAM\n" in fc:
in_cod = fc.index("@ FAM\n")
if "@ NOTE\n" in fc:
in_cod = fc.index("@ NOTE\n")
if "@ SOUR\n" in fc:
in_cod = fc.index("@ SOUR\n")
fc = "0 @0" + fc[in_cod:]
return fc
#Pré-traitement d'une fiche-personne pour séparer prénom et nom (et ajouter clan):
def rectif_nm(fc):
if "1 NAME" in fc:
ind_nm = fc.index("1 NAME")
fc_nm = fc[ind_nm:]
ind_nn = fc_nm.index("\n")
fc_nn = fc_nm[:ind_nn]
if "/" in fc_nn:
ind_ba = fc_nn.index("/")
if ind_ba > 7:
fc_ba = fc_nn[ind_ba + 1:]
if "/" in fc_ba:
ind_bz = fc_ba.index("/")
fc_lin = fc[ind_nm + ind_ba + 1:ind_nm + ind_ba + 1 + ind_bz]
if fc_lin != "":
fc_lin = "1 NOTE RDLINEAGE @" + fc_lin + "@\n"
if fc[ind_nm + ind_ba - 1] == " ":
fc = fc[:ind_nm + ind_ba - 1] + "\n1 NAME " + fc[ind_nm + ind_ba:]
else:
fc = fc[:ind_nm + ind_ba] + "\n1 NAME " + fc[ind_nm + ind_ba:]
#Segment pour ajouter clan:
fc_nl = fc[ind_nm + ind_ba + ind_bz:]
if "\n1 " in fc_nl:
ind_nl = fc_nl.index("\n1 ") + 1
ind_nl = ind_nm + ind_ba + ind_bz + ind_nl
fc = fc[:ind_nl] + fc_lin + fc[ind_nl:]
else:
fc = fc + fc_lin
return fc
#Pré-traitement d'une fiche pour ajouter une balise Rodovid "2 NOTE RDEVENTID @0@":
def rectif_nt(fc):
fc_t = fc + "1 \n"
fc_rc = fc_t
ind_bix = 0
while "\n1 " in fc_t:
fc_rd = ""
ind_bia = fc_t.index("\n1 ")
fc_bia = fc_t[ind_bia + 3:]
ind_bio = fc_bia.index("\n")
fc_bio = fc_t[ind_bia + 3: ind_bia + 3 + ind_bio]
if " " in fc_bio:
ind_bie = fc_bio.index(" ")
fc_bio = fc_bio[:ind_bie]
if fc_bio != "":
if fc_bio in lis_bio:
fc_rd = "2 NOTE RDEVENTID @0@\n"
fc_rc = fc_rc[:ind_bix + ind_bia + 3 + ind_bio + 1] + fc_rd + fc_rc[ind_bix + ind_bia + 3 + ind_bio + 1:]
ind_bix = ind_bix + len(fc_rd) + ind_bia + 3
fc_t = fc_t[ind_bia + 3:]
fc = fc_rc[:-3]
return fc
#Pré-traitement d'une fiche pour remplacer les matricules gedcom par les équivalents Rodovid (ou les supprimer) :
def rectif_lk(fc):
fc_xt = fc
for tp_x in ["HUSB", "WIFE", "FAMC", "FAMS", "CHIL"]:
fc_xr = ""
while tp_x in fc_xt:
ind_mt = fc_xt.index(tp_x)
fc_xa = fc_xt[:ind_mt - 2]
fc_xb = fc_xt[ind_mt - 2:ind_mt + 6]
fc_xc = fc_xt[ind_mt + 6:]
inz_mt = fc_xc.index("@")
fc_xc = fc_xt[ind_mt + 6:ind_mt + 6 + inz_mt]
fc_xd = fc_xt[ind_mt + 6 + inz_mt:ind_mt + 6 + inz_mt + 2]
fc_xe = fc_xt[ind_mt + 6 + inz_mt + 2:]
fc_xr = fc_xr + fc_xa
if fc_xc in lis_mat:
ind_gd = lis_mat.index(fc_xc)
mt_rd = lis_rod[ind_gd]
if mt_rd != "":
if tp_x == "FAMS":
if "@ INDI\n" in fc:
fc_xr = fc_xr + ""
else:
fc_xr = fc_xr + fc_xb + mt_rd + fc_xd
else:
fc_xr = fc_xr + fc_xb + mt_rd + fc_xd
else:
fc_xr = fc_xr + ""
else:
fc_xr = fc_xr + ""
fc_xt = fc_xe
fc_xt = fc_xr + fc_xt
return fc_xt
#Pré-traitement d'une fiche pour remplacer un style Gedcom par le style Gedcom de Rodovid :
def rectif_sr(fc):
fc_xt = fc + "1 TRLR"
fc_xr = fc_xt
#Sous-modification pour transformer un segment Gedcom en source :
def recodg_sr(fc_ss):
fc_sz = fc_ss
while "\n" in fc_ss:
ind_xn = fc_ss.index("\n") + 1
fc_sn = fc_ss[:ind_xn]
ind_xv = fc_sn.index(" ") + 1
fc_sv = fc_sn[ind_xv:]
if 1 == 1:
ind_xx = fc_sz.index(fc_ss)
ind_xy = fc_ss.index("\n")
ind_xz = ind_xx + ind_xy
fc_sz = fc_sz[:ind_xx] + "3 CONC " + fc_sz[ind_xx:ind_xz] + "<br>\n3 CONC" + fc_sz[ind_xz:]
fc_ss = fc_ss[ind_xn:]
if fc_sz[-6:] == " CONC\n":
if fc_sz[-8] == "\n":
fc_sz = fc_sz[:-12] + "\n"
if fc_sz[-9] == "\n":
fc_sz = fc_sz[:-13] + "\n"
fc_sz = "1 SOUR 1 NAME\n2 TEXT\n" + fc_sz
return fc_sz
#Sous-modification pour transformer un segment Gedcom en note :
def recodg_ev(fc_ss):
fc_sz = fc_ss
while "\n" in fc_ss:
ind_xn = fc_ss.index("\n") + 1
fc_sn = fc_ss[:ind_xn]
ind_xv = fc_sn.index(" ") + 1
fc_sv = fc_sn[ind_xv:]
if 1 == 1:
ind_xx = fc_sz.index(fc_ss)
ind_xy = fc_ss.index("\n")
ind_xz = ind_xx + ind_xy
fc_sz = fc_sz[:ind_xx] + "3 CONC " + fc_sz[ind_xx:ind_xz] + "<br>" + fc_sz[ind_xz:]
fc_ss = fc_ss[ind_xn:]
if fc_sz[-5:] == "<br>\n":
fc_sz = fc_sz[:-5] + "\n"
return fc_sz
#Préambule pour récupérer des sources extérieures à une fiche :
lis_sr = []
len_xt = len(fc_xt)
for srnt in range(len_xt):
if fc_xt[srnt:srnt + 7] == " SOUR @":
if "@" in fc_xt[srnt + 7:]:
ind_sn = fc_xt[srnt + 7:].index("@")
if fc_xt[srnt + 7:srnt + 7 + ind_sn] not in lis_sr:
lis_sr.append(fc_xt[srnt + 7:srnt + 7 + ind_sn])
tx_sc = ""
for el_sr in lis_sr:
if el_sr in lis_mat:
nm_sr = lis_mat.index(el_sr)
tx_sr = lis_fx[nm_sr]
tx_sr = recodg_ev(tx_sr)
tx_sr = tx_sr[6:]
ind_sc = tx_sr.index("\n") + 1
tx_sr = "1 SOUR" + tx_sr[:ind_sc - 5] + "\n" + "2 TEXT <br>\n" + "3 CONC\n" + tx_sr[ind_sc:]
tx_sc = tx_sc + tx_sr
#Corps principal du pré-traitement Gedcom :
while "\n1 " in fc_xt:
ind_rp = fc_xt.index("\n1 ") + 1
fc_xt = fc_xt[ind_rp:]
if "\n1 " in fc_xt:
ind_rq = fc_xt.index("\n1 ") + 1
fc_xs = fc_xt[:ind_rq]
#Passage pour traiter les données du nom inadaptées à Rodovid
if fc_xs[0:6] == "1 NAME":
if "\n2 " in fc_xs:
ind_rs = fc_xs.index("\n2 ") + 1
fc_xs = fc_xs[ind_rs:]
ind_zt = fc_xr.index(fc_xt)
fc_xz = fc_xs
len_xs = len(fc_xs)
fc_xz = recodg_sr(fc_xs)
fc_xr = fc_xr[:ind_zt + ind_rs] + fc_xz + fc_xr[ind_zt + ind_rs + len_xs:]
#Passage pour traiter les données d'un évènement inadaptées à Rodovid
if fc_xs[0:6] in lis_bis:
ids_zt = fc_xr.index(fc_xt)
zr = 0
fh_by = ""
fh_bz = ""
while "\n2 " in fc_xs:
xnote = 0
ids_bx = fc_xs.index("\n2 ") + 1
fh_bx = fc_xs[ids_bx:]
fc_xs = fc_xs[ids_bx:]
if zr == 0:
ids_bh = ids_bx
len_xh = len(fc_xs)
zr = zr + 1
if "\n2 " in fh_bx:
ids_by = fh_bx.index("\n2 ") + 1
fh_bx = fh_bx[:ids_by]
ids_bt = fh_bx.index("\n")
fh_bt = fh_bx[2:ids_bt]
if " " in fh_bt:
ids_bt = fh_bt.index(" ")
typ_bx = fh_bt[:ids_bt]
else:
typ_bx = fh_bt
if typ_bx in ['FAMC', 'ADDR', 'AGE', 'AGNC', 'CAUS', 'SOUR', 'OBJE']:
fh_bx = recodg_ev(fh_bx)
fh_bx = "2 NOTE N.B.<ref>\n" + fh_bx[:-1] + "</ref> \n"
fh_bz = fh_bz + fh_bx
else:
fh_by = fh_by + fh_bx
fh_bs = fh_by + fh_bz
if fh_bs != "":
fc_xr = fc_xr[:ids_zt + ids_bh] + fh_bs + fc_xr[ids_zt + ids_bh + len_xh:]
fc_xr = fc_xr[:-6] + tx_sc
return fc_xr
#Recherche sur Rodovid après l'enregistrement d'une fiche :
def confir_x(x_mt, x_tp):
urlcnt = "http://fr.rodovid.org/wk/Special:Contributions/" + nm_ur
txth = urllib.urlopen(urlcnt)
htmlSour = txth.read()
txth.close()
tem = 0
ht_cx = "pas vu"
g_mt = "::" + x_tp + " @" + x_mt + "@::"
while (tem == 0) and ("<li>" in htmlSour):
ind_sa = htmlSour.index("<li>")
ht_sa = htmlSour[ind_sa + 4:]
ind_sz = htmlSour.index("</li>")
ht_sn = htmlSour[ind_sa + 4: ind_sz]
htmlSour = htmlSour[ind_sz + 4:]
ind_ca = ht_sn.index("title=")
ht_ca = ht_sn[ind_ca + 6:]
ind_cz = ht_ca.index("&")
ht_cn = ht_ca[:ind_cz]
ind_cx = ht_cn.index(":")
ht_cx = ht_cn[ind_cx + 1:]
urlmat = "http://fr.rodovid.org/wk/" + ht_cn
tx_mt = urllib.urlopen(urlmat)
ht_mt = tx_mt.read()
tx_mt.close()
if g_mt in ht_mt:
tem = 1
if tem == 0:
ht_cx = "pas vu"
return ht_cx
#Démarrage du programme :
#Vérification du codage pour le fichier gedcom auprès de l'utilisateur :
print utf_c + o_n_q
utf_r = raw_input("? : ")
#Vérification des préférences auprès de l'utilisateur :
if utf_r == "o":
print prf_c + o_n_q
prf_r = raw_input("? : ")
#Saisie du nom du fichier Gedcom par l'utilisateur :
if prf_r == "o":
print ged_q
ged_r = raw_input("? : ")
#Chargement du fichier Gedcom :
in_file = open(ged_r, "r")
ged_txt = in_file.read()
in_file.close()
if ged_txt[0:3] == "\xef\xbb\xbf":
ged_txt = ged_txt[3:]
#Découpage du fichier Gedcom :
lis_fx = []
lis_mat = []
lis_typ = []
lis_rod = []
ged_txtm = ged_txt
rup = "\n0 "
lis_rup = ["@ INDI\n", "@ FAM\n", "@ NOTE\n", "@ SOUR\n", "@ REPO\n", "@ OBJE\n"]
rup_p = "@ INDI\n"
rup_f = "@ FAM\n"
while rup in ged_txtm:
ind_rup = ged_txtm.index(rup)
ged_txtm = ged_txtm[ind_rup + 1:]
if rup in ged_txtm:
inz_rup = ged_txtm.index(rup)
ged_txta = ged_txtm[:inz_rup + 1]
#Recherche du type et du matricule de chaque fiche :
for rup_x in lis_rup:
if rup_x in ged_txta:
ged_txtn = ged_txta[3:]
inr_rup = ged_txtn.index("@")
ged_txtr = ged_txtn[inr_rup + 2:]
ins_rup = ged_txtr.index("\n")
ged_typ = ged_txtr[:ins_rup]
ged_mat = ged_txtn[:inr_rup]
#Création des listes :
lis_typ.append(ged_typ)
lis_mat.append(ged_mat)
lis_fx.append(ged_txta)
#Recherche d'un identifiant Rodovid dans chaque section du fichier Gedcom :
if "1 NOTE RDID @" in ged_txta:
ind_rid = ged_txta.index("1 NOTE RDID @")
ged_txti = ged_txta[ind_rid + 13:]
inz_rid = ged_txti.index("@\n")
ged_txti = ged_txti[:inz_rid]
lis_rod.append(ged_txti)
else:
lis_rod.append("")
#Choix d'une fiche (personne ou famille) :
iter = "o"
while iter != "n":
succes = 0
while succes == 0:
try:
print qustn_fx
x_rch = raw_input("? : ")
ind_rch = lis_mat.index(x_rch)
fx_rch = lis_fx[ind_rch]
typ_rch = lis_typ[ind_rch]
#Remplacement du matricule par 0 :
fx_rch = rectif_x(fx_rch)
#Séparation du prénom et du nom :
if typ_rch == "INDI":
fx_rch = rectif_nm(fx_rch)
#Ajout ligne "2 NOTE RDEVENTID @0@" :
fx_rch = rectif_nt(fx_rch)
#Remplacement des matricules gedcom par leurs équivalents Rodovid :
fx_rch = rectif_lk(fx_rch)
fx_rch = rectif_sr(fx_rch)
#Ajout ligne Gedcom :
fx_rch = fx_rch + "1 REFN " + ged_r + "::" + typ_rch + " @" + x_rch + "@::" + nm_ur + "\n" + "2 TYPE GEDCOM\n"
#Création de l'adresse
if typ_rch == "INDI":
n_adr = "Personne:0"
if typ_rch == "FAM":
n_adr = "Famille:0"
#Codage du texte et du résumé :
u_txt = unicode(fx_rch, "utf-8")
n_cmnt = "Nouvelle page"
u_cmnt = unicode(n_cmnt, "utf-8")
print typ_rch
print x_rch
print fx_rch
print n_adr
#Enregistrement sur Rodovid :
if "1 NOTE RDID @" not in fx_rch:
print ("Verser cette fiche sur Rodovid ? : " + o_n_q)
fx_r = raw_input("? : ")
if fx_r == "o":
site = pywikibot.getSite()
page = pywikibot.Page(site, n_adr)
page.put(u_txt, comment=u_cmnt, watchArticle = None, minorEdit = False)
#Recherche du matricule du nouvel enregistrement sur Rodovid :
print "recherche"
rd_mt = confir_x(x_rch, typ_rch)
print rd_mt
if rd_mt != "pas vu":
fx_rch = fx_rch + "1 NOTE RDID @" + rd_mt + "@\n"
lis_fx[ind_rch] = fx_rch
ps_rch = "0 @" + x_rch + "@ " + typ_rch + "\n"
ind_ls = lis_mat.index(x_rch)
lis_rod[ind_ls] = rd_mt
ind_ps = ged_txt.index(ps_rch)
ind_pz = ged_txt[ind_ps:].index("\n0 ") + 1
ged_txt = ged_txt[:ind_ps + ind_pz] + "1 NOTE RDID @" + rd_mt + "@\n" + ged_txt[ind_ps + ind_pz:]
out_file = open(ged_r, "w")
out_file.write(ged_txt)
out_file.close()
else:
print "Sur Rodovid depuis un certain temps"
succes = 1
except ValueError:
print "Identifiant incorrect ou autre erreur. Recommencer ? o / n :"
x_prob = raw_input("? : ")
if x_prob == "n":
succes = 1
iter = raw_input("Autre fiche : o (pour oui) / n (pour non) : ")
Version pour Rodovid Engine :
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wikipedia as pywikibot
import urllib
nm_ur = pywikibot.getSite().loggedInAs()
nm_ur = nm_ur.encode('utf-8')
print "Connexion faite pour : ", nm_ur
lis_bio1 = ["BIRT", "BAPM", "CHR", "OCCU", "DEAT", "BURI", "CREM", "NCHI", "NMR"]
lis_bio2 = ["CENS", "DSCR", "EDUC", "GRAD", "CAST", "RESI", "RETI", "PROP", "TITL", "ADOP", "EMIG", "IMMI", "NATI", "NATU"]
lis_bio3 = ["RELI", "ORDN", "BLES", "CHRA", "CONF", "FCOM"]
lis_bio4 = ["WILL", "PROB"]
lis_bio5 = ["BARM", "BASM"]
lis_bio6 = ["BAPL" "CONL" "ENDL" "SLGC"]
lis_bio7 = ["EVEN"]
lis_bio8 = ["MARR", "MARB", "MARC", "ENGA", "DIV", "MARL", "MARS", "ANUL", "DIVF", "SLGS", "CENS", "EVEN"]
lis_bio = lis_bio1 + lis_bio2 + lis_bio3 + lis_bio4 + lis_bio5 + lis_bio6 + lis_bio7 + lis_bio8[0:-2]
#CENS et EVEN en double.
#IDNO (dédoublé en Y et note n°) et SSN (dédoublé en REFN n° et TYPE SSN)
lis_bis = []
for lis_bix in lis_bio:
lis_bis.append("1 " + lis_bix)
#Préparation des messages :
o_n_q = "Entrer o (pour oui) / n (pour non) ci-dessous : "
utf_q = "Votre fichier gedcom est-il enregistr\xc3\xa9 en codage utf-8 ? "
prf_q = "Avez-vous chang\xc3\xa9 vos pr\xc3\xa9f\xc3\xa9rences (onglet g\xc3\xa9n\xc3\xa9alogie) pour traiter les fiches Rodovid en mode Gedcom ? "
utf_c = unicode(utf_q, "utf-8")
prf_c = unicode(prf_q, "utf-8")
utf_r = ""
prf_r = ""
ged_q = "Entrer le nom complet du fichier gedcom (sans accents ni lettres d'autres alphabets) : "
qustn_fx = "Entrez l'identifiant de la fiche que vous recherchez (du type I007 ou F005) : "
#Pré-traitement d'une fiche pour remplacer l'identifiant par 0 :
def rectif_x(fc):
if "@ INDI\n" in fc:
in_cod = fc.index("@ INDI\n")
if "@ FAM\n" in fc:
in_cod = fc.index("@ FAM\n")
if "@ NOTE\n" in fc:
in_cod = fc.index("@ NOTE\n")
if "@ SOUR\n" in fc:
in_cod = fc.index("@ SOUR\n")
fc = "0 @0" + fc[in_cod:]
return fc
#Pré-traitement d'une fiche-personne pour séparer prénom et nom (et ajouter clan):
def rectif_nm(fc):
if "1 NAME" in fc:
ind_nm = fc.index("1 NAME")
fc_nm = fc[ind_nm:]
ind_nn = fc_nm.index("\n")
fc_nn = fc_nm[:ind_nn]
if "/" in fc_nn:
ind_ba = fc_nn.index("/")
if ind_ba > 7:
fc_ba = fc_nn[ind_ba + 1:]
if "/" in fc_ba:
ind_bz = fc_ba.index("/")
fc_lin = fc[ind_nm + ind_ba + 1:ind_nm + ind_ba + 1 + ind_bz]
if fc_lin != "":
fc_lin = "1 NOTE RDLINEAGE @" + fc_lin + "@\n"
if fc[ind_nm + ind_ba - 1] == " ":
fc = fc[:ind_nm + ind_ba - 1] + "\n1 NAME " + fc[ind_nm + ind_ba:]
else:
fc = fc[:ind_nm + ind_ba] + "\n1 NAME " + fc[ind_nm + ind_ba:]
#Segment pour ajouter clan:
fc_nl = fc[ind_nm + ind_ba + ind_bz:]
if "\n1 " in fc_nl:
ind_nl = fc_nl.index("\n1 ") + 1
ind_nl = ind_nm + ind_ba + ind_bz + ind_nl
fc = fc[:ind_nl] + fc_lin + fc[ind_nl:]
else:
fc = fc + fc_lin
return fc
#Pré-traitement d'une fiche pour ajouter une balise Rodovid "2 NOTE RDEVENTID @0@":
def rectif_nt(fc):
fc_t = fc + "1 \n"
fc_rc = fc_t
ind_bix = 0
while "\n1 " in fc_t:
fc_rd = ""
ind_bia = fc_t.index("\n1 ")
fc_bia = fc_t[ind_bia + 3:]
ind_bio = fc_bia.index("\n")
fc_bio = fc_t[ind_bia + 3: ind_bia + 3 + ind_bio]
if " " in fc_bio:
ind_bie = fc_bio.index(" ")
fc_bio = fc_bio[:ind_bie]
if fc_bio != "":
if fc_bio in lis_bio:
fc_rd = "2 NOTE RDEVENTID @0@\n"
fc_rc = fc_rc[:ind_bix + ind_bia + 3 + ind_bio + 1] + fc_rd + fc_rc[ind_bix + ind_bia + 3 + ind_bio + 1:]
ind_bix = ind_bix + len(fc_rd) + ind_bia + 3
fc_t = fc_t[ind_bia + 3:]
fc = fc_rc[:-3]
return fc
#Pré-traitement d'une fiche pour remplacer les matricules gedcom par les équivalents Rodovid (ou les supprimer) :
def rectif_lk(fc):
fc_xt = fc
for tp_x in ["HUSB", "WIFE", "FAMC", "FAMS", "CHIL"]:
fc_xr = ""
while tp_x in fc_xt:
ind_mt = fc_xt.index(tp_x)
fc_xa = fc_xt[:ind_mt - 2]
fc_xb = fc_xt[ind_mt - 2:ind_mt + 6]
fc_xc = fc_xt[ind_mt + 6:]
inz_mt = fc_xc.index("@")
fc_xc = fc_xt[ind_mt + 6:ind_mt + 6 + inz_mt]
fc_xd = fc_xt[ind_mt + 6 + inz_mt:ind_mt + 6 + inz_mt + 2]
fc_xe = fc_xt[ind_mt + 6 + inz_mt + 2:]
fc_xr = fc_xr + fc_xa
if fc_xc in lis_mat:
ind_gd = lis_mat.index(fc_xc)
mt_rd = lis_rod[ind_gd]
if mt_rd != "":
if tp_x == "FAMS":
if "@ INDI\n" in fc:
fc_xr = fc_xr + ""
else:
fc_xr = fc_xr + fc_xb + mt_rd + fc_xd
else:
fc_xr = fc_xr + fc_xb + mt_rd + fc_xd
else:
fc_xr = fc_xr + ""
else:
fc_xr = fc_xr + ""
fc_xt = fc_xe
fc_xt = fc_xr + fc_xt
return fc_xt
#Pré-traitement d'une fiche pour remplacer un style Gedcom par le style Gedcom de Rodovid :
def rectif_sr(fc):
fc_xt = fc + "1 TRLR"
fc_xr = fc_xt
#Sous-modification pour transformer un segment Gedcom en source :
def recodg_sr(fc_ss):
fc_sz = fc_ss
while "\n" in fc_ss:
ind_xn = fc_ss.index("\n") + 1
fc_sn = fc_ss[:ind_xn]
ind_xv = fc_sn.index(" ") + 1
fc_sv = fc_sn[ind_xv:]
if 1 == 1:
ind_xx = fc_sz.index(fc_ss)
ind_xy = fc_ss.index("\n")
ind_xz = ind_xx + ind_xy
fc_sz = fc_sz[:ind_xx] + "3 CONC " + fc_sz[ind_xx:ind_xz] + "<br>\n3 CONC" + fc_sz[ind_xz:]
fc_ss = fc_ss[ind_xn:]
if fc_sz[-6:] == " CONC\n":
if fc_sz[-8] == "\n":
fc_sz = fc_sz[:-12] + "\n"
if fc_sz[-9] == "\n":
fc_sz = fc_sz[:-13] + "\n"
fc_sz = "1 SOUR 1 NAME\n2 TEXT\n" + fc_sz
return fc_sz
#Sous-modification pour transformer un segment Gedcom en note :
def recodg_ev(fc_ss):
fc_sz = fc_ss
while "\n" in fc_ss:
ind_xn = fc_ss.index("\n") + 1
fc_sn = fc_ss[:ind_xn]
ind_xv = fc_sn.index(" ") + 1
fc_sv = fc_sn[ind_xv:]
if 1 == 1:
ind_xx = fc_sz.index(fc_ss)
ind_xy = fc_ss.index("\n")
ind_xz = ind_xx + ind_xy
fc_sz = fc_sz[:ind_xx] + "3 CONC " + fc_sz[ind_xx:ind_xz] + "<br>" + fc_sz[ind_xz:]
fc_ss = fc_ss[ind_xn:]
if fc_sz[-5:] == "<br>\n":
fc_sz = fc_sz[:-5] + "\n"
return fc_sz
#Préambule pour récupérer des sources extérieures à une fiche :
lis_sr = []
len_xt = len(fc_xt)
for srnt in range(len_xt):
if fc_xt[srnt:srnt + 7] == " SOUR @":
if "@" in fc_xt[srnt + 7:]:
ind_sn = fc_xt[srnt + 7:].index("@")
if fc_xt[srnt + 7:srnt + 7 + ind_sn] not in lis_sr:
lis_sr.append(fc_xt[srnt + 7:srnt + 7 + ind_sn])
tx_sc = ""
for el_sr in lis_sr:
if el_sr in lis_mat:
nm_sr = lis_mat.index(el_sr)
tx_sr = lis_fx[nm_sr]
tx_sr = recodg_ev(tx_sr)
tx_sr = tx_sr[6:]
ind_sc = tx_sr.index("\n") + 1
tx_sr = "1 SOUR" + tx_sr[:ind_sc - 5] + "\n" + "2 TEXT <br>\n" + "3 CONC\n" + tx_sr[ind_sc:]
tx_sc = tx_sc + tx_sr
#Corps principal du pré-traitement Gedcom :
while "\n1 " in fc_xt:
ind_rp = fc_xt.index("\n1 ") + 1
fc_xt = fc_xt[ind_rp:]
if "\n1 " in fc_xt:
ind_rq = fc_xt.index("\n1 ") + 1
fc_xs = fc_xt[:ind_rq]
#Passage pour traiter les données du nom inadaptées à Rodovid
if fc_xs[0:6] == "1 NAME":
if "\n2 " in fc_xs:
ind_rs = fc_xs.index("\n2 ") + 1
fc_xs = fc_xs[ind_rs:]
ind_zt = fc_xr.index(fc_xt)
fc_xz = fc_xs
len_xs = len(fc_xs)
fc_xz = recodg_sr(fc_xs)
fc_xr = fc_xr[:ind_zt + ind_rs] + fc_xz + fc_xr[ind_zt + ind_rs + len_xs:]
#Passage pour traiter les données d'un évènement inadaptées à Rodovid
if fc_xs[0:6] in lis_bis:
ids_zt = fc_xr.index(fc_xt)
zr = 0
fh_by = ""
fh_bz = ""
while "\n2 " in fc_xs:
xnote = 0
ids_bx = fc_xs.index("\n2 ") + 1
fh_bx = fc_xs[ids_bx:]
fc_xs = fc_xs[ids_bx:]
if zr == 0:
ids_bh = ids_bx
len_xh = len(fc_xs)
zr = zr + 1
if "\n2 " in fh_bx:
ids_by = fh_bx.index("\n2 ") + 1
fh_bx = fh_bx[:ids_by]
ids_bt = fh_bx.index("\n")
fh_bt = fh_bx[2:ids_bt]
if " " in fh_bt:
ids_bt = fh_bt.index(" ")
typ_bx = fh_bt[:ids_bt]
else:
typ_bx = fh_bt
if typ_bx in ['FAMC', 'ADDR', 'AGE', 'AGNC', 'CAUS', 'SOUR', 'OBJE']:
fh_bx = recodg_ev(fh_bx)
fh_bx = "2 NOTE N.B.<ref>\n" + fh_bx[:-1] + "</ref> \n"
fh_bz = fh_bz + fh_bx
else:
fh_by = fh_by + fh_bx
fh_bs = fh_by + fh_bz
if fh_bs != "":
fc_xr = fc_xr[:ids_zt + ids_bh] + fh_bs + fc_xr[ids_zt + ids_bh + len_xh:]
fc_xr = fc_xr[:-6] + tx_sc
return fc_xr
#Recherche sur Rodovid après l'enregistrement d'une fiche :
def confir_x(x_mt, x_tp):
urlcnt = "http://engine.rodovid.org/wk/Special:Contributions/" + nm_ur
txth = urllib.urlopen(urlcnt)
htmlSour = txth.read()
txth.close()
tem = 0
ht_cx = "pas vu"
g_mt = "::" + x_tp + " @" + x_mt + "@::"
while (tem == 0) and ("<li>" in htmlSour):
ind_sa = htmlSour.index("<li>")
ht_sa = htmlSour[ind_sa + 4:]
ind_sz = htmlSour.index("</li>")
ht_sn = htmlSour[ind_sa + 4: ind_sz]
htmlSour = htmlSour[ind_sz + 4:]
ind_ca = ht_sn.index("title=")
ht_ca = ht_sn[ind_ca + 6:]
ind_cz = ht_ca.index("&")
ht_cn = ht_ca[:ind_cz]
ind_cx = ht_cn.index(":")
ht_cx = ht_cn[ind_cx + 1:]
urlmat = "http://engine.rodovid.org/wk/" + ht_cn
tx_mt = urllib.urlopen(urlmat)
ht_mt = tx_mt.read()
tx_mt.close()
if g_mt in ht_mt:
tem = 1
if tem == 0:
ht_cx = "pas vu"
return ht_cx
#Démarrage du programme :
#Vérification du codage pour le fichier gedcom auprès de l'utilisateur :
print utf_c + o_n_q
utf_r = raw_input("? : ")
#Vérification des préférences auprès de l'utilisateur :
if utf_r == "o":
print prf_c + o_n_q
prf_r = raw_input("? : ")
#Saisie du nom du fichier Gedcom par l'utilisateur :
if prf_r == "o":
print ged_q
ged_r = raw_input("? : ")
#Chargement du fichier Gedcom :
in_file = open(ged_r, "r")
ged_txt = in_file.read()
in_file.close()
if ged_txt[0:3] == "\xef\xbb\xbf":
ged_txt = ged_txt[3:]
#Découpage du fichier Gedcom :
lis_fx = []
lis_mat = []
lis_typ = []
lis_rod = []
ged_txtm = ged_txt
rup = "\n0 "
lis_rup = ["@ INDI\n", "@ FAM\n", "@ NOTE\n", "@ SOUR\n", "@ REPO\n", "@ OBJE\n"]
rup_p = "@ INDI\n"
rup_f = "@ FAM\n"
while rup in ged_txtm:
ind_rup = ged_txtm.index(rup)
ged_txtm = ged_txtm[ind_rup + 1:]
if rup in ged_txtm:
inz_rup = ged_txtm.index(rup)
ged_txta = ged_txtm[:inz_rup + 1]
#Recherche du type et du matricule de chaque fiche :
for rup_x in lis_rup:
if rup_x in ged_txta:
ged_txtn = ged_txta[3:]
inr_rup = ged_txtn.index("@")
ged_txtr = ged_txtn[inr_rup + 2:]
ins_rup = ged_txtr.index("\n")
ged_typ = ged_txtr[:ins_rup]
ged_mat = ged_txtn[:inr_rup]
#Création des listes :
lis_typ.append(ged_typ)
lis_mat.append(ged_mat)
lis_fx.append(ged_txta)
#Recherche d'un identifiant Rodovid dans chaque section du fichier Gedcom :
if "1 NOTE RDID @" in ged_txta:
ind_rid = ged_txta.index("1 NOTE RDID @")
ged_txti = ged_txta[ind_rid + 13:]
inz_rid = ged_txti.index("@\n")
ged_txti = ged_txti[:inz_rid]
lis_rod.append(ged_txti)
else:
lis_rod.append("")
#Choix d'une fiche (personne ou famille) :
iter = "o"
while iter != "n":
succes = 0
while succes == 0:
try:
print qustn_fx
x_rch = raw_input("? : ")
ind_rch = lis_mat.index(x_rch)
fx_rch = lis_fx[ind_rch]
typ_rch = lis_typ[ind_rch]
#Remplacement du matricule par 0 :
fx_rch = rectif_x(fx_rch)
#Séparation du prénom et du nom :
if typ_rch == "INDI":
fx_rch = rectif_nm(fx_rch)
#Ajout ligne "2 NOTE RDEVENTID @0@" :
fx_rch = rectif_nt(fx_rch)
#Remplacement des matricules gedcom par leurs équivalents Rodovid :
fx_rch = rectif_lk(fx_rch)
fx_rch = rectif_sr(fx_rch)
#Ajout ligne Gedcom :
fx_rch = fx_rch + "1 REFN " + ged_r + "::" + typ_rch + " @" + x_rch + "@::" + nm_ur + "\n" + "2 TYPE GEDCOM\n"
#Création de l'adresse
if typ_rch == "INDI":
n_adr = "Person:0"
if typ_rch == "FAM":
n_adr = "Family:0"
#Codage du texte et du résumé :
u_txt = unicode(fx_rch, "utf-8")
n_cmnt = "Nouvelle page"
u_cmnt = unicode(n_cmnt, "utf-8")
print typ_rch
print x_rch
print fx_rch
print n_adr
#Enregistrement sur Rodovid :
if "1 NOTE RDID @" not in fx_rch:
print ("Verser cette fiche sur Rodovid ? : " + o_n_q)
fx_r = raw_input("? : ")
if fx_r == "o":
site = pywikibot.getSite()
page = pywikibot.Page(site, n_adr)
page.put(u_txt, comment=u_cmnt, watchArticle = None, minorEdit = False)
#Recherche du matricule du nouvel enregistrement sur Rodovid :
print "recherche"
rd_mt = confir_x(x_rch, typ_rch)
print rd_mt
if rd_mt != "pas vu":
fx_rch = fx_rch + "1 NOTE RDID @" + rd_mt + "@\n"
lis_fx[ind_rch] = fx_rch
ps_rch = "0 @" + x_rch + "@ " + typ_rch + "\n"
ind_ls = lis_mat.index(x_rch)
lis_rod[ind_ls] = rd_mt
ind_ps = ged_txt.index(ps_rch)
ind_pz = ged_txt[ind_ps:].index("\n0 ") + 1
ged_txt = ged_txt[:ind_ps + ind_pz] + "1 NOTE RDID @" + rd_mt + "@\n" + ged_txt[ind_ps + ind_pz:]
out_file = open(ged_r, "w")
out_file.write(ged_txt)
out_file.close()
else:
print "Sur Rodovid depuis un certain temps"
succes = 1
except ValueError:
print "Identifiant incorrect ou autre erreur. Recommencer ? o / n :"
x_prob = raw_input("? : ")
if x_prob == "n":
succes = 1
iter = raw_input("Autre fiche : o (pour oui) / n (pour non) : ")
