Esempio n. 1
0
def load(filename):
    tempData = {}
    prints = []
    for line in open(filename, "r"):
        if line not in ['\n', '\r\n']:
            data = line.split(":", 1)
            tempData[data[0].rstrip()] = data[1].rstrip().strip()
        else:
            if tempData:
                prints.append(Print.fromData(tempData))
                tempData = {}
    if tempData:
        prints.append(Print.fromData(tempData))
    return prints
Esempio n. 2
0
def process(block):
    _print = Print()
    composition = Composition()
    edition = Edition()
    voices = []
    _temp = {'print': _print, 'composition': composition, 'edition': edition, 'voices': voices}

    for line in block:
        parse(_temp, line)

    composition.voices = voices
    edition.composition = composition
    _print.edition = edition

    return _print
Esempio n. 3
0
def insert_score(p: Print) -> int:
    """ Vloží záznamy score(name, genre, key, incipit, year) do tabulky."""
    score = p.score()
    score.incipit = "".join(
        score.incipit)  # TODO: Jenom hack –> upravit formát 'incipit' pořádně

    incipit_lookup = 'incipit IS NULL' if not score.incipit else 'incipit=?'
    year_lookup = 'year IS NULL' if not score.year else 'year=?'

    # Vyhledávání v databázi v závislosti na přítomných parametrech
    get_score_id = f"SELECT id FROM score WHERE name=? AND genre=? AND key=? AND {incipit_lookup} AND {year_lookup}"
    sql_params = (score.name, score.genre, score.key)
    if score.incipit:
        sql_params += (score.incipit, )
    if score.year:
        sql_params += (score.year, )
    cursor.execute(get_score_id, sql_params)

    results = cursor.fetchall()
    if results:
        for result in results:
            if compare_authors(score, result[0]) and compare_voices(
                    score, result[0]):
                # Existující -> vrátit ID
                return result[0]

    # Chybí záznam -> přidat do tabulky
    cursor.execute(
        'INSERT INTO score(name, genre, key, incipit, year) VALUES(?, ?, ?, ?, ?)',
        (score.name, score.genre, score.key, score.incipit, score.year))
    db_connection.commit()

    score_id = cursor.lastrowid
    for voice in score.voices:
        # Vložit hlasy do 'voice' + commit
        cursor.execute(
            'INSERT INTO voice(number, score, range, name) VALUES(?, ?, ?, ?)',
            (
                voice.number,
                score_id,
                voice.range,
                voice.name,
            ))
        db_connection.commit()
    for author in score.authors:
        # Vložit autory do 'score_authors' + commit
        if not author.name:
            # Bezejmenný autor
            continue
        cursor.execute('SELECT id FROM person WHERE name=?',
                       (author.name, ))  # Najít ID autora
        result = cursor.fetchone()
        cursor.execute(
            'INSERT INTO score_author(score, composer) VALUES(?, ?)',
            (score_id, result[0]))  # Junction
        db_connection.commit()
    return score_id
Esempio n. 4
0
def parsePrint(line, edition):
    print_id = re.search("Print Number: (.*)", line)
    partiture_atr = False

    partiture = re.search("Partiture: (.*)", line)
    if partiture is not None:
        partiture_atr = False if 'yes' not in partiture.group(1) else True

    print_id_atr = print_id.group(1).strip()
    return Print(edition, print_id_atr, partiture_atr)
