Example #1
0
def csc_to_graph(name):
    tree = ET(name)
    mote_id = [int(t.text) for t in tree.findall(".//mote/interface_config/id")]
    mote_type = [t.text for t in tree.findall(".//mote/motetype_identifier")]
    x = [float(t.text) for t in tree.findall(".//mote/interface_config/x")]
    y = [float(t.text) for t in tree.findall(".//mote/interface_config/y")]
    z = [float(t.text) for t in tree.findall(".//mote/interface_config/z")]
Example #2
0
def csc_to_graph(name):
    tree = ET(name)
    mote_id = [
        int(t.text) for t in tree.findall(".//mote/interface_config/id")
    ]
    mote_type = [t.text for t in tree.findall(".//mote/motetype_identifier")]
    x = [float(t.text) for t in tree.findall(".//mote/interface_config/x")]
    y = [float(t.text) for t in tree.findall(".//mote/interface_config/y")]
    z = [float(t.text) for t in tree.findall(".//mote/interface_config/z")]
Example #3
0
    def parse_bitmasks(self, tree: ElementTree):
        for e_bitmask in tree.findall('./types/type[@category="bitmask"]'):
            if 'alias' in e_bitmask.attrib:
                continue

            bitmask_name = e_bitmask.find('./name').text

            if self.should_ignore(type_=bitmask_name):
                continue

            c_bitmask = CBitmask(bitmask_name)

            requires = e_bitmask.get('requires')
            if requires:
                cases = {}
                e_enum = tree.find(f'./enums[@name="{requires}"]')
                for e_case in e_enum.findall('./enum'):
                    if 'alias' in e_case.attrib:
                        continue
                    cases[e_case.attrib['name']] = parse_enum_value(e_case)

                for extension in self.extensions:
                    if extension.supported == 'disabled' or extension.platform:
                        continue
                    for ext_enum in extension.enums:
                        if ext_enum.name == requires:
                            for ext_case in ext_enum.cases:
                                cases[ext_case.name] = ext_case.value

                c_bitmask.enum = CEnum(
                    requires,
                    [CEnum.Case(name, value) for name, value in cases.items()])

            self.bitmasks.append(c_bitmask)
Example #4
0
    def parse_enums(self, tree: ElementTree):
        for e_enum in tree.findall('./enums[@type="enum"]'):
            enum_name = e_enum.attrib['name']

            if self.should_ignore(type_=enum_name):
                continue

            cases = {}
            for e_case in e_enum.findall('./enum'):
                if 'alias' in e_case.attrib:
                    continue
                cases[e_case.attrib['name']] = parse_enum_value(e_case)

            for extension in self.extensions:
                if extension.supported == 'disabled' or extension.platform:
                    continue
                for ext_enum in extension.enums:
                    if ext_enum.name == enum_name:
                        for ext_case in ext_enum.cases:
                            cases[ext_case.name] = ext_case.value

            c_enum = CEnum(
                enum_name,
                [CEnum.Case(name, value) for name, value in cases.items()])
            self.enums.append(c_enum)
Example #5
0
    def parse_handles(self, tree: ElementTree):
        handles: Dict[str, CHandle] = {}
        parents: Dict[str, str] = {}

        for e_handle in tree.findall('./types/type[@category="handle"]'):
            if 'alias' in e_handle.attrib:
                handle_name = e_handle.attrib['name']
                alias = handles[e_handle.attrib['alias']]
                protect = self.find_protect(type_=handle_name)
                self.aliases.append(CAlias(handle_name, alias.name, protect))
                if alias.protect and not protect:
                    alias.protect = None
                continue

            handle_name = e_handle.find('./name').text
            handle = CHandle(handle_name,
                             protect=self.find_protect(type_=handle_name))
            self.handles.append(handle)
            handles[handle_name] = handle
            parents[handle_name] = e_handle.get('parent')

        for handle_name, handle in handles.items():
            try:
                handle.parent = handles[parents[handle_name]]
            except KeyError:
                pass
Example #6
0
    def parse_extensions(self, tree: ElementTree):
        for e_extension in tree.findall('./extensions/extension'):
            extension_number = int(e_extension.attrib['number'])

            enums: Dict[str, List[CEnum.Case]] = {}
            for e_enum in e_extension.findall('./require/enum[@extends]'):
                if 'alias' in e_enum.attrib:
                    continue
                c_case = CEnum.Case(e_enum.attrib['name'],
                                    parse_enum_value(e_enum, extension_number))
                enums.setdefault(e_enum.attrib['extends'], []).append(c_case)

            platform = e_extension.get('platform')

            c_extension = CExtension(
                name=e_extension.attrib['name'],
                supported=e_extension.get('supported'),
                platform=platform,
                protect=self.platform_protects[platform] if platform else None,
                types=[
                    t.attrib['name']
                    for t in e_extension.findall('./require/type')
                ],
                enums=[
                    CEnum(enum_name, cases)
                    for enum_name, cases in enums.items()
                ],
                commands=[
                    c.attrib['name']
                    for c in e_extension.findall('./require/command')
                ],
            )
            self.extensions.append(c_extension)
Example #7
0
def _build_inner_html(el: ElementTree, base_href: str) -> str:
    """
    Extract HTML text from a xml.etree.ElementTree.

    Beware: this mutates `el`:

        * It modifies img src starting with `./` to start with `{base_href}/`
        * It prepends STATIC_URL to img src
        * It replaces `{{LESSON_FILES_URL}}` with `{base_href}`. (We assume
          `base_href` has no reserved HTML characters.)

    Params:

        el: the `xml.etree.ElementTree` we want to dump
        base_href: if image URLs begin with './', what text to prepend
    """
    for img in el.findall(".//img"):
        src = img.get("src")
        if src.startswith("./"):
            src = f"{base_href}{src[1:]}"  # include the '/'
        if "//" not in src:
            src = f"{settings.STATIC_URL}{src}"
        img.set("src", src)

    outer_html = ElementTree.tostring(el,
                                      encoding="unicode",
                                      method="html",
                                      short_empty_elements=True)
    open_tag_end = outer_html.index(">")
    close_tag_begin = outer_html.rindex("<")
    inner_html = outer_html[(open_tag_end + 1):close_tag_begin]
    inner_html = inner_html.replace("{{LESSON_FILES_URL}}",
                                    settings.STATIC_URL + base_href)

    return inner_html
Example #8
0
def get_transcript(tree: ElementTree, target_speaker: str):
    # fold text in different speech nodes by the same target_speaker
    speeches = [(k,list(v)) for k,v in itertools.groupby(tree.findall('.//speech'), key=lambda e: e.attrib['by'])]

    result = []
    for (g1, g2) in zip(speeches[:-1], speeches[1:]):
        if g2[0] == target_speaker:

            def process_speech(e):
                text = e.find('p').text
                if not text:
                    return ""
                else:
                    # Do some preprocessing of the tokens
                    return re.sub(r'[^\x00-\x7F]+', ' ', text)

            # hypothesis all that matters is the last 100 words
            input  = " ".join(" ".join([process_speech(e) for e in g1[1]]).split()[-100:])
            output = " ".join(" ".join([process_speech(e) for e in g2[1]]).split()[:900])
            row = Row(input, output)
            input_len = len(input.split())
            output_len = len(output.split())
            lens.add(input_len)
            lens.add(output_len)
            input_lens[input_len] += 1

            if input_len > 300:
                print(input)
                print(len(output.split()))

            result.append(row)
    return result
Example #9
0
 def run(self, doc_tree: ElementTree):
     images = doc_tree.findall(f".//*img")
     for img in images:
         # this uses cascading to determine what is applied--md-image should
         # be described later in the stylesheet than other relevant image styles
         # this will only be applied to images derived from ![]() tags, not raw html
         img.set('class', " ".join((img.get('class') or '', "md-image")))
    def buildMediaTypeList(self, configObj, isUpdate):
        #build fileTypeList
        fileTypeList = []

        if (isUpdate):
            fileTypes = configObj.tree.findall('FileTypes/FileType')
        else:
            #build fileTypeList
            configFile = util.joinPath(util.getAddonInstallPath(), 'resources',
                                       'database', 'config_template.xml')

            if (not xbmcvfs.exists(configFile)):
                Logutil.log(
                    'File config_template.xml does not exist. Place a valid config file here: '
                    + str(configFile), util.LOG_LEVEL_ERROR)
                return None, util.localize(32040)

            tree = ElementTree().parse(configFile)
            fileTypes = tree.findall('FileTypes/FileType')

        for fileType in fileTypes:
            name = fileType.attrib.get('name')
            if (name != None):
                type = fileType.find('type')
                if (type != None and type.text == 'video'):
                    name = name + ' (video)'
                fileTypeList.append(name)

        return fileTypeList, ''
Example #11
0
def verify_prop_values(doc: ElementTree, name: str, value: str) -> bool:
    matches = False
    for prop in doc.findall("property"):
        if prop.find("name").text == name and prop.find("value").text == value:
            print(f'{name} : {prop.find( "value" ).text} : {value}')
            matches = True
    return matches
	def buildMediaTypeList(self, configObj, isUpdate):
		#build fileTypeList
		fileTypeList = []
		
		if(isUpdate):
			fileTypes = configObj.tree.findall('FileTypes/FileType')
		else:
			#build fileTypeList
			configFile = util.joinPath(util.getAddonInstallPath(), 'resources', 'database', 'config_template.xml')
	
			if(not xbmcvfs.exists(configFile)):
				Logutil.log('File config_template.xml does not exist. Place a valid config file here: ' +str(configFile), util.LOG_LEVEL_ERROR)
				return None, util.localize(32040)
			
			tree = ElementTree().parse(configFile)			
			fileTypes = tree.findall('FileTypes/FileType')			
			
		for fileType in fileTypes:
			name = fileType.attrib.get('name')
			if(name != None):
				type = fileType.find('type')				
				if(type != None and type.text == 'video'):
					name = name +' (video)'
				fileTypeList.append(name)
				
		return fileTypeList, ''
Example #13
0
def _build_inner_html(el: ElementTree, base_href: str) -> str:
    """
    Extract HTML text from a xml.etree.ElementTree.

    Beware: this mutates `el`:

        * It modifies img src starting with `./` to start with `{base_href}/`
        * It prepends STATIC_URL to img src

    Params:

        el: the `xml.etree.ElementTree` we want to dump
        base_href: if image URLs begin with './', what text to prepend
    """
    for img in el.findall('.//img'):
        src = img.get('src')
        if src.startswith('./'):
            src = f'{base_href}{src[1:]}'  # include the '/'
        if '//' not in src:
            src = f'{settings.STATIC_URL}{src}'
        img.set('src', src)

    outer_html = ElementTree.tostring(el,
                                      encoding='unicode',
                                      method='html',
                                      short_empty_elements=True)
    open_tag_end = outer_html.index('>')
    close_tag_begin = outer_html.rindex('<')
    inner_html = outer_html[(open_tag_end + 1):close_tag_begin]

    return inner_html
