Home | History | Annotate | Download | only in mako
      1 import sys
      2 import time
      3 
      4 py3k = sys.version_info >= (3, 0)
      5 py33 = sys.version_info >= (3, 3)
      6 py2k = sys.version_info < (3,)
      7 py26 = sys.version_info >= (2, 6)
      8 jython = sys.platform.startswith('java')
      9 win32 = sys.platform.startswith('win')
     10 pypy = hasattr(sys, 'pypy_version_info')
     11 
     12 if py3k:
     13     from io import StringIO
     14     import builtins as compat_builtins
     15     from urllib.parse import quote_plus, unquote_plus
     16     from html.entities import codepoint2name, name2codepoint
     17     string_types = str,
     18     binary_type = bytes
     19     text_type = str
     20 
     21     from io import BytesIO as byte_buffer
     22 
     23     def u(s):
     24         return s
     25 
     26     def b(s):
     27         return s.encode("latin-1")
     28 
     29     def octal(lit):
     30         return eval("0o" + lit)
     31 
     32 else:
     33     import __builtin__ as compat_builtins
     34     try:
     35         from cStringIO import StringIO
     36     except:
     37         from StringIO import StringIO
     38 
     39     byte_buffer = StringIO
     40 
     41     from urllib import quote_plus, unquote_plus
     42     from htmlentitydefs import codepoint2name, name2codepoint
     43     string_types = basestring,
     44     binary_type = str
     45     text_type = unicode
     46 
     47     def u(s):
     48         return unicode(s, "utf-8")
     49 
     50     def b(s):
     51         return s
     52 
     53     def octal(lit):
     54         return eval("0" + lit)
     55 
     56 
     57 if py33:
     58     from importlib import machinery
     59     def load_module(module_id, path):
     60         return machinery.SourceFileLoader(module_id, path).load_module()
     61 else:
     62     import imp
     63     def load_module(module_id, path):
     64         fp = open(path, 'rb')
     65         try:
     66             return imp.load_source(module_id, path, fp)
     67         finally:
     68             fp.close()
     69 
     70 
     71 if py3k:
     72     def reraise(tp, value, tb=None, cause=None):
     73         if cause is not None:
     74             value.__cause__ = cause
     75         if value.__traceback__ is not tb:
     76             raise value.with_traceback(tb)
     77         raise value
     78 else:
     79     exec("def reraise(tp, value, tb=None, cause=None):\n"
     80             "    raise tp, value, tb\n")
     81 
     82 
     83 def exception_as():
     84     return sys.exc_info()[1]
     85 
     86 try:
     87     import threading
     88     if py3k:
     89         import _thread as thread
     90     else:
     91         import thread
     92 except ImportError:
     93     import dummy_threading as threading
     94     if py3k:
     95         import _dummy_thread as thread
     96     else:
     97         import dummy_thread as thread
     98 
     99 if win32 or jython:
    100     time_func = time.clock
    101 else:
    102     time_func = time.time
    103 
    104 try:
    105     from functools import partial
    106 except:
    107     def partial(func, *args, **keywords):
    108         def newfunc(*fargs, **fkeywords):
    109             newkeywords = keywords.copy()
    110             newkeywords.update(fkeywords)
    111             return func(*(args + fargs), **newkeywords)
    112         return newfunc
    113 
    114 
    115 all = all
    116 import json
    117 
    118 def exception_name(exc):
    119     return exc.__class__.__name__
    120 
    121 try:
    122     from inspect import CO_VARKEYWORDS, CO_VARARGS
    123     def inspect_func_args(fn):
    124         if py3k:
    125             co = fn.__code__
    126         else:
    127             co = fn.func_code
    128 
    129         nargs = co.co_argcount
    130         names = co.co_varnames
    131         args = list(names[:nargs])
    132 
    133         varargs = None
    134         if co.co_flags & CO_VARARGS:
    135             varargs = co.co_varnames[nargs]
    136             nargs = nargs + 1
    137         varkw = None
    138         if co.co_flags & CO_VARKEYWORDS:
    139             varkw = co.co_varnames[nargs]
    140 
    141         if py3k:
    142             return args, varargs, varkw, fn.__defaults__
    143         else:
    144             return args, varargs, varkw, fn.func_defaults
    145 except ImportError:
    146     import inspect
    147     def inspect_func_args(fn):
    148         return inspect.getargspec(fn)
    149 
    150 if py3k:
    151     def callable(fn):
    152         return hasattr(fn, '__call__')
    153 else:
    154     callable = callable
    155 
    156 
    157 ################################################
    158 # cross-compatible metaclass implementation
    159 # Copyright (c) 2010-2012 Benjamin Peterson
    160 def with_metaclass(meta, base=object):
    161     """Create a base class with a metaclass."""
    162     return meta("%sBase" % meta.__name__, (base,), {})
    163 ################################################
    164 
    165 
    166 def arg_stringname(func_arg):
    167     """Gets the string name of a kwarg or vararg
    168     In Python3.4 a function's args are
    169     of _ast.arg type not _ast.name
    170     """
    171     if hasattr(func_arg, 'arg'):
    172         return func_arg.arg
    173     else:
    174         return str(func_arg)
    175