Esempio n. 5
0
def insert_edition(p: Print, score_id: int) -> int:
    """ Vloží záznamy edition(score, name, year) do tabulky."""
    edition = p.edition

    name_lookup = 'name IS NULL' if not edition.name else 'name=?'
    year_lookup = 'year IS NULL' if not p.score().year else 'year=?'

    # Vyhledávání v databázi v závislosti na přítomných parametrech
    get_edition_id = f"SELECT id FROM edition WHERE score=? AND {name_lookup} AND {year_lookup}"
    sql_params = (score_id, )
    if edition.name:
        sql_params += (edition.name, )
    if p.score().year:
        sql_params += (p.score().year, )
    cursor.execute(get_edition_id, sql_params)

    results = cursor.fetchall()
    if results:
        for result in results:
            if compare_editors(edition, result[0]):
                # Existující -> vrátit ID
                return result[0]
    # Chybí záznam -> přidat do tabulky
    cursor.execute('INSERT INTO edition(score, name, year) VALUES(?, ?, ?)',
                   (score_id, edition.name, p.score().year))
    db_connection.commit()

    edition_id = cursor.lastrowid
    for editor in edition.authors:
        # Vložit autory do 'edition_author' + commit
        cursor.execute('SELECT id FROM person WHERE name=?', (editor.name, ))
        result = cursor.fetchone()
        cursor.execute(
            'INSERT INTO edition_author(edition, editor) VALUES(?, ?)', (
                edition_id,
                result[0],
            ))  # Junction
        db_connection.commit()
    return edition_id
Esempio n. 6
0
def insert_people(p: Print):
    """ Vloží záznamy person(born, died, name) do tabulky."""
    for person in p.get_people():
        cursor.execute('SELECT * FROM person WHERE name=?',
                       (person.name, ))  # Je už uvnitř osoba stejného jména?
        found = cursor.fetchone()  # nalezený řádek
        if not found:
            cursor.execute(
                'INSERT INTO person(born, died, name) VALUES(?, ?, ?)',
                (person.born, person.died, person.name))
        else:
            # Již existující autor --> případně doplnit údaje
            if not found[1] and person.born:
                cursor.execute('UPDATE person SET born=? WHERE name=?',
                               (person.born, person.name))
            if not found[2] and person.died:
                cursor.execute('UPDATE person SET died=? WHERE name=?',
                               (person.died, person.name))
        db_connection.commit()
Esempio n. 7
0
def load(filename):

    file = open(filename, 'r')

    r_print_id = re.compile("Print Number: (.*)")
    r_composer = re.compile("Composer: (.*)")
    r_title = re.compile("Title: (.*)")
    r_genre = re.compile("Genre: (.*)")
    r_key = re.compile("Key: (.*)")
    r_composition_year = re.compile("Composition Year: (.*)")
    r_edition = re.compile("Edition: (.*)")
    r_editor = re.compile("Editor: (.*)")
    r_voice = re.compile("Voice (\d*): (.*)")
    r_partiture = re.compile("Partiture: (.*)")
    r_incipit = re.compile("Incipit: (.*)")

    r_new_entity = re.compile("\n")
    prints = []

    voices = []
    composition_class = Composition(None, None, None, None, None, None, None)
    edition_class = Edition(None, None, None)
    print_class = Print(None, None, None)

    for line in file:
        m = r_print_id.match(line)
        if m:
            print_id = load_print_id(m)
            print_class.print_id = print_id
            continue

        m = r_composer.match(line)
        if m:
            composition_authors = load_composer(m)
            composition_class.authors = composition_authors
            continue

        m = r_title.match(line)
        if m:
            title = load_title(m)
            composition_class.name = title
            continue

        m = r_genre.match(line)
        if m:
            genre = load_genre(m)
            composition_class.genre = genre
            continue

        m = r_key.match(line)
        if m:
            key = load_key(m)
            composition_class.key = key
            continue

        m = r_composition_year.match(line)
        if m:
            year = load_composition_year(m)
            composition_class.year = year
            continue

        m = r_partiture.match(line)
        if m:
            paritture = load_partiture(m)
            print_class.partiture = paritture
            continue

        m = r_incipit.match(line)
        if m:
            incipit = load_incipit(m)
            composition_class.incipit = incipit
            continue

        m = r_edition.match(line)
        if m:
            edition = load_edition(m)
            edition_class.name = edition
            continue

        m = r_editor.match(line)
        if m:
            authors = load_editor(m)
            edition_class.authors = authors
            continue

        m = r_voice.match(line)

        if m:
            voice = load_voice(m)
            voices.append(voice)
            composition_class.voices = voices
            continue

        m = r_new_entity.match(line)
        if m:
            edition_class.composition = composition_class
            print_class.edition = edition_class
            if not (print_class.print_id == None):
                prints.append(print_class)

                voices = []
                composition_class = Composition(None, None, None, None, None,
                                                None, None)
                edition_class = Edition(None, None, None)
                print_class = Print(None, None, None)

            continue

    return prints
