def udict(*dictionary, **kwargs): "Return a dict with unicode keys. A stand-in for the dict constructor." kwargs = dict((text_type(k), v) for k, v in kwargs.items()) if dictionary: base = dict((text_type(k), v) for k, v in dictionary[0].items()) base.update(kwargs) return base else: return kwargs
def _simplify_stream(stream, ctxt, vars): # consumes stream, send a list parts = [] for idx, (kind, data, pos) in enumerate(stream): if kind is TEXT: parts.append(data) elif kind is EXPR: value = _eval_expr(data, ctxt, vars) if hasattr(value, '__html__'): value = _unpack(value) if hasattr(value, '__next__') or hasattr(value, 'next'): while hasattr(value, '__next__') or hasattr(value, 'next'): value = list(value) value = _simplify_stream(value, ctxt, vars) if not isinstance(value, text_type): stream[idx:idx + 1] = value else: stream[idx] = (TEXT, value, pos) elif isinstance(value, bytestring_type): value = value.decode('utf8', 'replace') elif not isinstance(value, text_type): value = text_type(value) parts.append(value) else: return stream return u''.join(parts)
def named(cls, name): """Return a class with ``name`` = *name* :param name: a string or None. :returns: a new class """ if not isinstance(name, (text_type, NoneType)): name = text_type(name) cls.name = name return cls
def named(cls, name): """Return a class with ``name`` = *name* :param name: a string or None. :returns: a new class """ if PY2 and isinstance(name, bytestring_type): name = text_type(name) cls.name = name return cls
def inject(self, mapping, ctxt, vars): """Inject the translated key and interpolated value into *mapping*.""" raw = self.raw_value if raw.__class__ is text_type: final_value = raw else: parts = [] for kind, value, pos in raw: if kind is TEXT: parts.append(value) else: value = _eval_expr(value, ctxt, vars) parts.append(text_type(value)) final_value = u''.join(parts) mapping[_to_context.get(self._name, self._name)] = final_value
def transform_tabindex(tagname, attributes, contents, context, bind): proceed, forced = _pop_toggle(u'auto_tabindex', attributes, context) if not proceed: return contents tabindex = context[u'tabindex'] if tabindex == 0: return contents current = attributes.get(u'tabindex') if forced or current is None and tagname in _auto_tags[u'tabindex']: attributes[u'tabindex'] = text_type(str((tabindex))) if tabindex > 0: context[u'tabindex'] = tabindex + 1 return contents
def set(self, obj): """Process *obj* and assign the native and text values. :returns: True if adaptation of *obj* was successful. Attempts to adapt the given object and assigns this element's :attr:`~flatland.Element.value` and :attr:`u` attributes in tandem. If adaptation succeeds, ``.value`` will contain the :meth:`adapted<adapt>` native Python value and ``.u`` will contain a text :meth:`serialized<serialize>` version of it. A native value of ``None`` will be represented as ``u''`` in ``.u``. If adaptation fails, ``.value`` will be ``None`` and ``.u`` will contain ``str(obj)`` (or unicode), or ``u''`` for none. """ self.raw = obj try: # adapt and normalize the value, if possible obj = self.value = self.adapt(obj) except AdaptationError: self.value = None # could not be adapted # but, still try to textify it if obj is None: self.u = u'' elif isinstance(obj, text_type): self.u = obj else: try: self.u = text_transform(obj) except TypeError: self.u = u'' except UnicodeDecodeError: self.u = text_type(obj, errors='replace') element_set.send(self, adapted=False) return False # stringify it, possibly storing what we received verbatim or a # normalized version of it. if obj is None: self.u = u'' else: self.u = self.serialize(obj) element_set.send(self, adapted=True) return True
def pathexpr(expr): if isinstance(expr, PathExpression): return expr elif PY2 and isinstance(expr, bytestring_type): expr = text_type(expr) elif isinstance(expr, text_type): pass elif hasattr(expr, '__iter__'): expr = u'/'.join(expr) try: return expression_cache[expr] except KeyError: compiled = PathExpression(expr) if len(expression_cache) < max_cache_size: return expression_cache.setdefault(expr, compiled) else: return compiled
def tag(self, tagname, bind=None, **attributes): """Generate any tag. :param tagname: the name of the tag. :param bind: optional, a flatland element. :param \*\*attributes: any desired XML/HTML attributes. :returns: a printable :class:`Tag` The attribute rules appropriate for *tagname* will be applied. For example, ``tag('input')`` is equivalent to ``input()``. """ if isinstance(tagname, bytestring_type): # pragma: nocover tagname = text_type(tagname) tagname = tagname.lower() if bind is None and not attributes: return self._tag(tagname) else: return self._tag(tagname)(bind, **attributes)
class adict(dict): """Allow dict keys to be accessed with getattr().""" def __getattr__(self, attr): try: return self[attr] except KeyError: raise AttributeError(attr) def re_ucompile(pattern, flags=0): """Compile a regex with re.UNICODE on by default.""" return re.compile(pattern, flags | re.UNICODE) _alphanum = set( text_type(string.digits + string.ascii_lowercase + string.ascii_uppercase)) def re_uescape(pattern): """A unicode-friendly version of re.escape.""" mutable = list(pattern) for idx, char in enumerate(pattern): if char not in _alphanum: if char == u"\000": mutable[idx] = u"\\000" else: mutable[idx] = u"\\" + char return u''.join(mutable) def to_pairs(dictlike):
def _unpack(html_string): """Extract HTML from a __html__() interface.""" unpacked = html_string.__html__() if unpacked.__class__ is text_type: return unpacked return text_type(unpacked)
"""Allow dict keys to be accessed with getattr().""" def __getattr__(self, attr): try: return self[attr] except KeyError: raise AttributeError(attr) def re_ucompile(pattern, flags=0): """Compile a regex with re.UNICODE on by default.""" return re.compile(pattern, flags | re.UNICODE) _alphanum = set(text_type(string.digits + string.ascii_lowercase + string.ascii_uppercase)) def re_uescape(pattern): """A unicode-friendly version of re.escape.""" mutable = list(pattern) for idx, char in enumerate(pattern): if char not in _alphanum: if char == u"\000": mutable[idx] = u"\\000" else: mutable[idx] = u"\\" + char return u''.join(mutable)
from nose.tools import eq_, assert_raises, raises from flatland._compat import long_type, text_type __all__ = [ 'asciistr', 'assert_raises', 'eq_', 'raises', 'fails', 'requires_unicode_coercion', 'udict', 'unicode_coercion_available' ] # acts like 'str', but safe to use when tests are running with # sys.getdefaultencoding() == 'nocoercion'. _ascii_codec = codecs.getencoder('ascii') asciistr = lambda s: _ascii_codec(s)[0] # acts like naive unicode() on simple types like int textstr = lambda o: text_type(str(o)) _coercion_override = None def fails(reason): """Mark a test case as expected to fail for *reason*.""" def decorator(fn): @wraps(fn) def decorated(*args, **kw): try: fn(*args, **kw) except (SystemExit, KeyboardInterrupt): raise except: pass # ok
def order_ok(): slot_names = list(_.name for _ in el._slots) for idx, name in enumerate(slot_names): assert name == text_type(str(idx))