Example #14
0
def getProjectWebClassPath():
    tree = ElementTree()
    project_cfg = getProjectCfg()

    classPathXmlPath = os.path.join(os.getcwd(), ".tomcatplugin")
    if not os.path.exists(classPathXmlPath):
        return None
    
    tree.parse(classPathXmlPath)
    entries = tree.findall("webClassPathEntries/webClassPathEntry")
    cp_entries = []
    for entry in  entries :
        path = entry.text
        if "M2_REPO" in path:
            path = path.replace("M2_REPO",M2_REPO)
        elif path.startswith("/"):
            prj_name , inner_path = path[1:].split("/",1)
            prj_ab_path = project_cfg.get(prj_name)
            if prj_ab_path == None :
                print "can't find project '%s' config in project.cfg " % (prj_name)
                sys.exit(0)
            path = os.path.normpath(os.path.join(prj_ab_path, inner_path))
        path = os.path.normpath(path)
        cp_entries.append(path)

    return cp_entries
Example #15
0
    def _parse_xml(xml: ElementTree, code='USD'):
        if not xml:
            return None

        date = datetime.strptime(xml.attrib.get('Tarih'), '%d.%m.%Y').date()
        for currency in xml.findall('Currency'):
            if currency.get('Kod') == code:
                return Currency(currency, date)
Example #16
0
def characteristicInstall(child: ET):
    for param in child.findall("parm"):
        name = param.attrib["name"]
        value = param.attrib["value"]
        if name == "AppName":
            print("AppName:"+value)
        if name == "InstallDir":
            print("InstallDir:"+value)
def coalesce(etree, xpath, get_text=False):
    elem = etree.findall(xpath)
    if len(elem) != 0:
        assert len(elem) == 1
        res = elem[0]
        if get_text:
            res = res.text
        return res
    return None
def coalesce(etree, xpath, get_text = False):
	elem = etree.findall(xpath)
	if len(elem) != 0:
		assert len(elem) == 1
		res = elem[0]
		if get_text:
			res = res.text
		return res
	return None
Example #19
0
def get_things(root: ET):
    TSDs = root.findall(".//ThingSaveData")
    result_dict = dict()

    for TSD in TSDs:
        ref_id = TSD.find('ReferenceId').text
        result_dict[ref_id] = TSD

    return result_dict
Example #20
0
def _tag(tag_schemes: ElementTree, base: str = "") -> dict:
    #print(f"_tag({tag_schemes.attrib['key']})")
    a = {
        ".".join([base, tag_schemes.attrib["key"],
                  scheme.attrib["key"]]).strip("."): scheme
        for scheme in tag_schemes.findall("./tag")
    }
    for b in tag_schemes.findall(".//tag-scheme"):
        if "src" in b.attrib:
            #print(b.attrib["src"])
            scheme = resolve_uri(b.attrib["src"])
            #print(scheme)
        else:
            scheme = b

        a.update(_tag(scheme, base=".".join([base,
                                             tag_schemes.attrib["key"]])))
    #print(f"a: {a}")
    return a
Example #21
0
    def fetchByEventId(self, eventId):
        eventId = str(eventId)
        doc = ElementTree(file="MarketData.xml")
        symbols = []
        for event in doc.findall(".//Event"):
            if event.get("id") == eventId:
                for symbol in event.findall(".//contract/symbol"):
                    symbols.append(symbol.text)

        return self.fetchBySymbols(symbols)
Example #22
0
    def fetchByEventId(self, eventId):
        eventId = str(eventId)
        doc = ElementTree(file="MarketData.xml")
        symbols = []
        for event in doc.findall(".//Event"):
            if event.get("id") == eventId:
                for symbol in event.findall(".//contract/symbol"):
                    symbols.append(symbol.text)

        return self.fetchBySymbols(symbols)
Example #23
0
def act2graph(graph: Graph, xml_root: Xml, registry: dict, namespaces: dict,
              tag: str) -> Graph:
    """ Transform activityName tag into RDF graph.

    The function transforms the Activity MasterData into identifier. The output
    is a RDF graph that represents a part of the Ecoinvent nomenclature
    structured with The IEO ontology. The output represents the centrally
    registrered identifier (CRID) by the database version and the activity name
    identifier, e.g. ecoinvent3.0:88d6c0aa-0053-4367-b0be-05e4b49ff3c5 for the
    copper production, primary.
    Variables:
    - graph: the graph to update
    - xml_root: the root of the xml file
    - registry: dictionary containing the reference/info of the data registry
    - tag: string containing the namespace tag
    - namespaces: dictionary containing the namespaces with tags
    """
    # crid_reg: CRID registry, e.g Ecoinvent
    crid_reg = registry['reg_id']
    crid_reg_label = registry['label']
    # Database identifier, e.g. EcoInvent3.1
    major_release = xml_root.attrib['majorRelease']
    minor_release = xml_root.attrib['minorRelease']
    database_version = f'v{major_release}_{minor_release}'
    database_label = f'{crid_reg_label}{major_release}.{minor_release}'
    database_id = crid_reg + database_version
    graph.add((ECO[crid_reg], RDFS.label, Literal(crid_reg_label, lang='en')))
    graph.add((ECO.activityId, RDFS.subClassOf, ACT_CRID))
    activity_id_label = 'EcoInvent activity identifier'
    graph.add((ECO.activityId, RDFS.label, Literal(activity_id_label,
                                                   lang='en')))
    graph.add((ECO.activity_name, RDFS.subClassOf, REF_ACTIVITY))
    activity_label = 'EcoInvent activity label'
    graph.add((ECO.activity_name, RDFS.label, Literal(activity_label,
                                                      lang='en')))
    for activity_name in xml_root.findall(tag, namespaces):
        activity_name_id = activity_name.attrib['id']
        crid = activity_name_id + database_version
        graph.add((ECO[crid], RDF.type, ECO.activityId))
        graph.add((ECO[activity_name_id], RDF.type, ECO.activity_name))
        # Define the property relation between the symbols of the CRID
        graph.add((ECO[crid], BFO.has_part, ECO[database_id]))
        graph.add((ECO[database_id], BFO.part_of, ECO[crid]))
        graph.add((ECO[crid], BFO.has_part, ECO[activity_name_id]))
        graph.add((ECO[activity_name_id], BFO.part_of, ECO[crid]))
        # Define the labels with the different languages
        xml_ns = namespaces['xml']
        for name in activity_name.findall('eco:name', namespaces):
            lang = name.attrib['{' + xml_ns + '}lang']
            activity_label = name.text
            crid_label = f'{database_label}:{activity_label}'
            graph.add((ECO[crid], RDFS.label, Literal(crid_label, lang=lang)))
            graph.add((ECO[activity_name_id], RDFS.label,
                       Literal(activity_label, lang=lang)))
    return graph
Example #24
0
    def _setup_codes(self, config_tree: ElementTree):
        codes = config_tree.findall('code')

        count = 0
        for code in codes:
            cid = code.get('id')
            cval = code.get('value')
            self.codes.append(CodeInfo(cid, cval))
            count += 1

        logging.debug("Loaded %i manual codes", count)
Example #25
0
def overwrite_prop( doc: ElementTree, prop_name: str, value: str ):
    for prop in doc.findall( "property" ):
        if prop.find( "name" ).text == prop_name:
            print( f'replacing {prop_name} : {prop.find( "value" ).text} with {value}' )
            prop.find( "value" ).text = value  ## careful, this modifies the doc
            break
    else:
        print( f'adding {prop_name} : {value}' )
        new_prop = ElementTree.SubElement( doc.getroot(), 'property' )
        ElementTree.SubElement( new_prop, 'name' ).text = prop_name
        ElementTree.SubElement( new_prop, 'value' ).text = value
Example #26
0
 def fetchEventsByGroupId(self, eventGroupId):
     eventGroupId = str(eventGroupId)
     ids = []
     doc = ElementTree(file="MarketData.xml")
     for group in doc.findall(".//EventGroup"):
         if group.get("id") == eventGroupId:
             for event in group.findall(".//Event"):
                 eventId = event.get("id")
                 name = event.find(".//name").text
                 ids.append((eventId, name))
     return ids
Example #27
0
def get_modules(directory):
    '''Analyses the pom.xml file and extracts declared modules'''
    tree = ElementTree()
    f = directory + "/pom.xml"
    if settings['verbose']:
      print "Parsing %s to get a list of modules in project" % f
    parser = XMLParser(target=CommentedTreeBuilder())
    tree.parse(f, parser=parser)
    mods = tree.findall(".//{%s}module" % maven_pom_xml_namespace)
    for m in mods:
        modules.append(m.text)
Example #28
0
    def _Parser(self, xmlfile):
        """MP.Parser(xmlfile)
        Parser the xmlfile. Old parser version. Only for Convert.
        """
        self.motifs = {}
        xmltree = ElementTree()
        try:
            xmltree.parse(xmlfile)
        except IOError:
            logger.error(
                "Fail to parser the xml file. Not exist or format error?")
            return None

        for pos in xmltree.findall("motif"):
            tag = 'id'
            attrib = pos.attrib
            id = attrib[tag]
            logger.debug(id)
            self.motifs[id] = {}
            self.motifs[id][tag] = [id]

            tag = 'edition'
            try:
                self.motifs[id][tag] = [attrib[tag]]
            except:
                self.motifs[id][tag] = []

            for tag in ('source', 'sourcefile', 'status', 'description', 'numseqs', 'pmid', 'dbd', \
            'comment1', 'comment2', 'comment3', 'comment4', 'comment5'):
                if pos.find(tag) != None:
                    self.motifs[id][tag] = [pos.find(tag).text.strip()]
                else:
                    self.motifs[id][tag] = []

            for tag in ('species', 'entrez', 'synonym', 'refseq', 'symbol'):
                if pos.find(tag + 'list') != None:
                    t = pos.find(tag + 'list')
                    self.motifs[id][tag] = [
                        i.text.strip() for i in t.findall(tag)
                    ]
                else:
                    self.motifs[id][tag] = []

            for tag in ('pssm', ):
                self.motifs[id][tag] = []
                if pos.find(tag) != None:
                    plist = pos.find(tag).findall('pos')
                    plist.sort()
                    for t in plist:
                        t = t.findall('*')
                        t.sort()
                        self.motifs[id][tag].append(
                            [float(s.text.strip()) for s in t])
                    self.motifs[id][tag] = [self.motifs[id][tag]]
