⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writer.py

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 PY
📖 第 1 页 / 共 2 页
字号:
        vi = self.__syntax.vi
        a = ''
        if self._flowing != self.__stack[-1][0]:
            if self._dtdflowing is not None \
               and self._flowing == self._dtdflowing:
                pass
            else:
                a = ' xml:space%s%s%s%s' \
                    % (vi, lit, ["default", "preserve"][self._flowing], lit)
        if self.__lang != self.__stack[-1][1]:
            a = '%s xml:lang%s%s%s%s' % (a, vi, lit, self.lang, lit)
        line = stag + a
        self._offset = self._offset + len(line)
        a = ''
        for k, v in attrs.items():
            if v is None:
                continue
            a = ' %s%s%s%s%s' % (k, vi, lit, escape(str(v)), lit)
            if (self._offset + len(a)) > self.lineLength:
                self._write(line + "\n")
                line = prefix + a
                self._offset = len(line)
            else:
                line = line + a
                self._offset = self._offset + len(a)
        self._write(line)
        self.__pending_content = 1
        if (  self.__dtdinfo and not
              (self.__dtdinfo.has_element_content(tag)
               or self.__dtdinfo.is_empty(tag))):
            self._packing = 1

    def endElement(self, tag):
        if self.__pending_content:
            if self._flowing:
                self._write(self.__syntax.empty_stagc)
                if self._packing:
                    self._offset = self._offset \
                                   + len(self.__syntax.empty_stagc)
                else:
                    self._write("\n")
                    self._offset = 0
            else:
                self._write(self.__syntax.empty_stagc)
                self._offset = self._offset + len(self.__syntax.empty_stagc)
            self.__pending_content = 0
            self.__poptag(tag)
            return
        depth = len(self.__stack)
        if depth == 1 or self._packing or not self._flowing:
            prefix = ''
        else:
            prefix = self._prefix[:-self.indentation] \
                     + (" " * self.indentEndTags)
        self.__poptag(tag)
        self._write("%s%s%s%s" % (
            prefix, self.__syntax.etago, tag, self.__syntax.tagc))
        if self._packing:
            self._offset = self._offset + len(tag) + 3
        else:
            self._write("\n")
            self._offset = 0

    def characters(self, data, start, length):
        data = data[start: start+length]
        if data:
            data = escape(data)
            if "\n" in data:
                p = string.find(data, "\n")
                self._offset = len(data) - (p + 1)
            else:
                self._offset = self._offset + len(data)
            self._write(data)

    def comment(self, data, start, length):
        data = data[start: start+length]
        self._check_pending_content()
        s = "%s%s%s%s%s" % (self.__syntax.mdo, self.__syntax.com,
                            data, self.__syntax.com, self.__syntax.mdc)
        p = string.rfind(s, "\n")
        if self._packing:
            if p >= 0:
                self._offset = len(s) - (p + 1)
            else:
                self._offset = self._offset + len(s)
        else:
            self._write("%s%s\n" % (self._prefix, s))
            self._offset = 0

    def ignorableWhitespace(self, data, start, length):
        pass

    def processingInstruction(self, target, data):
        s = "%s%s %s%s" % (self.__syntax.pio, target, data, self.__syntax.pic)
        prefix = self._prefix[:-self.indentation] \
                 + (" " * self.indentEndTags)
        if "\n" in s:
            pos = string.rfind(s, "\n")
            if self._flowing and not self._packing:
                self._write(prefix + s + "\n")
                self._offset = 0
            else:
                self._write(s)
                self._offset = len(s) - (p + 1)
        elif self._flowing and not self._packing:
            self._write(prefix + s + "\n")
            self._offset = 0
        else:
            self._write(s)
            self._offset = len(s) - (p + 1)


    # This doesn't actually have a SAX equivalent, so we'll use it as
    # an internal helper.

    def handle_doctype(self, root):
        self.__pending_doctype = 0
        if self.__dtdinfo:
            fpi = self.__dtdinfo.fpi
            sysid = self.__dtdinfo.sysid
        else:
            fpi = sysid = None
        lit = self.__syntax.lit
        isxml = self.__syntax.pic == "?>"
        if isxml and sysid:
            s = '%sDOCTYPE %s\n' % (self.__syntax.mdo, root)
            if fpi:
                s = s + '  PUBLIC %s%s%s\n' % (lit, fpi, lit)
                s = s + '         %s%s%s>\n' % (lit, sysid, lit)
            else:
                s = s + '  SYSTEM %s%s%s>\n' % (lit, sysid, lit)
            self._write(s)
            self._offset = 0
        elif not isxml:
            s = "%sDOCTYPE %s" % (self.__syntax.mdo, root)
            if fpi:
                s = '%s\n  PUBLIC %s%s%s' % (s, lit, fpi, lit)
            if sysid:
                s = '%s\n  SYSTEM %s%s%s' % (s, lit, sysid, lit)
            self._write("%s%s\n" % (s, self.__syntax.mdc))
            self._offset = 0

    def handle_cdata(self, data):
        self._check_pending_content()
        # There should be a better way to generate '[CDATA['
        start = self.__syntax.mdo + "[CDATA["
        end = self.__syntax.msc + self.__syntax.mdc
        s = "%s%s%s" % (start, escape(data), end)
        if self._packing:
            if "\n" in s:
                rpos = string.rfind(s, "\n")
                self._offset = len(s) - (rpos + 1) + len(end)
            else:
                self._offset = self.__offset + len(s) + len(start + end)
            self._write(s)
        else:
            self._offset = 0
            self._write(s + "\n")


    # Internal helper methods.

    def __poptag(self, tag):
        state = self.__stack.pop()
        self._flowing, self.__lang, expected_tag, \
                       self._packing, self._dtdflowing = state
        if tag != expected_tag:
            raise RuntimeError, \
                  "expected </%s>, got </%s>" % (expected_tag, tag)
        self._prefix = self._prefix[:-self.indentation]

    def __pushtag(self, tag):
        self.__stack.append((self._flowing, self.__lang, tag,
                             self._packing, self._dtdflowing))
        self._prefix = self._prefix + " " * self.indentation

    def __check_flowing(self, tag, attrs):
        """Check the contents of attrs and the DTD information to determine
        whether the following content should be flowed.

        tag -- general identifier of the element being opened
        attrs -- attributes dictionary as reported by the parser or
                 application

        This sets up both the _flowing and _dtdflowing (object) attributes.
        """
        docspec = dtdspec = None
        if self.__dtdinfo:
            try:
                info = self.__dtdinfo.get_attribute_info(tag, "xml:space")
            except KeyError:
                info = None
            if info is not None:
                self._flowing = info[2] != "preserve"
                self._dtdflowing = self._flowing
        if attrs.has_key("xml:space"):
            self._flowing = attrs["xml:space"] != "preserve"
            del attrs["xml:space"]

    def _check_pending_content(self):
        if self.__pending_content:
            s = self.__syntax.tagc
            if self._flowing and not self._packing:
                s = s + "\n"
                self._offset = 0
            else:
                self._offset = self._offset + len(s)
            self._write(s)
            self.__pending_content = 0


