Example #1
0
def createDocument(evaluation,repository="../sheets/"):
    """
    Creates a document object  from qsos raw evaluation. Relevant elements are
    extracted from families modelsheets (elements with sub-elements are skipped
    as they do not have any score nor comment tag) and their evaluations are
    extracted from the qsos evaluation. The authors and dates are the same
    (evaluation's in fact) for each family. Generic section of qsos evaluation
    is also added to families component of a document. 
    
    @param evaluation
            Qsos evaluation's string flow
    
    @param familypath
            Path to families model sheets.
            Default value is ../sheets/
        
    @return
        Evaluation's representation's document object
    """
    rawDocument = minidom.parseString(evaluation)
    #Define the ID attribute of each element tag of the raw document
    for element in rawDocument.getElementsByTagName("element"):
        element.setIdAttribute("name")
        
    #Create the property list from the content of header.
    #the first, second and last tag of header are ignored
    #as they are not document properties but part of families contents
    header = rawDocument.firstChild.firstChild.childNodes
    properties = {}
    for n in header[2:-1]:
        try :
            properties[n.tagName] = n.firstChild.data
        except AttributeError :
            properties[n.tagName] = "N/A"
                            
    
    #Instantiate a QSOS-Document object initiated with the properties extracted
    #from XML evaluation and empty family dictionnary
    qsos = document.Document(properties,{},{})
    
    #Extract  relevant information from the raw evaluation:
    #    - authors
    #    - dates
    #    - families (generic section is manually as it appears in each evaluation
    
    # TODO : Each information should be probed in case no values are
    #         provided in the XML document
    
    authors = [(item.firstChild.firstChild.data, item.lastChild.firstChild.data)
               for item in header[0].childNodes]

    families = [node.firstChild.data for node in header[-1].childNodes]
    qsos.families = families
    includes = ["generic"]
    for f in families :
        xml = os.path.join(repository, "templates", f + ".qtpl")
        xml = "".join(line.strip() for line in file(xml).readlines())
        xml = minidom.parseString(xml).firstChild
        for node in xml.childNodes :
            includes.append(node.firstChild.data)

    #Build the Family object for each family component of the evaluation :
    #    - Extract from repository the family sheet (.qin files)
    #    - Read the scores and comments from evaluation
    #    - Update entry in family object
    for include in includes :
        template = minidom.parse("/".join([repository,"families",".".join([include,"qin"])]))
        #Initiate the family object : 
        #    -same authors and dates for all families of the same evaluation
        #    -empty score and comments dictionnary
        f = family.include(authors, {}, {})
        for element in template.getElementsByTagName("desc0"):
            name = element.parentNode.getAttribute("name")
            
            # TODO : use a logger for AttributeError exception^^
        
            try :
                f.scores[name] = rawDocument.                                   \
                                    getElementById(name).                       \
                                    getElementsByTagName("score").              \
                                    item(0).firstChild.data
            except AttributeError :
#                print "No score found for element %s" % (name,)
                pass    
            try :
                f.comments[name] = rawDocument.                                 \
                                    getElementById(name).                       \
                                    getElementsByTagName("comment")             \
                                    .item(0).firstChild.data
            except AttributeError :
#                print "No comment found for element %s" % (name,)
                pass
        #End of iteration, just add the family in document object
        qsos.includes[include] = f
    return qsos