Example #29
0
 def fetchEventsByGroupId(self, eventGroupId):
     eventGroupId = str(eventGroupId)
     ids = []
     doc = ElementTree(file="MarketData.xml")
     for group in doc.findall(".//EventGroup"):
         if group.get("id") == eventGroupId:
             for event in group.findall(".//Event"):
                 eventId = event.get("id")
                 name = event.find(".//name").text
                 ids.append((eventId, name))
     return ids
Example #30
0
def get_rooms_dict(root: ET):
    rooms = root.findall('.//Room')
    by_id_dict = dict()
    by_position_dict = dict()

    reference_tuples = map(get_room_reference_tuple, rooms)

    for item in reference_tuples:
        by_id_dict[item[0]] = item[2]
        by_position_dict[item[1]] = item[2]

    return by_id_dict, by_position_dict
def _get_note(xml: ElementTree) -> str:
    """ Get note text if any """
    xpath = "./Subject/Descriptive_Notes/Descriptive_Note"
    for xml_item in xml.findall(xpath):
        language_node = xml_item.find("./Note_Language")
        if language_node is not None:
            if language_node.text == 'English':
                note_node = xml_item.find("./Note_Text")
                if note_node is not None:
                    value_found = note_node.text
                    return value_found
    return ''
Example #32
0
    def _setup_tags(self, config_tree: ElementTree):
        tags = config_tree.findall('tag')

        count = 0
        for tag in tags:
            tid = tag.get('id')
            tval = tag.get('value')
            tmode = tag.get('mode')
            self.tags.append(TagInfo(tid, tval, tmode))
            count += 1

        logging.debug("Loaded %i RFID tags", count)
Example #33
0
def main(args):
    if ((args.keyfile is None) or (args.target is None)):
        ops.error(
            'You must provide a keyfile and a target IP, please try again with -k and -t'
        )
        return
    confxml = ElementTree()
    configxmlfilename = os.path.join(dsz.lp.GetResourcesDirectory(), '..',
                                     'implants', 'Darkpulsar-1.0.0.0.xml')
    confxml.parse(configxmlfilename)
    f = open(args.keyfile)
    try:
        newkey = f.read()
    except Exception as ex:
        ops.error('Error reading keyfile')
        raise ex
    finally:
        f.close()
    for ele in confxml.findall('{urn:trch}inputparameters'):
        for subele in ele.findall('{urn:trch}parameter'):
            if (subele.get('name') == 'SigPrivateKey'):
                for keyele in subele.findall('{urn:trch}default'):
                    keyele.text = newkey
    outfile = open(configxmlfilename, 'w')
    try:
        confxml.write(outfile)
    except Exception as ex:
        ops.error('Could not update the FUZZBUNCH config for DAPU')
        raise ex
    finally:
        outfile.close()
    redirport = 0
    dsz.control.echo.Off()
    (success, cmdid) = dsz.cmd.RunEx('local netconnections',
                                     dsz.RUN_FLAG_RECORD)
    dsz.control.echo.On()
    print cmdid
    conns = ops.data.getDszObject(
        cmdid=cmdid).initialconnectionlistitem.connectionitem
    while (redirport == 0):
        redirport = random.randint(10000, 65500)
        for conn in conns:
            if (conn.local.port == redirport):
                redirport = 0
                break
    dsz.cmd.Run(('redirect -tcp -lplisten %d -target %s %s' %
                 (redirport, args.target, args.port)))
    ops.info((
        'Your redirector has been started, local listening port to connect for DAPU is %d'
        % redirport))
    ops.info(
        'You can now start FUZZBUNCH to connect to DARKPULSAR.  If you already launched FUZZBUNCH, you will need to start it again'
    )
Example #34
0
    def __changeset_parser(self, tree: ElemTree):
        tags = self.__kv_parser(tree.findall('tag'))
        cs_prop = {}
        for key in tree.keys():
            cs_prop[key] = tree.get(key)

        com_xml = tree.findall('discussion/comment')
        comments = []
        for com in com_xml:
            comments.append(Comment(com.find('text').text, com.get('uid'), com.get('user'), com.get('date')))
        try:
            bbox = cs_prop['min_lon'], cs_prop['min_lat'], cs_prop['max_lon'], cs_prop['max_lat']
        except KeyError:
            bbox = (None, None, None, None)
        created = datetime.strptime(cs_prop['created_at'], "%Y-%m-%dT%H:%M:%SZ")
        if cs_prop['created_at'] is True:
            closed = None
        else:
            closed = datetime.strptime(cs_prop['closed_at'], "%Y-%m-%dT%H:%M:%SZ")
        ch_set = ChangeSet(cs_prop['id'], cs_prop['user'], cs_prop['uid'], created,
                           cs_prop['open'], bbox, closed, tags, comments)
        return ch_set
Example #35
0
def fix_image_links(t: ET) -> ET:
    ns = {"svg": "http://www.w3.org/2000/svg", "xlink": "http://www.w3.org/1999/xlink"}

    for node in t.findall(".//svg:image[@xlink:href]", namespaces=ns):
        filepath = node.get(f"{{{ns['xlink']}}}href")
        if filepath is None:
            continue
        path = pathlib.Path(filepath)
        if not path.exists():
            logger.info("fix image links, %s is not found", path)
            continue
        node.set(f'{{{ns["xlink"]}}}href', "data:image/png;base64," + to_base64(path))
    return t
Example #36
0
    def _setup_users(self, config_tree: ElementTree):
        users = config_tree.findall('user')

        count = 0
        for user in users:
            #uid = user.get('id')
            uname = user.get('name')
            upass = user.get('pass')

            self.users.append(UserInfo(uname, upass))
            count += 1

        logging.debug("Loaded %i user infos", count)
Example #37
0
    def _Parser(self, xmlfile):
        """MP.Parser(xmlfile)
        Parser the xmlfile. Old parser version. Only for Convert.
        """
        self.motifs = {}
        xmltree = ElementTree()
        try:
            xmltree.parse(xmlfile)
        except IOError:
            logger.error("Fail to parser the xml file. Not exist or format error?")
            return None

        for pos in xmltree.findall("motif"):
            tag = 'id'
            attrib = pos.attrib
            id = attrib[tag]
            logger.debug(id)
            self.motifs[id] = {}
            self.motifs[id][tag] = [id]
            
            tag = 'edition'
            try:
                self.motifs[id][tag] = [attrib[tag]]
            except:
                self.motifs[id][tag] = []
    
            for tag in ('source', 'sourcefile', 'status', 'description', 'numseqs', 'pmid', 'dbd', \
            'comment1', 'comment2', 'comment3', 'comment4', 'comment5'):
                if pos.find(tag)!=None:
                    self.motifs[id][tag] = [pos.find(tag).text.strip()]
                else:
                    self.motifs[id][tag] = []
                    
            for tag in ('species', 'entrez', 'synonym', 'refseq', 'symbol'):
                if pos.find(tag+'list')!=None:
                    t = pos.find(tag+'list')
                    self.motifs[id][tag] = [i.text.strip() for i in t.findall(tag)]
                else:
                    self.motifs[id][tag] = []
                    
            for tag in ('pssm',):
                self.motifs[id][tag]=[]
                if pos.find(tag)!=None:
                    plist = pos.find(tag).findall('pos')
                    plist.sort()
                    for t in plist:
                        t = t.findall('*')
                        t.sort()
                        self.motifs[id][tag].append([ float(s.text.strip()) for s in t ])
                    self.motifs[id][tag] = [self.motifs[id][tag]]
Example #38
0
    def Parser(self, xmlfile):
        """MP.Parser(xmlfile)
        Parser the xmlfile.
        """
        self.motifs = {}
        xmltree = ElementTree()
        try:
            xmltree.parse(xmlfile)
        except IOError:
            logger.error("Fail to parser the xml file. Not exist or format error?")
            return None

        for pos in xmltree.findall("motif"):
            #get key and set empty element
            key = pos.get(self.keyName)
            if not key:
                logger.error('No %s found for node.' %self.keyName)
                return None
            if key in self.motifs.keys():
                logger.warning("%s has exist in instance."%key)
            self.motifs[key] = {}

            #add attribs and tags for each element.
            for iattr in self.attr_list:
                value = pos.get(iattr)
                if value:
                    self.motifs[key][iattr] = [value]
                else:
                    self.motifs[key][iattr] = []
                
            for itag in self.tag_list:
                self.motifs[key][itag] = [t.text.strip() for t in pos.findall(itag)]
                    
            itag = 'pssm'
            self.motifs[key][itag]=[]
            for ipssm in pos.findall(itag):
                matrix = []
                plist = ipssm.findall('pos')
                plist.sort(key=lambda x:int(x.get('num')))
                for t in plist:
                    base_A = float(t.find('A').text.strip())
                    base_C = float(t.find('C').text.strip())
                    base_G = float(t.find('G').text.strip())
                    base_T = float(t.find('T').text.strip())
                    matrix.append([base_A, base_C, base_G, base_T])
                self.motifs[key][itag].append(matrix)
Example #39
0
def main(args):
    if ((args.keyfile is None) or (args.target is None)):
        ops.error('You must provide a keyfile and a target IP, please try again with -k and -t')
        return
    confxml = ElementTree()
    configxmlfilename = os.path.join(dsz.lp.GetResourcesDirectory(), '..', 'implants', 'Darkpulsar-1.0.0.0.xml')
    confxml.parse(configxmlfilename)
    f = open(args.keyfile)
    try:
        newkey = f.read()
    except Exception as ex:
        ops.error('Error reading keyfile')
        raise ex
    finally:
        f.close()
    for ele in confxml.findall('{urn:trch}inputparameters'):
        for subele in ele.findall('{urn:trch}parameter'):
            if (subele.get('name') == 'SigPrivateKey'):
                for keyele in subele.findall('{urn:trch}default'):
                    keyele.text = newkey
    outfile = open(configxmlfilename, 'w')
    try:
        confxml.write(outfile)
    except Exception as ex:
        ops.error('Could not update the FUZZBUNCH config for DAPU')
        raise ex
    finally:
        outfile.close()
    redirport = 0
    dsz.control.echo.Off()
    (success, cmdid) = dsz.cmd.RunEx('local netconnections', dsz.RUN_FLAG_RECORD)
    dsz.control.echo.On()
    print cmdid
    conns = ops.data.getDszObject(cmdid=cmdid).initialconnectionlistitem.connectionitem
    while (redirport == 0):
        redirport = random.randint(10000, 65500)
        for conn in conns:
            if (conn.local.port == redirport):
                redirport = 0
                break
    dsz.cmd.Run(('redirect -tcp -lplisten %d -target %s %s' % (redirport, args.target, args.port)))
    ops.info(('Your redirector has been started, local listening port to connect for DAPU is %d' % redirport))
    ops.info('You can now start FUZZBUNCH to connect to DARKPULSAR.  If you already launched FUZZBUNCH, you will need to start it again')
