码迷,mamicode.com
首页 > 编程语言 > 详细

pyH支持python3

时间:2019-01-23 11:32:28      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:问题   close   attribute   lan   input   res   add   agg   ==   

记录下,感谢大神,原地址https://www.cnblogs.com/yunmenzhe/p/6293428.html,侵删

 

1.修改xxx/python3.5/pyh.py权限

sudo chmod 777 pyh.py

2.修改源码

  1 # @file: pyh.py
  2 # @purpose: a HTML tag generator
  3 # @author: Emmanuel Turlay <turlay@cern.ch>
  4 
  5 __doc__ = """The pyh.py module is the core of the PyH package. PyH lets you
  6 generate HTML tags from within your python code.
  7 See http://code.google.com/p/pyh/ for documentation.
  8 """
  9 __author__ = "Emmanuel Turlay <turlay@cern.ch>"
 10 __version__ = $Revision: 43 $
 11 __date__ = $Date$
 12 
 13 from sys import _getframe, stdout, modules, version
 14 nOpen={}
 15 
 16 nl = \n
 17 doctype = <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n
 18 charset = <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />\n
 19 
 20 tags = [html, body, head, link, meta, div, p, form, legend, 
 21         input, select, span, b, i, option, img, script,
 22         table, tr, td, th, h1, h2, h3, h4, h5, h6,
 23         fieldset, a, title, body, head, title, script, br, table]
 24 
 25 selfClose = [input, img, link, br]
 26 
 27 class Tag(list):
 28     tagname = ‘‘
 29     
 30     def __init__(self, *arg, **kw):
 31         self.attributes = kw
 32         if self.tagname : 
 33             name = self.tagname
 34             self.isSeq = False
 35         else: 
 36             name = sequence
 37             self.isSeq = True
 38         self.id = kw.get(id, name)
 39         #self.extend(arg)
 40         for a in arg: self.addObj(a)
 41 
 42     def __iadd__(self, obj):
 43         if isinstance(obj, Tag) and obj.isSeq:
 44             for o in obj: self.addObj(o)
 45         else: self.addObj(obj)
 46         return self
 47     
 48     def addObj(self, obj):
 49         if not isinstance(obj, Tag): obj = str(obj)
 50         id=self.setID(obj)
 51         setattr(self, id, obj)
 52         self.append(obj)
 53 
 54     def setID(self, obj):
 55         if isinstance(obj, Tag):
 56             id = obj.id
 57             n = len([t for t in self if isinstance(t, Tag) and t.id.startswith(id)])
 58         else:
 59             id = content
 60             n = len([t for t in self if not isinstance(t, Tag)])
 61         if n: id = %s_%03i % (id, n)
 62         if isinstance(obj, Tag): obj.id = id
 63         return id
 64 
 65     def __add__(self, obj):
 66         if self.tagname: return Tag(self, obj)
 67         self.addObj(obj)
 68         return self
 69 
 70     def __lshift__(self, obj):
 71         self += obj
 72         return obj
 73 
 74     def render(self):
 75         result = ‘‘
 76         if self.tagname:
 77             result = <%s%s%s> % (self.tagname, self.renderAtt(), self.selfClose()* /)
 78         if not self.selfClose():
 79             for c in self:
 80                 if isinstance(c, Tag):
 81                     result += c.render()
 82                 else: result += c
 83             if self.tagname: 
 84                 result += </%s> % self.tagname
 85         result += \n
 86         return result
 87 
 88     def renderAtt(self):
 89         result = ‘‘
 90         for n, v in self.attributes.items():
 91             if n != txt and n != open:
 92                 if n == cl: n = class
 93                 result +=  %s="%s" % (n, v)
 94         return result
 95 
 96     def selfClose(self):
 97         return self.tagname in selfClose        
 98     
 99 def TagFactory(name):
100     class f(Tag):
101         tagname = name
102     f.__name__ = name
103     return f
104 
105 thisModule = modules[__name__]
106 
107 for t in tags: setattr(thisModule, t, TagFactory(t)) 
108 
109 def ValidW3C():
110     out = a(img(src=http://www.w3.org/Icons/valid-xhtml10, alt=Valid XHTML 1.0 Strict), href=http://validator.w3.org/check?uri=referer)
111     return out
112 
113 class PyH(Tag):
114     tagname = html
115     
116     def __init__(self, name=MyPyHPage):
117         self += head()
118         self += body()
119         self.attributes = dict(xmlns=http://www.w3.org/1999/xhtml, lang=en)
120         self.head += title(name)
121 
122     def __iadd__(self, obj):
123         if isinstance(obj, head) or isinstance(obj, body): self.addObj(obj)
124         elif isinstance(obj, meta) or isinstance(obj, link): self.head += obj
125         else:
126             self.body += obj
127             id=self.setID(obj)
128             setattr(self, id, obj)
129         return self
130 
131     def addJS(self, *arg):
132         for f in arg: self.head += script(type=text/javascript, src=f)
133 
134     def addCSS(self, *arg):
135         for f in arg: self.head += link(rel=stylesheet, type=text/css, href=f)
136     
137     def printOut(self,file=‘‘):
138         if file: f = open(file, w)
139         else: f = stdout
140         f.write(doctype)
141         f.write(self.render())
142         f.flush()
143         if file: f.close()
144     
145 class TagCounter:
146     _count = {}
147     _lastOpen = []
148     for t in tags: _count[t] = 0
149     def __init__(self, name):
150         self._name = name
151     def open(self, tag):
152         if isLegal(tag): 
153             self._count[tag] += 1
154             self._lastOpen += [tag]
155     def close(self, tag):
156         if isLegal(tag) and self._lastOpen[-1] == tag: 
157             self._count[tag] -= 1
158             self._lastOpen.pop()
159         else:
160             print(Cross tagging is wrong)
161     def isAllowed(self, tag, open):
162         if not open and self.isClosed(tag):
163             print(TRYING TO CLOSE NON-OPEN TAG: %s % tag)
164             return False
165         return True
166     def isOpen(self, tag):
167         if isLegal(tag): return self._count[tag]
168     def isClosed(self, tag):
169         if isLegal(tag): return not self._count[tag]
170 
171     
172 def isLegal(tag):
173     if tag in tags: return True
174     else:
175         print(ILLEGAL TAG: %s % tag)
176         return False

3.避免文件被修改导致其他问题,权限改回

sudo chmod 644 pyh.py

 

pyH支持python3

标签:问题   close   attribute   lan   input   res   add   agg   ==   

原文地址:https://www.cnblogs.com/tester-rikaizhao/p/10307579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!