示例#1
0
 def _error_check(self, command_response: etree) -> None:
     """commmand_response will be an XML Etree object."""
     error_list = command_response.find("./clierror")
     command_obj = command_response.find("./input")
     if error_list is not None:
         command = command_obj.text if command_obj is not None else "Unknown command"
         msg = etree.tostring(error_list).decode()
         raise NXAPICommandError(command, msg)
示例#2
0
def create_game(root: Element):
    game = game_models.Game(root.attrib['name'])
    game.max_players = root.attrib['max_players']
    game.min_players = root.attrib['min_players']
    add_actions(root.find("actions"), game)
    add_collections(root.find("collections"), game)
    add_turns(root.find("turns"), game)
    add_pieces(root.find("pieces"), game)
    return game
示例#3
0
def convert_metars(root: etree) -> List[Metar]:
    """
    Convert metar data for the database.

    :param root: XML etree root.
    :return: List of SQLAlchemy Base classes for Metars.
    """
    def process(kids: List[Element], xml_class: MetarXML) -> MetarXML:
        """
        Process the XML data so that it can be mapped to the database.

        :param kids: child branches of the etree.
        :param xml_class: Empty XML class object.
        :return: Instantiated class with loaded data.
        """
        for elt in kids:
            if elt.attrib:
                xml_class.add_child(process_attrib_metar(elt))
            else:
                kwarg = {elt.tag: elt.text}
                xml_class.set(**kwarg)
        return xml_class

    data = root.find("data")
    elems = data.findall("METAR")
    maps = []
    for elm in elems:
        children = list(elm)
        proc = process(children, MetarXML())
        mapped = proc.create_mapping()
        maps.append(mapped)
    return maps
示例#4
0
 def get_post_url(etroot: lxml.etree) -> str:
     """
     get post url from etree object
     :param etroot: lxml.etree object
     :return: url
     """
     post_url = "http:" + etroot.find('post_url').text
     return post_url
def insert_element(xml_tree: et, element: et.Element) -> None:
    namespaces = {'default': "http://www.tei-c.org/ns/1.0"}

    div_deposition = xml_tree.find('.//default:div[@type="deposition"]',
                                   namespaces=namespaces)
    div_deposition_parent = div_deposition.getparent()

    injection_position = div_deposition_parent.index(div_deposition) + 1
    div_deposition_parent.insert(injection_position, element)
示例#6
0
 def __init__(self, root: etree) -> None:
     self.no = int(get_xml_attrib(root, "no"))
     self.refid = root.find("refid").text
     self.name = root.find("name").text
     self.chara = int(root.find("chara").text)
     self.uid = root.find("uid").text
     self.cabid = int(root.find("cabid").text)
     self.is_succession = itob(int(root.find("is_succession").text))
示例#7
0
 def __init__(self, root: etree) -> None:
     self.name = root.find("name").text
     self.pref = int(root.find("pref").text)
     self.systemid = root.find("systemid").text
     self.softwareid = root.find("softwareid").text
     self.hardwareid = root.find("hardwareid").text
     self.locationid = root.find("locationid").text
     self.testmode = Testmode(root.find("testmode"))
示例#8
0
def get_abstract(parsed_document: etree, alt_text: str = 'n/a') -> str:
    try:
        abstract = parsed_document.find('front/article-meta/abstract/p').text
        if abstract is None:
            abstract = ''
            for section in parsed_document.findall(
                    'front/article-meta/abstract/sec/p'):
                # for sectioned abstracts
                abstract += str(section.text)
            if abstract == '':
                abstract = alt_text
    except AttributeError:
        abstract = alt_text
    return abstract
示例#9
0
def get_article_info(parsed_document: etree,
                     xml_location: str,
                     alt_text: str = 'n/a'):
    try:
        information = parsed_document.find(xml_location).text
    except AttributeError:
        pmc = get_pmc_from_xml(parsed_document)
        logging.error(f'Failed to parse information in PMC{pmc}')
        information = alt_text
    if information is None:
        pmc = get_pmc_from_xml(parsed_document)
        logging.warning(
            f'Unsuccessful parsing in PMC{pmc}. Replacing with: "{alt_text}"')
        information = alt_text
    return information