def find_option_and_change(document: ElementTree, name: AnyStr, value: AnyStr):
    """Finds option in xml document and sets value

    :param document: ElementTree object
    :param name: swa option name
    :param value: value for option
    :return:
    """
    xpath = './/preference[@name="{tag_name}"]'.format(tag_name=name)
    for elem in document.findall(xpath):
        elem.text = value
        break
    else:
        warnings.warn(
            'SWA option {name} is not found in SWA preference document. This'
            ' option wont be added to the document with value '
            '"{value}".'.format(name=name, value=value),
            RuntimeWarning
        )
Example #41
0
def getProjectClassPath():
    tree = ElementTree()
    classPathXmlPath = os.path.join(os.getcwd(), ".classpath")
    if not os.path.exists(classPathXmlPath):
        return None
    
    tree.parse(classPathXmlPath)
    entries = tree.findall("classpathentry")
    cp_entries = []
    for entry in  entries :
        if entry.get("kind") == "var" :
            path = entry.get("path")
            if "M2_REPO" in path:
                path = path.replace("M2_REPO",M2_REPO)
            path = os.path.normpath(path)
            cp_entries.append(path)
        if entry.get("kind") == "output" :
            path = os.path.normpath(os.path.join(os.getcwd(), entry.get("path")))
            cp_entries.append(path)

    return cp_entries
Example #42
0
import urllib
import xml.etree.ElementTree as ET

#serviceurl = 'http://maps.googleapis.com/maps/api/geocode/xml?'
url = raw_input("Enter URL: ")
if url is '':
    url = 'http://python-data.dr-chuck.net/comments_42.xml'

tree = ET.fromstring(url)
names = ET.findall('comments/comment')
sum = 0
# <comment>
#    <name>Romina</name>
#    <count>97</count>
# </comment>
for i in names:
    sum = sum + int(i.find('count').text)

print sum

# while True:
#     address = raw_input('Enter location: ')
#     if len(address) < 1 : break
#
#     url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
#     print 'Retrieving', url
#     uh = urllib.urlopen(url)
#     data = uh.read()
#     print 'Retrieved',len(data),'characters'
#     print data
#     tree = ET.fromstring(data)
Example #43
0
 def makeSymbolToIdMap(self):
     doc = ElementTree(file="MarketData.xml")
     for event in doc.findall(".//contract"):
         symbol = event.find("symbol").text
         id = event.get("id")
         self.symbol_id_map[symbol] = id
	def parse_document(self, etree, systemdate):
		query_queue = collections.defaultdict(list)

		systemdate_sqlite_str = systemdate.strftime('%Y-%m-%d %H:%M:%S.%f')


		snapshot_utc_date = datetime.datetime.utcfromtimestamp(
				int(etree.find('PinnacleFeedTime').text) / 1e3
		)
		snapshot_sqlite_str = snapshot_utc_date.strftime('%Y-%m-%d %H:%M:%S.%f')
		for event_el in etree.findall('events/event'):
			islive = coalesce(event_el, 'IsLive', 1)
			eventid = event_el.find('gamenumber').text
			is_contest = len(event_el.findall('contest_maximum')) != 0
			evdate_str = event_el.find('event_datetimeGMT').text
			evdate_dt = datetime.datetime.strptime(evdate_str, '%Y-%m-%d %H:%M')
# For unknown reasons, pinnacle's xml lists some matches as starting on weird
# times like 19:59 and 21:44. This will fix this.
			if evdate_dt.minute % 5 == 4:
				LOGGER.debug('Adding 1 minute to datetime: %s', evdate_dt)
				evdate_dt += datetime.timedelta(minutes = 1)

			def add_args(*args, **kwargs):
				if args[0] == 'odds':
					query_queue[eventid].append([
								BEFORE_SQL,
								[ kwargs['periodnumber'],
								kwargs['type'],
								kwargs['eventid']]
					])
				args_to_append = cl.get_insert_args_mysql(*args, **kwargs)
				query_queue[eventid].append(args_to_append)
				if args[0] == 'odds':
					query_queue[eventid].append([
								AFTER_SQL1,
								[ kwargs['periodnumber'],
								kwargs['type'],
								kwargs['eventid']]
					])
					query_queue[eventid].append([
								AFTER_SQL2,
								[ kwargs['periodnumber'],
								kwargs['type'],
								kwargs['eventid']]
					])

			add_args(
				'events',
				'replace',
				id = eventid,
				date = cl.datetime_to_sqlite_str(evdate_dt),
				sporttype = event_el.find('sporttype').text,
				league = event_el.find('league').text,
				islive = (1 if islive == 'Yes' else 0),
				description = coalesce(event_el, 'description', 1),
			)
			for participant_el in event_el.findall('participants/participant'):
				contestantnum = participant_el.find('contestantnum').text
				rotnum = participant_el.find('rotnum').text
				vhd = coalesce(participant_el, 'visiting_home_draw', 1)
				pitcher = coalesce(participant_el, 'pitcher', 1)
				add_args(
					'participants',
					'replace',
					eventid = eventid,
					contestantnum = contestantnum,
					rotnum = rotnum,
					vhdou = VHDOU_VAL[vhd],
					pitcher = pitcher,	
					name = participant_el.find('participant_name').text,
				)
				if is_contest:
					add_args(
						'snapshots',
						'replace',
						eventid = eventid,
						date = snapshot_sqlite_str,
						systemdate = systemdate_sqlite_str,
						mlmax = int(coalesce(event_el, 'contest_maximum', 1)),
					)
					add_args(
						'odds',
						'replace',
						eventid = eventid,
						periodnumber = None,
						snapshotdate = snapshot_sqlite_str,
						type = 'm',
						threshold = 0,
						vhdou = None,
						price = dec_odds(
							coalesce(
								participant_el, 'odds/moneyline_value', 1
							)
						),
						to_base = fix_to_base(coalesce(
									participant_el, 'odds/to_base', 1
						)),
						contestantnum = contestantnum,
						rotnum = rotnum,

					)
				
			if not is_contest:
				for period_el in event_el.findall('periods/period'):
					periodnumber = period_el.find('period_number').text
					add_args(
						'periods',
						'replace',
						eventid = eventid,
						number = periodnumber,
						description = period_el.find('period_description').text,
						cutoff = period_el.find('periodcutoff_datetimeGMT').text,
					)
					add_args(
						'snapshots',
						'replace', 
						eventid = eventid,
						periodnumber = periodnumber,
						date = snapshot_sqlite_str,
						systemdate = systemdate_sqlite_str,
						status = period_el.find('period_status').text,
						upd = period_el.find('period_update').text,
						spreadmax = int(float(period_el.find('spread_maximum').text)),
						mlmax = int(float(period_el.find('moneyline_maximum').text)),
						totalmax = int(float(period_el.find('total_maximum').text)),
					)

					moneyline_el = coalesce(period_el, 'moneyline')
					if moneyline_el is not None:
						for vhd in ['home', 'visiting', 'draw']:
							if len(moneyline_el.findall('moneyline_' + vhd)) > 0:
								contestantnum, rotnum = period_el_to_contestantnum_rotnum(period_el, event_el, vhd)
								add_args(
									'odds',
									None,
									eventid = eventid,
									periodnumber = periodnumber,
									snapshotdate = snapshot_sqlite_str,
									type = 'm',
									threshold = 0,
									vhdou = VHDOU_VAL[vhd],
									price = dec_odds(
										coalesce(
											moneyline_el, 'moneyline_' + vhd, 1
										)
									),
									contestantnum = contestantnum,
									rotnum = rotnum,
								)

					spread_el = coalesce(period_el, 'spread')
					if spread_el is not None:
						for vhd in ['home', 'visiting']:
							if len(spread_el.findall('spread_' + vhd)) > 0:
								contestantnum, rotnum = period_el_to_contestantnum_rotnum(period_el, event_el, vhd)
								add_args(
									'odds',
									None,
									eventid = eventid,
									periodnumber = periodnumber,
									snapshotdate = snapshot_sqlite_str,
									type = 's',
									vhdou = VHDOU_VAL[vhd],
									threshold = float(spread_el.find('spread_' + vhd).text),
									price = dec_odds(
										coalesce(
											spread_el, 'spread_adjust_' + vhd, 1
										)
									),
									contestantnum = contestantnum,
									rotnum = rotnum,
								)

					total_el = coalesce(period_el, 'total')
					if total_el is not None:
						for ou in ['over', 'under']:
							contestantnum, rotnum = period_el_to_contestantnum_rotnum(period_el, event_el, ou)
							add_args(
								'odds',
								None,
								eventid = eventid,
								periodnumber = periodnumber,
								snapshotdate = snapshot_sqlite_str,
								type = 't',
								vhdou = VHDOU_VAL[ou],
								threshold = float(total_el.find('total_points').text),
								price = dec_odds(
									coalesce(
										total_el, ou + '_adjust', 1
									)
								),
								contestantnum = contestantnum,
								rotnum = rotnum,
							)

		with get_conn_mysql() as conn:
			LOGGER.info('Queue holds %s queries. Starting execution...', sum(len(al) for al in query_queue.itervalues()))
			for argslist in query_queue.itervalues():
				for args in argslist:
					try:
						if args[0] != AFTER_SQL1 and args[0] != AFTER_SQL2:
							conn.execute(*args)
						else:
							odds_ids = [r[0] for r in conn.execute(args[0], args[1]).fetchall()]
							for odds_id in odds_ids:
								odds_update_sql = '''
									update odds
									set {col} = 1
									where id = %s
								'''.format(col = 'opening' 
										if args[0] == AFTER_SQL1 
										else 'latest')
								conn.execute(odds_update_sql, [odds_id])
					except Exception as e:
						print 'Query that caused exception:'
						print args[0]
						print args[1]
						print
						raise
				conn.commit()
			LOGGER.info('Queue execution done.')
			if self.call_when_update_done is not None:
				self.call_when_update_done(systemdate)
