Example #1
0
class Px:
    '''Represents a (chunk of) PX code.'''
    xmlPrologue = xmlPrologue
    xhtmlPrologue = xhtmlPrologue

    def __init__(self,
                 content,
                 isFileName=False,
                 partial=True,
                 template=None,
                 hook=None,
                 prologue=None,
                 unicode=True):
        '''p_content is the PX code, as a string, or a file name if p_isFileName
           is True. If this code represents a complete XML file, p_partial is
           False. Else, we must surround p_content with a root tag to be able
           to parse it with a SAX parser.

           If this PX is based on another PX template, specify the PX template
           in p_template and the name of the p_hook where to insert this PX into
           the template PX.

           If a p_prologue is specified, it will be rendered at the start of the
           PX result.

           By default, a PX's result will be a unicode. If you want to get an
           encoded str instead, use p_unicode=False.
        '''
        # Get the PX content
        if isFileName:
            f = file(content)
            self.content = f.read()
            f.close()
        else:
            self.content = content
        # It this content a complete XML file, or just some part of it?
        if partial:
            # Surround the partial chunk with a root tag: it must be valid XML.
            self.content = '<x>%s</x>' % self.content
        self.partial = partial
        # Create a PX parser
        self.parser = PxParser(PxEnvironment(), self)
        # Parses self.content (a PX code in a string) with self.parser, to
        # produce a tree of memory buffers.
        try:
            self.parser.parse(self.content)
        except xml.sax.SAXParseException, spe:
            self.completeErrorMessage(spe)
            raise spe
        # Is this PX based on a template PX?
        self.template = template
        self.hook = hook
        # Is there some (XML, XHTML...) prologue to dump?
        self.prologue = prologue
        # Will the result be unicode or str?
        self.unicode = unicode
Example #2
0
class Px:
    '''Represents a (chunk of) PX code.'''
    xmlPrologue = xmlPrologue
    xhtmlPrologue = xhtmlPrologue

    def __init__(self, content, isFileName=False, partial=True,
                 template=None, hook=None, prologue=None, unicode=True):
        '''p_content is the PX code, as a string, or a file name if p_isFileName
           is True. If this code represents a complete XML file, p_partial is
           False. Else, we must surround p_content with a root tag to be able
           to parse it with a SAX parser.

           If this PX is based on another PX template, specify the PX template
           in p_template and the name of the p_hook where to insert this PX into
           the template PX.

           If a p_prologue is specified, it will be rendered at the start of the
           PX result.

           By default, a PX's result will be a unicode. If you want to get an
           encoded str instead, use p_unicode=False.
        '''
        # Get the PX content
        if isFileName:
            f = file(content)
            self.content = f.read()
            f.close()
        else:
            self.content = content
        # It this content a complete XML file, or just some part of it?
        self.partial = partial
        # Is this PX based on a template PX?
        self.template = template
        self.hook = hook
        # Is there some (XML, XHTML...) prologue to dump?
        self.prologue = prologue
        # Will the result be unicode or str?
        self.unicode = unicode
        self.parse()

    def parse(self):
        '''Parses self.content and create the structure corresponding to this
           PX.'''
        if self.partial:
            # Surround the partial chunk with a root tag: it must be valid XML.
            self.content = '<x>%s</x>' % self.content
        # Create a PX parser
        self.parser = PxParser(PxEnvironment(), self)
        # Parses self.content (a PX code in a string) with self.parser, to
        # produce a tree of memory buffers.
        try:
            self.parser.parse(self.content)
        except xml.sax.SAXParseException, spe:
            self.completeErrorMessage(spe)
            raise spe
Example #3
0
 def parse(self):
     '''Parses self.content and create the structure corresponding to this
        PX.'''
     if self.partial:
         # Surround the partial chunk with a root tag: it must be valid XML.
         self.content = '<x>%s</x>' % self.content
     # Create a PX parser
     self.parser = PxParser(PxEnvironment(), self)
     # Parses self.content (a PX code in a string) with self.parser, to
     # produce a tree of memory buffers.
     try:
         self.parser.parse(self.content)
     except xml.sax.SAXParseException, spe:
         self.completeErrorMessage(spe)
         raise spe
Example #4
0
    def __init__(self, content, isFileName=False, partial=True, template=None, hook=None, prologue=None, unicode=True):
        """p_content is the PX code, as a string, or a file name if p_isFileName
           is True. If this code represents a complete XML file, p_partial is
           False. Else, we must surround p_content with a root tag to be able
           to parse it with a SAX parser.

           If this PX is based on another PX template, specify the PX template
           in p_template and the name of the p_hook where to insert this PX into
           the template PX.

           If a p_prologue is specified, it will be rendered at the start of the
           PX result.

           By default, a PX's result will be a unicode. If you want to get an
           encoded str instead, use p_unicode=False.
        """
        # Get the PX content
        if isFileName:
            f = file(content)
            self.content = f.read()
            f.close()
        else:
            self.content = content
        # It this content a complete XML file, or just some part of it?
        if partial:
            # Surround the partial chunk with a root tag: it must be valid XML.
            self.content = "<x>%s</x>" % self.content
        self.partial = partial
        # Create a PX parser
        self.parser = PxParser(PxEnvironment(), self)
        # Parses self.content (a PX code in a string) with self.parser, to
        # produce a tree of memory buffers.
        try:
            self.parser.parse(self.content)
        except xml.sax.SAXParseException, spe:
            self.completeErrorMessage(spe)
            raise spe