# Path: implementation/primaries/ExtractMetadata/classes/MetaParser.py
# class MetaParser(object):
# 
#     def __init__(self):
#         self.tags = []
#         self.attribs = {}
#         self.chars = {}
#         self.handlers = {
#             "part-name": make_new_part,
#             "key": handle_clef_or_key,
#             "clef": handle_clef_or_key,
#             "transpose": handleTransposition,
#             "time": handleMeter,
#             "metronome": handle_tempo,
#             "movement-title": handleBibliography,
#             "work-title": handleBibliography,
#             "creator": handleBibliography,
#             "credit-words": handleBibliography}
#         self.close_tags = ["beat-unit-dot"]
#         self.current_handler = None
#         self.parts = {}
#         self.data = {}
# 
#     def startTag(self, name, attrs):
#         self.tags.append(name)
#         self.attribs[name] = attrs
#         if name in self.handlers:
#             self.current_handler = self.handlers[name]
# 
#     def validateData(self, data):
# 
#         pass
# 
#     def newData(self, data):
#         if len(self.tags) > 0:
#             self.chars[self.tags[-1]] = data
#             if self.current_handler is not None and self.tags[
#                     -1] not in self.close_tags:
#                 self.current_handler(
#                     self.tags,
#                     self.attribs,
#                     self.chars,
#                     self.parts,
#                     self.data)
# 
#     def endTag(self, name):
#         if name in self.close_tags:
#             if self.current_handler is not None:
#                 self.current_handler(
#                     self.tags,
#                     self.attribs,
#                     self.chars,
#                     self.parts,
#                     self.data)
#         self.pop_name(name)
# 
#         if len(self.tags) > 0 and self.tags[-1] in self.handlers:
#             self.current_handler = self.handlers[self.tags[-1]]
# 
#     def pop_name(self, name):
#         if name in self.tags:
#             self.tags.remove(name)
#         if name in self.attribs:
#             self.attribs.pop(name)
#         if name in self.chars:
#             self.chars.pop(name)
# 
#     def parse(self, file):
#         parser = make_parser()
# 
#         parser.setContentHandler(Extractor(self))
#         # OFFLINE MODE
#         parser.setFeature(handler.feature_external_ges, False)
#         fob = open(file, 'r')
#         parser.parse(fob)
#         self.collate_parts()
#         fob.close()
#         return self.data
# 
#     def collate_parts(self):
#         instrument_list = []
#         keys = ["key", "clef"]
#         for part in self.parts:
#             data = {}
#             data["name"] = get_if_exists(
#                 self.parts[part], "name", "hello, world")
# 
#             if "transposition" in self.parts[part]:
#                 data.update(self.parts[part]["transposition"])
#                 # data["transposition"] = self.parts[part]["transposition"]
# 
#             for key in keys:
#                 if key in self.parts[part]:
#                     init_kv(self.data, "{}s".format(key), init_value={})
#                     self.data["{}s".format(key)][
#                         data["name"]] = remove_duplicates(
#                         self.parts[part][key])
# 
#             instrument_list.append(data)
#         self.data["instruments"] = instrument_list


import unittest
from unittest.mock import MagicMock
from implementation.primaries.ExtractMetadata.classes import MetaParser

    def testTransChromatic(self, parser):
        parser.startTag("part", {"id": "P1"})
        parser.startTag("transpose", {})
        parser.startTag("chromatic", {})
        parser.newData("1")
        assert parser.parts == {
            "P1": {
                "transposition": {
                    "chromatic": 1}}}

    def testTransChromaticAndDiatonic(self, parser):
        parser.startTag("part", {"id": "P1"})
        parser.startTag("transpose", {})
        parser.startTag("diatonic", {})
        parser.newData("0")
        parser.endTag("diatonic")
        parser.startTag("chromatic", {})
        parser.newData("1")
        assert parser.parts == {
            "P1": {
                "transposition": {
                    "diatonic": 0, "chromatic": 1}}}


class TestAddMeter(TestMetaParser):

    def testMeterHandler(self, parser):
        parser.startTag("part", {"id": "P1"})
        parser.startTag("time", {})