class ConfigXmlWriter(RcbXmlReaderWriter):

	def __init__(self, createNew):

		Logutil.log('init ConfigXmlWriter', util.LOG_LEVEL_INFO)

		self.createNew = createNew

		if(createNew):
			configFile = os.path.join(util.getAddonInstallPath(), 'resources', 'database', 'config_template.xml')
		else:
			configFile = util.getConfigXmlPath()

		if(not os.path.isfile(configFile)):
			Logutil.log('File config.xml does not exist. Place a valid config file here: ' + str(configFile), util.LOG_LEVEL_ERROR)
		else:
			self.tree = ElementTree().parse(configFile)

	""" Functions for generating XML objects from objects """
	def getXmlAttributesForRomCollection(self, rc):
		return {'id': str(rc.id), 'name': rc.name}

	def getXmlElementsForRomCollection(self, rc):
		elements = []
		# String attributes
		for e in ['gameclient', 'emulatorCmd', 'emulatorParams', 'preCmd', 'postCmd',
				  'saveStatePath', 'saveStateParams']:
			try:
				el = Element(e, {})
				el.text = getattr(rc, e)
				elements.append(el)
			except AttributeError as exc:
				# Skip any errors
				pass

		# Non-string attributes
		for e in ['useBuiltinEmulator', 'useEmuSolo', 'usePopen', 'ignoreOnScan', 'allowUpdate', 'autoplayVideoMain',
				  'autoplayVideoInfo', 'useFoldernameAsGamename', 'maxFolderDepth', 'doNotExtractZipFiles',
				  'makeLocalCopy', 'diskPrefix']:
			try:
				el = Element(e, {})
				el.text = str(getattr(rc, e))
				elements.append(el)
			except AttributeError as exc:
				# Skip any errors
				pass

		for romPath in rc.romPaths:
			el = Element('romPath', {})
			el.text = romPath
			elements.append(el)

		return elements

	def getXmlAttributesForSite(self, site):
		attrs = {'name': site.name, 'path': site.path}
		return attrs

	def getXmlElementsForSite(self, site):
		""" Not needed """
		pass

	def writeRomCollections(self, romCollections, isEdit):

		Logutil.log('write Rom Collections', util.LOG_LEVEL_INFO)

		romCollectionsXml = self.tree.find('RomCollections')

		#HACK: remove all Rom Collections and create new
		if(isEdit):
			for romCollectionXml in romCollectionsXml.findall('RomCollection'):
				romCollectionsXml.remove(romCollectionXml)


		for romCollection in romCollections.values():

			Logutil.log('write Rom Collection: ' + str(romCollection.name), util.LOG_LEVEL_INFO)

			romCollectionXml = SubElement(romCollectionsXml, 'RomCollection', self.getXmlAttributesForRomCollection(romCollection))

			for subel in self.getXmlElementsForRomCollection(romCollection):
				romCollectionXml.append(subel)


			for mediaPath in romCollection.mediaPaths:

				success, message = self.searchConfigObjects('FileTypes/FileType', mediaPath.fileType.name, 'FileType')
				if(not success):
					return False, message

				SubElement(romCollectionXml, 'mediaPath', {'type': mediaPath.fileType.name}).text = mediaPath.path

			#image placing
			if(not self.createNew):
				#in case of an update we have to create new options
				if(romCollection.name == 'MAME' and not self.createNew):
					self.addFileTypesForMame()
					self.addImagePlacingForMame()

			if(romCollection.imagePlacingMain != None and romCollection.imagePlacingMain.name != ''):
				success, message = self.searchConfigObjects('ImagePlacing/fileTypeFor', romCollection.imagePlacingMain.name, 'ImagePlacing')
				if(not success):
					return False, message
				SubElement(romCollectionXml, 'imagePlacingMain').text = romCollection.imagePlacingMain.name
			else:
				SubElement(romCollectionXml, 'imagePlacingMain').text = 'gameinfobig'

			if(romCollection.imagePlacingInfo != None and romCollection.imagePlacingInfo.name != ''):
				success, message = self.searchConfigObjects('ImagePlacing/fileTypeFor', romCollection.imagePlacingInfo.name, 'ImagePlacing')
				if(not success):
					return False, message
				SubElement(romCollectionXml, 'imagePlacingInfo').text = romCollection.imagePlacingInfo.name
			else:
				SubElement(romCollectionXml, 'imagePlacingInfo').text = 'gameinfosmall'

			if (romCollection.scraperSites == None or len(romCollection.scraperSites) == 0):
				#use thegamesdb.net as default scraper in online scraping scenario
				SubElement(romCollectionXml, 'scraper', {'name': 'thegamesdb.net'})
			else:
				for scraperSite in romCollection.scraperSites:
					if(scraperSite == None):
						continue
					SubElement(romCollectionXml, 'scraper', {'name' : scraperSite.name, 'path' : scraperSite.path})

		success, message = self.writeFile()
		return success, message


	def writeMissingFilter(self, showHideOption, artworkOrGroup, artworkAndGroup, infoOrGroup, infoAndGroup):

		Logutil.log('write Missing Info Filter', util.LOG_LEVEL_INFO)

		missingFilterXml = self.tree.find('MissingFilter')

		#HACK: remove MissingFilter-element
		if(missingFilterXml != None):
			self.tree.remove(missingFilterXml)

		missingFilterXml = SubElement(self.tree, 'MissingFilter')
		SubElement(missingFilterXml, 'showHideOption').text = showHideOption

		if(len(artworkOrGroup) > 0 or len(artworkAndGroup) > 0):
			missingArtworkXml = SubElement(missingFilterXml, 'missingArtworkFilter')
			self.addMissingFilterItems(missingArtworkXml, artworkOrGroup, 'orGroup')
			self.addMissingFilterItems(missingArtworkXml, artworkAndGroup, 'andGroup')
		if(len(infoOrGroup) > 0 or len(infoAndGroup) > 0):
			missingInfoXml = SubElement(missingFilterXml, 'missingInfoFilter')
			self.addMissingFilterItems(missingInfoXml, infoOrGroup, 'orGroup')
			self.addMissingFilterItems(missingInfoXml, infoAndGroup, 'andGroup')

		success, message = self.writeFile()
		return success, message


	def addMissingFilterItems(self, missingXml, group, groupName):
		if(len(group) > 0):
			groupXml = SubElement(missingXml, groupName)
			for item in group:
				SubElement(groupXml, 'item').text = item


	def searchConfigObjects(self, xPath, nameToCompare, objectType):
		objects = self.tree.findall(xPath)
		objectFound = False
		for obj in objects:
			objectName = obj.attrib.get('name')
			if(objectName == nameToCompare):
				objectFound = True
				break

		if(not objectFound):
			return False, util.localize(32009) % (objectType, nameToCompare)

		return True, ''


	def removeRomCollection(self, RCName):

		Logutil.log('removeRomCollection', util.LOG_LEVEL_INFO)

		configFile = util.getConfigXmlPath()
		self.tree = ElementTree().parse(configFile)
		romCollectionsXml = self.tree.find('RomCollections')
		for romCollectionXml in romCollectionsXml.findall('RomCollection'):
			name = romCollectionXml.attrib.get('name')
			if(name == RCName):
				romCollectionsXml.remove(romCollectionXml)

		success, message = self.writeFile()
		return success, message

	def addFileTypesForMame(self):
		Logutil.log('addFileTypesForMame', util.LOG_LEVEL_INFO)

		fileTypesXml = self.tree.find('FileTypes')

		#check if the MAME FileTypes already exist
		cabinetExists = False
		marqueeExists = False
		actionExists = False
		titleExists = False
		highestId = 0
		fileTypeXml = fileTypesXml.findall('FileType')
		for fileType in fileTypeXml:
			name = fileType.attrib.get('name')
			if name == 'cabinet':
				cabinetExists = True
			elif name == 'marquee':
				marqueeExists = True
			elif name == 'action':
				actionExists = True
			elif name == 'title':
				titleExists = True

			id = fileType.attrib.get('id')
			if int(id) > highestId:
				highestId = int(id)

		if not cabinetExists:
			self.createFileType(fileTypesXml, str(highestId + 1), 'cabinet', 'image', 'game')
		if not marqueeExists:
			self.createFileType(fileTypesXml, str(highestId + 2), 'marquee', 'image', 'game')
		if not actionExists:
			self.createFileType(fileTypesXml, str(highestId + 3), 'action', 'image', 'game')
		if not titleExists:
			self.createFileType(fileTypesXml, str(highestId + 4), 'title', 'image', 'game')


	def createFileType(self, fileTypesXml, id, name, type, parent):
		fileType = SubElement(fileTypesXml, 'FileType', {'id' : str(id), 'name' : name})
		SubElement(fileType, 'type').text = type
		SubElement(fileType, 'parent').text = parent


	def addImagePlacingForMame(self):
		Logutil.log('addImagePlacingForMame', util.LOG_LEVEL_INFO)

		imagePlacingXml = self.tree.find('ImagePlacing')

		#check if the MAME ImagePlacing options already exist
		cabinetExists = False
		marqueeExists = False
		fileTypeForXml = imagePlacingXml.findall('fileTypeFor')
		for fileTypeFor in fileTypeForXml:
			name = fileTypeFor.attrib.get('name')
			if name == 'gameinfomamecabinet':
				cabinetExists = True
			elif name == 'gameinfomamemarquee':
				marqueeExists = True

		if not cabinetExists:
			fileTypeFor = SubElement(imagePlacingXml, 'fileTypeFor', {'name' : 'gameinfomamecabinet'})
			for imgtype in ['cabinet', 'boxfront', 'title']:
				SubElement(fileTypeFor, 'fileTypeForGameList').text = imgtype
				SubElement(fileTypeFor, 'fileTypeForGameListSelected').text = imgtype

			for imgtype in ['boxfront', 'title', 'action']:
				SubElement(fileTypeFor, 'fileTypeForMainViewBackground').text = imgtype

			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoUpperLeft').text = 'title'
			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoUpperRight').text = 'action'
			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoLower').text = 'marquee'

		if not marqueeExists:
			fileTypeFor = SubElement(imagePlacingXml, 'fileTypeFor', {'name' : 'gameinfomamemarquee'})
			for imgtype in ['marquee', 'boxfront', 'title']:
				SubElement(fileTypeFor, 'fileTypeForGameList').text = imgtype
				SubElement(fileTypeFor, 'fileTypeForGameListSelected').text = imgtype

			for imgtype in ['boxfront', 'title', 'action']:
				SubElement(fileTypeFor, 'fileTypeForMainViewBackground').text = imgtype

			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoLeft').text = 'cabinet'
			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoUpperRight').text = 'action'
			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoLowerRight').text = 'title'

	# FIXME TODO This function is only called within this class - raise exception rather than return tuple
	def writeFile(self):
		Logutil.log('writeFile', util.LOG_LEVEL_INFO)
		#write file
		try:
			configFile = util.getConfigXmlPath()

			self.indentXml(self.tree)
			treeToWrite = ElementTree(self.tree)
			treeToWrite.write(configFile)

			return True, ""

		except Exception, (exc):
			print("Error: Cannot write config.xml: " + str(exc))
			return False, util.localize(32008) + ": " + str(exc)
  def getMoviesFromRss(self, url, build_only=False):

    rss = urlfetch.Fetch(url)
          
    tree = ElementTree(fromstring(rss.content))
    
    img_re = re.compile('<img src="([a-zA-Z0-9/:._\-]+)" ')
    content_re = re.compile('&#0151; \n(.+)')

    release_re = re.compile('Release:</strong> ([a-zA-Z]{3,4})\. ([0-9]{1,2}), ([0-9]{4})')

    new_movies = []
    update_movies = []

    for item in tree.findall('.//item'):
      title = item.find('title').text.strip()

      is_new = True

      movie = getMovie(title)

      if (not movie):
        movie = Movie()
      else:
        is_new = False
        if (build_only):
          continue

      description = item.find('description').text.strip()
      summary = (content_re.search(description)).groups()[0]

      release_match = (release_re.search(description))

      imageMatch = img_re.search(description)

      movie.title = title
      movie.full_content = description
      movie.summary = db.Text(summary)
      
      if (release_match):
        release_month = MONTHS.index(release_match.groups()[0].upper()) + 1
        release_date = int(release_match.groups()[1])
        release_year = int(release_match.groups()[2])

        movie.release_date = datetime.datetime(release_year, release_month, release_date)

      if (imageMatch):          
        picture_url = imageMatch.groups()[0]
        movie.picture = db.Blob(urlfetch.Fetch(picture_url).content)
      
      movie.put()
      
      if (is_new):
        new_movies.append(title)
      else:
        update_movies.append(title)

    movies = {}
    movies['new'] = new_movies
    movies['update'] = update_movies

    return movies
