Example #1
0
 def writeXML(self,xmlpath="template.xml"):
     tree=ElementTree()
     root=Element("ts")
     for t in self.__torrentStores:
         root.append(t.toElement())
     tree._setroot(root)
     tree.write(xmlpath)
Example #2
0
def save_settings_data(mainTree,data):
    for s in data:
        if mainTree.find(s)!=None:
            if s=="ICON_PATH" or s=="SAVE_SCREEN_MAP_PATH" or s=="SET_FILE_PATH" or s=="GRAP_PF_NAME"  or s=="GRAP_PF_NAME" :
                mainTree.find(s).text=str(data[s])
            else:
                ()#mainTree.find(s).text=str(data[s])#str(data[s])
                print "xxx"
        else:
            item=Element(s)
            if s=="ICON_PATH" or  s=="SAVE_SCREEN_MAP_PATH" or s=="SET_FILE_PATH" or s=="GRAP_PF_NAME"  or s=="GRAP_PF_NAME" :
                item.text=str(data[s])#"\""+data[s].replace("\\","/")+"\""
            else:
                item.text=str(data[s])
            mainTree.append(item)

        #string
        '''
        #int
        elif s=="LANGUAGE_TYPE" or s=="IMAGE_MAX_SIZE" or s== "IMAGE_MIN_SIZE":
            data_str=str(data[s])
        #float
        elif s=="ADJUST_SCALE_SPEED" or s=="SCALE_SPEED" :
            data_str=str(data[s])
        #array
        elif s=="IMAGE_SCALE_MIN_MAX" :
            for s in data[s]:
                data_str=+str(s)+" "
        '''
        #item.text=data_str
        #mainTree.append(item)
        settings=ElementTree("root")
        settings._setroot(indent(mainTree))
        settings.write(SET_FILE_PATH,"utf-8")
        print "\n save_settings_data: "
Example #3
0
    def makeXML(self, work_directory):
        # </Artifact>
        TheArtifact = Element( 'Artifact')
        TheArtifact.attrib['xmlns']="http://geni.net/schema"
        TheArtifact.attrib['xmlns:xsi']="http://www.w3.org/2001/XMLSchema-instance"
        TheArtifact.attrib['xsi:schemaLocation']="http://geni.net/schema GENIObject.xsd"

        # <Artifact><Type/>
        Type = SubElement( TheArtifact, 'Type')

        # <Artifact><Type><Primary/>
        Primary = SubElement( Type, 'Primary')
        Primary.text = self.art_type_prime

        # <Artifact><Interpretation/>
        Interpretation = SubElement( TheArtifact, 'Interpretation')
    
        # <Artifact><Interpretation><Interpretation_read_me_text/>
        Read_me_text = SubElement( Interpretation, 'Interpretation_read_me_text')
        Read_me_text.text = self.interpretation_read_me

        #To print to file
        Test = ElementTree()
        Test._setroot(TheArtifact)
        root = Test.getroot()
        self.indent(root)
        Test.write(work_directory+'/artifact.xml')
Example #4
0
    def GeneXml(self):        
        Simulators = Element('Simulators');
        #ipList = ['10.36.65.55','10.36.65.25','10.36.65.24','10.36.65.89','10.36.65.15','10.36.65.91','10.36.65.60','10.36.65.112'];

        ipList = ['10.36.65.89'];
        
        simulatorId = 0;
        for ip in ipList:
            port = 37777;        
            for i in range(0,40):            
                simulator = Element('Simulator', {'id': str(simulatorId)});
                SubElement(simulator, 'Name',{'value': ip});
                SubElement(simulator, 'DeviceIP',{'value': ip});
                SubElement(simulator, 'DevicePort',{'value': str(port)});
                SubElement(simulator, 'UserName',{'value': ''});
                SubElement(simulator, 'Password',{'value': ''});
            
                ChannelList = Element('ChannelList');
                channelId = 0;
                for i in range(0,16):                
                    Channel = Element('Channel', {'id': str(channelId),'name': 'XXXtongdaoXXX'+str(channelId)});
                    ChannelList.append(Channel);
                    channelId = channelId+1;

                simulator.append(ChannelList);
                Simulators.append(simulator);
                port = port+1;
                simulatorId = simulatorId+1;
        
        simulateXml = ElementTree();
        simulateXml._setroot(Simulators);
        simulateXml.write('Simulate.xml','utf-8');   
Example #5
0
def saveChange(mainTree,name,s,pos,sc):

	sTree=grapPartElement(name,s,pos,str(sc))

	if mainTree.find(name)!=None:
		mainTree.remove(mainTree.find(name))
	mainTree.append(sTree)

	settings=ElementTree("fengx")
	settings._setroot(indent(mainTree))
	settings.write(SET_FILE_PATH,"utf-8")
Example #6
0
def changeGameMap(usd,m):
    uf=getXMLRoot(usd)

    uf.clear()
    
    item=Element("root")
    itemm=Element(m)
    itemm.text=" "
    item.append(itemm)

    outV = ElementTree()
    outV._setroot(item)
    outV.write(usd)#,"utf-8")
Example #7
0
def addEmoticon(xmlPath,files):
	ID=int(time.time())
	print  xmlPath, files
	fs=files#os.listdir(ROOT_DIR)
	mainXML=ElementTree(file=xmlPath)
	mainRoot=mainXML.getroot()
	haveOn=False
	mainData=mainRoot.find("Catalog")
	'''
	for s in mainRoot.findall("Catalog"):
		if s.get("Title")=="fengx":
			haveOn=True
			#mainData=s
			mainData=mainRoot.find("Catalog")
	if haveOn==False:
		mainData=Element("Catalog", {'id' : "ID_fengx", 'Path' : ' ', "Title":"fengx"})
	
	print haveOn
	'''
	if len(fs)!=0:
		i=1
		for f in fs:
			fileType=os.path.splitext(os.path.split(f)[1])[1] 
			name=os.path.splitext(os.path.split(f)[1])[0]
			if fileType==".jpg" or fileType==".png" or fileType==".bmp" or fileType==".JPG" or fileType==".PNG" or fileType==".GIF" or fileType==".gif":  
				n=name+str(i)
				print n
				print ID
				ID+=1
				print ID
				fileName=n+fileType

				tPath=os.path.split(xmlPath)[0]+"\\"+fileName 		
				shutil.copy(f,  tPath)

				#copyFiles(f, tPath)

				#os.system ("xcopy /s %s %s" % (f, tPath))
				item=emotionElement(str(ID),n,fileName)
				i+=1
				mainData.append(item)
		#if haveOn==False:
		#	mainRoot.append(mainData)
		mainXML._setroot(indent(mainRoot))
		mainXML.write(xmlPath,"utf-8")
