refactoring

develop
gwen 2 years ago
parent 8e5d857d09
commit b15df2d4c5

@ -1,19 +1,17 @@
""" """
Mongodb storage init Mongodb storage init
- collections creation - collections queries initialization
- helper queries
TODO : maybe put the mongodb connector into the flask app object TODO : maybe put the mongodb connector into the flask app object
with the pymongo library. with the pymongo library.
""" """
import urllib.parse import urllib.parse
from unidecode import unidecode # to remove accents in house and prince names in the urls
from pymongo import MongoClient from pymongo import MongoClient
from .config import dbadmin, dbpassword, server_ip from .config import dbadmin, dbpassword, server_ip
from .helper import normalize_trigrams
# ______________________________________________________________________________ # ______________________________________________________________________________
# database connexion # database connexion
@ -37,6 +35,11 @@ helpers = actesdb["helpers"]
# ______________________________________________________________________________ # ______________________________________________________________________________
# storage extractor utilities # storage extractor utilities
def make_acteid_from_route(house=None, prince=None, date_and_item=None):
"/acte/Anjou/Isabelle_i/1441_08_05a -> anj_isa_i_1441_08_05a"
return "_".join([trigram_house(house), bigram_prince(prince), date_and_item])
def extract_princes_in_houses(): def extract_princes_in_houses():
"""Extracts all princes from a house """Extracts all princes from a house
by queries in the storage, (not by using csv metadatas) by queries in the storage, (not by using csv metadatas)
@ -70,14 +73,6 @@ def extract_prince_in_house(house, prince_code):
return pr return pr
def normalize_trigrams(trigram):
"""normalizes names, usefull for the uris routes
sample: Alençon -> Alencon
Orléans -> Orleans
"""
return {unidecode(value):key for key, value in trigram.items()}
# ______________________________________________________________________________ # ______________________________________________________________________________
# in memory storage extracted 'meta' informations upon the database # in memory storage extracted 'meta' informations upon the database
# TODO: if it takes too much time at launch, put it in something like # TODO: if it takes too much time at launch, put it in something like
@ -99,3 +94,23 @@ prince_bigram sample:
""" """
prince_bigram = normalize_trigrams(prince_bigram) prince_bigram = normalize_trigrams(prince_bigram)
def inverted_prince_bigram(bigram):
"Translates ch -> Charles"
inverted_prince_bigram = {value: key for key, value in prince_bigram.items()}
return inverted_prince_bigram[bigram]
def bigram_prince(prince):
"Translates Charles_i -> ch_i"
name, number = prince.split("_")
return prince_bigram[name] + "_" + number
def trigram_house(house):
return house_trigram[house]

@ -2,26 +2,7 @@
TODO: maybe all these calculations are to be put in the db storage TODO: maybe all these calculations are to be put in the db storage
""" """
from unidecode import unidecode # to remove accents in house and prince names in the urls
def inverted_prince_bigram(bigram):
"Translates ch -> Charles"
inverted_prince_bigram = {value: key for key, value in prince_bigram.items()}
return inverted_prince_bigram[bigram]
def bigram_prince(prince):
"Translates Charles_i -> ch_i"
name, number = prince.split("_")
return prince_bigram[name] + "_" + number
def trigram_house(house):
return house_trigram[house]
def make_acteid_from_route(house=None, prince=None, date_and_item=None):
"/acte/Anjou/Isabelle_i/1441_08_05a -> anj_isa_i_1441_08_05a"
return "_".join([trigram_house(house), bigram_prince(prince), date_and_item])
def make_timeitem_from_filename(filename): def make_timeitem_from_filename(filename):
""" """
@ -30,6 +11,14 @@ def make_timeitem_from_filename(filename):
trs_fname = filename.split('_') trs_fname = filename.split('_')
return "_".join(trs_fname[3:]) return "_".join(trs_fname[3:])
def normalize_trigrams(trigram):
"""normalizes names, usefull for the uris routes
sample: Alençon -> Alencon
Orléans -> Orleans
"""
return {unidecode(value):key for key, value in trigram.items()}
#from flask import abort #from flask import abort
# #
#def find_one_or_404(collection, **kwargs): #def find_one_or_404(collection, **kwargs):

@ -24,8 +24,9 @@ import typing as t
from flask import Blueprint, abort, render_template, request, send_from_directory from flask import Blueprint, abort, render_template, request, send_from_directory
import folium import folium
from pymongo import ASCENDING from pymongo import ASCENDING
from .helper import *
from .dbinit import * from .dbinit import *
from .helper import make_timeitem_from_filename
main = Blueprint("main", __name__, url_prefix="/") main = Blueprint("main", __name__, url_prefix="/")
@ -57,7 +58,7 @@ def corpora_all():
@main.route("/actes/<house>") # dont put a slash at the end @main.route("/actes/<house>") # dont put a slash at the end
def actes(house): def actes(house):
"""actes route """*actes* route, sample route: '/actes/Bourbon' or '/actes/Berry'
shows the princes in the selected house shows the princes in the selected house
@ -80,6 +81,8 @@ def actes(house):
@main.route("/actes/<house>/<prince>") # don't put a slash at the end @main.route("/actes/<house>/<prince>") # don't put a slash at the end
def prince_corpus(house=None, prince=None): def prince_corpus(house=None, prince=None):
"""copora prince, *timeline* view """copora prince, *timeline* view
sample route: '/actes/Berry/je_i' or '/actes/Anjou/lo_i'
""" """
prince_code = prince.lower() prince_code = prince.lower()
house = house.lower() house = house.lower()

Loading…
Cancel
Save