Example #47
0
        self.name = ''
        self.attr = ''
        self.seq = ''
        self.peptide = []
class Peptide:
    def __init__(self):
        self.seq = ''
        self.attr = ''
        self.modification = []


inF = sys.argv[1]
ouFile = open(sys.argv[1]+'.txt','w')
tree = ElementTree()
tree.parse(inF)
group=tree.findall('group')
for item in group :
    if item.get('id') :
        Spec = Spectrum()
        s = item.findall('group/note')
        if len(s) == 1:
            Spec.name = s[0].text.strip()
        else:
            print('spec error')
        #s = item.getchildren()[-1].getchildren()[0].get('label')
        #if s.find('.spectrum') != -1:
        #    Spec.name=s
        #else:
        #    print('spec error')

        pro=item.findall('protein')
Example #48
0
        self.seq = ""
        self.peptide = []


class Peptide:
    def __init__(self):
        self.seq = ""
        self.attr = ""
        self.modification = []


inF = sys.argv[1]
ouFile = open(sys.argv[1] + ".txt", "w")
tree = ElementTree()
tree.parse(inF)
group = tree.findall("group")
for item in group:
    if item.get("id"):
        Spec = Spectrum()
        # s = item.findall('group/note')
        # if len(s) == 1:
        #    Spec.name = s[0].text.strip()
        # else:
        #    print('spec error')
        s = item.getchildren()[-1].getchildren()[0].get("label")
        if s.find(".spectrum") != -1:
            Spec.name = s
        else:
            print("spec error")

        pro = item.findall("protein")
Example #49
0
def setMaterials(VPath,outVPath): 
    global ROOTDIR  
    global LOG
    global materialChange
    vf=ElementTree(file=VPath)
    v=ElementTree(file=VPath).getroot()
    
    #print dump(v)
    #shaderPath=v.find("renderSet/geometry/primitiveGroup/material/fx").text
    mateID=v.findall("renderSet/geometry/primitiveGroup")


    miPaths=[]
    miRootPath="e:/mf_pangu/tw2/res/mi/"

    mapTypes=["\tdiffuseTexMap","\tnormalTexMap","\tspecularTexMap"]
    DPath=""
    #-----------only support defult export visual

    #----get mi dirs
    for p in os.listdir(miRootPath):
        miPaths.append((p+"/"))
    for p in os.listdir(miRootPath+"common/"):
        miPaths.append(("common/"+p+"/"))
    
    for mid in mateID:
        #shaderType
        
        for m in mid:
            LOG+=("\n start............:   ")
            changeMapOn=True
            #---material 
            for mmap in m.findall("property"):
                try:
                    m.find("fx").text
                except:
                    LOG+=("\n   can't find material type :   ")
                    changeMapOn=False
                else:
                    if m.find("fx").text.replace("\t","")=="shaders/std_effects/lightonly.fx":
                        changeMapOn=True
                    else:
                        changeMapOn=False
                try:
                    mmap.find("Texture").text
                except:
                    #print mmap.text
                    #print m.text
                    if changeMapOn==True:
                          LOG+=("\n     can't find diffuse path :   ")
                    changeMapOn=False
                else:
                    DPath=mmap.find("Texture").text.replace("\t","")
                    if  os.path.isfile((ROOTDIR+DPath).replace("\t","")) and changeMapOn==True:
                        LOG+=("\n   difusseMap path :   " +DPath)
                        m.find("fx").text="\tshaders/core/scene_dns.fx\t"
                        
                        try:
                            m.find("collisionFlags").text
                        except:
                            SubElement(m,"collisionFlags").text=" 0 "
                        
                        try:
                            m.find("materialKind").text
                        except:
                            SubElement(m,"materialKind").text=" 0 "
                        mmap.text=mapTypes[0]
                        materialChange=True
            if changeMapOn==True:
                for mmap in m.findall("property"):
                    if len(m.findall("property"))<3:
                        LOG+=("\n   start change material......")
                        miOn=False
                        # check mi 
                        #----set mi
                        try:
                            a=m.find("mi").text
                            #b=m.find("mixxx").text
                            #if a=="" or a=="\r":
                                #miOn=True
                            #changeMapOn=False
                        except:
                            miPath=""
                            name=os.path.splitext(os.path.split(DPath)[1])[0]
                            for p in miPaths:
                                #print (miRootPath+p+"/"+name+".mi")
                                if os.path.isfile((miRootPath+p+"/"+name+".mi").replace("\t","")):
                                    miPath="mi/"+p+name+".mi"  
                                    SubElement(m,"mi").text=miPath
                                    LOG+="\n        set material son: "+miPath
                                    miOn=True
                        if miOn==False:
                            LOG+="\n        can't find the material son  so starting set map:"
                            #----set normal map and specular map
                            maps=getDNS(DPath)
                            #LOG+="\n找到的法线高光"+str(maps)
                            i=0
                            LOG+="\n            set map...."
                            for texP in  maps:
                                if i!=0:
                                    item= Element("property")
                                    item.text=mapTypes[i]
                                    texN=Element("Texture")
                                    if os.path.isfile((ROOTDIR+texP).replace("\t","")):
                                        LOG+="\n                set map ....."+mapTypes[i] + " : "+texP
                                        texN.text=texP
                                        item.append(texN)
                                        m.append(item)
                                    else: 
                                        LOG+="\n                 set map ....."+mapTypes[i] + " : can't find"
                                        texN.text="   "
                                        item.append(texN)
                                        m.append(item)
                                i+=1

                            #---set alhpa
                            name=os.path.splitext(os.path.split(DPath)[1])[0]
                            nameParts=string.split(name,"_")
                            if len(nameParts)>3:
                                if nameParts[len(nameParts)-2]=="a":
                                    LOG+="\n                    set Alpha True"
                                    try:
                                        m.find("\tdoubleSided").text
                                        m.find("\tdoubleSided").find("Int").text
                                    except:
                                        item= Element("property")
                                        item.text=("\tdoubleSided")
                                        itemA=Element("Int")
                                        itemA.text=("\t1\t")
                                        item.append(itemA)
                                        m.append(item)
                                    else:
                                        m.find("\tdoubleSided").find("Int").text="\t1\t"
    outV = ElementTree()
    #print materialChange
    #if materialChange==True:
    outV._setroot(indent(v))
    LOG+="\n        save to a temp "
    print LOG
    #raw_input("LOG")
    outV.write(outVPath,"utf-8")
Example #50
0
class BattleFeed(eRepublikXMLProcessor):
    " eRepublik battle feed class "
    url = 'http://api.erepublik.com/v1/feeds/battle_logs/%s/%s'

    def __init__(self, arg=None):
        super(BattleFeed, self).__init__()

        self.id = arg
        self.curPage = -1
        self.maxPages = -1
        self.attacker = None
        self.attackerId = None
        self.defender = None
        self.defenderId = None
        self.raw_battles = None
        self.battles = None
        self.loaded = False
        self.processed = False

    def load(self):
        if self.id == None:
            raise Exception('id=None and BattleFeed.load was called')
        if self.loaded or self.processed:
            return
        while self._loadNextPage():
            Logger.log('Loaded page ' + str(self.curPage))
        self.loaded = True

    def process(self):
        self.load()
        if self.processed:
            return
        battles = Element('battles')
        battles.attrib['id'] = str(self.id)
        battles.attrib['attacker'] = self.attacker
        battles.attrib['attacker-id'] = str(self.attackerId)
        battles.attrib['defender'] = self.defender
        battles.attrib['defender-id'] = str(self.defenderId)
        for b in self.raw_battles.findall('battle'):
            battle = Element('battle')
            battle.attrib['time'] = b.find('time').text
            battle.attrib['damage'] = b.find('damage').text
            battle.attrib['citizen'] = b.find('citizen').text
            battle.attrib['citizen-id'] = b.find('citizen-id').text
            battles.append(battle)
        self.battles = ElementTree(battles)
        del self.raw_battles # free some memory
        self.processed = True

    def _loadNextPage(self):
        if not self._getNextPage():
            return False
        
        xml = ''.join(urlopen(BattleFeed.url % (str(self.id), str(self.curPage))).readlines())
        bl = XML(xml)
        battleInfo = bl.find('battle-info')
        self.maxPages = self.findInt(battleInfo, 'max-pages')
        if not self.attacker:
            self.attacker = self.findString(battleInfo, 'attacker')
            self.attackerId = self.findInt(battleInfo, 'attacker-id')
            self.defender = self.findString(battleInfo, 'defender')
            self.defenderId = self.findInt(battleInfo, 'defender-id')

        if not self.raw_battles:
            self.raw_battles = ElementTree(bl.find('battles'))
        else:
            for b in bl.findall('battles/battle'):
                self.raw_battles.getroot().append(b)
        return True

    def _getNextPage(self):
        """ Sets curPage to next available page and returns true or
            returns false if there are no more pages available.
            """
        if self.curPage == -1:
            self.curPage = 0
            return True
        if self.maxPages > self.curPage:
            self.curPage = self.curPage + 1
            return True
        return False

    def _findBattlesByCitizenId(self, citizenId):
        """
        returns list of damages done by citizen(by citizenId)
        """
        self.process()

        damage = []
        for b in self.battles.findall('battle'):
            if int(b.attrib['citizen-id']) == citizenId:
                # Found
                damage.append(int(b.attrib['damage']))
        return damage

    def findBattles(self, arg):
        self.process()

        if type(arg) == int:
            return self._findBattlesByCitizenId(arg)
        elif type(arg) == Citizen or type(arg) == CitizenWithHistory:
            return self._findBattlesByCitizenId(arg.id)
        elif type(arg) == Army:
            damage = dict()
            for m in arg.members:
                damage[m.name] = self._findBattlesByCitizenId(m.id)
            return damage

    def __str__(self):
        self.process()

        f = StringIO()
        self.battles.write(f)
        f.reset()
        return ''.join(f.readlines())

    def __getstate__(self):
        odict = dict()
        odict['battles'] = str(self)
        return odict

    def __setstate__(self, dict):
        self.battles = ElementTree(XML(dict['battles']))
        self.processed = True
        self.loaded = True
        root = self.battles.getroot()
        self.id = root.attrib['id']
        self.attacker = root.attrib['attacker']
        self.attackerId = int(root.attrib['attacker-id'])
        self.defender = root.attrib['defender']
        self.defenderId = int(root.attrib['defender-id'])
