def test_unicode(self): from stuf.six import u, b self.assertEqual( self.mclass( [1], True, r't', b('i'), u('g'), None, (1,) ).unicode().oneach().peek(), [u('[1]'), u('True'), u('t'), u('i'), u('g'), u('None'), u('(1,)')] )
def ez_id(this): ''' easy unique identifier for an object @param this: an object ''' return hashlib.sha1(u(id(this))).digest()
def fromstring(self, instr): ''' Create template from source string. :argument str instr: template source ''' # extract fields, groups from source addgroup = self._addgroup addfield = self._addfield for mo in self._match.finditer(instr): first, second = mo.groups() # add groups if first is not None: addgroup(first) # add fields elif second is not None: addfield(second) # pnly initialize templates with fields if self._fields: # internal template self._template = self._match.sub(u'%s', instr) # text stub self._text = u('') # backup text self._btext = instr
def slugify(cls, value): ''' normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens ''' value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') return cls._second('-', u(cls._first('', value).strip().lower()))
def transform(self, stylesheet=None, **kw): ''' Transforms template based on XSLT stylesheet. :keyword str stylesheet: XSLT document :param kw: keyword arguments ''' if stylesheet is not None: self._setxslt(stylesheet) return u(self._xslt(self._tree, **kw))
def on(self, key, thing): ''' bind thing to event @param label: event label @param key: event key @param call: some thing ''' self.subscribe(self._key, key, thing.commit) self.register([self._key], key, u(''), thing)
def __init__(self, name, auto=True, maxlen=25, **kw): ''' :argument str name: field name :keyword bool auto: turns automagic on and off :keyword int maxlen: maximum number of times a field can repeat ''' super(_TextField, self).__init__(auto, maxlen, **kw) self.__name__ = name # set text to empty unicode string self.text = self._btext = u('')
def __iadd__(self, data): ''' Inserts string or template after current content. ''' # process strings if isinstance(data, basestring): # append string onto internal template self._template = u''.join([self._template, data]) # process templates elif hasattr(data, 'mark'): if hasattr(data, 'groupmark'): # add group-like template fields to _tempfield tracker self._tempfields.extend(data._fields) # add group-like template's template to temporary template self._ttemplate = u('').join((self._ttemplate, data._template)) else: # add fields to _tempfield tracker self._tempfields.append(data) # add delimiter onto temp template self._ttemplate = u('').join((self._ttemplate, u('%s'))) else: raise TypeError(_exceptions[2]) return self
import random as _random from sys import maxint as _maxint from random import randint as _randint from xml.etree.ElementTree import Element as ETElement from stuf.six import items, u from lxml.html import HTMLParser, tostring as htostring, xhtml_to_html from lxml.etree import ( Element, ElementTree, HTML, XSLT, XML, parse, tostring as xtostring) from webstring.base import ( _checkname, _Template, _Group, _Field, _exceptions, _Stemplate, ) # HTML 5 doctype _html5 = u('<!DOCTYPE html>') # XHTML 1.0 strict doctype _xhtml10 = u( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' ) # XHTML 1.1 strict doctype _xhtml11 = u( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ' '"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' ) def _copytree(tree): # copies an element. element = tree.makeelement(tree.tag, tree.attrib)
def test_unicode(self): from stuf.six import u, b, tounicode self.assertEqual( [tounicode(i) for i in [[1], True, r't', b('i'), u('g'), None, (1,)]], [u('[1]'), u('True'), u('t'), u('i'), u('g'), u('None'), u('(1,)')] )
def test_ascii(self): from stuf.six import u, b, tobytes self.assertEqual( [tobytes(i, 'ascii') for i in [[1], True, r't', b('i'), u('g'), None, (1,)]], [b('[1]'), b('True'), b('t'), b('i'), b('g'), b('None'), b('(1,)')] )
def __unicode__(self): return u('') if self.msg is None else tounicode(self.__bytes__())