Example #8
0
def createSetingsFile():
	global ROOT_DIR
	global GRAP_PF_NAME
	t=Element("root")
	#t.text="fengx"
	files= os.listdir(ROOT_DIR)
	fElement=[]
	i=1
	for m in files:
		nameParts=string.split(os.path.basename(m),"_")
		print nameParts
		if nameParts[0]==GRAP_PF_NAME:
			if os.path.splitext(m)[1]==".png":
				fElement.append(grapPartElement(m,True,[10,10],1))
	for s in fElement:
		t.append(s)

	settings=ElementTree("fengxx")
	settings._setroot(indent(t))
	settings.write(SET_FILE_PATH,"utf-8")
Example #9
0
def createSetingsFile():
    global ROOT_DIR
    global GRAP_PF_NAME
    main_element=Element("root")
    images_element=Element("images")
    #images_element.text="imagePin"
    files= os.listdir(ROOT_DIR)
    haveImage=False
    #print files
    for m in files:
        nameParts=os.path.basename(m).split("_")
        if nameParts[0]==GRAP_PF_NAME:
            if os.path.splitext(m)[1]==".png":
                #image_element.text=m
                haveImage=True
                images_element.append(grapPartElement(m,True,[10,10],1,m))
    settings=ElementTree("fengxx")
    main_element.append(images_element)
    settings._setroot(indent(main_element))
    settings.write(SET_FILE_PATH,"utf-8")
    print "\ncreateSetingsFile: ", SET_FILE_PATH
Example #10
0
def saveChange(mainTree,name,s,pos,sc,filePath):
    global SET_FILE_PATH
    sTree=grapPartElement(name,s,pos,str(sc),filePath)
    changeOn=False
    for s in mainTree.findall("images/image"):
        if s.find("path").text==filePath:
            s.find("path").text=sTree.find("path").text
            s.find("name").text=sTree.find("name").text
            s.find("miniState").text=sTree.find("miniState").text
            s.find("posx").text=sTree.find("posx").text
            s.find("posy").text=sTree.find("posy").text
            s.find("scale").text=sTree.find("scale").text
            changeOn=True
    if changeOn==False:
        mainTree.find("images").append(sTree)


        #for s in mainTree.findall("images/image"):
        #print  s.find("path").text,filePath
    settings=ElementTree("root")
    settings._setroot(indent(mainTree))
    settings.write(SET_FILE_PATH,"utf-8")
Example #11
0
def makeDITA(ts, ctp, node, idir):
    global node_data

    # maximum images per row
    maxrow = 3

    if debugMode():
        print("makeDITA", ctp, node.get("id"), node.text)

    # initialize the DITA XML
    root = fromstring(ts)
    tree = ElementTree()
    tree._setroot(root)

    node_text = node.text
    node_id = node.get("id")
    node_author = node.get("user")
    node_created = node.get("created")
    node_textp = node.get("path")

    # read in the node body text file
    fp = open(node_textp, "r")
    node_text = fp.read()
    fp.close()

    # set output id and file path
    ditaid = ctp + "_" + node_id
    ditafile = ditaid + ".dita"

    if sorttype == SCHRON:
        node_data[node_id] = [ctp, node_created, ditafile, "?"]
    else:
        title_key = node.text.upper()
        title_key = title_key.strip()
        title_key = title_key.replace('\n', '')
        while '  ' in title_key:
            title_key = title_key.replace('  ', ' ')
        node_data[node_id] = [ctp, title_key, node_created, ditafile, "?"]

    title_date = titleDate(node_created)

    # get all the image elements for the node
    images = node.find("images")
    imagelist = iter(images)

    top_images = []
    bot_images = []

    # build the image lists
    for image in imagelist:
        top_images.append(image)

    # common for all content types
    root.set("id", ditaid)
    titlee = root.find("title")
    # title
    if develMode():
        title_date = title_date + " " + ditafile
    if longtitle:
        titlee.text = node.text + " - " + title_date + ", by " + node_author
    else:
        titlee.text = node.text

    # find the body
    conbody = root.find("conbody")

    # tags
    keywords = root.find("prolog/metadata/keywords")
    tags = node.find("tags")
    tlist = iter(tags)
    for t in tlist:
        indt = SubElement(keywords, "indexterm")
        indt.text = t.text

    # create a section for the top images
    if len(top_images) > 0:
        section = SubElement(conbody, "section")
        section.set("id", "images_top")
        sectp = SubElement(section, "p")
        ni = 0
        for imge in top_images:
            imagefn = idir + '/' + imge.get("filename")
            imagefn = imagefn.replace("\\", "/")
            img = SubElement(sectp, "image")
            img.set("href", imagePath(imagefn))
            if not imge.text == None:
                img.set("alt", imge.text)
            ni = ni + 1
            if ni >= maxrow:
                sectp = SubElement(section, "p")
                ni = 0

    # create a section for the text

    # get filtered node text
    filtered = filterText(node_text)

    # write out text in case we bomb out trying to parse it
    fpp = open("debug.xml", "w")
    fpp.write(filtered)
    fpp.close()

    # make sure text is valid XML
    try:
        section = XML(filtered)
        # make the root be a section
        section.tag = "section"
        section.set("id", "node_text")
        if debugMode():
            print("filtered section:")
            print(tostring(section))

    except:
        webErrorLog("Invalid node text in:", ctp, node.get("id"), node.text)
        logText("Invalid node text")
        logText(" node id: " + node.get("id"))
        logText(" node_text:")
        logText(node_text)
        logText(" filtered text:")
        logText(filtered)
        logText(" ")

        section = SubElement(conbody, "section")
        sectp = SubElement(section, "p")
        sectp.text = "** invalid XML **"

    dita_sections = html2dita(section, ditafile, ditaid)

    # add the text as sections
    for dita_section in dita_sections:
        if debugMode():
            print("dita_section:", tostring(dita_section))
        conbody.append(dita_section)

    # create a section for the bottom images
    if len(bot_images) > 0:
        section = SubElement(conbody, "section")
        section.set("id", "images_bottom")
        sectp = SubElement(section, "p")
        ni = 0
        for imge in bot_images:
            imagefn = idir + '/' + imge.get("filename")
            imagefn = imagefn.replace("\\", "/")
            img = SubElement(sectp, "image")
            img.set("href", imagePath(imagefn))
            if not imge.text == None:
                img.set("alt", imge.text)
            ni = ni + 1
            if ni >= maxrow:
                sectp = SubElement(section, "p")
                ni = 0

    # patch things up for image href values
    imgs = root.findall("*//image")
    for img in imgs:
        ipath = img.get("href")
        ipath_base = os.path.basename(ipath)
        ipath_dir = os.path.dirname(ipath)
        ipath_full = imagedir + "/" + ipath_base

        apath_full = actualPath(ipath_full)
        apath_base = os.path.basename(apath_full)

        if not os.path.exists(ipath_full):
            webErrorLog(ditafile)
            webErrorLog("missing image", ipath_full)
            img.set(
                "href",
                os.path.dirname(ipath) + "/" + os.path.basename(missing_image))
        else:
            img.set("href", os.path.dirname(ipath) + "/" + apath_base)

    try:
        # return the DITA topic as a string
        retstr = tostring(root)
    except:
        webErrorLog("Oh oh!, the DITA file is not XML")
        dump(root)
        exit(0)

    return retstr