Example #51
0
import csv
from xml.etree import ElementTree

def add_csv_result(FileName,CaseName,CaseResult):
	f=csv.writer(file(FileName,'ab'))
	f.writerow([CaseName,CaseResult])
	#f.close()

//生成结果目录
case = []
start_time = time.strtime('%Y%m%d_%H%M%S',time.localtime(time.time()))
result_dir = "./Result"+start_time

//解析xml中的testcase
tcRoot = ElementTree("./testCases.xml")
cn = tcRoot.findall("case")
for i in cn:
	cases.append(i.text)
caseList = list(set(cases))
caseList.sort(key=cases.index)
title = ['CaseNames','Verify_result']
csv_results = open(result_dir+"/"+result_dir+".csv","wb")
f_csv = csv.writer(csv_results)
f_csv.writerow(title)
#f_csv.close()

for caseName in caseList:
	case_result_dir = result_dir+"/"+caseName
	os.popen("mkdir -p %s"%(case_result_dir))
	try:
		os.sys.path.append("TestCases/MRD8/")
Example #52
0
def main():
    usage = "usage: %prog [options] dumpFile [URL]"
    parser = OptionParser(usage, version="%prog 0.1")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
    parser.add_option("-p", "--password", dest="password", help="HTTP Digest password", default="123456")
    url = "http://www.fluffyhome.com"
    url = "http://localhost:8088"

    (options, args) = parser.parse_args()
    if len(args) > 2:
        parser.error("incorrect number of arguments")
    if len(args) < 1:
        parser.error("incorrect number of arguments")

    fileName = args[0]
    if len(args) == 2:
        url = args[1]

    print "Uploading %s to %s" % (fileName, url)

    tree = ElementTree(file=fileName)
    root = tree.getroot()
    assert root is not None

    user = root.find("user")
    assert user is not None

    userName = user.attrib.get("userName")

    # create the user
    u = url + "/admin/user/" + userName + "/"
    post(None, u, "application/x-www-form-urlencoded", "admin", "none")

    # update the user record
    email = user.attrib.get("email")
    attributes = user.items()
    data = urlencode(attributes)
    u = url + "/user/" + userName + "/prefs/"
    post(data, u, "application/x-www-form-urlencoded", userName, options.password)

    # create the sensors
    sensors = tree.findall("sensor")
    for sensor in sensors:
        sensorName = sensor.attrib.get("sensorName")

        attributes = sensor.items()
        data = urlencode(attributes)
        u = url + "/sensor/" + userName + "/" + sensorName + "/create/"

        post(data, u, "application/x-www-form-urlencoded", userName, options.password)

    # create the meassurement data
    count = 0
    data = ""
    measurements = tree.findall("measurement")
    for measurement in measurements:
        sensorName = str(measurement.attrib.get("sensor"))
        time = int(measurement.attrib.get("time"))

        # print sensorName,time,value,integral

        if count == 0:
            data = "[\n"
        else:
            data += ", \n"

        data += '  { "n":"%s", "t":%d' % (sensorName, time)

        value = measurement.attrib.get("value")
        if value is not None:
            data += ', "v":%f' % float(value)

        integral = measurement.attrib.get("integral")
        if integral is not None:
            data += ', "s":%f' % float(integral)

        joules = measurement.attrib.get("joules")
        if joules is not None:
            data += ', "j":%f' % float(joules)

        patchLevel = measurement.attrib.get("patchLevel")
        if patchLevel is None:
            patchLevel = 0
        data += ', "pl":%d' % int(patchLevel)

        data += " }"

        count = count + 1

        if count > 20:
            data += "\n]\n"
            u = url + "/sensorValues/"
            post(data, u, "application/json", userName, options.password)
            count = 0
            data = ""
    if count > 0:
        data += "\n]\n"
        u = url + "/sensorValues/"
        post(data, u, "application/json", userName, options.password)
        count = 0
        data = ""
class ConfigXmlWriter:
	
	def __init__(self, createNew):
		
		Logutil.log('init ConfigXmlWriter', util.LOG_LEVEL_INFO)
		
		self.createNew = createNew
		
		if(createNew):
			configFile = os.path.join(util.getAddonInstallPath(), 'resources', 'database', 'config_template.xml')
		else:
			configFile = util.getConfigXmlPath()
		
		if(not os.path.isfile(configFile)):
			Logutil.log('File config.xml does not exist. Place a valid config file here: ' +str(configFile), util.LOG_LEVEL_ERROR)
			return False, util.localize(32003)
		
		self.tree = ElementTree().parse(configFile)
	
	
	def writeRomCollections(self, romCollections, isEdit):
				
		Logutil.log('write Rom Collections', util.LOG_LEVEL_INFO)
				
		romCollectionsXml = self.tree.find('RomCollections')
		
		#HACK: remove all Rom Collections and create new
		if(isEdit):
			for romCollectionXml in romCollectionsXml.findall('RomCollection'):				
				romCollectionsXml.remove(romCollectionXml)
				
		
		for romCollection in romCollections.values():
			
			Logutil.log('write Rom Collection: ' +str(romCollection.name), util.LOG_LEVEL_INFO)
			
			romCollectionXml = SubElement(romCollectionsXml, 'RomCollection', {'id' : str(romCollection.id), 'name' : romCollection.name})
			SubElement(romCollectionXml, 'useBuiltinEmulator').text = str(romCollection.useBuiltinEmulator)
			SubElement(romCollectionXml, 'gameclient').text = romCollection.gameclient
			SubElement(romCollectionXml, 'emulatorCmd').text = romCollection.emulatorCmd
			SubElement(romCollectionXml, 'emulatorParams').text = romCollection.emulatorParams
			
			for romPath in romCollection.romPaths:
				SubElement(romCollectionXml, 'romPath').text = romPath
							
			SubElement(romCollectionXml, 'saveStatePath').text = romCollection.saveStatePath
			SubElement(romCollectionXml, 'saveStateParams').text = romCollection.saveStateParams
				
			for mediaPath in romCollection.mediaPaths:
				
				success, message = self.searchConfigObjects('FileTypes/FileType', mediaPath.fileType.name, 'FileType')
				if(not success):
					return False, message								
												
				SubElement(romCollectionXml, 'mediaPath', {'type' : mediaPath.fileType.name}).text = mediaPath.path
				
			SubElement(romCollectionXml, 'preCmd').text = romCollection.preCmd
			SubElement(romCollectionXml, 'postCmd').text = romCollection.postCmd
			SubElement(romCollectionXml, 'useEmuSolo').text = str(romCollection.useEmuSolo)
			SubElement(romCollectionXml, 'usePopen').text = str(romCollection.usePopen)
			SubElement(romCollectionXml, 'ignoreOnScan').text = str(romCollection.ignoreOnScan)
			SubElement(romCollectionXml, 'allowUpdate').text = str(romCollection.allowUpdate)
			SubElement(romCollectionXml, 'autoplayVideoMain').text = str(romCollection.autoplayVideoMain)
			SubElement(romCollectionXml, 'autoplayVideoInfo').text = str(romCollection.autoplayVideoInfo)
			SubElement(romCollectionXml, 'useFoldernameAsGamename').text = str(romCollection.useFoldernameAsGamename)
			SubElement(romCollectionXml, 'maxFolderDepth').text = str(romCollection.maxFolderDepth)
			SubElement(romCollectionXml, 'doNotExtractZipFiles').text = str(romCollection.doNotExtractZipFiles)
			SubElement(romCollectionXml, 'diskPrefix').text = str(romCollection.diskPrefix)
			
			if (os.environ.get( "OS", "xbox" ) == "xbox"):
				SubElement(romCollectionXml, 'xboxCreateShortcut').text = str(romCollection.xboxCreateShortcut)
				SubElement(romCollectionXml, 'xboxCreateShortcutAddRomfile').text = str(romCollection.xboxCreateShortcutAddRomfile)
				SubElement(romCollectionXml, 'xboxCreateShortcutUseShortGamename').text = str(romCollection.xboxCreateShortcutUseShortGamename)
				
			#image placing
			if(not self.createNew):
				#in case of an update we have to create new options
				if(romCollection.name == 'MAME' and not self.createNew):
					self.addFileTypesForMame()
					self.addImagePlacingForMame()
					
			if(romCollection.imagePlacingMain != None and romCollection.imagePlacingMain.name != ''):
				success, message = self.searchConfigObjects('ImagePlacing/fileTypeFor', romCollection.imagePlacingMain.name, 'ImagePlacing')
				if(not success):
					return False, message
				SubElement(romCollectionXml, 'imagePlacingMain').text = romCollection.imagePlacingMain.name 
			else:
				SubElement(romCollectionXml, 'imagePlacingMain').text = 'gameinfobig'
				
			if(romCollection.imagePlacingInfo != None and romCollection.imagePlacingInfo.name != ''):
				success, message = self.searchConfigObjects('ImagePlacing/fileTypeFor', romCollection.imagePlacingInfo.name, 'ImagePlacing')
				if(not success):
					return False, message
				SubElement(romCollectionXml, 'imagePlacingInfo').text = romCollection.imagePlacingInfo.name 
			else:
				SubElement(romCollectionXml, 'imagePlacingInfo').text = 'gameinfosmall'
			
			if(romCollection.scraperSites == None or len(romCollection.scraperSites) == 0):
				SubElement(romCollectionXml, 'scraper', {'name' : 'thegamesdb.net', 'replaceKeyString' : '', 'replaceValueString' : ''})
				SubElement(romCollectionXml, 'scraper', {'name' : 'archive.vg', 'replaceKeyString' : '', 'replaceValueString' : ''})
				SubElement(romCollectionXml, 'scraper', {'name' : 'mobygames.com', 'replaceKeyString' : '', 'replaceValueString' : ''})
			else:
				for scraperSite in romCollection.scraperSites:
				
					if(scraperSite == None):
						continue
						
					#HACK: use replaceKey and -Value only from first scraper
					firstScraper = scraperSite.scrapers[0]
					SubElement(romCollectionXml, 'scraper', {'name' : scraperSite.name, 'replaceKeyString' : firstScraper.replaceKeyString, 'replaceValueString' : firstScraper.replaceValueString})
					
					#create Scraper element
					scrapersXml = self.tree.find('Scrapers')
					
					#check if the current scraper already exists
					siteExists = False
					sitesXml = scrapersXml.findall('Site')
					for site in sitesXml:
						name = site.attrib.get('name')
						if name == scraperSite.name:
							siteExists = True
							break
						
					if not siteExists:
						#HACK: this only covers the first scraper (for offline scrapers)
						site = SubElement(scrapersXml, 'Site', 
							{ 
							'name' : scraperSite.name,
							'descFilePerGame' : str(scraperSite.descFilePerGame),
							'searchGameByCRC' : str(scraperSite.searchGameByCRC),
							'useFoldernameAsCRC' : str(scraperSite.useFoldernameAsCRC),
							'useFilenameAsCRC' : str(scraperSite.useFilenameAsCRC)
							})
																		
						scraper = scraperSite.scrapers[0]
						
						SubElement(site, 'Scraper', 
							{ 
							'parseInstruction' : scraper.parseInstruction,
							'source' : scraper.source,
							'encoding' : scraper.encoding
							})
				
		success, message = self.writeFile()
		return success, message
	
	
	def writeScrapers(self, scrapers):
		
		Logutil.log('write scraper sites', util.LOG_LEVEL_INFO)
				
		scraperSitesXml = self.tree.find('Scrapers')
				
		#HACK: remove all scrapers and create new
		for scraperSiteXml in scraperSitesXml.findall('Site'):				
			scraperSitesXml.remove(scraperSiteXml)
			
		for scraperSite in scrapers.values():
			
			Logutil.log('write scraper site: ' +str(scraperSite.name), util.LOG_LEVEL_INFO)
			
			#Don't write None-Scraper
			if(scraperSite.name == util.localize(32854)):
				Logutil.log('None scraper will be skipped', util.LOG_LEVEL_INFO)
				continue
			
			scraperSiteXml = SubElement(scraperSitesXml, 'Site', 
					{ 
					'name' : scraperSite.name,
					'descFilePerGame' : str(scraperSite.descFilePerGame),
					'searchGameByCRC' : str(scraperSite.searchGameByCRC),
					'useFoldernameAsCRC' : str(scraperSite.useFoldernameAsCRC),
					'useFilenameAsCRC' : str(scraperSite.useFilenameAsCRC)
					})
			
			for scraper in scraperSite.scrapers:
				
				#check if we can use a relative path to parseInstructions
				rcbScraperPath = os.path.join(util.RCBHOME, 'resources', 'scraper')
				pathParts = os.path.split(scraper.parseInstruction)
				if(pathParts[0].upper() == rcbScraperPath.upper()):
					scraper.parseInstruction = pathParts[1]
				
				scraperXml = SubElement(scraperSiteXml, 'Scraper', 
					{ 
					'parseInstruction' : scraper.parseInstruction,
					'source' : scraper.source,
					'encoding' : scraper.encoding,
					'returnUrl' : str(scraper.returnUrl)
					})
		
		success, message = self.writeFile()
		return success, message
	
	
	def writeMissingFilter(self, showHideOption, artworkOrGroup, artworkAndGroup, infoOrGroup, infoAndGroup):
		
		Logutil.log('write Missing Info Filter', util.LOG_LEVEL_INFO)
		
		missingFilterXml = self.tree.find('MissingFilter')
		
		#HACK: remove MissingFilter-element
		if(missingFilterXml != None):				
			self.tree.remove(missingFilterXml)
		
		missingFilterXml = SubElement(self.tree, 'MissingFilter')
		SubElement(missingFilterXml, 'showHideOption').text = showHideOption
		
		if(len(artworkOrGroup) > 0 or len(artworkAndGroup) > 0):
			missingArtworkXml = SubElement(missingFilterXml, 'missingArtworkFilter')
			self.addMissingFilterItems(missingArtworkXml, artworkOrGroup, 'orGroup')
			self.addMissingFilterItems(missingArtworkXml, artworkAndGroup, 'andGroup')
		if(len(infoOrGroup) > 0 or len(infoAndGroup) > 0):
			missingInfoXml = SubElement(missingFilterXml, 'missingInfoFilter')
			self.addMissingFilterItems(missingInfoXml, infoOrGroup, 'orGroup')
			self.addMissingFilterItems(missingInfoXml, infoAndGroup, 'andGroup')
				
		success, message = self.writeFile()
		return success, message
		
		
	def addMissingFilterItems(self, missingXml, group, groupName):		
		if(len(group) > 0):
			groupXml = SubElement(missingXml, groupName)
			for item in group:
				SubElement(groupXml, 'item').text = item
		
	
	def searchConfigObjects(self, xPath, nameToCompare, objectType):		
		objects = self.tree.findall(xPath)
		objectFound = False
		for obj in objects:
			objectName = obj.attrib.get('name')
			if(objectName == nameToCompare):
				objectFound = True
				break
		
		if(not objectFound):
			return False,  util.localize(32009) %(objectType, nameToCompare)
		
		return True, ''
	
		
	def removeRomCollection(self, RCName):
		
		Logutil.log('removeRomCollection', util.LOG_LEVEL_INFO)
		
		configFile = util.getConfigXmlPath()
		self.tree = ElementTree().parse(configFile)
		romCollectionsXml = self.tree.find('RomCollections')
		for romCollectionXml in romCollectionsXml.findall('RomCollection'):
			name = romCollectionXml.attrib.get('name')
			if(name == RCName):
				romCollectionsXml.remove(romCollectionXml)	
				
		success, message = self.writeFile()
		return success, message
		
	def addFileTypesForMame(self):
		Logutil.log('addFileTypesForMame', util.LOG_LEVEL_INFO)
		
		fileTypesXml = self.tree.find('FileTypes')
				
		#check if the MAME FileTypes already exist
		cabinetExists = False
		marqueeExists = False
		actionExists = False
		titleExists = False
		highestId = 0
		fileTypeXml = fileTypesXml.findall('FileType')
		for fileType in fileTypeXml:
			name = fileType.attrib.get('name')
			if name == 'cabinet':
				cabinetExists = True
			elif name == 'marquee':
				marqueeExists = True
			elif name == 'action':
				actionExists = True
			elif name == 'title':
				titleExists = True
			
			id = fileType.attrib.get('id')
			if int(id) > highestId:
				highestId = int(id)
		
		if not cabinetExists:
			self.createFileType(fileTypesXml, str(highestId +1), 'cabinet', 'image', 'game')
		if not marqueeExists:
			self.createFileType(fileTypesXml, str(highestId +2), 'marquee', 'image', 'game')			
		if not actionExists:
			self.createFileType(fileTypesXml, str(highestId +3), 'action', 'image', 'game')
		if not titleExists:
			self.createFileType(fileTypesXml, str(highestId +4), 'title', 'image', 'game')			
			
		
	def createFileType(self, fileTypesXml, id, name, type, parent):
		fileType = SubElement(fileTypesXml, 'FileType', {'id' : str(id), 'name' : name})
		SubElement(fileType, 'type').text = type
		SubElement(fileType, 'parent').text = parent
		
		
	def addImagePlacingForMame(self):
		Logutil.log('addImagePlacingForMame', util.LOG_LEVEL_INFO)
		
		imagePlacingXml = self.tree.find('ImagePlacing')
		
		#check if the MAME ImagePlacing options already exist
		cabinetExists = False
		marqueeExists = False		
		fileTypeForXml = imagePlacingXml.findall('fileTypeFor')
		for fileTypeFor in fileTypeForXml:
			name = fileTypeFor.attrib.get('name')
			if name == 'gameinfomamecabinet':
				cabinetExists = True
			elif name == 'gameinfomamemarquee':
				marqueeExists = True
				
		if not cabinetExists:
			fileTypeFor = SubElement(imagePlacingXml, 'fileTypeFor', {'name' : 'gameinfomamecabinet'})
			SubElement(fileTypeFor, 'fileTypeForGameList').text = 'cabinet'
			SubElement(fileTypeFor, 'fileTypeForGameList').text = 'boxfront'
			SubElement(fileTypeFor, 'fileTypeForGameList').text = 'title'
			SubElement(fileTypeFor, 'fileTypeForGameListSelected').text = 'cabinet'
			SubElement(fileTypeFor, 'fileTypeForGameListSelected').text = 'boxfront'
			SubElement(fileTypeFor, 'fileTypeForGameListSelected').text = 'title'			
			SubElement(fileTypeFor, 'fileTypeForMainViewBackground').text = 'boxfront'
			SubElement(fileTypeFor, 'fileTypeForMainViewBackground').text = 'title'
			SubElement(fileTypeFor, 'fileTypeForMainViewBackground').text = 'action'
			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoUpperLeft').text = 'title'
			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoUpperRight').text = 'action'
			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoLower').text = 'marquee'
			
		if not marqueeExists:
			fileTypeFor = SubElement(imagePlacingXml, 'fileTypeFor', {'name' : 'gameinfomamemarquee'})
			SubElement(fileTypeFor, 'fileTypeForGameList').text = 'marquee'
			SubElement(fileTypeFor, 'fileTypeForGameList').text = 'boxfront'
			SubElement(fileTypeFor, 'fileTypeForGameList').text = 'title'
			SubElement(fileTypeFor, 'fileTypeForGameListSelected').text = 'marquee'
			SubElement(fileTypeFor, 'fileTypeForGameListSelected').text = 'boxfront'
			SubElement(fileTypeFor, 'fileTypeForGameListSelected').text = 'title'			
			SubElement(fileTypeFor, 'fileTypeForMainViewBackground').text = 'boxfront'
			SubElement(fileTypeFor, 'fileTypeForMainViewBackground').text = 'title'
			SubElement(fileTypeFor, 'fileTypeForMainViewBackground').text = 'action'
			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoLeft').text = 'cabinet'
			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoUpperRight').text = 'action'
			SubElement(fileTypeFor, 'fileTypeForMainViewGameInfoLowerRight').text = 'title'
		
						
	def writeFile(self):
		Logutil.log('writeFile', util.LOG_LEVEL_INFO)
		#write file
		try:
			configFile = util.getConfigXmlPath()
			
			util.indentXml(self.tree)
			treeToWrite = ElementTree(self.tree)			
			treeToWrite.write(configFile)
			
			return True, ""
			
		except Exception, (exc):
			print("Error: Cannot write config.xml: " +str(exc))
			return False, util.localize(32008) +": " +str(exc)