Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19733851
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2010-02-09 15:06:57

from abstractxmlwriter import AbstractXmlWriter
from htmlutils import html_escape, html_attr_escape
from robottypes import unic


class HtmlWriter(AbstractXmlWriter):
 
    def __init__(self, output):
        """'output' is an open file object.
       
        Given 'output' must have been opened with 'wb' to be able to
        write into it with UTF-8 encoding.
       
        'self.output.name' is later used by serializers
        """
        self.output = output
       
    def start(self, name, attrs=None, newline=True):
        self._start(name, attrs, close=False, newline=newline)
       
    def start_and_end(self, name, attrs=None, newline=True):
        self._start(name, attrs, close=True, newline=newline)
               
    def content(self, content=None, escape=True):
        """Given content doesn't need to be a string"""
        if content is not None:
            if escape:
                content = self._escape_content(content)
            self._write(content)

    def end(self, name, newline=True):
        elem = '' % name
        if newline:
            elem += '\n'
        self._write(elem)
           
    def element(self, name, content=None, attrs=None, escape=True,
                newline=True):
        self.start(name, attrs, newline=False)
        self.content(content, escape)
        self.end(name, newline)
   
    def start_many(self, names, newline=True):
        for name in names:
            self.start(name, newline=newline)
           
    def end_many(self, names, newline=True):
        for name in names:
            self.end(name, newline)

    def _start(self, name, attrs, close=False, newline=True):
        elem = '<%s' % name
        elem = self._add_attrs(elem, attrs)
        elem += (close and ' />' or '>')
        if newline:
            elem += '\n'
        self._write(elem)           
           
    def _add_attrs(self, elem, attrs):
        if not attrs:
            return elem
        attrs = attrs.items()
        attrs.sort()
        attrs = [ '%s="%s"' % (name, html_attr_escape(value))
                  for name, value in attrs ]
        return '%s %s' % (elem, ' '.join(attrs))
   
    def _escape_content(self, content):
        return html_escape(unic(content))

    def _write(self, text):
        self.output.write(text.encode('UTF-8'))

文件路径:robotframework-2.1.2\src\robot\utils\htmlwriter.py
功能:定义了写html文件的类
 
阅读(29277) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~