示例#10
0
def convert_airsigmets(root: etree) -> List[AirSigmet]:
    """
    Iterate through the airsigmets and their child data.

    :param root: Etree root.
    :return: Mapped data.
    """
    def process(
        kids: List[Element], asigx: Union[AirSigmetXML2, PointsXML2]
    ) -> Union[AirSigmetXML2, PointsXML2]:
        """
        Process the XML data so that it can be mapped to the database.

        :param kids: child branches of the etree.
        :param asigx: AirSigmetXML2 class
        :return: AirSigmetXML2 with data.
        """
        for elt in kids:
            grandkids = list(elt)
            if elt.attrib:
                for k, v in elt.attrib.items():
                    kwarg = {"{}__{}".format(elt.tag, k): v}
                    asigx.set(**kwarg)
            else:
                kwarg = {elt.tag: elt.text}
                asigx.set(**kwarg)
            if grandkids:
                for grandchild in grandkids:
                    if grandchild.tag == "point":
                        asigx.add_child(
                            process(grandchild.getchildren(), PointsXML2()))
                continue
        return asigx

    data = root.find("data")
    elems = data.findall("AIRSIGMET")
    maps = []
    for elm in elems:
        children = list(elm)
        proc = process(children, AirSigmetXML2())
        mapped = proc.create_mapping()
        maps.append(mapped)
    return maps
示例#11
0
    def __validate_message(root: etree, ignore_invalid_cert):

        server_cert_text = root.find(".//{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}BinarySecurityToken").text
        server_cert = helpers.parse_cert(b"-----BEGIN CERTIFICATE-----\n" + textwrap.fill(server_cert_text, 64).encode() + b"\n-----END CERTIFICATE-----\n")

        if server_cert.subject.get_attributes_for_oid(oid.NameOID.ORGANIZATION_NAME)[0].value != "Česká republika - Generální finanční ředitelství":
            raise ValueError("invalid server certificate")
            
        if not helpers._check_validity(server_cert):
            raise ValueError("server certificate expired")

        if not ignore_invalid_cert:
            # validate signature
            signature = root.find(".//{http://www.w3.org/2000/09/xmldsig#}SignatureValue").text
            signed_info = root.find(".//{http://www.w3.org/2000/09/xmldsig#}SignedInfo")
            signed_text = etree.tostring(signed_info, method='c14n', exclusive=True, with_comments=False)
            server_cert.public_key().verify(base64.b64decode(signature), signed_text, padding.PKCS1v15(), hashes.SHA256())
        
        # validate digest
        body = root.find(".//{http://schemas.xmlsoap.org/soap/envelope/}Body")
        body_text = etree.tostring(body, method='c14n', exclusive=True, with_comments=False)
        digest = base64.b64encode(hashlib.sha256(body_text).digest()).decode()
        if not root.find(".//{http://www.w3.org/2000/09/xmldsig#}DigestValue").text == digest:
            raise ValueError("invalid digest")

        # validate ref
        body_id = body.get("{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Id")
        ref_id = root.find(".//{http://www.w3.org/2000/09/xmldsig#}Reference").get("URI")

        if ref_id != "#" + body_id:
            raise ValueError("invalid body ref")

        # validate token
        BinarySecurityToken = root.find(".//{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}BinarySecurityToken")
        sec_token = BinarySecurityToken.get("{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Id")
        ref_token = root.find(".//{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}Reference").get("URI")

        if ref_token != "#" + sec_token:
            raise ValueError("invalid security token")
示例#12
0
def convert_tafs(root: etree) -> List[Taf]:
    """
    Convert Taf data for the database.

    :param root: XML etree root.
    :return: Lit of SQLAlchemy Base Taf classes.
    """
    def process(
            kids: List[Element],
            xml_class: Union[TafXML,
                             ForecastXML]) -> Union[TafXML, ForecastXML]:
        """
        Process the XML data so that it can be mapped to the database.

        :param kids: child branches of the etree.
        :param xml_class: Empty XML class object.
        :return: Instantiated class with loaded data.
        """
        for elt in kids:
            if elt.tag == "forecast":
                xml_class.add_child(process(list(elt), ForecastXML()))
                continue
            if elt.attrib:
                xml_class.add_child(process_attrib(elt))
            else:
                kwarg = {elt.tag: elt.text}
                xml_class.set(**kwarg)
        return xml_class

    data = root.find("data")
    elems = data.findall("TAF")
    maps = []
    for elm in elems:
        children = list(elm)
        proc = process(children, TafXML())
        mapped = proc.create_mapping()
        maps.append(mapped)
    return maps