# Path: implementation/primaries/ExtractMetadata/classes/MetaParser.py
# class MetaParser(object):
# 
#     def __init__(self):
#         self.tags = []
#         self.attribs = {}
#         self.chars = {}
#         self.handlers = {
#             "part-name": make_new_part,
#             "key": handle_clef_or_key,
#             "clef": handle_clef_or_key,
#             "transpose": handleTransposition,
#             "time": handleMeter,
#             "metronome": handle_tempo,
#             "movement-title": handleBibliography,
#             "work-title": handleBibliography,
#             "creator": handleBibliography,
#             "credit-words": handleBibliography}
#         self.close_tags = ["beat-unit-dot"]
#         self.current_handler = None
#         self.parts = {}
#         self.data = {}
# 
#     def startTag(self, name, attrs):
#         self.tags.append(name)
#         self.attribs[name] = attrs
#         if name in self.handlers:
#             self.current_handler = self.handlers[name]
# 
#     def validateData(self, data):
# 
#         pass
# 
#     def newData(self, data):
#         if len(self.tags) > 0:
#             self.chars[self.tags[-1]] = data
#             if self.current_handler is not None and self.tags[
#                     -1] not in self.close_tags:
#                 self.current_handler(
#                     self.tags,
#                     self.attribs,
#                     self.chars,
#                     self.parts,
#                     self.data)
# 
#     def endTag(self, name):
#         if name in self.close_tags:
#             if self.current_handler is not None:
#                 self.current_handler(
#                     self.tags,
#                     self.attribs,
#                     self.chars,
#                     self.parts,
#                     self.data)
#         self.pop_name(name)
# 
#         if len(self.tags) > 0 and self.tags[-1] in self.handlers:
#             self.current_handler = self.handlers[self.tags[-1]]
# 
#     def pop_name(self, name):
#         if name in self.tags:
#             self.tags.remove(name)
#         if name in self.attribs:
#             self.attribs.pop(name)
#         if name in self.chars:
#             self.chars.pop(name)
# 
#     def parse(self, file):
#         parser = make_parser()
# 
#         parser.setContentHandler(Extractor(self))
#         # OFFLINE MODE
#         parser.setFeature(handler.feature_external_ges, False)
#         fob = open(file, 'r')
#         parser.parse(fob)
#         self.collate_parts()
#         fob.close()
#         return self.data
# 
#     def collate_parts(self):
#         instrument_list = []
#         keys = ["key", "clef"]
#         for part in self.parts:
#             data = {}
#             data["name"] = get_if_exists(
#                 self.parts[part], "name", "hello, world")
# 
#             if "transposition" in self.parts[part]:
#                 data.update(self.parts[part]["transposition"])
#                 # data["transposition"] = self.parts[part]["transposition"]
# 
#             for key in keys:
#                 if key in self.parts[part]:
#                     init_kv(self.data, "{}s".format(key), init_value={})
#                     self.data["{}s".format(key)][
#                         data["name"]] = remove_duplicates(
#                         self.parts[part][key])
# 
#             instrument_list.append(data)
#         self.data["instruments"] = instrument_list


# Path: implementation/primaries/ExtractMetadata/tests/test_meta_parser.py
import unittest
from unittest.mock import MagicMock
from implementation.primaries.ExtractMetadata.classes import MetaParser


    def testTransChromatic(self, parser):
        parser.startTag("part", {"id": "P1"})
        parser.startTag("transpose", {})
        parser.startTag("chromatic", {})
        parser.newData("1")
        assert parser.parts == {
            "P1": {
                "transposition": {
                    "chromatic": 1}}}

    def testTransChromaticAndDiatonic(self, parser):
        parser.startTag("part", {"id": "P1"})
        parser.startTag("transpose", {})
        parser.startTag("diatonic", {})
        parser.newData("0")
        parser.endTag("diatonic")
        parser.startTag("chromatic", {})
        parser.newData("1")
        assert parser.parts == {
            "P1": {
                "transposition": {
                    "diatonic": 0, "chromatic": 1}}}


class TestAddMeter(TestMetaParser):

    def testMeterHandler(self, parser):
        parser.startTag("part", {"id": "P1"})
        parser.startTag("time", {})

        assert parser.current_handler == MetaParser.handleMeter