Esempio n. 8
0
def load(filename):
    prints = []
    tmpValues = Template()
    for line in open(filename,"r"):
        if line == '\n': # save current print, and create blank one from template
            if tmpValues.print_num is not None: # when there are more new lines between prints, ignore second one
                prints.append(Print(Edition(Composition(tmpValues.title
                    ,tmpValues.incipit,tmpValues.key,tmpValues.genre
                    ,tmpValues.comp_year,tmpValues.voices
                    ,tmpValues.composers),tmpValues.editors
                    ,tmpValues.edition),tmpValues.print_num
                    ,tmpValues.partiture))
            tmpValues = Template()
        # DONE
        if line.startswith("Print Number"):
            number = line.split(':')[1].strip()
            tmpValues.print_num = None if number == "" else int(number)
        # DONE
        if line.startswith("Composer"):
            r = re.compile(r"Composer: (.*)")
            m = r.match(line)
            if m is None or m.group(1) == "": #  when there is no name
                continue
            rawcomp = m.group(1)
            comp = rawcomp.split(";")
            for c in comp:
                if not c:
                    continue
                s = re.compile(r"(.*) \((.*)\)") # separete name and years
                n = s.match(c)
                if n is None: # doesnt have (years)
                    composer = Person(c.strip(),None,None)
                    tmpValues.composers.append(composer)
                else:
                    name = n.group(1).strip()
                    n.group(2).strip()
                    t = re.compile(r"\d\d\d\d") # pattern for four digits = year
                    born = None
                    died = None
                    if "-" in n.group(2):
                        # if there is "-", split by "-" or "--"
                        if "--" in n.group(2):
                            years = n.group(2).split("--")
                        else:
                            years = n.group(2).split("-")
                        o = t.match(years[0])
                        if o is not None:
                            born = int(o.group(0))
                        o = t.match(years[1])
                        if o is not None:
                            died = int(o.group(0))
                    else: # otherwise try to find *,+, or there will be only one year
                        if "*" in n.group(2):
                            o = t.match(n.group(2)[1:])
                            if o is not None and o.group(0) != "":
                                born = int(o.group(0))
                        elif "+" in n.group(2):
                            o = t.match(n.group(2)[1:])
                            if o is not None and o.group(0) != "":
                                died = int(o.group(0))
                        else: # when there is only one year, i assign it to born
                            o = t.match(n.group(2))
                            if o is not None and o.group(0) != "":
                                born = int(o.group(0))
                    tmpValues.composers.append(Person(name, born, died))
        # DONE
        if line.startswith("Title"):
            title = line.split(":")[1].strip()
            tmpValues.title = None if title == "" else title
        # DONE
        if line.startswith("Genre"):
            genre = line.split(":")[1].strip()
            tmpValues.genre = None if genre == "" else genre
        # DONE
        if line.startswith("Key"):
            key = line.split(":")[1].strip()
            tmpValues.key = None if key == "" else key
        # DONE
        if line.startswith("Composition Year"):
            r = re.compile(r"Composition Year: (\d{4})")
            m = r.match(line)
            if m is not None:
                tmpValues.comp_year = int(m.group(1))
        # DONE
        if line.startswith("Edition"):
            edition = line.split(":")[1].strip()
            tmpValues.edition = None if edition == "" else edition
        # DONE
        if line.startswith("Editor"):
            r = re.compile(r"Editor: (.*)")
            m = r.match(line)
            if m is not None and m.group(1) != "":
                r = re.compile(r"((\w+, \w+.?),?)+") # pattern for word, word = lastname, firstname and there may be comma and other persons
                text = m.group(1)
                if r.match(text) is not None: # if firstname and lastname are separated by comma
                    while text != "":
                        m = r.match(text) # match them
                        tmpValues.editors.append(Person(m.group(2).strip(), None,None))# add them to output
                        text = text.replace(m.group(2), "")[2:] # remove them from string; # [2:] because there is ", " left in the beginning
                else: # if firstname and lastname are together, and persons are separated by comma
                    comps = text.split(",")
                    for comp in comps:
                        tmpValues.editors.append(Person(comp.strip(),None,None))
        # DONE
        if line.startswith("Voice"):
            voice = line.split(":")[1].strip()
            if voice != "": # if there is some voice
                r = re.compile(r"(\w+--\w+).*") # match two words and "--"" between them
                m = r.match(voice)
                if m is not None: # if there is range
                    range = m.group(1)
                    voice = voice.replace(m.group(1),"")[2:].strip() # strip range and ", "
                    name = None
                    if voice != "":
                        name = voice # if there is anything left for the name, assign it
                    tmpValues.voices.append(Voice(name, range))
                else: # there is no range
                    tmpValues.voices.append(Voice(voice.strip(),None))
            else: # there is no voice, but i need to remember position (Voice Number)
                tmpValues.voices.append(None)
        # DONE
        if line.startswith("Partiture"):
            partiture = line.split(":")[1].strip()
            if "yes" in partiture:
                tmpValues.partiture = True
        # DONE
        if line.startswith("Incipit"):
            incipit = line.split(":")[1].strip()
            if incipit != "" and tmpValues.incipit == None:
                tmpValues.incipit = incipit
    # on last print it doesnt catch new line at the end of the file
    # so after assigning all tmpValues, it ends reading the file in Incipit
    # and doesnt add it to prints
    # till it is still saved in tmValues
    # i can add that print afterwards
    # when there are two new lines at the end
    # then the last print will be added in for loop
    # and this condition catches that option
    # so it wont add new new print with deafalut None print number
    if tmpValues.print_num is not None:
        prints.append(Print(Edition(Composition(tmpValues.title
                        ,tmpValues.incipit,tmpValues.key,tmpValues.genre
                        ,tmpValues.comp_year,tmpValues.voices
                        ,tmpValues.composers),tmpValues.editors
                        ,tmpValues.edition),tmpValues.print_num
                        ,tmpValues.partiture))
    return prints