示例#13
0
 def __init__(self, root: etree) -> None:
     self.game_joining_period = int(root.find("game_joining_period").text)
示例#14
0
 def __init__(self, root: etree) -> None:
     self.mode = int(root.find("mode").text)
     self.boss = int(root.find("boss").text)
     self.battle_aniv = int(root.find("battle_aniv").text)  # Possible Bool?
     self.free_music = int(root.find("free_music").text)
     self.free_chara = int(root.find("free_chara").text)
     self.event = int(root.find("event").text)
     self.battle_event = int(root.find("battle_event").text)
     self.champ = int(root.find("champ").text)
     self.item = int(root.find("item").text)
     self.quest = int(root.find("quest").text)
     self.campaign = int(root.find("campaign").text)
     self.gdp = int(root.find("gdp").text)
     self.v7 = int(root.find("v7").text)  # Possible Bool?
     self.champ_result = [
         int(x) for x in root.find("champ_result").text.split()
     ]
示例#15
0
def get_pmc_from_xml(parsed_document: etree) -> str:
    pmc = parsed_document.find(
        'front/article-meta/article-id[@pub-id-type="pmc"]').text
    return pmc
示例#16
0
 def __init__(self, root: etree) -> None:
     self.no = int(root.find("no").text)
     self.musicid = int(root.find("musicid").text)
     self.newmusic = itob(int(root.find("newmusic").text))
     self.longmusic = itob(int(root.find("longmusic").text))
     self.kind = int(root.find("kind").text)
示例#17
0
 def __init__(self, root: etree) -> None:
     self.mode = get_xml_attrib(root, "mode")
     self.stages = [Stage(x) for x in root.findall("stage")]
     self.session = int(root.find("session").text)
示例#18
0
 def __init__(self, root: etree) -> None:
     self.card = get_xml_attrib(root, "card")
     self.no = int(get_xml_attrib(root, "no"))
     self.playerinfo = Playerinfo(root.find("playerinfo"))
     self.playdata = [Playdata(x) for x in root.findall("playdata")]
示例#19
0
 def __init__(self, root: etree) -> None:
     self.enable = itob(int(root.find("enable").text))
示例#20
0
 def __init__(self, root: etree) -> None:
     self.refid = root.find("refid").text
     self.gdp = int(root.find("gdp").text)
     self.bad_play_kind = int(root.find("bad_play_kind").text)
     self.total_skill_point = int(root.find("total_skill_point").text)
     self.styles = int(root.find("styles").text)
     self.styles_2 = int(root.find("styles_2").text)
     self.secret_music = [
         int(x) for x in root.find("secret_music").text.split()
     ]
     self.chara = int(root.find("chara").text)
     self.secret_chara = int(root.find("secret_chara").text)
     self.syogo = [int(x) for x in root.find("syogo").text.split()]
     self.shutter_list = int(root.find("shutter_list").text)
     self.judge_logo_list = int(root.find("judge_logo_list").text)
     self.skin_list = int(root.find("skin_list").text)
     self.movie_list = int(root.find("movie_list").text)
     self.attack_effect_list = int(root.find("attack_effect_list").text)
     self.idle_screen = int(root.find("idle_screen").text)
     self.chance_point = int(root.find("chance_point").text)
     self.failed_cnt = int(root.find("failed_cnt").text)
     self.perfect = int(root.find("perfect").text)
     self.great = int(root.find("great").text)
     self.good = int(root.find("good").text)
     self.poor = int(root.find("poor").text)
     self.miss = int(root.find("miss").text)
     self.time = int(root.find("time").text)
     self.exc_clear_num = int(root.find("exc_clear_num").text)
     self.full_clear_num = int(root.find("full_clear_num").text)
     self.max_clear_difficulty = int(root.find("max_clear_difficulty").text)
     self.max_fullcombo_difficulty = int(
         root.find("max_fullcombo_difficulty").text)
     self.max_excellent_difficulty = int(
         root.find("max_excellent_difficulty").text)
     self.champ_group = int(root.find("champ_group").text)
     self.champ_kind = int(root.find("champ_kind").text)
     self.musicid = [int(x) for x in root.find("musicid").text.split()]
     self.object_musicid = [
         int(x) for x in root.find("object_musicid").text.split()
     ]
     self.seqmode = [int(x) for x in root.find("seqmode").text.split()]
     self.flags = [int(x) for x in root.find("flags").text.split()]
     self.score = [int(x) for x in root.find("score").text.split()]
     self.point = [int(x) for x in root.find("point").text.split()]
     self.info = Info(root.find("info"))
     self.customize = Customize(root.find("customize"))
     # There seems to be another `perfect` here for some reason
     self.bp = int(root.find("bp").text)
     self.reserv_item_list = [
         int(x) for x in root.find("reserv_item_list").text.split()
     ]
     self.rival_id_1 = root.find("rival_id_1").text
     self.rival_id_2 = root.find("rival_id_2").text
     self.rival_id_3 = root.find("rival_id_3").text