class PrettyPrinter(XmlWriter):
    """Pretty-printing XML output handler."""

    def __init__(self, fp, standalone=None, dtdinfo=None,
                 syntax=None, linelength=None,
                 indentation=2, endtagindentation=None):
        XmlWriter.__init__(self, fp, standalone=standalone, dtdinfo=dtdinfo,
                           syntax=syntax, linelength=linelength)
        self.indentation = indentation
        if endtagindentation is not None:
            self.indentEndTags = endtagindentation
        else:
            self.indentEndTags = indentation

    def characters(self, data, start, length):
        data = data[start: start + length]
        if not data:
            return
        self._check_pending_content()
        data = escape(data)
        if not self._flowing:
            self._write(data)
            return
        words = string.split(data)
        begspace = data[0] in string.whitespace
        endspace = words and (data[-1] in string.whitespace)
        prefix = self._prefix
        if len(prefix) > 40:
            prefix = "    "
        offset = self._offset
        L = []
        append = L.append
        if begspace:
            append(" ")
            offset = offset + 1
        ws = ""
        ws_len = 0
        while words:
            w = words[0]
            del words[0]
            if (offset + ws_len + len(w)) > self.lineLength:
                append("\n")
                append(prefix)
                append(w)
                offset = len(prefix) + len(w)
            else:
                append(ws)
                ws, ws_len = " ", 1
                append(w)
                offset = offset + 1 + len(w)
        if endspace:
            append(" ")
            offset = offset + 1
        self._offset = offset
        self._write(string.join(L, ""))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -