Ejemplo n.º 1
0
    def extract_subtitle_data(ttml_file):
        data = minidom.parse(ttml_file)

        s_encoding = data.encoding
        if s_encoding and s_encoding.lower() not in ['utf8', 'utf-8']:
            # Don't bother with subtitles that aren't utf-8 encoded
            # but assume utf-8 when the encoding attr is missing
            raise NotImplementedError('Source is not declared as utf-8')

        # Get the root tt element (assume the file contains
        # a single subtitle document)
        tt_element = data.getElementsByTagName('tt')[0]

        # Get Language
        try:
            lang = str(tt_element.attributes['xml:lang'].value)
        except KeyError:
            lang = None

        # Detect source FPS and tick rate
        try:
            fps = int(tt_element.attributes['ttp:frameRate'].value)
        except KeyError:
            fps = None
        try:
            tick_rate = int(tt_element.attributes['ttp:tickRate'].value)
        except KeyError:
            tick_rate = None

        lines = [i for i in data.getElementsByTagName('p') if 'begin' in list(i.attributes.keys())]

        return {'fps': fps, 'tick_rate': tick_rate, 'lines': lines, 'lang': lang}
Ejemplo n.º 2
0
 def is_format(self):
     """Return True if the file is an XML file."""
     try:
         self.dom = parse(self.file)
     except (ParseError, ExpatError, UnicodeDecodeError):
         return False
     return True
Ejemplo n.º 3
0
def read_file(filename: str) -> SierraEcgFile:
    xdom = minidom.parse(filename)
    root = get_node(xdom, "restingecgdata")
    (doc_type, doc_ver) = assert_version(root)

    leads = assert_leads(root)

    lead_i = leads[0].samples
    lead_ii = leads[1].samples
    lead_iii = leads[2].samples
    lead_avr = leads[3].samples
    lead_avl = leads[4].samples
    lead_avf = leads[5].samples

    for i in range(len(lead_iii)):
        lead_iii[i] = lead_ii[i] - lead_i[i] - lead_iii[i]

    for i in range(len(lead_avr)):
        lead_avr[i] = -lead_avr[i] - floor((lead_i[i] + lead_ii[i]) / 2)

    for i in range(len(lead_avl)):
        lead_avl[i] = floor((lead_i[i] - lead_iii[i]) / 2) - lead_avl[i]

    for i in range(len(lead_avf)):
        lead_avf[i] = floor((lead_ii[i] + lead_iii[i]) / 2) - lead_avf[i]

    sierra_ecg_file = SierraEcgFile()
    sierra_ecg_file.doc_type = doc_type
    sierra_ecg_file.doc_ver = doc_ver
    sierra_ecg_file.leads = leads

    return sierra_ecg_file
Ejemplo n.º 4
0
    def get_animation_details(self):
        """ Build a dictionary of all animation settings and properties from XML """

        if not self.selected:
            return {}
        elif self.selected and self.selected.row() == -1:
            return {}

        # Get all selected rows items
        ItemRow = self.blender_model.model.itemFromIndex(self.selected).row()
        animation_title = self.blender_model.model.item(ItemRow, 1).text()
        xml_path = self.blender_model.model.item(ItemRow, 2).text()
        service = self.blender_model.model.item(ItemRow, 3).text()

        # load xml effect file
        xmldoc = xml.parse(xml_path)

        # Get list of params
        animation = {
            "title": animation_title,
            "path": xml_path,
            "service": service,
            "params": []
        }

        # Loop through params
        for param in xmldoc.getElementsByTagName("param"):
            # Set up item dict, "default" key is required
            param_item = {"default": ""}

            # Get details of param
            for att in ["title", "description", "name", "type"]:
                if param.attributes[att]:
                    param_item[att] = param.attributes[att].value

            for tag in ["min", "max", "step", "digits", "default"]:
                for p in param.getElementsByTagName(tag):
                    if p.childNodes:
                        param_item[tag] = p.firstChild.data

            try:
                # Build values dict from list of (name, num) tuples
                param_item["values"] = dict([
                    (p.attributes["name"].value, p.attributes["num"].value)
                    for p in param.getElementsByTagName("value")
                    if ("name" in p.attributes and "num" in p.attributes)
                ])
            except (TypeError, AttributeError) as ex:
                log.warn("XML parser: %s", ex)
                pass

            # Append param object to list
            animation["params"].append(param_item)

        # Free up XML document memory
        xmldoc.unlink()

        # Return animation dictionary
        return animation
Ejemplo n.º 5
0
    def __init__(self, bytes_):
        docx_io = io.BytesIO(bytes_)
        self._docx = zipfile.ZipFile(docx_io)

        document_xml_bytes = self._docx.read("word/document.xml")

        document_xml_io = io.BytesIO(document_xml_bytes)
        encoding = parse(document_xml_io).encoding

        self.document_xml = document_xml_bytes.decode(encoding)
Ejemplo n.º 6
0
def steam_profile_games(profile):
	try:
		from defusedxml import minidom
	except ImportError:
		print>>sys.stderr, 'WARNING: pyhon-defusedxml not available, falling back to unsafe standard libraries...'
		from xml.dom import minidom

	sub = steam_profile_sub(profile)
	xml = minidom.parse(geturl_cached(profile_games_url % sub, profile_games_cache % sub))

	return [ int(game.getElementsByTagName('appID')[0].firstChild.data) \
			for game in xml.getElementsByTagName('game') ]
Ejemplo n.º 7
0
def process_news_file(file_path, img_path, rconn):
    if not rconn.hsetnx('news:lock', file_path, True):
        return ()

    with open(file_path, 'r') as file_object:
        dom = parse(file_object)

    result = itertools.imap(functools.partial(process_news_item,
        file_path=file_path, img_path), dom.getElementsByTagName('NewsItem'))
    rconn.hdel('news:lock', file_path)

    return result
Ejemplo n.º 8
0
 def parse(self, fn: str):
     try:
         if fn.find('://') > -1:
             re = requests.get(fn)
             re.encoding = 'utf8'
             if re.status_code == 200:
                 self.xmldoc = parseString(re.text)
         else:
             self.xmldoc = parse(fn)
         self.normalize()
         return True
     except:
         return False