Example #12
0
def item2DITA(item,topic_string):
    if debugMode():
        print("item2DITA",item.tag,topic_string[0:10])

    DITAnode = None
    
    # initialize the DITA XML from the template
    root = fromstring(topic_string)
    tree = ElementTree()
    tree._setroot(root)
    # find the body
    econbody = root.find('conbody')
    
    # start by setting the title
    eititle = item.find('title')
    edtitle = root.find('title')
    edtitle.text = eititle.text
    if develMode():
        edtitle.text = itemFile(item,'dita')+": "+edtitle.text

    # point to the shortdesc element
    edshort = root.find("shortdesc")
    
    # set the author information
    eprolog = root.find('prolog')
    eauthors = eprolog.findall('author')
    eiauthor = item.find(dcns+'creator')
    creator = eiauthor.text
    for a in eauthors:
        a.text = author_table[creator]
        if debugMode():
            print("author set to",a.text)

    
    
    # set the dates
    epubdate = item.find(wpns+'post_date')
    pubstring = epubdate.text[0:10]
    if develMode():
        edtitle.text = edtitle.text+" - "+pubstring
    edates = eprolog.find('critdates')
    ecreate = edates.find('created')
    ecreate.set('date',pubstring)
    erevised = edates.find('revised')
    erevised.set('modified',pubstring)

    # set the short description
    edshort.text = "Posted "+pubstring+" by "+author_table[creator]

    # make the metadata list
    meta_list = []
    all_cats = item.findall('category')
    for c in all_cats:
        try:
           mkey = c.get('nicename')           
           metadata = meta_table[c.get('nicename')]
           meta_list.append(metadata)
           if debugMode():
            print(' *metadata*',c.get('domain'),metadata)
        except:
            meta_list.append(c.text)

    # set the topic metadata index and keyword information
    emetadata = eprolog.find('metadata')
    ekeywords = emetadata.find('keywords')
    for m in meta_list:
        ek = SubElement(ekeywords,'indexterm')
        ek.text = m
    
    elink = item.find('link')
    eother = SubElement(emetadata,'othermeta')
    eother.set('content',elink.text)
    eother.set('name','URL')
    epostid = item.find(wpns+'post_id')
    eother = SubElement(emetadata,'othermeta')
    eother.set('content',epostid.text)
    eother.set('name','post_id')
    eposttype = item.find(wpns+'post_type')
    eother = SubElement(emetadata,'othermeta')
    eother.set('content',eposttype.text)
    ret_type = eposttype.text
    eother.set('name','post_type')

    # if there is a featured image, create a section for it
    allpmeta = item.findall(wpns+'postmeta')
    if len(allpmeta)>0:
        for pm in allpmeta:
            ekey = pm.find(wpns+'meta_key')
            if not ekey==None:
                if ekey.text=='_thumbnail_id':
                    evalue = pm.find(wpns+'meta_value')
                    if debugMode():
                        print("**featured image is",evalue.text)
                    esection = SubElement(econbody,'section')
                    ep = SubElement(esection,'p')
                    eimage = SubElement(ep,'image')
                    eimage.set("id","featured_image")
                    eimage.set("href",iPath(evalue.text))

    # convert the html body text to DITA sections
    sections, links = html2dita(item)

    # add the sections to the output
    for s in sections:
        econbody.append(s)
        if debugMode():
            print("**section:")
            dump(s)
            print()

    # add in any related links
    if len(links)>0:
        erl = SubElement(root,"related-links")
        for link in links:
            erl.append(link)
            
    if debugMode():
        print("*dump of root")
        dump(root)
    
    return ret_type, doctype+tostring(root).decode()
Example #13
0
    def makeXML(self, work_directory):
        # </Experiment>
        TheExperiment =Element( 'Experiment')
        TheExperiment.attrib['xmlns']="http://geni.net/schema"
        TheExperiment.attrib['xmlns:xsi']="http://www.w3.org/2001/XMLSchema-instance"
        TheExperiment.attrib['xsi:schemaLocation']="http://geni.net/schema GENIObject.xsd"


        # <Experiment><Name/>
        Name = SubElement( TheExperiment, 'Name')

        # <Experiment><Name><Authority/>
        Authority = SubElement( Name, 'Authority')
        Authority.text = self.exp_authority

        # <Experiment><Name><Name2/>
        Name2 = SubElement( Name, 'Name')
        Name2.attrib['xml:lang']="en-US"
        Name2.text = self.name

        # <Experiment><GENI_experiment_identifier/>
        Exp_id = SubElement( TheExperiment, 'GENI_experiment_identifier')
        Exp_id.text = self.exp_id

        # <Experiment><Individuals/>
        Individuals = SubElement( TheExperiment, 'Individuals')

        # <Experiment><Individuals><Individual/>
        Individual = SubElement( Individuals, 'Individual')

        # <Experiment><Individuals><Individual><Role/>
        Role = SubElement( Individual, 'Role')
        Role.text = self.individual_role

        # <Experiment><Individuals><Individual><Authority/>
        IndAuthority = SubElement( Individual, 'Authority')
        IndAuthority.text = self.individual_authority
  
        # <Experiment><Individuals><Individual><User_name/>
        User = SubElement( Individual, 'User_name')
        User.text = self.individual_user

        # <Experiment><Dates_and_times/>
        Dates_and_times = SubElement( TheExperiment, 'Dates_and_times')
    
        # <Experiment><Dates_and_times><Date_and_time/>
        Date_and_time = SubElement( Dates_and_times, 'Date_and_time')

        # <Experiment><Dates_and_times><Date_and_time><Date_time_type/>
        Date_time_type = SubElement( Date_and_time, 'Date_time_type')
        Date_time_type.text = self.date_time_type

        # <Experiment><Dates_and_times><Date_and_time><Date_time_start/>
        Start = SubElement( Date_and_time, 'Date_time_start')
        Start.text = self.start


        #To print to file
        Test = ElementTree()
        Test._setroot(TheExperiment)
        root = Test.getroot()
        self.indent(root)
        Test.write(work_directory+'/experiment.xml')