Esempio n. 9
0
def process_print(lines, conn):

    if not lines:
        return None

    print_id, composition_name, genre, key, year, edition_name, incipit = [None]*7
    authors, editors, voices = [], [], []
    partiture = False

    for line in lines:
        key_val = line.split(':')

        if key_val[0] == 'Print Number':
            print_id = int(key_val[1].strip())

        elif key_val[0] == 'Composer':
            authors = get_composers(key_val[1].strip(), conn)

        elif key_val[0] == 'Title':
            composition_name = key_val[1].strip()
            if not composition_name:
                composition_name = None

        elif key_val[0] == 'Genre':
            genre = key_val[1].strip()
            if not genre:
                genre = None

        elif key_val[0] == 'Key':
            key = key_val[1].strip()
            if not key:
                key = None

        elif key_val[0] == 'Composition Year':
            year = get_year(key_val[1].strip())
            if not year:
                year = None

        elif key_val[0] == 'Publication Year':
            pass

        elif key_val[0] == 'Edition':
            edition_name = key_val[1].strip()
            if not edition_name:
                edition_name = None

        elif key_val[0] == 'Editor':
            editors = get_editors(key_val[1].strip(), conn)

        elif 'Voice' in key_val[0]:
            voice_num = 1
            match = re.search(voice_regex, key_val[0])
            if match:
                voice_num = match.group(0)
            new_voice = get_voice(key_val[1].strip(), int(voice_num))
            if new_voice:
                voices.append(new_voice)

        elif key_val[0] == 'Partiture':
            if 'yes' in key_val[1]:
                partiture = True

        elif key_val[0] == 'Incipit':
            incipit = key_val[1].strip()
            if not incipit:
                incipit = None

    composition = Composition(composition_name, incipit, key, genre, year, voices, authors)
    comp_id = composition.store(conn)
    edition = Edition(composition, editors, edition_name)
    edition_id = edition.store(conn, comp_id)
    p = Print(edition, print_id, partiture)
    p.store(conn, edition_id)