Ejemplo n.º 9
0
def GetURL(content_type, url):
    __log('Get URL url - ' + str(url))
    __log('Get URL type - ' + str(content_type))
    request = urllib2.Request(url)
    request.add_header('User-Agent', \
        'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
    if content_type == 'json':
        url_parse = json.load(urllib2.urlopen(request))
    elif content_type == 'html':
        url_parse = urllib2.urlopen(request)
    elif content_type == 'xml':
        url_parse = parse(urllib2.urlopen(request))
    return url_parse
Ejemplo n.º 10
0
def process_news_file(file_path, img_path, rconn):
    if not rconn.hsetnx('news:lock', file_path, True):
        return ()

    with open(file_path, 'r') as file_object:
        dom = parse(file_object)

    result = itertools.imap(
        functools.partial(process_news_item, file_path=file_path, img_path),
        dom.getElementsByTagName('NewsItem'))
    rconn.hdel('news:lock', file_path)

    return result
Ejemplo n.º 11
0
 def parse(self, fn: str):
     try:
         if fn.find('://') > -1:
             header = {"Accept-Encoding": "gzip, deflate"}
             re = requests.get(fn, headers=header)
             re.encoding = 'utf8'
             if re.status_code == 200:
                 self.xmldoc = parseString(re.text)
         else:
             self.xmldoc = parse(fn)
         self.normalize()
         return True
     except:
         return False
Ejemplo n.º 12
0
	def testURLInvocation_doctype(self):
		#Reset the server back to "0"                                           
		r = requests.get(self._URL_+"/reset")                         
		r = requests.get(self._URL_ +"/getCounter")                                           
		request_content = r.text.replace("\r\n","")                             
		self.assertEqual("0", request_content)     
		
		document = '../../xml_files_windows/url_invocation_doctype.xml'
		doc = minidom.parse(document)   
		content = doc.documentElement.toxml()          
		
		#Check if a request has been made                                       
		r = requests.get(self._URL_ +"/getCounter")                                           
		request_content = r.text.replace("\r\n","")                             
		self.assertEqual("0", request_content)   
Ejemplo n.º 13
0
def get_filelist(path, rconn):
    for index_path in (os.path.join(group[0], filename)
                       for group in os.walk(path) for filename in group[2]
                       if filename == 'index.xml'):
        path_hash = hashlib.sha1(index_path).hexdigest()
        file_hash = get_file_sha1(index_path)
        directory_path = os.path.dirname(index_path)
        if rconn.hget('afp:file_index', path_hash) != file_hash:
            with open(index_path, 'r') as index_file:
                index_dom = parse(index_file)
                for node in index_dom.getElementsByTagName('NewsItemRef'):
                    yield os.path.join(directory_path,
                                       node.getAttribute('NewsItem'))

            rconn.hset('afp:file_index', path_hash, file_hash)
Ejemplo n.º 14
0
def _parse(file):
    """
    Formats a minidom.Document file and returns XML as string.

    Args:
        file -- An io.TextIOWrapper object

    Returns:
        str: formated string object
    """
    xml = minidom.parse(file)
    _format(xml)
    xml.normalize()

    return xml.toprettyxml(indent=2 * ' ', encoding='utf-8').decode('utf-8')
Ejemplo n.º 15
0
 def buildMetadata(self):
     AnalysisFactory.buildMetadata(self)
     
     #print("Doing XMLFile analysis...")
     try:
         if self.filename is not None:
             doc = minidom.parse(self.filename)
         else:
             self.fh.seek(0)
             doc = minidom.parse(self.fh)
         #print("Got doc %s" % str(doc))
     except DTDForbidden:
         print("XML file defines DTD")
         self.metadata.append(("defines-dtd", "true"))
         return True
     except EntitiesForbidden:
         print("XML file defines entities")
         self.metadata.append(("defines-entities", "true"))
         return True                
     except Exception, err:
         template = "Failed to seek/open/parse XML: {0} Arguments:\n{1!r}"
         message = template.format(type(err).__name__, err.args)
         print(message)
         return False 
Ejemplo n.º 16
0
    def testURLInvocation_doctype(self):
        #Reset the server back to "0"
        r = requests.get(self._URL_ + "/reset")
        r = requests.get(self._URL_ + "/getCounter")
        request_content = r.text.replace("\r\n", "")
        self.assertEqual("0", request_content)

        document = '../../xml_files_windows/ssrf/url_invocation_doctype.xml'
        doc = minidom.parse(document)
        content = doc.documentElement.toxml()

        #Check if a request has been made
        r = requests.get(self._URL_ + "/getCounter")
        request_content = r.text.replace("\r\n", "")
        self.assertEqual("0", request_content)
Ejemplo n.º 17
0
def get_filelist(path, rconn):
    for index_path in (os.path.join(group[0], filename)
         for  group in os.walk(path)
         for filename in group[2]
         if filename == 'index.xml'):
        path_hash = hashlib.sha1(index_path).hexdigest()
        file_hash = get_file_sha1(index_path)
        directory_path = os.path.dirname(index_path)
        if rconn.hget('afp:file_index', path_hash) != file_hash:
            with open(index_path, 'r') as index_file:
                index_dom = parse(index_file)
                for node in index_dom.getElementsByTagName('NewsItemRef'):
                    yield os.path.join(directory_path, node.getAttribute('NewsItem'))

            rconn.hset('afp:file_index', path_hash, file_hash)
Ejemplo n.º 18
0
    def parse_kernel_cache(self, data: str) -> list:
        """
        Extract App namespaces from the Kernel cache container file.

        The XML Kernel cache file contains a list of services with the associated classes. This list can be extracted
        to retrieve source code file paths. Namespaces can be extracted from the class attribute of service elements or
        the key attribute of argument elements.

        <services>
          <service id="kernel" class="App\\Kernel" public="true" synthetic="true">
            <tag name="routing.route_loader"/>
          </service>
          <service id="App\\Command\\AddUserCommand" class="App\\Command\\AddUserCommand">
            <tag name="console.command"/>
            <argument type="service" id="doctrine.orm.default_entity_manager"/>
            <argument type="service" id="security.user_password_encoder.generic"/>
            <argument type="service" id="App\\Utils\\Validator"/>
            <argument type="service" id="App\\Repository\\UserRepository"/>
            <call method="setName">
              <argument>app:add-user</argument>
            </call>
          </service>

        :param data: file content
        :return: list of source file paths relative to the application root
        """

        dom = minidom.parse(BytesIO(data.strip().encode()))
        services = dom.getElementsByTagName('service')
        arguments = dom.getElementsByTagName('argument')

        classes = set()
        for service in services:
            cls = service.attributes.get('class')
            if cls:
                classes.add(cls.value)

        for argument in arguments:
            id = argument.attributes.get('id')
            key = argument.attributes.get('key')
            if id:
                classes.add(id.value.split(':')[0])
            if key:
                classes.add(key.value.split(':')[0])

        classes = set(filter(lambda ns: ns and ns.startswith(self.symfony.namespace + '\\'), classes))

        return [self.class_to_path(cls, self.symfony.root) for cls in classes]
Ejemplo n.º 19
0
	def testURLInvocation_externalGeneralEntity(self):
		#Reset the server back to "0"                                           
		r = requests.get(self._URL_+"/reset")                         
		r = requests.get(self._URL_ +"/getCounter")                                           
		request_content = r.text.replace("\r\n","")                             
		self.assertEqual("0", request_content)       
		
		with self.assertRaises(EntitiesForbidden):	
			document = '../../xml_files_windows/url_invocation_externalGeneralEntity.xml'
			doc = minidom.parse(document)   
			content = doc.documentElement.toxml()      
		
		#Check if a request has been made                                       
		r = requests.get(self._URL_ +"/getCounter")                                           
		request_content = r.text.replace("\r\n","")                             
		self.assertEqual("0", request_content)    
Ejemplo n.º 20
0
    def testURLInvocation_externalGeneralEntity(self):
        #Reset the server back to "0"
        r = requests.get(self._URL_ + "/reset")
        r = requests.get(self._URL_ + "/getCounter")
        request_content = r.text.replace("\r\n", "")
        self.assertEqual("0", request_content)

        with self.assertRaises(EntitiesForbidden):
            document = '../../xml_files_windows/ssrf/url_invocation_externalGeneralEntity.xml'
            doc = minidom.parse(document)
            content = doc.documentElement.toxml()

        #Check if a request has been made
        r = requests.get(self._URL_ + "/getCounter")
        request_content = r.text.replace("\r\n", "")
        self.assertEqual("0", request_content)
Ejemplo n.º 21
0
    def buildMetadata(self):
        # ZIPFile.buildMetadata(self)
        
        print "Doing Office Analysis..."
        extractList = { "docProps/core.xml" : [
                           'dc:title', 'dc:subject', 'dc:creator', 'cp:keywords', 
                           'dc:description', 'cp:lastModifiedBy', 'cp:revision',
                           'cp:lastPrinted', 'dcterms:created', 'dcterms:modified'                           
                        ],
                        "docProps/app.xml" : [
                           'Template', 'TotalTime', 'Words', 'Application',
                           'PresentationFormat', 'Paragraphs', 'Slides', 'Notes',
                           'HiddenSlides', 'MMClips', 'Company', 'AppVersion'
                        ]
                       }
            
        zf = None
    
        try:
            zf = ZipFile(self.filename)

            # For each of several XML files in the archive,
            # open them, extract a few fields, and add to the 
            # metadata list.            
            for extractFile in extractList.keys():
                f = zf.open(extractFile, "r")
                try:
                    xmldoc = minidom.parse(f)
                
                    extractFields = extractList[extractFile]
                    for extractField in extractFields:
                        itemlist = xmldoc.getElementsByTagName(extractField)
                        if len(itemlist) > 0:
                            try:  txt = itemlist[0].firstChild.nodeValue
                            except:  # node was empty
                                txt = ""
                        
                            self.metadata.append((extractField, txt))
                finally:            
                    files.filetype.qClose(f)
            return True
        except Exception, err:
            # Fall back to conservative method
            print("Failed processing office zipfile: %s" % str(err))
            traceback.print_exc(file=sys.stdout)
            return False
Ejemplo n.º 22
0
 def __init__(self, raw_data: tuple, output_file: str):
     self.raw_data = raw_data
     self.output_file = output_file
     self.output_file = self.process_xml_extension(self.output_file)
     root = ET.Element("deck", attrib={"name": output_file[:-4]})
     for data in raw_data:
         card = ET.Element("card")
         id_ = ET.SubElement(card, "id")
         id_.text = str(data[0])
         front = ET.SubElement(card, "front")
         front.text = data[1]
         back = ET.SubElement(card, 'back')
         back.text = data[2]
         root.append(card)
     buf = BytesIO()
     buf.write(ET.tostring(root))
     buf.seek(0)
     string = minidom.parse(buf).toprettyxml(indent=" " * 4)
     with open(self.output_file, mode="w+", encoding="utf-8") as file_:
         file_.write(string)
Ejemplo n.º 23
0
        os.remove(os.path.join(language_folder_path, temp_file))
    f = open(os.path.join(language_folder_path, temp_file), "w")
    f.write(entire_source)
    f.close()

log.info("-----------------------------------------------------")
log.info(" Scanning custom XML files and finding text")
log.info("-----------------------------------------------------")

# Loop through the Effects XML
effects_text = {}
for file in os.listdir(effects_path):
    if os.path.isfile(os.path.join(effects_path, file)):
        # load xml effect file
        full_file_path = os.path.join(effects_path, file)
        xmldoc = xml.parse(os.path.join(effects_path, file))

        # add text to list
        effects_text[xmldoc.getElementsByTagName("title")
                     [0].childNodes[0].data] = full_file_path
        effects_text[xmldoc.getElementsByTagName("description")
                     [0].childNodes[0].data] = full_file_path

        # get params
        params = xmldoc.getElementsByTagName("param")

        # Loop through params
        for param in params:
            if param.attributes["title"]:
                effects_text[param.attributes["title"].value] = full_file_path
Ejemplo n.º 24
0
    def update_model(self, clear=True):
        log.info("updating effects model.")

        _ = self.app._tr

        # Clear all items
        if clear:
            self.model_paths = {}
            self.model.clear()

        # Add Headers
        self.model.setHorizontalHeaderLabels([_("Thumb"), _("Name")])

        # get a list of files in the application blender directory
        blender_dir = os.path.join(info.PATH, "blender")
        icons_dir = os.path.join(blender_dir, "icons")

        for file in sorted(os.listdir(blender_dir)):
            path = os.path.join(blender_dir, file)
            if path in self.model_paths:
                continue
            if os.path.isfile(path) and ".xml" in file:
                # load xml effect file
                xmldoc = xml.parse(path)

                # Get column data for model
                title = xmldoc.getElementsByTagName(
                    "title")[0].childNodes[0].data
                icon_name = xmldoc.getElementsByTagName(
                    "icon")[0].childNodes[0].data
                icon_path = os.path.join(icons_dir, icon_name)
                service = xmldoc.getElementsByTagName(
                    "service")[0].childNodes[0].data
                xmldoc.unlink()

                # Check for thumbnail path (in build-in cache)
                thumb_path = os.path.join(info.IMAGES_PATH, "cache",
                                          "blender_{}".format(icon_name))

                # Check built-in cache (if not found)
                if not os.path.exists(thumb_path):
                    # Check user folder cache
                    thumb_path = os.path.join(info.CACHE_PATH,
                                              "blender_{}".format(icon_name))

                # Check if thumb exists
                if not os.path.exists(thumb_path):

                    try:
                        # Reload this reader
                        clip = openshot.Clip(icon_path)
                        reader = clip.Reader()
                        reader.Open()

                        # Save thumbnail
                        reader.GetFrame(0).Thumbnail(thumb_path, 98, 64, "",
                                                     "", "#000", False, "png",
                                                     85, 0.0)
                        reader.Close()
                    except Exception:
                        log.info('Invalid blender image file: %s', icon_path)
                        continue

                row = []
                flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable
                # Append thumbnail
                col = QStandardItem(self.app._tr(title))
                icon_pixmap = QPixmap(thumb_path).scaled(
                    QSize(93, 62), Qt.IgnoreAspectRatio,
                    Qt.SmoothTransformation)
                col.setIcon(QIcon(icon_pixmap))
                col.setToolTip(self.app._tr(title))
                col.setFlags(flags)
                row.append(col)

                # Append Name
                col = QStandardItem(self.app._tr(title))
                col.setData(self.app._tr(title), Qt.DisplayRole)
                col.setFlags(flags)
                row.append(col)

                # Append Path
                col = QStandardItem(path)
                col.setData(path, Qt.DisplayRole)
                col.setFlags(flags)
                row.append(col)

                # Append Service
                col = QStandardItem(service)
                col.setData(service, Qt.DisplayRole)
                col.setFlags(flags)
                row.append(col)

                self.model.appendRow(row)
                self.model_paths[path] = path

                # Process events in QT (to keep the interface responsive)
                self.app.processEvents()
Ejemplo n.º 25
0
	def testXInclude(self):
		document = '../../xml_files_windows/xinclude.xml'
		doc = minidom.parse(document)   		
		content = doc.documentElement.firstChild.nodeName
		self.assertEqual("xi:include", content)   
Ejemplo n.º 26
0
    def _load_ttml_doc(self, filepath):
        """Read TTML file. Extract <p> elements and various attributes.
        """

        ttml_dom = minidom.parse(filepath)
        self.encoding = ttml_dom.encoding

        if self.encoding and self.encoding.lower() not in ['utf8', 'utf-8']:
            # Don't bother with subtitles that aren't utf-8 encoded
            # but assume utf-8 when the encoding attr is missing
            raise NotImplementedError('Source is not utf-8 encoded')

        # Get the root tt element (assume the file contains
        # a single subtitle document)
        tt_element = ttml_dom.getElementsByTagName('tt')[0]

        # Extract doc language
        # https://tools.ietf.org/html/rfc4646#section-2.1
        language_tag = tt_element.getAttribute('xml:lang') or ''
        self.lang = re.split(r'\s+', language_tag.strip())[0].split('-')[0]

        # Store TT parameters as instance vars (in camel case)
        for ttp_name, defval, convfn in (
                # (tt param, default val, fn to process the str)
            ('frameRate', 0, lambda x: float(x)),
            ('tickRate', 0, lambda x: int(x)),
            ('timeBase', 'media', lambda x: x),
            ('clockMode', '', lambda x: x),
            ('frameRateMultiplier', 1, lambda x: int(x)),
            ('subFrameRate', 1, lambda x: int(x)),
            ('markerMode', '', lambda x: x),
            ('dropMode', '', lambda x: x),
        ):
            ttp_val = getattr(tt_element.attributes.get('ttp:' + ttp_name),
                              'value', defval)
            setattr(self, Ttml2Srt.snake_to_camel(ttp_name), convfn(ttp_val))

        if self.time_base not in Ttml2Srt.TIME_BASES:
            raise NotImplementedError('No support for "{}" time base'.format(
                self.time_base))

        # Grab <style>s
        # https://www.w3.org/TR/ttml1/#styling-attribute-vocabulary
        for styles_container in ttml_dom.getElementsByTagName('styling'):
            for style in styles_container.getElementsByTagName('style'):
                style_id = getattr(style.attributes.get('xml:id', {}), 'value',
                                   None)
                if not style_id:
                    continue
                self.styles[style_id] = self.get_tt_style_attrs(style, True)

        # Set effective tick rate as per
        # https://www.w3.org/TR/ttml1/#parameter-attribute-tickRate
        # This will obviously only be made use of if we encounter offset-time
        # expressions that have the tick metric.
        if not self.tick_rate and self.frame_rate:
            self.tick_rate = int(self.frame_rate * self.sub_frame_rate)
        elif not self.tick_rate:
            self.tick_rate = 1

        # Get em <p>s.
        #
        # CAUTION: This is very naive and will fail us when the TTML
        # document contains multiple local time contexts with their own
        # offsets, or even just a single context with an offset other
        # than zero.
        self.lines = [
            i for i in ttml_dom.getElementsByTagName('p')
            if 'begin' in list(i.attributes.keys())
        ]
Ejemplo n.º 27
0
                    raise XmlError(e)
            elif isinstance(xml, unicode):
                try:
                    #  Try to grab the "encoding" attribute from the XML.
                    #  It probably won't exist, so default to utf8.
                    encoding = _XML_ENCODING_RE.match(xml)
                    if encoding is None:
                        encoding = "utf8"
                    else:
                        encoding = encoding.group(1)
                    xml = minidom.parseString(xml.encode(encoding))
                except Exception, e:
                    raise XmlError(e)
            elif hasattr(xml, "read"):
                try:
                    xml = minidom.parse(xml)
                except Exception, e:
                    raise XmlError(e)
            else:
                raise ValueError("Can't convert that to an XML DOM node")
            node = xml.documentElement
        else:
            if ntype == xml.DOCUMENT_NODE:
                node = xml.documentElement
            else:
                node = xml
        return node

    @classmethod
    def validate_xml_node(cls, node):
        """Check that the given xml node is valid for this object.
Ejemplo n.º 28
0
    def update_model(self, clear=True):
        log.info("updating effects model.")
        app = get_app()

        # Get window to check filters
        win = app.window
        _ = app._tr

        # Clear all items
        if clear:
            self.model_paths = {}
            self.model.clear()

        # Add Headers
        self.model.setHorizontalHeaderLabels([_("Thumb"), _("Name")])

        # get a list of files in the OpenShot /effects directory
        effects_dir = os.path.join(info.PATH, "blender")
        icons_dir = os.path.join(effects_dir, "icons")

        for file in sorted(os.listdir(effects_dir)):
            if os.path.isfile(os.path.join(effects_dir,
                                           file)) and ".xml" in file:
                # Split path
                path = os.path.join(effects_dir, file)

                # load xml effect file
                xmldoc = xml.parse(path)

                # Get all attributes
                title = xmldoc.getElementsByTagName(
                    "title")[0].childNodes[0].data
                description = xmldoc.getElementsByTagName(
                    "description")[0].childNodes[0].data
                icon_name = xmldoc.getElementsByTagName(
                    "icon")[0].childNodes[0].data
                icon_path = os.path.join(icons_dir, icon_name)
                category = xmldoc.getElementsByTagName(
                    "category")[0].childNodes[0].data
                service = xmldoc.getElementsByTagName(
                    "service")[0].childNodes[0].data

                # Check for thumbnail path (in build-in cache)
                thumb_path = os.path.join(info.IMAGES_PATH, "cache",
                                          "blender_{}".format(icon_name))

                # Check built-in cache (if not found)
                if not os.path.exists(thumb_path):
                    # Check user folder cache
                    thumb_path = os.path.join(info.CACHE_PATH,
                                              "blender_{}".format(icon_name))

                # Check if thumb exists
                if not os.path.exists(thumb_path):

                    try:
                        # Reload this reader
                        clip = openshot.Clip(icon_path)
                        reader = clip.Reader()

                        # Open reader
                        reader.Open()

                        # Determine scale of thumbnail
                        scale = 95.0 / reader.info.width

                        # Save thumbnail
                        reader.GetFrame(0).Thumbnail(thumb_path, 98, 64, "",
                                                     "", "#000", False, "png",
                                                     85, 0.0)
                        reader.Close()

                    except:
                        # Handle exception
                        log.info('Invalid blender image file: %s' % icon_path)
                        msg = QMessageBox()
                        msg.setText(
                            _("{} is not a valid image file.".format(
                                icon_path)))
                        msg.exec_()
                        continue

                row = []

                # Append thumbnail
                col = QStandardItem()
                icon_pixmap = QPixmap(thumb_path)
                scaled_pixmap = icon_pixmap.scaled(QSize(93, 62),
                                                   Qt.IgnoreAspectRatio,
                                                   Qt.SmoothTransformation)
                col.setIcon(QIcon(scaled_pixmap))
                col.setText(self.app._tr(title))
                col.setToolTip(self.app._tr(title))
                col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled
                             | Qt.ItemIsUserCheckable)
                row.append(col)

                # Append Name
                col = QStandardItem("Name")
                col.setData(self.app._tr(title), Qt.DisplayRole)
                col.setText(self.app._tr(title))
                col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled
                             | Qt.ItemIsUserCheckable)
                row.append(col)

                # Append Path
                col = QStandardItem("Path")
                col.setData(path, Qt.DisplayRole)
                col.setText(path)
                col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled
                             | Qt.ItemIsUserCheckable)
                row.append(col)

                # Append Service
                col = QStandardItem("Service")
                col.setData(service, Qt.DisplayRole)
                col.setText(service)
                col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled
                             | Qt.ItemIsUserCheckable)
                row.append(col)

                # Append ROW to MODEL (if does not already exist in model)
                if not path in self.model_paths:
                    self.model.appendRow(row)
                    self.model_paths[path] = path

                # Process events in QT (to keep the interface responsive)
                app.processEvents()
Ejemplo n.º 29
0
	def testInternalSubset_PEReferenceInDTD(self):
		with self.assertRaises(EntitiesForbidden):
			document = '../../xml_files_windows/internalSubset_PEReferenceInDTD.xml'
			doc = minidom.parse(document)   
Ejemplo n.º 30
0
	def testDefault_noAttack(self):
		document = '../../xml_files_windows/standard.xml'
		doc = minidom.parse(document)    
		content = doc.documentElement.firstChild.nodeValue
		self.assertEqual("4", content)
Ejemplo n.º 31
0
	def testXSLT(self):
		document = '../../xml_files_windows/optional/xslt.xsl'
		doc = minidom.parse(document)    
		content = doc.documentElement.nodeName
		self.assertEqual("xsl:stylesheet", content)
Ejemplo n.º 32
0
	def testDOS_entitySize(self):
		with self.assertRaises(EntitiesForbidden):
			document = '../../xml_files_windows/dos_entitySize.xml'
			doc = minidom.parse(document)    			
Ejemplo n.º 33
0
def detect_opensearch(err, package, listed=False):
    "Detect, parse, and validate an OpenSearch provider"

    # Parse the file.
    try:
        # Check if it is a file object.
        if hasattr(package, 'read'):
            srch_prov = parse(package)
        else:
            # It's not a file object; open it (the XML parser is bad at this).
            with open(package, 'rb') as package_file:
                srch_prov = parse(package_file)
    except DefusedXmlException:
        url = "https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors"
        err.error(
            err_id=("opensearch", "security_error"),
            error="OpenSearch: XML Security Error",
            description="The OpenSearch extension could not be parsed due to "
                        "a security error in the XML. See {url} for more "
                        "info.".format(url=url))
        return err
    except ExpatError:
        err.error(
            err_id=("opensearch", "parse_error"),
            error="OpenSearch: XML Parse Error",
            description="The OpenSearch extension could not be parsed due to "
                        "a syntax error in the XML.")
        return err

    # Make sure that the root element is OpenSearchDescription.
    if srch_prov.documentElement.tagName != "OpenSearchDescription":
        err.error(
            err_id=("opensearch", "invalid_document_root"),
            error="OpenSearch: Invalid Document Root",
            description="The root element of the OpenSearch provider is not "
                        "'OpenSearchDescription'.")

    # Per bug 617822
    if not srch_prov.documentElement.hasAttribute("xmlns"):
        err.error(
            err_id=("opensearch", "no_xmlns"),
            error="OpenSearch: Missing XMLNS attribute",
            description="The XML namespace attribute is missing from the "
                        "OpenSearch document.")

    if ("xmlns" not in srch_prov.documentElement.attributes.keys() or
        srch_prov.documentElement.attributes["xmlns"].value not in (
                    'http://a9.com/-/spec/opensearch/1.0/',
                    'http://a9.com/-/spec/opensearch/1.1/',
                    'http://a9.com/-/spec/opensearchdescription/1.1/',
                    'http://a9.com/-/spec/opensearchdescription/1.0/')):
        err.error(
            err_id=("opensearch", "invalid_xmlns"),
            error="OpenSearch: Bad XMLNS attribute",
            description="The XML namespace attribute contains an "
                        "value.")

    # Make sure that there is exactly one ShortName.
    sn = srch_prov.documentElement.getElementsByTagName("ShortName")
    if not sn:
        err.error(
            err_id=("opensearch", "missing_shortname"),
            error="OpenSearch: Missing <ShortName> elements",
            description="ShortName elements are mandatory OpenSearch provider "
                        "elements.")
    elif len(sn) > 1:
        err.error(
            err_id=("opensearch", "extra_shortnames"),
            error="OpenSearch: Too many <ShortName> elements",
            description="Too many ShortName elements exist in the OpenSearch "
                        "provider.")
    else:
        sn_children = sn[0].childNodes
        short_name = 0
        for node in sn_children:
            if node.nodeType == node.TEXT_NODE:
                short_name += len(node.data)
        if short_name > 16:
            err.error(
                err_id=("opensearch", "big_shortname"),
                error="OpenSearch: <ShortName> element too long",
                description="The ShortName element must contains less than "
                            "seventeen characters.")

    # Make sure that there is exactly one Description.
    if len(srch_prov.documentElement.getElementsByTagName("Description")) != 1:
        err.error(
            err_id=("opensearch", "missing_description"),
            error="OpenSearch: Invalid number of <Description> elements",
            description="There are too many or too few Description elements "
                        "in the OpenSearch provider.")

    # Grab the URLs and make sure that there is at least one.
    urls = srch_prov.documentElement.getElementsByTagName("Url")
    if not urls:
        err.error(
            err_id=("opensearch", "missing_url"),
            error="OpenSearch: Missing <Url> elements",
            description="The OpenSearch provider is missing a Url element.")

    if listed and any(url.hasAttribute("rel") and
                      url.attributes["rel"].value == "self" for
                      url in urls):
        err.error(
            err_id=("opensearch", "rel_self"),
            error="OpenSearch: <Url> elements may not be rel=self",
            description="Per AMO guidelines, OpenSearch providers cannot "
                        "contain <Url /> elements with a 'rel' attribute "
                        "pointing to the URL's current location. It must be "
                        "removed before posting this provider to AMO.")

    acceptable_mimes = ("text/html", "application/xhtml+xml")
    acceptable_urls = [url for url in urls if url.hasAttribute("type") and
                          url.attributes["type"].value in acceptable_mimes]

    # At least one Url must be text/html
    if not acceptable_urls:
        err.error(
            err_id=("opensearch", "missing_url_texthtml"),
            error="OpenSearch: Missing <Url> element with 'text/html' type",
            description="OpenSearch providers must have at least one Url "
                        "element with a type attribute set to 'text/html'.")

    # Make sure that each Url has the require attributes.
    for url in acceptable_urls:

        if url.hasAttribute("rel") and url.attributes["rel"].value == "self":
            continue

        if url.hasAttribute("method") and \
           url.attributes["method"].value.upper() not in ("GET", "POST"):
            err.error(
                err_id=("opensearch", "missing_method"),
                error="OpenSearch: <Url> element with invalid 'method'",
                description="A Url element in the OpenSearch provider lists a "
                            "method attribute, but the value is not GET or "
                            "POST.")

        # Test for attribute presence.
        if not url.hasAttribute("template"):
            err.error(
                err_id=("opensearch", "missing_template"),
                error="OpenSearch: <Url> element missing template attribute",
                description="<Url> elements of OpenSearch providers must "
                            "include a template attribute.")
        else:
            url_template = url.attributes["template"].value
            if url_template[:4] != "http":
                err.error(
                    err_id=("opensearch", "invalid_template"),
                    error="OpenSearch: `<Url>` element with invalid "
                          "`template`",
                    description="A `<Url>` element in the OpenSearch "
                                "provider lists a template attribute, but "
                                "the value is not a valid HTTP URL.")

            # Make sure that there is a {searchTerms} placeholder in the
            # URL template.
            found_template = url_template.count("{searchTerms}") > 0

            # If we didn't find it in a simple parse of the template=""
            # attribute, look deeper at the <Param /> elements.
            if not found_template:
                for param in url.getElementsByTagName("Param"):
                    # As long as we're in here and dependent on the
                    # attributes, we'd might as well validate them.
                    attribute_keys = param.attributes.keys()
                    if "name" not in attribute_keys or \
                       "value" not in attribute_keys:
                        err.error(
                            err_id=("opensearch", "param_missing_attrs"),
                            error="OpenSearch: `<Param>` element missing "
                                  "name/value",
                            description="Param elements in the OpenSearch "
                                        "provider must include a name and a "
                                        "value attribute.")

                    param_value = (param.attributes["value"].value if
                                   "value" in param.attributes.keys() else
                                   "")
                    if param_value.count("{searchTerms}"):
                        found_template = True

                        # Since we're in a validating spirit, continue
                        # looking for more errors and don't break

            # If the template still hasn't been found...
            if not found_template:
                tpl = url.attributes["template"].value
                err.error(
                    err_id=("opensearch", "template_not_found"),
                    error="OpenSearch: <Url> element missing template "
                          "placeholder",
                    description=["`<Url>` elements of OpenSearch providers "
                                 "must include a template attribute or "
                                 "specify a placeholder with "
                                 "`{searchTerms}`.",
                                 "Missing template: %s" % tpl])

    # Make sure there are no updateURL elements
    if srch_prov.getElementsByTagName("updateURL"):
        err.error(
            err_id=("opensearch", "banned_updateurl"),
            error="OpenSearch: <updateURL> elements are banned in OpenSearch "
                  "providers.",
            description="OpenSearch providers may not contain <updateURL> "
                        "elements.")

    # The OpenSearch provider is valid!
    return err
Ejemplo n.º 34
0
	def testParameterEntity_doctype(self):        		
		document = '../../xml_files_windows/parameterEntity_doctype.xml'
		doc = minidom.parse(document)   
		content = doc.documentElement.toxml()		
		self.assertEqual("<data/>", content) 
Ejemplo n.º 35
0
def annotate_search_plugin_validation(results, file_path, channel):
    if not file_path.endswith('.xml'):
        return

    try:
        # Requires bytes because defusedxml fails to detect
        # unicode strings as filenames.
        # https://gist.github.com/EnTeQuAk/25f99701d8b123f7611acd6ce0d5840b
        dom = minidom.parse(force_bytes(file_path))
    except DefusedXmlException:
        url = 'https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors'
        insert_validation_message(
            results,
            message='OpenSearch: XML Security error.',
            description=[
                'The OpenSearch extension could not be parsed due to a '
                'security error in the XML. See {} for more info.'.format(url)
            ])
        return
    except ExpatError:
        insert_validation_message(
            results,
            message='OpenSearch: XML Parse Error.',
            description=[
                'The OpenSearch extension could not be parsed due to a syntax '
                'error in the XML.'
            ])
        return

    # Make sure that the root element is OpenSearchDescription.
    if dom.documentElement.tagName != 'OpenSearchDescription':
        insert_validation_message(
            results,
            message='OpenSearch: Invalid Document Root.',
            description=[
                'The root element of the OpenSearch provider is not '
                '"OpenSearchDescription".'
            ])

    # Per bug 617822
    if not dom.documentElement.hasAttribute('xmlns'):
        insert_validation_message(
            results,
            message='OpenSearch: Missing XMLNS attribute.',
            description=[
                'The XML namespace attribute is missing from the '
                'OpenSearch document.'
            ])

    if ('xmlns' not in dom.documentElement.attributes.keys()
            or dom.documentElement.attributes['xmlns'].value
            not in ('http://a9.com/-/spec/opensearch/1.0/',
                    'http://a9.com/-/spec/opensearch/1.1/',
                    'http://a9.com/-/spec/opensearchdescription/1.1/',
                    'http://a9.com/-/spec/opensearchdescription/1.0/')):
        insert_validation_message(
            results,
            message='OpenSearch: Bad XMLNS attribute.',
            description=['The XML namespace attribute contains an value.'])

    # Make sure that there is exactly one ShortName.
    sn = dom.documentElement.getElementsByTagName('ShortName')
    if not sn:
        insert_validation_message(
            results,
            message='OpenSearch: Missing <ShortName> elements.',
            description=[
                'ShortName elements are mandatory OpenSearch provider '
                'elements.'
            ])
    elif len(sn) > 1:
        insert_validation_message(
            results,
            message='OpenSearch: Too many <ShortName> elements.',
            description=[
                'Too many ShortName elements exist in the OpenSearch provider.'
            ])
    else:
        sn_children = sn[0].childNodes
        short_name = 0
        for node in sn_children:
            if node.nodeType == node.TEXT_NODE:
                short_name += len(node.data)
        if short_name > 16:
            insert_validation_message(
                results,
                message='OpenSearch: <ShortName> element too long.',
                description=[
                    'The ShortName element must contains less than seventeen '
                    'characters.'
                ])

    # Make sure that there is exactly one Description.
    if len(dom.documentElement.getElementsByTagName('Description')) != 1:
        insert_validation_message(
            results,
            message='OpenSearch: Invalid number of <Description> elements.',
            description=[
                'There are too many or too few Description elements '
                'in the OpenSearch provider.'
            ])

    # Grab the URLs and make sure that there is at least one.
    urls = dom.documentElement.getElementsByTagName('Url')
    if not urls:
        insert_validation_message(
            results,
            message='OpenSearch: Missing <Url> elements.',
            description=['The OpenSearch provider is missing a Url element.'])

    ref_self_disallowed = (channel == amo.RELEASE_CHANNEL_LISTED and any(
        url.hasAttribute('rel') and url.attributes['rel'].value == 'self'
        for url in urls))

    if ref_self_disallowed:
        insert_validation_message(
            results,
            message='OpenSearch: <Url> elements may not be rel=self.',
            description=[
                'Per AMO guidelines, OpenSearch providers cannot '
                "contain <Url /> elements with a 'rel' attribute "
                "pointing to the URL's current location. It must be "
                'removed before posting this provider to AMO.'
            ])

    acceptable_mimes = ('text/html', 'application/xhtml+xml')
    acceptable_urls = [
        u for u in urls if u.hasAttribute('type')
        and u.attributes['type'].value in acceptable_mimes
    ]

    # At least one Url must be text/html
    if not acceptable_urls:
        insert_validation_message(
            results,
            message=(
                'OpenSearch: Missing <Url> element with \'text/html\' type.'),
            description=[
                'OpenSearch providers must have at least one Url '
                'element with a type attribute set to \'text/html\'.'
            ])

    # Make sure that each Url has the require attributes.
    for url in acceptable_urls:
        if url.hasAttribute('rel') and url.attributes['rel'].value == 'self':
            continue

        if url.hasAttribute('method') and \
           url.attributes['method'].value.upper() not in ('GET', 'POST'):
            insert_validation_message(
                results,
                message='OpenSearch: <Url> element with invalid \'method\'.',
                description=[
                    'A Url element in the OpenSearch provider lists a '
                    'method attribute, but the value is not GET or '
                    'POST.'
                ])

        # Test for attribute presence.
        if not url.hasAttribute('template'):
            insert_validation_message(
                results,
                message=(
                    'OpenSearch: <Url> element missing template attribute.'),
                description=[
                    '<Url> elements of OpenSearch providers must '
                    'include a template attribute.'
                ])
        else:
            url_template = url.attributes['template'].value
            if url_template[:4] != 'http':
                insert_validation_message(
                    results,
                    message=(
                        'OpenSearch: `<Url>` element with invalid `template`.'
                    ),
                    description=[
                        'A `<Url>` element in the OpenSearch '
                        'provider lists a template attribute, but '
                        'the value is not a valid HTTP URL.'
                    ])

            # Make sure that there is a {searchTerms} placeholder in the
            # URL template.
            found_template = url_template.count('{searchTerms}') > 0

            # If we didn't find it in a simple parse of the template=""
            # attribute, look deeper at the <Param /> elements.
            if not found_template:
                for param in url.getElementsByTagName('Param'):
                    # As long as we're in here and dependent on the
                    # attributes, we'd might as well validate them.
                    attribute_keys = param.attributes.keys()
                    if 'name' not in attribute_keys or \
                       'value' not in attribute_keys:
                        insert_validation_message(
                            results,
                            message=('OpenSearch: `<Param>` element missing '
                                     '\'name/value\'.'),
                            description=[
                                'Param elements in the OpenSearch '
                                'provider must include a name and a '
                                'value attribute.'
                            ])

                    param_value = (param.attributes['value'].value if 'value'
                                   in param.attributes.keys() else '')

                    if param_value.count('{searchTerms}'):
                        found_template = True

            # If the template still hasn't been found...
            if not found_template:
                tpl = url.attributes['template'].value
                insert_validation_message(
                    results,
                    message=('OpenSearch: <Url> element missing template '
                             'placeholder.'),
                    description=[
                        '`<Url>` elements of OpenSearch providers '
                        'must include a template attribute or specify a '
                        'placeholder with `{searchTerms}`.',
                        'Missing template: %s' % tpl
                    ])

    # Make sure there are no updateURL elements
    if dom.getElementsByTagName('updateURL'):
        insert_validation_message(
            results,
            message=(
                'OpenSearch: <updateURL> elements are banned in OpenSearch '
                'providers.'),
            description=[
                'OpenSearch providers may not contain <updateURL> elements.'
            ])
Ejemplo n.º 36
0
	def testXXE(self):
		with self.assertRaises(EntitiesForbidden):
			document = '../../xml_files_windows/xxe.xml'
			doc = minidom.parse(document)    
Ejemplo n.º 37
0
 def format_xml_file_with_encoding(filepath: str, encoding: str) -> None:
     with codecs.open(filepath, 'r', encoding=encoding) as file:
         text = file.read()
     text = parse(text).toprettyxml()
     with codecs.open(filepath, 'w', encoding=encoding) as file:
         file.write(text)
Ejemplo n.º 38
0
def detect_opensearch(err, package, listed=False):
    "Detect, parse, and validate an OpenSearch provider"

    # Parse the file.
    try:
        # Check if it is a file object.
        if hasattr(package, 'read'):
            srch_prov = parse(package)
        else:
            # It's not a file object; open it (the XML parser is bad at this).
            with open(package, 'rb') as package_file:
                srch_prov = parse(package_file)
    except DefusedXmlException:
        url = "https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors"
        err.error(
            err_id=("opensearch", "security_error"),
            error="OpenSearch: XML Security Error",
            description="The OpenSearch extension could not be parsed due to "
            "a security error in the XML. See {url} for more "
            "info.".format(url=url))
        return err
    except ExpatError:
        err.error(
            err_id=("opensearch", "parse_error"),
            error="OpenSearch: XML Parse Error",
            description="The OpenSearch extension could not be parsed due to "
            "a syntax error in the XML.")
        return err

    # Make sure that the root element is OpenSearchDescription.
    if srch_prov.documentElement.tagName != "OpenSearchDescription":
        err.error(
            err_id=("opensearch", "invalid_document_root"),
            error="OpenSearch: Invalid Document Root",
            description="The root element of the OpenSearch provider is not "
            "'OpenSearchDescription'.")

    # Per bug 617822
    if not srch_prov.documentElement.hasAttribute("xmlns"):
        err.error(
            err_id=("opensearch", "no_xmlns"),
            error="OpenSearch: Missing XMLNS attribute",
            description="The XML namespace attribute is missing from the "
            "OpenSearch document.")

    if ("xmlns" not in srch_prov.documentElement.attributes.keys()
            or srch_prov.documentElement.attributes["xmlns"].value
            not in ('http://a9.com/-/spec/opensearch/1.0/',
                    'http://a9.com/-/spec/opensearch/1.1/',
                    'http://a9.com/-/spec/opensearchdescription/1.1/',
                    'http://a9.com/-/spec/opensearchdescription/1.0/')):
        err.error(err_id=("opensearch", "invalid_xmlns"),
                  error="OpenSearch: Bad XMLNS attribute",
                  description="The XML namespace attribute contains an "
                  "value.")

    # Make sure that there is exactly one ShortName.
    sn = srch_prov.documentElement.getElementsByTagName("ShortName")
    if not sn:
        err.error(
            err_id=("opensearch", "missing_shortname"),
            error="OpenSearch: Missing <ShortName> elements",
            description="ShortName elements are mandatory OpenSearch provider "
            "elements.")
    elif len(sn) > 1:
        err.error(
            err_id=("opensearch", "extra_shortnames"),
            error="OpenSearch: Too many <ShortName> elements",
            description="Too many ShortName elements exist in the OpenSearch "
            "provider.")
    else:
        sn_children = sn[0].childNodes
        short_name = 0
        for node in sn_children:
            if node.nodeType == node.TEXT_NODE:
                short_name += len(node.data)
        if short_name > 16:
            err.error(
                err_id=("opensearch", "big_shortname"),
                error="OpenSearch: <ShortName> element too long",
                description="The ShortName element must contains less than "
                "seventeen characters.")

    # Make sure that there is exactly one Description.
    if len(srch_prov.documentElement.getElementsByTagName("Description")) != 1:
        err.error(
            err_id=("opensearch", "missing_description"),
            error="OpenSearch: Invalid number of <Description> elements",
            description="There are too many or too few Description elements "
            "in the OpenSearch provider.")

    # Grab the URLs and make sure that there is at least one.
    urls = srch_prov.documentElement.getElementsByTagName("Url")
    if not urls:
        err.error(
            err_id=("opensearch", "missing_url"),
            error="OpenSearch: Missing <Url> elements",
            description="The OpenSearch provider is missing a Url element.")

    if listed and any(
            url.hasAttribute("rel") and url.attributes["rel"].value == "self"
            for url in urls):
        err.error(
            err_id=("opensearch", "rel_self"),
            error="OpenSearch: <Url> elements may not be rel=self",
            description="Per AMO guidelines, OpenSearch providers cannot "
            "contain <Url /> elements with a 'rel' attribute "
            "pointing to the URL's current location. It must be "
            "removed before posting this provider to AMO.")

    acceptable_mimes = ("text/html", "application/xhtml+xml")
    acceptable_urls = [
        url for url in urls if url.hasAttribute("type")
        and url.attributes["type"].value in acceptable_mimes
    ]

    # At least one Url must be text/html
    if not acceptable_urls:
        err.error(
            err_id=("opensearch", "missing_url_texthtml"),
            error="OpenSearch: Missing <Url> element with 'text/html' type",
            description="OpenSearch providers must have at least one Url "
            "element with a type attribute set to 'text/html'.")

    # Make sure that each Url has the require attributes.
    for url in acceptable_urls:

        if url.hasAttribute("rel") and url.attributes["rel"].value == "self":
            continue

        if url.hasAttribute("method") and \
           url.attributes["method"].value.upper() not in ("GET", "POST"):
            err.error(
                err_id=("opensearch", "missing_method"),
                error="OpenSearch: <Url> element with invalid 'method'",
                description="A Url element in the OpenSearch provider lists a "
                "method attribute, but the value is not GET or "
                "POST.")

        # Test for attribute presence.
        if not url.hasAttribute("template"):
            err.error(
                err_id=("opensearch", "missing_template"),
                error="OpenSearch: <Url> element missing template attribute",
                description="<Url> elements of OpenSearch providers must "
                "include a template attribute.")
        else:
            url_template = url.attributes["template"].value
            if url_template[:4] != "http":
                err.error(err_id=("opensearch", "invalid_template"),
                          error="OpenSearch: `<Url>` element with invalid "
                          "`template`",
                          description="A `<Url>` element in the OpenSearch "
                          "provider lists a template attribute, but "
                          "the value is not a valid HTTP URL.")

            # Make sure that there is a {searchTerms} placeholder in the
            # URL template.
            found_template = url_template.count("{searchTerms}") > 0

            # If we didn't find it in a simple parse of the template=""
            # attribute, look deeper at the <Param /> elements.
            if not found_template:
                for param in url.getElementsByTagName("Param"):
                    # As long as we're in here and dependent on the
                    # attributes, we'd might as well validate them.
                    attribute_keys = param.attributes.keys()
                    if "name" not in attribute_keys or \
                       "value" not in attribute_keys:
                        err.error(
                            err_id=("opensearch", "param_missing_attrs"),
                            error="OpenSearch: `<Param>` element missing "
                            "name/value",
                            description="Param elements in the OpenSearch "
                            "provider must include a name and a "
                            "value attribute.")

                    param_value = (param.attributes["value"].value if "value"
                                   in param.attributes.keys() else "")
                    if param_value.count("{searchTerms}"):
                        found_template = True

                        # Since we're in a validating spirit, continue
                        # looking for more errors and don't break

            # If the template still hasn't been found...
            if not found_template:
                tpl = url.attributes["template"].value
                err.error(err_id=("opensearch", "template_not_found"),
                          error="OpenSearch: <Url> element missing template "
                          "placeholder",
                          description=[
                              "`<Url>` elements of OpenSearch providers "
                              "must include a template attribute or "
                              "specify a placeholder with "
                              "`{searchTerms}`.",
                              "Missing template: %s" % tpl
                          ])

    # Make sure there are no updateURL elements
    if srch_prov.getElementsByTagName("updateURL"):
        err.error(
            err_id=("opensearch", "banned_updateurl"),
            error="OpenSearch: <updateURL> elements are banned in OpenSearch "
            "providers.",
            description="OpenSearch providers may not contain <updateURL> "
            "elements.")

    # The OpenSearch provider is valid!
    return err
Ejemplo n.º 39
0
# LearnRL a python library to learn and use reinforcement learning
# Copyright (C) 2020 Mathïs FEDERICO <https://www.gnu.org/licenses/>
""" Module to get coverage score. """

import sys
from defusedxml.minidom import parse
from utils import score_to_rgb_color

if __name__ == '__main__':
    file = parse('coverage.xml')
    coverage = file.getElementsByTagName('coverage')
    coverage = float(coverage[0].attributes['line-rate'].value)
    coverage_min, coverage_max = 0, 1
    if sys.argv[1] == '--score':
        print(f"{coverage:.1%}")
    elif sys.argv[1] == '--color':
        print(score_to_rgb_color(coverage, coverage_min, coverage_max))
    else:
        raise ValueError(f"Unknowed argument: {sys.argv[1]}")
Ejemplo n.º 40
0
 def handle(self, *args, **options):
     creator = get_user_model().objects.get(email=options["creator_email"])
     dom = minidom.parse(options["kml_file"])
     map_name = self._get_map_name(dom)
     places = self._get_places(dom)
     self._create(map_name, options["map_slug"], places, creator)
Ejemplo n.º 41
0
def ReadXML(filename):
    file_parse = parse(xbmc.translatePath('special://home/addons/' + ADDON_ID + \
       '/resources/data/' + filename))
    __log('Read XML - '+ xbmc.translatePath('special://home/addons/' + ADDON_ID + \
       '/resources/data/' + filename))
    for item in file_parse.getElementsByTagName('item'):
        file_provider = item.getElementsByTagName(
            'provider')[0].firstChild.data
        file_title = item.getElementsByTagName('title')[0].firstChild.data
        try:
            file_id = item.getElementsByTagName('id')[0].firstChild.data
        except:
            file_id = ''
        try:
            file_logo = item.getElementsByTagName('logo')[0].firstChild.data
        except:
            file_logo = ''
        try:
            file_custom_logo = item.getElementsByTagName(
                'customlogo')[0].firstChild.data
        except:
            file_custom_logo = ''
        if file_provider == 'MenuLink':
            __log('Provider - ' + file_provider)
            AddMenuLink(file_title, 'url', file_id, file_custom_logo)
        if file_provider == 'MenuItem':
            __log('Provider - ' + file_provider)
            AddMenuItem(file_title, file_custom_logo)
        if file_provider == 'SoundCloud':
            __log('Provider - ' + file_provider)
            if file_custom_logo:
                AddListItemAudio(file_title, soundcloud_user_url + file_id + \
                    soundcloud_user_url2 + '', 11, file_custom_logo + '')
            else:
                AddListItemAudio(file_title, soundcloud_user_url + file_id + \
                    soundcloud_user_url2 + '', 11, soundcloud_logo_url + file_logo + '')
            xbmcplugin.addSortMethod(int(sys.argv[1]),
                                     xbmcplugin.SORT_METHOD_LABEL)
        if file_provider == 'Youtube':
            file_type = file_id[:2]
            if file_type == 'PL':
                __log('Provider -' + file_provider + 'Type - Playlist')
                if file_custom_logo:
                    AddListItemVideo(file_title, youtube_playlist_url + file_id + \
                       '', 12, file_custom_logo  + '')
                else:
                    AddListItemVideo(file_title, youtube_playlist_url + file_id + \
                       '', 12, youtube_logo_url  +file_logo  + '')
            elif file_type == 'UC':
                __log('Provider -' + file_provider + 'Type - Channel')
                if file_custom_logo:
                    AddListItemVideo(file_title, youtube_channel_url + file_id + \
                        youtube_channel_url_end +'', 14, file_custom_logo + '')
                else:
                    AddListItemVideo(file_title, youtube_channel_url + file_id + \
                        youtube_channel_url_end +'', 14, youtube_logo_url + file_logo + '')
            else:
                __log('Provider -' + file_provider + 'Type - User')
                youtube_channel_id = GetChannelID(file_id)
                if youtube_channel_id:
                    __log('Provider -' + file_provider + 'Type - Channel')
                    if file_custom_logo:
                        AddListItemVideo(file_title, youtube_channel_url +  youtube_channel_id + \
                            youtube_channel_url_end +'', 14, file_custom_logo + '')
                    else:
                        AddListItemVideo(file_title, youtube_channel_url + youtube_channel_id + \
                           youtube_channel_url_end +'', 14, youtube_logo_url + file_logo + '')
                else:
                    if file_custom_logo:
                        AddListItemVideo(file_title, youtube_user_url + file_id + \
                            '', 10, file_custom_logo + '')
                    else:
                        AddListItemVideo(file_title, youtube_user_url + file_id + \
                            '', 10, youtube_logo_url + file_logo + '')
            xbmcplugin.addSortMethod(int(sys.argv[1]),
                                     xbmcplugin.SORT_METHOD_LABEL)
        if file_provider == 'VimeoChannel':
            __log('Provider -' + file_provider)
            if file_custom_logo:
                AddListItemVideo(file_title, vimeo_channel_url + file_id + vimeo_url_end + \
                    '', 13, file_custom_logo + '')
            else:
                AddListItemVideo(file_title, vimeo_channel_url + file_id + vimeo_url_end + \
                    '', 13, vimeo_thumbnail_url +  file_logo + '')
            xbmcplugin.addSortMethod(int(sys.argv[1]),
                                     xbmcplugin.SORT_METHOD_LABEL)
        if file_provider == 'VimeoUser':
            __log('Provider -' + file_provider)
            if file_custom_logo:
                AddListItemVideo(file_title, vimeo_user_url + file_id + vimeo_url_end + \
                    '', 13, file_custom_logo + '')
            else:
                AddListItemVideo(file_title, vimeo_user_url + file_id + vimeo_url_end + \
                    '', 13, vimeo_thumbnail_url +  file_logo + '')
            xbmcplugin.addSortMethod(int(sys.argv[1]),
                                     xbmcplugin.SORT_METHOD_LABEL)
Ejemplo n.º 42
0
def annotate_search_plugin_validation(results, file_path, channel):
    if not file_path.endswith('.xml'):
        return

    try:
        with open(file_path, 'r') as file_obj:
            dom = minidom.parse(file_obj)
    except DefusedXmlException:
        url = 'https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors'
        insert_validation_message(
            results,
            message='OpenSearch: XML Security error.',
            description=[
                'The OpenSearch extension could not be parsed due to a '
                'security error in the XML. See {} for more info.'
                .format(url)])
        return
    except ExpatError:
        insert_validation_message(
            results,
            message='OpenSearch: XML Parse Error.',
            description=[
                'The OpenSearch extension could not be parsed due to a syntax '
                'error in the XML.'])
        return

    # Make sure that the root element is OpenSearchDescription.
    if dom.documentElement.tagName != 'OpenSearchDescription':
        insert_validation_message(
            results,
            message='OpenSearch: Invalid Document Root.',
            description=[
                'The root element of the OpenSearch provider is not '
                '"OpenSearchDescription".'])

    # Per bug 617822
    if not dom.documentElement.hasAttribute('xmlns'):
        insert_validation_message(
            results,
            message='OpenSearch: Missing XMLNS attribute.',
            description=[
                'The XML namespace attribute is missing from the '
                'OpenSearch document.'])

    if ('xmlns' not in dom.documentElement.attributes.keys() or
        dom.documentElement.attributes['xmlns'].value not in (
            'http://a9.com/-/spec/opensearch/1.0/',
            'http://a9.com/-/spec/opensearch/1.1/',
            'http://a9.com/-/spec/opensearchdescription/1.1/',
            'http://a9.com/-/spec/opensearchdescription/1.0/')):
        insert_validation_message(
            results,
            message='OpenSearch: Bad XMLNS attribute.',
            description=['The XML namespace attribute contains an value.'])

    # Make sure that there is exactly one ShortName.
    sn = dom.documentElement.getElementsByTagName('ShortName')
    if not sn:
        insert_validation_message(
            results,
            message='OpenSearch: Missing <ShortName> elements.',
            description=[
                'ShortName elements are mandatory OpenSearch provider '
                'elements.'])
    elif len(sn) > 1:
        insert_validation_message(
            results,
            message='OpenSearch: Too many <ShortName> elements.',
            description=[
                'Too many ShortName elements exist in the OpenSearch provider.'
            ]
        )
    else:
        sn_children = sn[0].childNodes
        short_name = 0
        for node in sn_children:
            if node.nodeType == node.TEXT_NODE:
                short_name += len(node.data)
        if short_name > 16:
            insert_validation_message(
                results,
                message='OpenSearch: <ShortName> element too long.',
                description=[
                    'The ShortName element must contains less than seventeen '
                    'characters.'])

    # Make sure that there is exactly one Description.
    if len(dom.documentElement.getElementsByTagName('Description')) != 1:
        insert_validation_message(
            results,
            message='OpenSearch: Invalid number of <Description> elements.',
            description=[
                'There are too many or too few Description elements '
                'in the OpenSearch provider.'])

    # Grab the URLs and make sure that there is at least one.
    urls = dom.documentElement.getElementsByTagName('Url')
    if not urls:
        insert_validation_message(
            results,
            message='OpenSearch: Missing <Url> elements.',
            description=['The OpenSearch provider is missing a Url element.'])

    ref_self_disallowed = (
        channel == amo.RELEASE_CHANNEL_LISTED and
        any(url.hasAttribute('rel') and url.attributes['rel'].value == 'self'
            for url in urls))

    if ref_self_disallowed:
        insert_validation_message(
            results,
            message='OpenSearch: <Url> elements may not be rel=self.',
            description=[
                'Per AMO guidelines, OpenSearch providers cannot '
                "contain <Url /> elements with a 'rel' attribute "
                "pointing to the URL's current location. It must be "
                'removed before posting this provider to AMO.'])

    acceptable_mimes = ('text/html', 'application/xhtml+xml')
    acceptable_urls = [
        u for u in urls if u.hasAttribute('type') and
        u.attributes['type'].value in acceptable_mimes]

    # At least one Url must be text/html
    if not acceptable_urls:
        insert_validation_message(
            results,
            message=(
                'OpenSearch: Missing <Url> element with \'text/html\' type.'),
            description=[
                'OpenSearch providers must have at least one Url '
                'element with a type attribute set to \'text/html\'.'])

    # Make sure that each Url has the require attributes.
    for url in acceptable_urls:
        if url.hasAttribute('rel') and url.attributes['rel'].value == 'self':
            continue

        if url.hasAttribute('method') and \
           url.attributes['method'].value.upper() not in ('GET', 'POST'):
            insert_validation_message(
                results,
                message='OpenSearch: <Url> element with invalid \'method\'.',
                description=[
                    'A Url element in the OpenSearch provider lists a '
                    'method attribute, but the value is not GET or '
                    'POST.'])

        # Test for attribute presence.
        if not url.hasAttribute('template'):
            insert_validation_message(
                results,
                message=(
                    'OpenSearch: <Url> element missing template attribute.'),
                description=[
                    '<Url> elements of OpenSearch providers must '
                    'include a template attribute.'])
        else:
            url_template = url.attributes['template'].value
            if url_template[:4] != 'http':
                insert_validation_message(
                    results,
                    message=(
                        'OpenSearch: `<Url>` element with invalid `template`.'
                    ),
                    description=[
                        'A `<Url>` element in the OpenSearch '
                        'provider lists a template attribute, but '
                        'the value is not a valid HTTP URL.'])

            # Make sure that there is a {searchTerms} placeholder in the
            # URL template.
            found_template = url_template.count('{searchTerms}') > 0

            # If we didn't find it in a simple parse of the template=""
            # attribute, look deeper at the <Param /> elements.
            if not found_template:
                for param in url.getElementsByTagName('Param'):
                    # As long as we're in here and dependent on the
                    # attributes, we'd might as well validate them.
                    attribute_keys = param.attributes.keys()
                    if 'name' not in attribute_keys or \
                       'value' not in attribute_keys:
                        insert_validation_message(
                            results,
                            message=(
                                'OpenSearch: `<Param>` element missing '
                                '\'name/value\'.'),
                            description=[
                                'Param elements in the OpenSearch '
                                'provider must include a name and a '
                                'value attribute.'])

                    param_value = (
                        param.attributes['value'].value if
                        'value' in param.attributes.keys() else '')

                    if param_value.count('{searchTerms}'):
                        found_template = True

            # If the template still hasn't been found...
            if not found_template:
                tpl = url.attributes['template'].value
                insert_validation_message(
                    results,
                    message=(
                        'OpenSearch: <Url> element missing template '
                        'placeholder.'),
                    description=[
                        '`<Url>` elements of OpenSearch providers '
                        'must include a template attribute or specify a '
                        'placeholder with `{searchTerms}`.',
                        'Missing template: %s' % tpl])

    # Make sure there are no updateURL elements
    if dom.getElementsByTagName('updateURL'):
        insert_validation_message(
            results,
            message=(
                'OpenSearch: <updateURL> elements are banned in OpenSearch '
                'providers.'),
            description=[
                'OpenSearch providers may not contain <updateURL> elements.'])
Ejemplo n.º 43
0
def detect_opensearch(err, package, listed=False):
    'Detect, parse, and validate an OpenSearch provider'

    # Parse the file.
    try:
        # Check if it is a file object.
        if hasattr(package, 'read'):
            srch_prov = parse(package)
        else:
            # It's not a file object; open it (the XML parser is bad at this).
            with open(package, 'rb') as package_file:
                srch_prov = parse(package_file)
    except DefusedXmlException:
        url = 'https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors'
        err.error(
            err_id=('opensearch', 'security_error'),
            error='OpenSearch: XML Security Error',
            description='The OpenSearch extension could not be parsed due to '
            'a security error in the XML. See {url} for more '
            'info.'.format(url=url))
        return err
    except ExpatError:
        err.error(
            err_id=('opensearch', 'parse_error'),
            error='OpenSearch: XML Parse Error',
            description='The OpenSearch extension could not be parsed due to '
            'a syntax error in the XML.')
        return err

    # Make sure that the root element is OpenSearchDescription.
    if srch_prov.documentElement.tagName != 'OpenSearchDescription':
        err.error(
            err_id=('opensearch', 'invalid_document_root'),
            error='OpenSearch: Invalid Document Root',
            description='The root element of the OpenSearch provider is not '
            "'OpenSearchDescription'.")

    # Per bug 617822
    if not srch_prov.documentElement.hasAttribute('xmlns'):
        err.error(
            err_id=('opensearch', 'no_xmlns'),
            error='OpenSearch: Missing XMLNS attribute',
            description='The XML namespace attribute is missing from the '
            'OpenSearch document.')

    if ('xmlns' not in srch_prov.documentElement.attributes.keys()
            or srch_prov.documentElement.attributes['xmlns'].value
            not in ('http://a9.com/-/spec/opensearch/1.0/',
                    'http://a9.com/-/spec/opensearch/1.1/',
                    'http://a9.com/-/spec/opensearchdescription/1.1/',
                    'http://a9.com/-/spec/opensearchdescription/1.0/')):
        err.error(err_id=('opensearch', 'invalid_xmlns'),
                  error='OpenSearch: Bad XMLNS attribute',
                  description='The XML namespace attribute contains an '
                  'value.')

    # Make sure that there is exactly one ShortName.
    sn = srch_prov.documentElement.getElementsByTagName('ShortName')
    if not sn:
        err.error(
            err_id=('opensearch', 'missing_shortname'),
            error='OpenSearch: Missing <ShortName> elements',
            description='ShortName elements are mandatory OpenSearch provider '
            'elements.')
    elif len(sn) > 1:
        err.error(
            err_id=('opensearch', 'extra_shortnames'),
            error='OpenSearch: Too many <ShortName> elements',
            description='Too many ShortName elements exist in the OpenSearch '
            'provider.')
    else:
        sn_children = sn[0].childNodes
        short_name = 0
        for node in sn_children:
            if node.nodeType == node.TEXT_NODE:
                short_name += len(node.data)
        if short_name > 16:
            err.error(
                err_id=('opensearch', 'big_shortname'),
                error='OpenSearch: <ShortName> element too long',
                description='The ShortName element must contains less than '
                'seventeen characters.')

    # Make sure that there is exactly one Description.
    if len(srch_prov.documentElement.getElementsByTagName('Description')) != 1:
        err.error(
            err_id=('opensearch', 'missing_description'),
            error='OpenSearch: Invalid number of <Description> elements',
            description='There are too many or too few Description elements '
            'in the OpenSearch provider.')

    # Grab the URLs and make sure that there is at least one.
    urls = srch_prov.documentElement.getElementsByTagName('Url')
    if not urls:
        err.error(
            err_id=('opensearch', 'missing_url'),
            error='OpenSearch: Missing <Url> elements',
            description='The OpenSearch provider is missing a Url element.')

    if listed and any(
            url.hasAttribute('rel') and url.attributes['rel'].value == 'self'
            for url in urls):
        err.error(
            err_id=('opensearch', 'rel_self'),
            error='OpenSearch: <Url> elements may not be rel=self',
            description='Per AMO guidelines, OpenSearch providers cannot '
            "contain <Url /> elements with a 'rel' attribute "
            "pointing to the URL's current location. It must be "
            'removed before posting this provider to AMO.')

    acceptable_mimes = ('text/html', 'application/xhtml+xml')
    acceptable_urls = [
        url for url in urls if url.hasAttribute('type')
        and url.attributes['type'].value in acceptable_mimes
    ]

    # At least one Url must be text/html
    if not acceptable_urls:
        err.error(
            err_id=('opensearch', 'missing_url_texthtml'),
            error="OpenSearch: Missing <Url> element with 'text/html' type",
            description='OpenSearch providers must have at least one Url '
            "element with a type attribute set to 'text/html'.")

    # Make sure that each Url has the require attributes.
    for url in acceptable_urls:

        if url.hasAttribute('rel') and url.attributes['rel'].value == 'self':
            continue

        if url.hasAttribute('method') and \
           url.attributes['method'].value.upper() not in ('GET', 'POST'):
            err.error(
                err_id=('opensearch', 'missing_method'),
                error="OpenSearch: <Url> element with invalid 'method'",
                description='A Url element in the OpenSearch provider lists a '
                'method attribute, but the value is not GET or '
                'POST.')

        # Test for attribute presence.
        if not url.hasAttribute('template'):
            err.error(
                err_id=('opensearch', 'missing_template'),
                error='OpenSearch: <Url> element missing template attribute',
                description='<Url> elements of OpenSearch providers must '
                'include a template attribute.')
        else:
            url_template = url.attributes['template'].value
            if url_template[:4] != 'http':
                err.error(err_id=('opensearch', 'invalid_template'),
                          error='OpenSearch: `<Url>` element with invalid '
                          '`template`',
                          description='A `<Url>` element in the OpenSearch '
                          'provider lists a template attribute, but '
                          'the value is not a valid HTTP URL.')

            # Make sure that there is a {searchTerms} placeholder in the
            # URL template.
            found_template = url_template.count('{searchTerms}') > 0

            # If we didn't find it in a simple parse of the template=""
            # attribute, look deeper at the <Param /> elements.
            if not found_template:
                for param in url.getElementsByTagName('Param'):
                    # As long as we're in here and dependent on the
                    # attributes, we'd might as well validate them.
                    attribute_keys = param.attributes.keys()
                    if 'name' not in attribute_keys or \
                       'value' not in attribute_keys:
                        err.error(
                            err_id=('opensearch', 'param_missing_attrs'),
                            error='OpenSearch: `<Param>` element missing '
                            'name/value',
                            description='Param elements in the OpenSearch '
                            'provider must include a name and a '
                            'value attribute.')

                    param_value = (param.attributes['value'].value if 'value'
                                   in param.attributes.keys() else '')
                    if param_value.count('{searchTerms}'):
                        found_template = True

                        # Since we're in a validating spirit, continue
                        # looking for more errors and don't break

            # If the template still hasn't been found...
            if not found_template:
                tpl = url.attributes['template'].value
                err.error(
                    err_id=('opensearch', 'template_not_found'),
                    error='OpenSearch: <Url> element missing template '
                    'placeholder',
                    description=('`<Url>` elements of OpenSearch providers '
                                 'must include a template attribute or '
                                 'specify a placeholder with '
                                 '`{searchTerms}`.',
                                 'Missing template: %s' % tpl))

    # Make sure there are no updateURL elements
    if srch_prov.getElementsByTagName('updateURL'):
        err.error(
            err_id=('opensearch', 'banned_updateurl'),
            error='OpenSearch: <updateURL> elements are banned in OpenSearch '
            'providers.',
            description='OpenSearch providers may not contain <updateURL> '
            'elements.')

    # The OpenSearch provider is valid!
    return err
Ejemplo n.º 44
0
	def testParameterEntity_core(self):
		with self.assertRaises(EntitiesForbidden):
			document = '../../xml_files_windows/parameterEntity_core.xml'
			doc = minidom.parse(document)   		
Ejemplo n.º 45
0
from defusedxml import minidom

doc = minidom.parse('../../xml_files_windows/standard.xml')
print doc.documentElement.nodeName
print doc.getElementsByTagName('data')[0].nodeName
print doc.getElementsByTagName('data')[0].firstChild.nodeValue




Ejemplo n.º 46
0
]

# Loop through all profiles (include any FPS used in OpenShot)
for file in os.listdir(info.PROFILES_PATH):
    filepath = os.path.join(info.PROFILES_PATH, file)
    profile = openshot.Profile(filepath)
    fps_num = profile.info.fps.num
    fps_den = profile.info.fps.den
    fps = float(fps_num) / float(fps_den)
    if fps not in all_fps:
        all_fps.append(fps)

# Loop through all export presets (include any sample rates used in OpenShot)
for file in os.listdir(info.EXPORT_PRESETS_PATH):
    filepath = os.path.join(info.EXPORT_PRESETS_PATH, file)
    xmldoc = xml.parse(filepath)
    sampleRate = float(
        xmldoc.getElementsByTagName("samplerate")[0].childNodes[0].nodeValue)
    if sampleRate not in all_rates:
        all_rates.append(sampleRate)

# Display all common fps and sample rates
print("---------------------------------")
print("Common Sample Rates / Common FPS")
print("---------------------------------")
print("FPS: %s" % sorted(all_fps))
print("Sample Rates: %s" % sorted(all_rates))
print("---------------------------------")
print("GOOD = Sample rate evenly divisible by frame rate")
print("BAD = Sample rate NOT evenly divisible by frame rate")
print("---------------------------------\n")
Ejemplo n.º 47
0
def detect_opensearch(err, package, listed=False):
    'Detect, parse, and validate an OpenSearch provider'

    # Parse the file.
    try:
        # Check if it is a file object.
        if hasattr(package, 'read'):
            srch_prov = parse(package)
        else:
            # It's not a file object; open it (the XML parser is bad at this).
            with open(package, 'rb') as package_file:
                srch_prov = parse(package_file)
    except DefusedXmlException:
        url = 'https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors'
        err.error(
            err_id=('opensearch', 'security_error'),
            error='OpenSearch: XML Security Error',
            description='The OpenSearch extension could not be parsed due to '
                        'a security error in the XML. See {url} for more '
                        'info.'.format(url=url))
        return err
    except ExpatError:
        err.error(
            err_id=('opensearch', 'parse_error'),
            error='OpenSearch: XML Parse Error',
            description='The OpenSearch extension could not be parsed due to '
                        'a syntax error in the XML.')
        return err

    # Make sure that the root element is OpenSearchDescription.
    if srch_prov.documentElement.tagName != 'OpenSearchDescription':
        err.error(
            err_id=('opensearch', 'invalid_document_root'),
            error='OpenSearch: Invalid Document Root',
            description='The root element of the OpenSearch provider is not '
                        "'OpenSearchDescription'.")

    # Per bug 617822
    if not srch_prov.documentElement.hasAttribute('xmlns'):
        err.error(
            err_id=('opensearch', 'no_xmlns'),
            error='OpenSearch: Missing XMLNS attribute',
            description='The XML namespace attribute is missing from the '
                        'OpenSearch document.')

    if ('xmlns' not in srch_prov.documentElement.attributes.keys() or
        srch_prov.documentElement.attributes['xmlns'].value not in (
                    'http://a9.com/-/spec/opensearch/1.0/',
                    'http://a9.com/-/spec/opensearch/1.1/',
                    'http://a9.com/-/spec/opensearchdescription/1.1/',
                    'http://a9.com/-/spec/opensearchdescription/1.0/')):
        err.error(
            err_id=('opensearch', 'invalid_xmlns'),
            error='OpenSearch: Bad XMLNS attribute',
            description='The XML namespace attribute contains an '
                        'value.')

    # Make sure that there is exactly one ShortName.
    sn = srch_prov.documentElement.getElementsByTagName('ShortName')
    if not sn:
        err.error(
            err_id=('opensearch', 'missing_shortname'),
            error='OpenSearch: Missing <ShortName> elements',
            description='ShortName elements are mandatory OpenSearch provider '
                        'elements.')
    elif len(sn) > 1:
        err.error(
            err_id=('opensearch', 'extra_shortnames'),
            error='OpenSearch: Too many <ShortName> elements',
            description='Too many ShortName elements exist in the OpenSearch '
                        'provider.')
    else:
        sn_children = sn[0].childNodes
        short_name = 0
        for node in sn_children:
            if node.nodeType == node.TEXT_NODE:
                short_name += len(node.data)
        if short_name > 16:
            err.error(
                err_id=('opensearch', 'big_shortname'),
                error='OpenSearch: <ShortName> element too long',
                description='The ShortName element must contains less than '
                            'seventeen characters.')

    # Make sure that there is exactly one Description.
    if len(srch_prov.documentElement.getElementsByTagName('Description')) != 1:
        err.error(
            err_id=('opensearch', 'missing_description'),
            error='OpenSearch: Invalid number of <Description> elements',
            description='There are too many or too few Description elements '
                        'in the OpenSearch provider.')

    # Grab the URLs and make sure that there is at least one.
    urls = srch_prov.documentElement.getElementsByTagName('Url')
    if not urls:
        err.error(
            err_id=('opensearch', 'missing_url'),
            error='OpenSearch: Missing <Url> elements',
            description='The OpenSearch provider is missing a Url element.')

    if listed and any(url.hasAttribute('rel') and
                      url.attributes['rel'].value == 'self' for
                      url in urls):
        err.error(
            err_id=('opensearch', 'rel_self'),
            error='OpenSearch: <Url> elements may not be rel=self',
            description='Per AMO guidelines, OpenSearch providers cannot '
                        "contain <Url /> elements with a 'rel' attribute "
                        "pointing to the URL's current location. It must be "
                        'removed before posting this provider to AMO.')

    acceptable_mimes = ('text/html', 'application/xhtml+xml')
    acceptable_urls = [url for url in urls if url.hasAttribute('type') and
                          url.attributes['type'].value in acceptable_mimes]

    # At least one Url must be text/html
    if not acceptable_urls:
        err.error(
            err_id=('opensearch', 'missing_url_texthtml'),
            error="OpenSearch: Missing <Url> element with 'text/html' type",
            description='OpenSearch providers must have at least one Url '
                        "element with a type attribute set to 'text/html'.")

    # Make sure that each Url has the require attributes.
    for url in acceptable_urls:

        if url.hasAttribute('rel') and url.attributes['rel'].value == 'self':
            continue

        if url.hasAttribute('method') and \
           url.attributes['method'].value.upper() not in ('GET', 'POST'):
            err.error(
                err_id=('opensearch', 'missing_method'),
                error="OpenSearch: <Url> element with invalid 'method'",
                description='A Url element in the OpenSearch provider lists a '
                            'method attribute, but the value is not GET or '
                            'POST.')

        # Test for attribute presence.
        if not url.hasAttribute('template'):
            err.error(
                err_id=('opensearch', 'missing_template'),
                error='OpenSearch: <Url> element missing template attribute',
                description='<Url> elements of OpenSearch providers must '
                            'include a template attribute.')
        else:
            url_template = url.attributes['template'].value
            if url_template[:4] != 'http':
                err.error(
                    err_id=('opensearch', 'invalid_template'),
                    error='OpenSearch: `<Url>` element with invalid '
                          '`template`',
                    description='A `<Url>` element in the OpenSearch '
                                'provider lists a template attribute, but '
                                'the value is not a valid HTTP URL.')

            # Make sure that there is a {searchTerms} placeholder in the
            # URL template.
            found_template = url_template.count('{searchTerms}') > 0

            # If we didn't find it in a simple parse of the template=""
            # attribute, look deeper at the <Param /> elements.
            if not found_template:
                for param in url.getElementsByTagName('Param'):
                    # As long as we're in here and dependent on the
                    # attributes, we'd might as well validate them.
                    attribute_keys = param.attributes.keys()
                    if 'name' not in attribute_keys or \
                       'value' not in attribute_keys:
                        err.error(
                            err_id=('opensearch', 'param_missing_attrs'),
                            error='OpenSearch: `<Param>` element missing '
                                  'name/value',
                            description='Param elements in the OpenSearch '
                                        'provider must include a name and a '
                                        'value attribute.')

                    param_value = (param.attributes['value'].value if
                                   'value' in param.attributes.keys() else
                                   '')
                    if param_value.count('{searchTerms}'):
                        found_template = True

                        # Since we're in a validating spirit, continue
                        # looking for more errors and don't break

            # If the template still hasn't been found...
            if not found_template:
                tpl = url.attributes['template'].value
                err.error(
                    err_id=('opensearch', 'template_not_found'),
                    error='OpenSearch: <Url> element missing template '
                          'placeholder',
                    description=('`<Url>` elements of OpenSearch providers '
                                 'must include a template attribute or '
                                 'specify a placeholder with '
                                 '`{searchTerms}`.',
                                 'Missing template: %s' % tpl))

    # Make sure there are no updateURL elements
    if srch_prov.getElementsByTagName('updateURL'):
        err.error(
            err_id=('opensearch', 'banned_updateurl'),
            error='OpenSearch: <updateURL> elements are banned in OpenSearch '
                  'providers.',
            description='OpenSearch providers may not contain <updateURL> '
                        'elements.')

    # The OpenSearch provider is valid!
    return err
Ejemplo n.º 48
0
 def testDOS_indirections(self):
     with self.assertRaises(EntitiesForbidden):
         document = "../../xml_files_windows/dos/dos_indirections.xml"
         doc = minidom.parse(document)