Example #14
0
class OvalDocument(object):
    """
    For working with OVAL documents.  That interaction will entail the use of the other classes.
    Can be used to find certain elements within the document, update the document, and save the changes to a file
    """

    # A time format to match what OVAL expects
    TIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z"

    NS_DEFAULT = {"def": "http://oval.mitre.org/XMLSchema/oval-definitions-5"}
    NS_OVAL = {"oval": "http://oval.mitre.org/XMLSchema/oval-common-5"}
    NS_XSI = {"xsi": "http://www.w3.org/2001/XMLSchema-instance"}

    #     xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5"
    #     xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5"
    #     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    #     xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd
    #     http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd
    #     http://oval.mitre.org/XMLSchema/oval-definitions-5#solaris solaris-definitions-schema.xsd
    #     http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd
    #     http://oval.mitre.org/XMLSchema/oval-definitions-5#unix unix-definitions-schema.xsd">^M

    @staticmethod
    def indent(elem, level=0):
        i = "\n" + level * "  "
        if len(elem):
            if not elem.text or not elem.text.strip():
                elem.text = i + "  "
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
            for elem in elem:
                OvalDocument.indent(elem, level + 1)
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
        else:
            if level and (not elem.tail or not elem.tail.strip()):
                elem.tail = i

    @staticmethod
    def getOvalTimestamp(timestamp=None):
        """Renders a datetime to a string formatted according to the OVAL specification.
        if the timestamp argument is None (which it is by default) or is not of type datetime,
        this function will return a string using the current datetime.

        @type timestamp: datetime
        @param timestamp: A datetime to be formatted as an OVAL timestamp, or None to use the current time.

        @rtype: string
        @return: a string formatted as per OVAL
        """
        if timestamp is None or not isinstance(timestamp, datetime):
            now = datetime.date.today()
            return now.strftime(OvalDocument.TIME_FORMAT)
        else:
            return timestamp.strftime(OvalDocument.TIME_FORMAT)

    def __init__(self, tree):
        #         if not tree or not isinstance(tree, ElementTree):
        if not tree:
            root = Element("oval_definitions")
            self.tree = ElementTree.ElementTree(root)
            element = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}generator")
            gen = OvalGenerator(element)
            gen.setProduct("The CIS OVAL Repository")
            gen.setTimestamp(None)
            gen.setSchemaVersion("5.10.1")
            root.append(gen.get_element())
            return

        self.tree = tree
        self.id_to_definition = (
            {el.getId(): el for el in self.getDefinitions()} if self.getDefinitions() else {}
        )
        self.id_to_test = {el.getId(): el for el in self.getTests()} if self.getTests() else {}
        self.id_to_object = (
            {el.getId(): el for el in self.getObjects()} if self.getObjects() else {}
        )
        self.id_to_state = {el.getId(): el for el in self.getStates()} if self.getStates() else {}
        self.id_to_variable = (
            {el.getId(): el for el in self.getVariables()} if self.getVariables() else {}
        )

    def parseFromFile(self, filename):
        """
        Load an OVAL document from a filename and parse that into an ElementTree
        Returns False if the filename is empty or there is an error parsing the XML document
        @type filename: string
        @param filename: The path to the OVAL XML document to parse

        @rtype:    boolean
        @return:    True on success, otherwise False
        """
        try:
            if not filename:
                self.tree = None
                return False
            else:
                self.tree = ElementTree.parse(filename)
                return True
        except Exception:
            return False

    def parseFromText(self, xmltext):
        """
        Initializes the ElementTree by parsing the xmltext as XML
        Returns False if the string could not be parsed as XML

        @rtype:    boolean
        @return:    True on success, otherwise False
        """
        try:
            if not xmltext:
                return False
            else:
                root = ElementTree.fromstring(xmltext)
                self.tree = ElementTree(root)
                return True
        except Exception:
            return False

    def writeToFile(self, filename):
        """
        Writes the internal representation of the XmlTree to the given file name
        Returns False on error

        @rtype:    boolean
        @return:    True on success, otherwise False
        """
        try:
            if not filename:
                return False
            if not self.tree:
                return False

            ### TODO:  Add all necessary namespaces
            self.tree.write(filename, "UTF-8", True, OvalDocument.NS_DEFAULT, "xml")

        except Exception:
            return False

    def to_string(self):

        if not self.tree:
            return None

        root = self.tree.getroot()
        if root is None:
            return ""
        OvalDocument.indent(root)
        return ElementTree.tostring(root, "UTF-8", "xml").decode("utf-8")

    def getDocumentRoot(self):
        """
        Returns the root element of the XML tree if one has been loaded.
        Otherwise, returns None

        @rtype:    Element
        @return:    The root Element of the OVAL document, or None
        """
        if not self.tree:
            return None

        return self.tree.getroot()

    def getGenerator(self, create=False):
        """
        Gets the generator for this OVAL document as an OvalGenerator object.
        If the generator element does not exist, the default behavior is to
        return none.  However, setting the optional parameter to True will cause
        a default generate element to be created, added to the document, and that will be returned.
        A value of None may also be returned if this OvalDocument is empty

        @rtype:    OvalGenerator
        @return:    An OvalGenerator object, or None if it does not exist and create was not set to True
        """
        if not self.tree:
            return None

        root = self.getDocumentRoot()
        if not root:
            return None

        gen_element = root.find("def:generator", OvalDocument.NS_DEFAULT)

        if gen_element is not None:
            return OvalGenerator(gen_element)

        if not create:
            return None
        else:
            element = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}generator")
            gen = OvalGenerator(element)
            gen.setProduct("The CIS OVAL Repository")
            gen.setTimestamp(None)
            gen.setSchemaVersion("5.10.1")
            return gen

    def getDefinitions(self):
        """
        Returns a list of all definitions found in this OvalDocment where each item in the list is of type OvalDefinition
        Returns None if no definitions could be found

        @rtype:    List
        @return:    All definitions in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None

        defroot = root.find("def:definitions", OvalDocument.NS_DEFAULT)

        if defroot is None:
            return None

        element_list = list(defroot)
        if not element_list:
            return None

        return [OvalDefinition(element) for element in element_list]

    def getTests(self):
        """
        Returns a list of all tests in this OvalDocument where each list item is of type OvalTest
        Returns None if no tests could be found

        @rtype:    List
        @return:    All tests in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None

        testroot = root.find("def:tests", OvalDocument.NS_DEFAULT)

        if testroot is None:
            return None

        element_list = list(testroot)
        if not element_list:
            return None

        return [OvalTest(element) for element in element_list]

    def getObjects(self):
        """
        Returns a list of all objects in this OvalDocument where each list item is of type OvalObject
        Returns None if no objects could be found

        @rtype:    List
        @return:    All objects in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None

        objectroot = root.find("def:objects", OvalDocument.NS_DEFAULT)

        if objectroot is None:
            return None

        element_list = list(objectroot)
        if not element_list:
            return None

        return [OvalObject(element) for element in element_list]

    def getStates(self):
        """
        Returns a list of all states in this OvalDocument where each list item is of type OvalState
        Returns None if no states could be found

        @rtype:    List
        @return:    All states in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None

        stateroot = root.find("def:states", OvalDocument.NS_DEFAULT)

        if stateroot is None:
            return None

        element_list = list(stateroot)
        if not element_list:
            return None

        return [OvalState(element) for element in element_list]

    def getVariables(self):
        """
        Returns a list of all variables in this OvalDocument where each list item is of type OvalVariable
        Returns None if no variables could be found

        @rtype:    List
        @return:    All variables in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None

        varroot = root.find("def:variables", OvalDocument.NS_DEFAULT)

        if varroot is None:
            return None

        element_list = list(varroot)
        if not element_list:
            return None

        return [OvalVariable(element) for element in element_list]

    def getElementByID(self, ovalid):
        """
        Uses the ovalid argument to determine what type of element is being referenced and locate that element
        in the OVAL ElementTree.
        Returns an OvalElement of the appropriate class (OvalDefinition, OvalTest, ...)
        or None if there is no ElementTree or if a matching item could not be found

        @rtype:    OvalElement
        @return:    The located element as the appropriate OvalElement subclass, or None if no matching element was found.
        """
        if not ovalid:
            return None

        root = self.getDocumentRoot()
        if not root:
            return None

        try:
            oval_type = OvalElement.getElementTypeFromOvalID(ovalid)
        except Exception:
            return None

        if oval_type == OvalDefinition.DEFINITION:
            return self.id_to_definition[ovalid]
        elif oval_type == OvalDefinition.TEST:
            return self.id_to_test[ovalid]
        elif oval_type == OvalDefinition.OBJECT:
            return self.id_to_object[ovalid]
        elif oval_type == OvalDefinition.STATE:
            return self.id_to_state[ovalid]
        elif oval_type == OvalDefinition.VARIABLE:
            return self.id_to_variable[ovalid]
        else:
            return None

    def addElement(self, element, replace=True):
        """
        Adds the element to the ElementTree for this OVAL document
        The element argument must be of type OvalElement
        This method uses the OVALID of the element to determine what type of element it is
        and if an existing element with that OVALID already exists.
        This method will also create the necessary structure (id est, adding <definitions>, <tests>, etc)
        if the ElementTree does not already contain it.
        By default this method will replace an existing item with the same OVALID, but this behavior can
        be overridden by changing the option second argument to a value of "False"
        Returns True on success, otherwise False

        @rtype:    boolean
        @return:    True if the element was added to the document, otherwise False
        """
        if not element or element is None:
            return False
        if not self.tree or self.tree is None:
            return False

        ovalid = element.getId()
        if not ovalid:
            return False

        root = self.tree.getroot()
        if not root:
            root = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}oval_definitions")
            self.tree._setroot(root)

        # If replace has been set to False, then we want to exit with no changes
        #  when an element with this OVALID already appears in the document
        if not replace:
            existing = self.getElementByID(ovalid)
            if existing:
                return False

        try:
            oval_type = OvalElement.getElementTypeFromOvalID(ovalid)
        except Exception:
            return False

        # Depending on the ID type, find the parent for it or create that parent if it doesn't exist
        # Then append the current element
        if oval_type == OvalDefinition.DEFINITION:
            parent = root.find("def:definitions", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}definitions")
                root.append(parent)

            parent.append(element.getElement())
            self.id_to_definition[ovalid] = element
            return True

        elif oval_type == OvalDefinition.TEST:
            parent = root.find("def:tests", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}tests")
                root.append(parent)

            parent.append(element.getElement())
            self.id_to_test[ovalid] = element
            return True

        elif oval_type == OvalDefinition.OBJECT:
            parent = root.find("def:objects", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}objects")
                root.append(parent)

            parent.append(element.getElement())
            self.id_to_object[ovalid] = element
            return True

        elif oval_type == OvalDefinition.STATE:
            parent = root.find("def:states", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}states")
                root.append(parent)

            parent.append(element.getElement())
            self.id_to_state[ovalid] = element
            return True

        elif oval_type == OvalDefinition.VARIABLE:
            parent = root.find("def:variables", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}variables")
                root.append(parent)

            self.id_to_variable[ovalid] = element
            parent.append(element.getElement())
            return True

        else:
            return False
Example #15
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 #16
0
def load_weibo_from_bs():
    bs_input = load_bs()
    h = 0
    f = open('jsonTOXML.xml', 'w')
    f.write('<?xml version="1.0" encoding="utf-8"?>'+'\n'+'<statuses>')
    
    for _id, weibo in bs_input.reads():
        id = _id
        created_at = weibo['created_at']
        text = weibo['text']
        source = weibo['source']
        reposts_count = weibo['reposts_count']
        comments_count = weibo['comments_count']
        attitudes_count = weibo['attitudes_count']
        geo = weibo['geo']

        if 'user' not in weibo:
            continue
        user = weibo['user']

        u_id = user['id']
        u_name = user['name']
        u_gender = user['gender']
        u_province = user['province']
        u_city = user['city']
        u_location = user['location']

        if 'description' not in user:
            continue
        u_description = user['description']

        u_verified = user['verified']
        u_followers_count = user['followers_count']
        u_statuses_count = user['statuses_count']
        u_friends_count = user['friends_count']
        u_profile_image_url = user['profile_image_url']
        u_bi_followers_count = user['bi_followers_count']

        if 'verified_type' not in user:
            continue
        u_verified_type = user['verified_type']

        if 'retweeted_status' not in weibo:
            continue

        
        h = h + 1
        print h
        if h > 4:
            break
        if not weibo['retweeted_status']:
            xml_data = ElementTree()
            root = Element('status')
            xml_data._setroot(root)

            root = buildET('id',root,id)
            root = buildET('created_at',root,created_at)
            root = buildET('text',root,text)
            root = buildET('source',root,source)
            root = buildET('reposts_count',root,reposts_count)
            root = buildET('comments_count',root,comments_count)
            root = buildET('attitudes_count',root,attitudes_count)
            root = buildET('geo',root,geo)

            weibo_ip = generateIP()#随机产生一个微博的IP地址
            weibo_city = IP_City(weibo_ip)#将随机产生的IP解析成对应的城市

            while weibo_city == 'Error':#判断IP是否存在
                weibo_ip = generateIP()
                weibo_city = IP_City(weibo_ip)

            root = buildET('IP',root,weibo_ip)            
            root = buildET('address',root,weibo_city)
            
            root = subET(user,'user',root)

            xml_data.write(f, 'utf-8')
            f.write('\n')

        else:
            if 'description' not in weibo['retweeted_status']['user']:
                continue
            if 'user' not in weibo['retweeted_status']:
                continue
            if 'verified_type' not in weibo['retweeted_status']['user']:
                continue
            xml_data = ElementTree()
            root = Element('status')
            xml_data._setroot(root)

            root = buildET('id',root,id)
            root = buildET('created_at',root,created_at)
            root = buildET('text',root,text)
            root = buildET('source',root,source)
            root = buildET('reposts_count',root,reposts_count)
            root = buildET('comments_count',root,comments_count)
            root = buildET('attitudes_count',root,attitudes_count)
            root = buildET('geo',root,geo)

            weibo_ip = generateIP()#随机产生一个微博的IP地址
            weibo_city = IP_City(weibo_ip)#将随机产生的IP解析成对应的城市

            while weibo_city == 'Error':#判断IP是否存在
                weibo_ip = generateIP()
                weibo_city = IP_City(weibo_ip)

            root = buildET('IP',root,weibo_ip)
            root = buildET('address',root,weibo_city)

            root = subET(user,'user',root)

            retweeted_data = Element('retweeted_status')
            root.append(retweeted_data)

##            retweeted_ip = generateIP()#随机产生一个微博的IP地址
##            retweeted__city = IP_City(retweeted__ip)#将随机产生的IP解析成对应的城市
##
##            while retweeted__city == 'Error':#判断IP是否存在
##                retweeted__ip = generateIP()
##                retweeted__city = IP_City(retweeted__ip)

            SubElement(retweeted_data, 'r_ip').text = weibo_ip
            SubElement(retweeted_data, 'r_address').text = weibo_city

            SubElement(retweeted_data, 'r_id').text = str(weibo['retweeted_status']['mid'])
            SubElement(retweeted_data, 'r_created').text = weibo['retweeted_status']['created_at']
            SubElement(retweeted_data, 'r_text').text = weibo['retweeted_status']['text']
            retweeted_data = subET(weibo['retweeted_status']['user'],'user',retweeted_data)

            xml_data.write(f, 'utf-8')
            f.write('\n')
                
    f.write('\n'+'</statuses>')
    f.close()
Example #17
0
class OvalDocument(object):
    """
    For working with OVAL documents.  That interaction will entail the use of the other classes.
    Can be used to find certain elements within the document, update the document, and save the changes to a file
    """
        
    
    # A time format to match what OVAL expects
    TIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
    
    NS_DEFAULT  = {"def": "http://oval.mitre.org/XMLSchema/oval-definitions-5"}
    NS_OVAL     = {"oval": "http://oval.mitre.org/XMLSchema/oval-common-5"}
    NS_XSI      = {"xsi": "http://www.w3.org/2001/XMLSchema-instance"}
    
#     xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" 
#     xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5" 
#     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
#     xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd
#     http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd 
#     http://oval.mitre.org/XMLSchema/oval-definitions-5#solaris solaris-definitions-schema.xsd 
#     http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd 
#     http://oval.mitre.org/XMLSchema/oval-definitions-5#unix unix-definitions-schema.xsd">^M
    
    
    @staticmethod
    def indent(elem, level=0):
        i = "\n" + level*"  "
        if len(elem):
            if not elem.text or not elem.text.strip():
                elem.text = i + "  "
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
            for elem in elem:
                OvalDocument.indent(elem, level+1)
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
        else:
            if level and (not elem.tail or not elem.tail.strip()):
                elem.tail = i    
    
    
    @staticmethod            
    def getOvalTimestamp(timestamp=None):
        """Renders a datetime to a string formatted according to the OVAL specification.
        if the timestamp argument is None (which it is by default) or is not of type datetime,
        this function will return a string using the current datetime.
        
        @type timestamp: datetime
        @param timestamp: A datetime to be formatted as an OVAL timestamp, or None to use the current time.
        
        @rtype: string
        @return: a string formatted as per OVAL
        """
        if timestamp is None or not isinstance(timestamp, datetime):
            now = datetime.date.today() 
            return now.strftime(OvalDocument.TIME_FORMAT)
        else:
            return timestamp.strftime(OvalDocument.TIME_FORMAT)
 
    
    
    def __init__(self, tree):
#         if not tree or not isinstance(tree, ElementTree):
        if not tree:
            root = Element("oval_definitions")
            self.tree = ElementTree.ElementTree(root)
            element = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}generator")
            gen = OvalGenerator(element)
            gen.setProduct("The CIS OVAL Repository")
            gen.setTimestamp(None)
            gen.setSchemaVersion("5.10.1")
            root.append(gen.get_element())
            return        
        
        self.tree = tree
        
        
    def parseFromFile(self, filename):
        """
        Load an OVAL document from a filename and parse that into an ElementTree
        Returns False if the filename is empty or there is an error parsing the XML document
        @type filename: string
        @param filename: The path to the OVAL XML document to parse
        
        @rtype:    boolean
        @return:    True on success, otherwise False
        """
        try: 
            if not filename:
                self.tree = None
                return False
            else:
                self.tree = ElementTree.parse(filename)
                return True
        except Exception:
            return False
        
        
    def parseFromText(self, xmltext):
        """
        Initializes the ElementTree by parsing the xmltext as XML
        Returns False if the string could not be parsed as XML
        
        @rtype:    boolean
        @return:    True on success, otherwise False
        """
        try:
            if not xmltext:
                return False
            else:
                root = ElementTree.fromstring(xmltext)
                self.tree = ElementTree(root)
                return True
        except Exception:
            return False
            
            
    def writeToFile(self, filename):
        """
        Writes the internal representation of the XmlTree to the given file name
        Returns False on error
        
        @rtype:    boolean
        @return:    True on success, otherwise False
        """
        try:
            if not filename:
                return False
            if not self.tree:
                return False
            
            ### TODO:  Add all necessary namespaces
            self.tree.write(filename, "UTF-8", True, OvalDocument.NS_DEFAULT, "xml")
                
        except Exception:
            return False
        
        
    def to_string(self):
        
        if not self.tree:
            return None
        
        root = self.tree.getroot()
        if root is None:
            return ""
        OvalDocument.indent(root)
        return ElementTree.tostring(root, "UTF-8", "xml").decode("utf-8")
    
        
    def getDocumentRoot(self):
        """
        Returns the root element of the XML tree if one has been loaded.
        Otherwise, returns None
        
        @rtype:    Element
        @return:    The root Element of the OVAL document, or None
        """
        if not self.tree:
            return None
        
        return self.tree.getroot()
    
    
    def getGenerator(self, create=False):
        """
        Gets the generator for this OVAL document as an OvalGenerator object.
        If the generator element does not exist, the default behavior is to
        return none.  However, setting the optional parameter to True will cause
        a default generate element to be created, added to the document, and that will be returned.
        A value of None may also be returned if this OvalDocument is empty
        
        @rtype:    OvalGenerator
        @return:    An OvalGenerator object, or None if it does not exist and create was not set to True
        """
        if not self.tree:
            return None
        
        root = self.getDocumentRoot()
        if not root:
            return None
        
        gen_element = root.find("def:generator", OvalDocument.NS_DEFAULT)
        
        if gen_element is not None:
            return OvalGenerator(gen_element)
        
        if not create:
            return None
        else:
            element = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}generator")
            gen = OvalGenerator(element)
            gen.setProduct("The CIS OVAL Repository")
            gen.setTimestamp(None)
            gen.setSchemaVersion("5.10.1")
            return gen
            
    
    
                
    def getDefinitions(self):
        """
        Returns a list of all definitions found in this OvalDocment where each item in the list is of type OvalDefinition
        Returns None if no definitions could be found
        
        @rtype:    List
        @return:    All definitions in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None
        
        defroot = root.find("def:definitions", OvalDocument.NS_DEFAULT)
        
        if defroot is None:
            return None
        
        element_list = list(defroot)
        if not element_list:
            return None
        
        return [OvalDefinition(element) for element in element_list]
        
        

        
    def getTests(self):
        """
        Returns a list of all tests in this OvalDocument where each list item is of type OvalTest
        Returns None if no tests could be found
        
        @rtype:    List
        @return:    All tests in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None
        
        testroot = root.find("def:tests", OvalDocument.NS_DEFAULT)

        if testroot is None:
            return None
        
        element_list = list(testroot)
        if not element_list:
            return None
        
        return [OvalTest(element) for element in element_list]
        
        
        
    def getObjects(self):
        """
        Returns a list of all objects in this OvalDocument where each list item is of type OvalObject
        Returns None if no objects could be found
        
        @rtype:    List
        @return:    All objects in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None
        
        objectroot = root.find("def:objects", OvalDocument.NS_DEFAULT)
        
        if objectroot is None:
            return None
        
        element_list = list(objectroot)
        if not element_list:
            return None
        
        return [OvalObject(element) for element in element_list]
    
        
        
    def getStates(self):
        """
        Returns a list of all states in this OvalDocument where each list item is of type OvalState
        Returns None if no states could be found
        
        @rtype:    List
        @return:    All states in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None
        
        stateroot = root.find("def:states", OvalDocument.NS_DEFAULT)
        
        if stateroot is None:
            return None
        
        element_list = list(stateroot)
        if not element_list:
            return None
        
        return [OvalState(element) for element in element_list]
        
        
        
        
    def getVariables(self):
        """
        Returns a list of all variables in this OvalDocument where each list item is of type OvalVariable
        Returns None if no variables could be found
        
        @rtype:    List
        @return:    All variables in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None
        
        varroot = root.find("def:variables", OvalDocument.NS_DEFAULT)
        
        if varroot is None:
            return None
        
        element_list = list(varroot)
        if not element_list:
            return None
        
        return [OvalVariable(element) for element in element_list]
        
        
        
        
    def getElementByID(self, ovalid):
        """
        Uses the ovalid argument to determine what type of element is being referenced and locate that element
        in the OVAL ElementTree.
        Returns an OvalElement of the appropriate class (OvalDefinition, OvalTest, ...) 
        or None if there is no ElementTree or if a matching item could not be found
        
        @rtype:    OvalElement
        @return:    The located element as the appropriate OvalElement subclass, or None if no matching element was found.
        """
        if not ovalid:
            return None
        
        root = self.getDocumentRoot()
        if not root:
            return None
        
        try:
            oval_type = OvalElement.getElementTypeFromOvalID(ovalid)
        except Exception:
            return None
        
        if oval_type == OvalDefinition.DEFINITION:
            elist = self.getDefinitions()
        elif oval_type == OvalDefinition.TEST:
            elist = self.getTests()
        elif oval_type == OvalDefinition.OBJECT:
            elist = self.getObjects()
        elif oval_type == OvalDefinition.STATE:
            elist = self.getStates()
        elif oval_type == OvalDefinition.VARIABLE:
            elist = self.getVariables()
        else:
            return None
            
        if not elist:
            return None
        
        for element in elist:
            defid = element.getId()
            if defid and defid == ovalid:
                return element
            
                    
        
    
    def addElement(self, element, replace=True):
        """
        Adds the element to the ElementTree for this OVAL document
        The element argument must be of type OvalElement
        This method uses the OVALID of the element to determine what type of element it is
        and if an existing element with that OVALID already exists.
        This method will also create the necessary structure (id est, adding <definitions>, <tests>, etc)
        if the ElementTree does not already contain it.
        By default this method will replace an existing item with the same OVALID, but this behavior can
        be overridden by changing the option second argument to a value of "False"
        Returns True on success, otherwise False
        
        @rtype:    boolean
        @return:    True if the element was added to the document, otherwise False
        """
        if not element or element is None:
            return False
        if not self.tree or self.tree is None:
            return False
        
        ovalid = element.getId()
        if not ovalid:
            return False
        
        root = self.tree.getroot()
        if not root:
            root = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}oval_definitions")
            self.tree._setroot(root)
            
        # If replace has been set to False, then we want to exit with no changes
        #  when an element with this OVALID already appears in the document            
        if not replace:
            existing = self.getElementByID(ovalid)
            if existing:
                return False;
            
        
        try:
            oval_type = OvalElement.getElementTypeFromOvalID(ovalid)
        except Exception:
            return False
                
        # Depending on the ID type, find the parent for it or create that parent if it doesn't exist
        # Then append the current element
        if oval_type == OvalDefinition.DEFINITION:
            parent = root.find("def:definitions", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}definitions")
                root.append(parent)
                
            parent.append(element.getElement())
            return True
        
        elif oval_type == OvalDefinition.TEST:
            parent = root.find("def:tests", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}tests")
                root.append(parent)
                
            parent.append(element.getElement())
            return True
        
        elif oval_type == OvalDefinition.OBJECT:
            parent = root.find("def:objects", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}objects")
                root.append(parent)
                
            parent.append(element.getElement())
            return True
        
        elif oval_type == OvalDefinition.STATE:
            parent = root.find("def:states", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}states")
                root.append(parent)
                
            parent.append(element.getElement())
            return True
        
        elif oval_type == OvalDefinition.VARIABLE:
            parent = root.find("def:variables", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}variables")
                root.append(parent)
                
            parent.append(element.getElement())
            return True
        
        else:
            return False
