def fromData(data) -> 'CompositionDB': voices = [] for key, val in data.items(): voiceMatch = re.match(r"(.*)Voice(.*)", key) if voiceMatch: voices.append(VoiceDB.fromData(voiceMatch.group(2), val)) composers = [] if "Composer" in data: for composer in data["Composer"].split(";"): if Person.fromData(composer.strip()): composers.append(Person.fromData(composer.strip())) title = data["Title"].rstrip().strip() if "Title" in data else None incipit = data["Incipit"].rstrip().strip() if "Incipit" in data else None key = data["Key"].rstrip().strip() if "Key" in data else None genre = data["Genre"].rstrip().strip() if "Genre" in data else None return Composition( title if title else None, incipit if incipit else None, key if key else None, genre if genre else None, Composition.processCompositionYear(data["Composition Year"]) if "Composition Year" in data else None, voices, composers )
def load_editor(match): editors = [] r1 = re.compile("(.*)(\((\+)*\d{4}(.*)\)){1}") r_borned = re.compile("\((\+)*(\d{4})(.*)") r_died = re.compile("(.*)(\d{4})(.*)") name = match.group(1).strip() for n in name.split(';'): match = r1.match(n) if match: person_name = match.group(1).strip() match_borned = r_borned.match(match.group(2).strip()) if match_borned: if (match_borned.group(1) == "+"): borned = None died = match_borned.group(2).strip() else: borned = match_borned.group(2).strip() match_died = r_died.match(match_borned.group(3).strip()) if match_died: died = match_died.group(2).strip() else: died = None else: borned = None died = None editors.append(Person(person_name, borned, died)) elif (not (n == "")): editors.append(Person(n, None, None)) return editors
def parseEditor(line): editors = [] if line == None: return editors r = re.compile(Regex['EDITOR']) m = r.findall(line) for name in m: p = Person() p.name = name.strip() editors.append(p) return editors
def parsePerson(line): people = line.split(';') peopleInstances = [] for item in people: if '(' not in item: parsedItem = Person(item.strip(), None, None) peopleInstances.append(parsedItem) else: r = re.search('[(](.*)[)]', item) isName = re.search('([^\d+*/-]+)', r.group(1)) name = item.strip().split('(')[0].strip() if isName is None else item.strip() born = re.search('^[*]?([0-9]{4})[-+]*[\s*-]', r.group(1)) died = re.search('[-+]+([0-9]{4})$', r.group(1)) born = born.group(1) if born is not None else None died = died.group(1) if died is not None else None parsedItem = Person(name, born, died) peopleInstances.append(parsedItem) return peopleInstances
def get_editors(line, conn): results = [] editors = line.split(',') if len(editors) < 2: names = editors else: names = [] for i in range(0, len(editors), 2): names.append(editors[i] + ',' + editors[i+1]) for name in names: p = Person(name.strip()) id = p.store(conn) if id: results.append(id) return results
def parseEditors(line): editors_arr = [] r = re.compile('(?:(?:[^\,]+\.?)(?:\,?\s+))?(?:[^\,]+\.?)') if line is not None: m = r.findall(line.group(1)) for editor in m: person = Person(editor.strip(), None, None) editors_arr.append(person) return editors_arr
def get_composers(composers, conn): composers = composers.split(';') results = [] for composer in composers: name = re.sub('\(.*\)', "", composer).strip() born, died = None, None match = re.search(born_regex, composer) if match: born = match.group(1) match = re.search(died_regex, composer) if match: died = match.group(1) p = Person(name, born, died) id = p.store(conn) if id: results.append(id) return results
def parseComposer(line): if line == None: return [] authors = [] for composer in line.split(';'): composer = composer.strip() person = Person() r = re.compile(Regex['COMPOSER']) m = r.match(composer) if m: person.name = m.group(1) if m.group(2) and isInt(m.group(2)): person.born = int(m.group(2)) if m.group(4) and isInt(m.group(4)): if m.group(3) == '*': person.born = int(m.group(4)) else: person.died = int(m.group(4)) else: person.name = composer authors.append(person) return authors
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