Example #2
0
def build(evaluation, repositoryroot):
    """
    Builds name-version's qsos evaluation document
    
    @param id
            Evaluation identifier
    @param repositoryroot
            Path to local repository's root.
    @return
        Built Document object of name-version's qsos evaluation
    """

    #Unpack evaluation parameter and build base path
    (id, version) = evaluation
    base = os.path.join(repositoryroot, "sheets")

    #Read the header of requested evaluation from repository
    header = os.path.join(base, "evaluations", id, version, "header.qscore")
    header = "".join(line.strip() for line in file(header).readlines())

    #Extract template content
    content = minidom.parseString(header).firstChild

    #Extract properties from header contents
    properties = {}
    for node in content.firstChild.childNodes:
        try:
            properties[node.tagName] = node.firstChild.data
        except AttributeError:
            properties[node.tagName] = "N/A"

    #Set appname and release properties from evaluation parameter
    properties["release"] = version
    properties["qsosappname"] = id

    #Read includes of families from header. generic include must also be added
    families = [node.firstChild.data for node in content.lastChild.childNodes]
    includes = ["generic"]
    for f in families:
        xml = os.path.join(base, "templates", f + ".qtpl")
        xml = "".join(line.strip() for line in file(xml).readlines())
        xml = minidom.parseString(xml).firstChild
        for node in xml.childNodes:
            includes.append(node.firstChild.data)

    #Handle each family to be included
    tree = {}
    authors = {}
    for include in includes:
        #parse .qscore file
        relPath = os.path.join("evaluations", id, version, include + ".qscore")
        absPath = os.path.join(base, relPath)
        sheet = "".join(line.strip() for line in file(absPath).readlines())
        xml = minidom.parseString(sheet).firstChild
        (name, email) = getAuthor(os.path.join("sheets", relPath))
        authors[email] = name
        #Scores and comments extraction loop
        scores = {}
        comments = {}
        for node in xml.childNodes:
            n = node.getAttribute("name")
            v = node.getElementsByTagName("score")
            if v: scores[n] = v[0].firstChild.data
            v = node.getElementsByTagName("comment")
            if v: comments[n] = v[0].firstChild.data

        #Add the family into family list for the document
        tree[include] = family.include(authors, scores, comments)

    #Create and return the expected documents
    return document.Document(properties, families, tree)
Example #3
0
def build(evaluation, repositoryroot):
    """
    Builds name-version's qsos evaluation document
    
    @param id
            Evaluation identifier
    @param repositoryroot
            Path to local repository's root.
    @return
        Built Document object of name-version's qsos evaluation
    """
    
    #Unpack evaluation parameter and build base path
    (id,version)=evaluation
    base = os.path.join(repositoryroot,"sheets")
    
    #Read the header of requested evaluation from repository
    header = os.path.join(base,"evaluations", id, version, "header.qscore")
    header = "".join(line.strip() for line in file(header).readlines())
    
    #Extract template content
    content = minidom.parseString(header).firstChild
    
    #Extract properties from header contents
    properties = {}
    for node in content.firstChild.childNodes :
        try :
            properties[node.tagName] = node.firstChild.data
        except AttributeError:
            properties[node.tagName] = "N/A"
            
    #Set appname and release properties from evaluation parameter
    properties["release"]=version
    properties["qsosappname"]=id
    
    #Read includes of families from header. generic include must also be added
    families = [node.firstChild.data for node in content.lastChild.childNodes]
    includes = ["generic"]
    for f in families :
        xml = os.path.join(base, "templates", f + ".qtpl")
        xml = "".join(line.strip() for line in file(xml).readlines())
        xml = minidom.parseString(xml).firstChild
        for node in xml.childNodes :
            includes.append(node.firstChild.data)
    
    #Handle each family to be included
    tree = {}
    authors = {}
    for include in includes :
        #parse .qscore file
        relPath = os.path.join("evaluations" ,id, version, include + ".qscore")
        absPath = os.path.join(base, relPath)
        sheet = "".join(line.strip() for line in file(absPath).readlines())
        xml = minidom.parseString(sheet).firstChild
        (name,email) = getAuthor(os.path.join("sheets" ,relPath))
        authors[email]=name
        #Scores and comments extraction loop
        scores = {}
        comments = {}
        for node in xml.childNodes :
            n = node.getAttribute("name")
            v = node.getElementsByTagName("score")
            if v : scores[n] = v[0].firstChild.data 
            v = node.getElementsByTagName("comment")
            if v : comments[n] = v[0].firstChild.data
            
        #Add the family into family list for the document 
        tree[include]=family.include(authors, scores, comments)
        
    #Create and return the expected documents
    return document.Document(properties,families, tree)