Example #18
0
def makeDITA(ts,ctp,node,idir):
    global node_data

    # maximum images per row
    maxrow = 3
    
    if debugMode():
        print("makeDITA",ctp,node.get("id"),node.text)
  
    
    # initialize the DITA XML
    root = fromstring(ts)
    tree = ElementTree()
    tree._setroot(root)
            
    node_text = node.text
    node_id = node.get("id")
    node_author = node.get("user")
    node_created = node.get("created")
    node_textp = node.get("path")
    
    # read in the node body text file
    fp = open(node_textp,"r")
    node_text = fp.read()
    fp.close()

    # set output id and file path
    ditaid = ctp+"_"+node_id
    ditafile = ditaid+".dita"

    if sorttype == SCHRON:
     node_data[node_id] = [ctp,node_created,ditafile,"?"]
    else:
     title_key = node.text.upper()
     title_key = title_key.strip()
     title_key = title_key.replace('\n','')
     while '  ' in title_key:
         title_key = title_key.replace('  ',' ')
     node_data[node_id] = [ctp,title_key,node_created,ditafile,"?"]
     
    title_date = titleDate(node_created)

    # get all the image elements for the node
    images = node.find("images")
    imagelist = iter(images)

    top_images = []
    bot_images = []
    
    # build the image lists
    for image in imagelist:
        top_images.append(image)
       
    # common for all content types
    root.set("id",ditaid)
    titlee = root.find("title")
    # title
    if develMode():
      title_date=title_date+" "+ditafile
    if longtitle:
      titlee.text = node.text+" - "+title_date+", by "+node_author
    else:
        titlee.text = node.text

    # find the body
    conbody = root.find("conbody")

    # tags
    keywords = root.find("prolog/metadata/keywords")
    tags = node.find("tags")
    tlist = iter(tags)
    for t in tlist:
        indt = SubElement(keywords,"indexterm")
        indt.text = t.text

    # create a section for the top images
    if len(top_images)>0:
        section = SubElement(conbody,"section")
        section.set("id","images_top")
        sectp = SubElement(section,"p")
        ni = 0
        for imge in top_images:
          imagefn = idir+'/'+imge.get("filename")
          imagefn = imagefn.replace("\\","/")
          img = SubElement(sectp,"image")
          img.set("href",imagePath(imagefn))
          if not imge.text==None:
            img.set("alt",imge.text)
          ni=ni+1
          if ni>=maxrow:
              sectp = SubElement(section,"p")
              ni=0
              
        
    # create a section for the text
    
    # get filtered node text
    filtered = filterText(node_text)
        
    # write out text in case we bomb out trying to parse it
    fpp = open("debug.xml","w")
    fpp.write(filtered)
    fpp.close()
        
        
    # make sure text is valid XML
    try:
        section = XML(filtered)
        # make the root be a section
        section.tag = "section"
        section.set("id","node_text")
        if debugMode():
            print("filtered section:")
            print(tostring(section))
                
            
    except:
        webErrorLog("Invalid node text in:",ctp,node.get("id"),node.text)
        logText("Invalid node text")
        logText(" node id: "+node.get("id"))
        logText(" node_text:")
        logText(node_text)
        logText(" filtered text:")
        logText(filtered)
        logText(" ")
            
        section = SubElement(conbody,"section")
        sectp = SubElement(section,"p")
        sectp.text = "** invalid XML **"

    dita_sections = html2dita(section,ditafile,ditaid)
            
    # add the text as sections
    for dita_section in dita_sections:
        if debugMode():
          print("dita_section:",tostring(dita_section))
        conbody.append(dita_section)
                 
    # create a section for the bottom images
    if len(bot_images)>0:
        section = SubElement(conbody,"section")
        section.set("id","images_bottom")
        sectp = SubElement(section,"p")
        ni=0
        for imge in bot_images:
          imagefn = idir+'/'+imge.get("filename")
          imagefn = imagefn.replace("\\","/")
          img = SubElement(sectp,"image")
          img.set("href",imagePath(imagefn))
          if not imge.text==None:
             img.set("alt",imge.text)
          ni=ni+1
          if ni>=maxrow:
              sectp = SubElement(section,"p")
              ni=0

    # patch things up for image href values
    imgs = root.findall("*//image")
    for img in imgs:
        ipath = img.get("href")
        ipath_base = os.path.basename(ipath)
        ipath_dir  = os.path.dirname(ipath)
        ipath_full = imagedir+"/"+ipath_base
        
        apath_full = actualPath(ipath_full)
        apath_base = os.path.basename(apath_full)
        
        if not os.path.exists(ipath_full):
            webErrorLog(ditafile)
            webErrorLog("missing image",ipath_full)
            img.set("href",os.path.dirname(ipath)+"/"+os.path.basename(missing_image))
        else:
            img.set("href",os.path.dirname(ipath)+"/"+apath_base)
        
    try:
        # return the DITA topic as a string
        retstr = tostring(root)
    except:
        webErrorLog("Oh oh!, the DITA file is not XML")
        dump(root)
        exit(0)
        
    return retstr