示例#21
0
 def __init__(self, root: etree) -> None:
     self.no = int(root.find("no").text)
     self.seqmode = int(root.find("seqmode").text)
     self.clear = itob(int(root.find("clear").text))
     self.auto_clear = itob(int(root.find("auto_clear").text))
     self.score = int(root.find("score").text)
     self.flags = int(root.find("flags").text)
     self.fullcombo = itob(int(root.find("fullcombo").text))
     self.excellent = itob(int(root.find("excellent").text))
     self.combo = int(root.find("combo").text)
     self.skill_point = int(root.find("skill_point").text)
     self.skill_perc = int(root.find("skill_perc").text)
     self.result_rank = int(root.find("result_rank").text)
     self.difficulty = int(root.find("difficulty").text)
     self.combo_rate = int(root.find("combo_rate").text)
     self.perfect_rate = int(root.find("perfect_rate").text)
示例#22
0
 def __init__(self, root: etree) -> None:
     self.is_shop_close_on = int(root.find("is_shop_close_on").text)
示例#23
0
 def __init__(self, root: etree) -> None:
     self.stand_alone = StandAlone(root.find("stand_alone"))
     self.session = Session(root.find("session"))
     self.game_settings = GameSettings(root.find("game_settings"))
示例#24
0
def drop_children_from_node(element: etree, children: List[str]) -> None:
    for child in children:
        element.remove(element.find(child))
示例#25
0
 def __init__(self, root: etree) -> None:
     self.kind = int(root.find("kind").text)
     self.offset = int(root.find("offset").text)
     self.music_nr = int(root.find("music_nr").text)
     self.cabid = int(root.find("cabid").text)
示例#26
0
 def __init__(self, root: etree) -> None:
     self.cabid = int(root.find("cabid").text)
示例#27
0
 def __init__(self, root: etree) -> None:
     self.card = get_xml_attrib(root, "card")
     self.no = int(get_xml_attrib(root, "no"))
     self.refid = root.find("refid").text
     self.request = Request(root.find("request"))
示例#28
0
 def __init__(self, root: etree) -> None:
     self.coin_slot = int(root.find("coin_slot").text)
示例#29
0
 def __init__(self, xml_root: etree) -> None:
     # Build our data from the `item` xml root object
     self.name = xml_root.find("name").text
     self.value = int(xml_root.find("value").text)
     self.time = datetime.fromtimestamp(int(xml_root.find("time").text))
示例#30
0
 def __init__(self, root: etree) -> None:
     self.shutter = int(root.find("shutter").text)
     self.info_level = int(root.find("info_level").text)
     self.name_disp = int(root.find("name_disp").text)  # Possible Bool?
     self.auto = int(root.find("auto").text)
     self.random = int(root.find("random").text)
     self.judge_logo = int(root.find("judge_logo").text)
     self.skin = int(root.find("skin").text)
     self.movie = int(root.find("movie").text)
     self.attack_effect = int(root.find("attack_effect").text)
     self.layout = int(root.find("layout").text)
     self.target_skill = int(root.find("target_skill").text)
     self.comparison = int(root.find("comparison").text)
     self.meter_custom = [
         int(x) for x in root.find("meter_custom").text.split()
     ]