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
def fromData(data) -> 'PrintDB': if "Partiture" in data: if re.search("yes", data["Partiture"].lower()): partiture = True else: partiture = False else: partiture = False return PrintDB( Edition.fromData(data, CompositionDB.fromData(data)), data["Print Number"] if "Print Number" in data else None, partiture )
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
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
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)
def parseEdition(line, editors_arr, composition, year): edition_name = re.search("Edition: (.*[\S].*)", line) edition_name = edition_name.group( 1).strip() if edition_name is not None else None return Edition(composition, editors_arr, edition_name, year)