Example #19
0
class Construct:
    """
    construct soap envelope of xml structure
    """
    def __init__(self):
        """
        do some initial
        """

        # create an empty tree
        self.soap = ElementTree()

        # this variable used to save created soap envelpoe's xml content
        self.str_xml = ""

    def create_soap_envelope(self,
                             rpc_name,
                             cwmp_version="cwmp-1-0",
                             rpc_args="",
                             cwmp_id=""):
        """
        create soap envelope, and convert the structure to xml
        rpc_name: RPC name need to create
        cwmp_version: CPE supported cwmp version, default is cwmp-1-0
        rpc_args: RPC arguments, default is ""
        """

        log.debug_info("create_soap_envelope")

        try:
            dict_envelope_attrib = {
                'xmlns:SOAP-ENV': 'http://schemas.xmlsoap.org/soap/envelope/',
                'xmlns:SOAP-ENC': 'http://schemas.xmlsoap.org/soap/encoding/',
                'xmlns:xsd': 'http://www.w3.org/2001/XMLSchema',
                'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance'
            }

            dict_envelope_attrib[
                'xmlns:cwmp'] = '' + 'urn:dslforum-org:' + cwmp_version

            # create an element
            self.soap_envelope = Element('SOAP-ENV:Envelope',
                                         dict_envelope_attrib)

            # set root of tree
            self.soap._setroot(self.soap_envelope)

            # create sub elemnts of soap_envelop
            self.soap_header = Element('SOAP-ENV:Header')
            self.soap_body = Element('SOAP-ENV:Body')

            # add soap_header and soap_body to soap_envelope
            self.soap_envelope.append(self.soap_header)
            self.soap_envelope.append(self.soap_body)

            # create sub elements of soap header
            self.create_soap_header(cwmp_id)

            # create sub elements of soap body
            self.create_soap_body(rpc_name, rpc_args)

            # convert structure to xml
            self.str_xml = tostring(self.soap_envelope)

        except Exception, e:

            log.debug_err(e)
            return CONSTRUCT_FAIL, e

        return CONSTRUCT_SUC, ""