def test_attributes(self): xml_with_attrib = parse_xml(self.get_file_path('CPL_SMPTE.xml')) xml_without_attrib = parse_xml(self.get_file_path('CPL_SMPTE.xml'), xml_attribs=False) self.assertNotEqual(xml_with_attrib, xml_without_attrib) xml_with_attrib = remove_key_dict(xml_with_attrib, ['@']) self.assertEqual(xml_with_attrib, xml_without_attrib)
def generic_parse( path, root_name, force_list=(), namespaces=DCP_SETTINGS['xmlns'] ): """ Parse an XML and returns a Python Dictionary """ try: res_dict = parse_xml( path, namespaces=namespaces, force_list=force_list) if res_dict and root_name in res_dict: node = res_dict[root_name] discover_schema(node) return { 'FileName': os.path.basename(path), 'FilePath': path, 'Info': { root_name: node } } except Exception as e: get_log().info("Error parsing XML {} : {}".format(path, str(e)))
def kdm_parse(path): """ Parse KDM XML """ in_dict = parse_xml(path, namespaces=DCP_SETTINGS['xmlns']) out_dict = {} keys = {} counter_mapping = { 'MDIK': 0, 'MDAK': 0, 'MDSK': 0, 'MDEK': 0 } root = None if in_dict and 'DCinemaSecurityMessage' in in_dict: auth_pub = in_dict['DCinemaSecurityMessage']['AuthenticatedPublic'] root = auth_pub['RequiredExtensions'] req_ext = auth_pub['RequiredExtensions'] if ('KDMRequiredExtensions' in req_ext and req_ext['KDMRequiredExtensions']['AuthorizedDeviceInfo']): root = req_ext['KDMRequiredExtensions'] keys_pub = root['KeyIdList']['TypedKeyId'] keys_priv = in_dict['DCinemaSecurityMessage']['AuthenticatedPrivate']['EncryptedKey'] for pub, priv in zip(keys_pub, keys_priv): keys[pub['KeyId']] = { 'Cipher': priv['CipherData']['CipherValue'] } key = pub['KeyType'] if key in counter_mapping: counter_mapping[key] += 1 devices = root['AuthorizedDeviceInfo']['DeviceList'] out_dict['AuthorizedDevice'] = devices['CertificateThumbprint'] out_dict['ContentTitleText'] = root['ContentTitleText'] out_dict['CompositionPlaylistId'] = root['CompositionPlaylistId'] out_dict['StartDate'] = root['ContentKeysNotValidBefore'] out_dict['EndDate'] = root['ContentKeysNotValidAfter'] out_dict['Recipient'] = root['Recipient']['X509SubjectName'] out_dict['Recipient'] = out_dict['Recipient'].split(',')[1] out_dict['ImageKeys'] = counter_mapping['MDIK'] out_dict['AudioKeys'] = counter_mapping['MDAK'] out_dict['SubtitleKeys'] = counter_mapping['MDSK'] out_dict['AtmosKeys'] = counter_mapping['MDEK'] out_dict['Keys'] = keys return { 'FileName': os.path.basename(path), 'FilePath': path, 'Info': { 'KDM': out_dict } }
def get_subtitle_xml(self, asset, folder): _, asset = asset if asset['Path'].endswith('.xml'): xml_path = os.path.join(self.dcp.path, asset['Path']) else: xml_path = os.path.join(folder, os.path.splitext(asset['Path'])[0]) if not os.path.exists(xml_path): return return parse_xml(xml_path, namespaces=DCP_SETTINGS['xmlns'], force_list=('Subtitle', ))
def filter_xml_by_root(self, root_name): """ Build a list of package XML files having a specific root node. """ xml_list = [] candidates = [ f for f in self._list_files if f.endswith('.xml') and not f.startswith('.') and os.path.dirname(f) == self.path ] for c in candidates: nodes = parse_xml(c, namespaces=DCP_SETTINGS['xmlns']) if root_name in nodes: xml_list.append(c) return xml_list
def kdm_parse(path): """ Parse KDM XML """ in_dict = parse_xml(path, namespaces=DCP_SETTINGS['xmlns']) out_dict = {} counter_mapping = { 'MDIK': 0, 'MDAK': 0, 'MDEK': 0 } root = None if in_dict and 'DCinemaSecurityMessage' in in_dict: auth_pub = in_dict['DCinemaSecurityMessage']['AuthenticatedPublic'] root = auth_pub['RequiredExtensions'] req_ext = auth_pub['RequiredExtensions'] if ('KDMRequiredExtensions' in req_ext and req_ext['KDMRequiredExtensions']['AuthorizedDeviceInfo']): root = req_ext['KDMRequiredExtensions'] for item in root['KeyIdList']['TypedKeyId']: if isinstance(item['KeyType'], dict): key = item['KeyType']['#text'] else: key = item['KeyType'] counter_mapping[key] += 1 devices = root['AuthorizedDeviceInfo']['DeviceList'] out_dict['AuthorizedDevice'] = devices['CertificateThumbprint'] out_dict['ContentTitleText'] = root['ContentTitleText'] out_dict['CompositionPlaylistId'] = root['CompositionPlaylistId'] out_dict['StartDate'] = root['ContentKeysNotValidBefore'] out_dict['EndDate'] = root['ContentKeysNotValidAfter'] out_dict['Recipient'] = root['Recipient']['X509SubjectName'] out_dict['Recipient'] = out_dict['Recipient'].split(',')[1] out_dict['ImageKeys'] = counter_mapping['MDIK'] out_dict['AudioKeys'] = counter_mapping['MDAK'] out_dict['AtmosKeys'] = counter_mapping['MDEK'] return { 'FileName': os.path.basename(path), 'FilePath': path, 'Info': { 'KDM': out_dict } }