Exemplo n.º 1
0
def Broken(self, oid, pair):
    broken_klasses_lock.acquire()
    try:
        if pair in broken_klasses:
            klass = broken_klasses[pair]
        else:
            module, klassname = pair
            d = {'BrokenClass': BrokenClass}
            exec_("class %s(BrokenClass): ' '; __module__=%r" %
                  (klassname, module), d)
            klass = broken_klasses[pair] = d[klassname]
            module = module.split('.')
            if len(module) > 2 and module[0] == 'Products':
                klass.product_name = module[1]
            klass.title = (
                'This object from the %s product '
                'is broken!' %
                klass.product_name)
            klass.info = (
                'This object\'s class was %s in module %s.' %
                (klass.__name__, klass.__module__))
            klass = persistentBroken(klass)
            LOG.warning(
                'Could not import class %r '
                'from module %r' % (klass.__name__, klass.__module__))
    finally:
        broken_klasses_lock.release()
    if oid is None:
        return klass
    i = klass()
    i._p_oid = oid
    i._p_jar = self
    return i
Exemplo n.º 2
0
def _make_eq(cls, names, include_super, include_type):
    # 1 and 0 are constants and faster to load than the globals True/False
    # (in python 2)

    eq_stmt = 'def __eq__(self, other'
    if include_type or include_super:
        # capture the type
        eq_stmt += ', cls=cls'
    eq_stmt += '):\n'
    eq_stmt += '    if self is other: return 1\n'
    if include_type:
        eq_stmt += '    if not isinstance(other, cls): return 0\n'
    if include_super:
        eq_stmt += '    s = super(cls, self).__eq__(other)\n'
        eq_stmt += '    if s is NotImplemented or not s: return s\n'

    # We take these one at a time (rather than using
    # operator.attrgetter). In the cases where some attributes
    # are computed, this can be more efficient if we discover
    # a mismatch early. Also, it lets us easily distinguish
    # between an AttributeError on self (which is a
    # programming error in calling EqHash) or the other object
    for name in names:
        eq_stmt += '    a = self.' + name + '\n'
        eq_stmt += '    try:\n        b = other.' + name + '\n'
        eq_stmt += '    except AttributeError: return NotImplemented\n'
        eq_stmt += '    if a != b: return 0\n\n'

    eq_stmt += '    return 1'

    # Must use a custom dictionary under Py3
    lcls = dict(locals())
    six.exec_(eq_stmt, globals(), lcls)

    return lcls['__eq__']
Exemplo n.º 3
0
def live_profile(script, timer, interval, pickle_protocol, mono):
    """Profile a Python script continuously."""
    filename, code, globals_ = script
    sys.argv[:] = [filename]
    parent_sock, child_sock = socket.socketpair()
    pid = os.fork()
    if pid == 0:
        # child
        devnull = os.open(os.devnull, os.O_RDWR)
        for f in [sys.stdin, sys.stdout, sys.stderr]:
            os.dup2(devnull, f.fileno())
        frame = sys._getframe()
        profiler = BackgroundProfiler(timer, frame, code)
        profiler.prepare()
        server_args = (noop, interval, pickle_protocol)
        server = SelectProfilingServer(None, profiler, *server_args)
        server.clients.add(child_sock)
        spawn_thread(server.connected, child_sock)
        try:
            exec_(code, globals_)
        finally:
            child_sock.close()
    else:
        # parent
        viewer, loop = make_viewer(mono)
        title = get_title(filename)
        client = ProfilingClient(viewer, loop.event_loop, parent_sock, title)
        client.start()
        try:
            loop.run()
        except KeyboardInterrupt:
            pass
        finally:
            parent_sock.close()
            os.kill(pid, signal.SIGINT)
Exemplo n.º 4
0
 def load_module(self, name):
     co, pyc = self.modules.pop(name)
     if name in sys.modules:
         # If there is an existing module object named 'fullname' in
         # sys.modules, the loader must use that existing module. (Otherwise,
         # the reload() builtin will not work correctly.)
         mod = sys.modules[name]
     else:
         # I wish I could just call imp.load_compiled here, but __file__ has to
         # be set properly. In Python 3.2+, this all would be handled correctly
         # by load_compiled.
         mod = sys.modules[name] = imp.new_module(name)
     try:
         mod.__file__ = co.co_filename
         # Normally, this attribute is 3.2+.
         mod.__cached__ = pyc
         mod.__loader__ = self
         # Normally, this attribute is 3.4+
         mod.__spec__ = spec_from_file_location(name, co.co_filename, loader=self)
         six.exec_(co, mod.__dict__)
     except:  # noqa
         if name in sys.modules:
             del sys.modules[name]
         raise
     return sys.modules[name]
Exemplo n.º 5
0
def remote_profile(script, timer, interval, pickle_protocol,
                   addr, start_signo, stop_signo, verbose):
    """Launch a server to profile continuously.  The default address is
    127.0.0.1:8912.
    """
    filename, code, globals_ = script
    sys.argv[:] = [filename]
    # create listener.
    listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    listener.bind(addr)
    listener.listen(1)
    # be verbose or quiet.
    if verbose:
        log = lambda x: click.echo(click.style(' > ', fg='cyan') + x)
        bound_addr = listener.getsockname()
        log('Listening on {0}:{1} for profiling...'.format(*bound_addr))
    else:
        log = noop
    # start profiling server.
    frame = sys._getframe()
    profiler = BackgroundProfiler(timer, frame, code, start_signo, stop_signo)
    profiler.prepare()
    server_args = (log, interval, pickle_protocol)
    server = SelectProfilingServer(listener, profiler, *server_args)
    spawn_thread(server.serve_forever)
    # exec the script.
    try:
        exec_(code, globals_)
    except KeyboardInterrupt:
        pass
Exemplo n.º 6
0
def timeit_profile(stmt, number, repeat, setup,
                   timer, pickle_protocol, dump_filename, mono, **_ignored):
    """Profile a Python statement like timeit."""
    del _ignored
    sys.path.insert(0, os.curdir)
    globals_ = {}
    exec_(setup, globals_)
    if number is None:
        # determine number so that 0.2 <= total time < 2.0 like timeit.
        dummy_profiler = Profiler()
        dummy_profiler.start()
        for x in range(1, 10):
            number = 10 ** x
            t = time.time()
            for y in range(number):
                exec_(stmt, globals_)
            if time.time() - t >= 0.2:
                break
        dummy_profiler.stop()
        del dummy_profiler
    code = compile('for _ in range(%d): %s' % (number, stmt),
                   'STATEMENT', 'exec')
    __profile__(stmt, code, globals_,
                timer=timer, pickle_protocol=pickle_protocol,
                dump_filename=dump_filename, mono=mono)
Exemplo n.º 7
0
        def run_plain():
            # Using normal Python shell
            import code
            imported_objects = import_objects(options, self.style)
            try:
                # Try activating rlcompleter, because it's handy.
                import readline
            except ImportError:
                pass
            else:
                # We don't have to wrap the following import in a 'try', because
                # we already know 'readline' was imported successfully.
                import rlcompleter
                readline.set_completer(rlcompleter.Completer(imported_objects).complete)
                readline.parse_and_bind("tab:complete")

            # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
            # conventions and get $PYTHONSTARTUP first then import user.
            if use_pythonrc:
                pythonrc = os.environ.get("PYTHONSTARTUP")
                if pythonrc and os.path.isfile(pythonrc):
                    global_ns = {}
                    with open(pythonrc) as rcfile:
                        try:
                            six.exec_(compile(rcfile.read(), pythonrc, 'exec'), global_ns)
                            imported_objects.update(global_ns)
                        except NameError:
                            pass
                # This will import .pythonrc.py as a side-effect
                try:
                    import user  # NOQA
                except ImportError:
                    pass
            code.interact(local=imported_objects)
def _get_task(tasks_file, task_name):
    ctx.logger.debug('Getting tasks file...')
    try:
        tasks_code = ctx.get_resource(tasks_file)
    except Exception as e:
        raise exceptions.NonRecoverableError(
            "Could not get '{0}' ({1}: {2})".format(tasks_file,
                                                    type(e).__name__, e))
    exec_globs = exec_env.exec_globals(tasks_file)
    try:
        exec_(tasks_code, _globs_=exec_globs)
    except Exception as e:
        raise exceptions.NonRecoverableError(
            "Could not load '{0}' ({1}: {2})".format(tasks_file,
                                                     type(e).__name__, e))
    task = exec_globs.get(task_name)
    if not task:
        raise exceptions.NonRecoverableError(
            "Could not find task '{0}' in '{1}'"
            .format(task_name, tasks_file))
    if not callable(task):
        raise exceptions.NonRecoverableError(
            "'{0}' in '{1}' is not callable"
            .format(task_name, tasks_file))
    return task
Exemplo n.º 9
0
def __profile__(filename, code, globals_,
                timer=None, pickle_protocol=PICKLE_PROTOCOL,
                dump_filename=None, mono=False):
    frame = sys._getframe()
    profiler = Profiler(timer, top_frame=frame, top_code=code)
    profiler.start()
    try:
        exec_(code, globals_)
    except:
        # don't profile print_exc().
        profiler.stop()
        traceback.print_exc()
    else:
        profiler.stop()
    if PY2:
        # in Python 2, exec's cpu time is duplicated with actual cpu time.
        stat = profiler.stats.get_child(frame.f_code)
        stat.remove_child(exec_.func_code)
    if dump_filename is None:
        viewer, loop = make_viewer(mono)
        viewer.set_stats(profiler.stats, get_title(filename))
        try:
            loop.run()
        except KeyboardInterrupt:
            pass
    else:
        stats = profiler.result()
        with open(dump_filename, 'wb') as f:
            pickle.dump(stats, f, pickle_protocol)
        click.echo('To view statistics:')
        click.echo('  $ python -m profiling view ', nl=False)
        click.secho(dump_filename, underline=True)
Exemplo n.º 10
0
  def parse(cls, path, symbol_table_cls):
    parse_globals = cls._get_globals(symbol_table_cls)

    python = _read(path)
    symbols = {}
    six.exec_(python, parse_globals, symbols)

    objects = []
    for name, obj in symbols.items():
      if isinstance(obj, type):
        # Allow type imports
        continue

      if not Serializable.is_serializable(obj):
        raise ParseError('Found a non-serializable top-level object: {}'.format(obj))

      attributes = obj._asdict()
      if 'name' in attributes:
        attributes = attributes.copy()
        redundant_name = attributes.pop('name', None)
        if redundant_name and redundant_name != name:
          raise ParseError('The object named {!r} is assigned to a mismatching name {!r}'
                          .format(redundant_name, name))
      obj_type = type(obj)
      named_obj = obj_type(name=name, **attributes)
      objects.append(named_obj)
    return objects
Exemplo n.º 11
0
def conv_fn_str_to_obj(fn_tup, tok, sim_funcs):
    d_orig = {}
    d_orig.update(tok)
    d_orig.update(sim_funcs)
    d_ret_list = []
    for f in fn_tup:
        d_ret = {}
        name = f[0]
        attr1 = f[1]
        attr2 = f[2]
        tok_1 = f[3]
        tok_2 = f[4]
        simfunction = f[5]
        # exec(f[6] in d_orig)
        six.exec_(f[6], d_orig)
        d_ret['function'] = d_orig[name]
        d_ret['feature_name'] = name
        d_ret['left_attribute'] = attr1
        d_ret['right_attribute'] = attr2
        d_ret['left_attr_tokenizer'] = tok_1
        d_ret['right_attr_tokenizer'] = tok_2
        d_ret['simfunction'] = simfunction
        d_ret['function_source'] = f[6]

        d_ret_list.append(d_ret)
    return d_ret_list
Exemplo n.º 12
0
def _set_source(func, new_source):
    # Fetch the actual function we are changing
    real_func = _get_real_func(func)

    # Figure out any future headers that may be required
    feature_flags = real_func.__code__.co_flags & FEATURE_MASK

    # Compile and retrieve the new Code object
    localz = {}
    new_code = compile(
        new_source,
        '<patchy>',
        'exec',
        flags=feature_flags,
        dont_inherit=True
    )

    six.exec_(new_code, func.__globals__, localz)
    new_func = localz[func.__name__]

    # Figure out how to get the Code object
    if isinstance(new_func, (classmethod, staticmethod)):
        new_code = new_func.__func__.__code__
    else:
        new_code = new_func.__code__

    # Put the new Code object in place
    real_func.__code__ = new_code
    # Store the modified source. This used to be attached to the function but
    # that is a bit naughty
    _source_map[real_func] = new_source
Exemplo n.º 13
0
 def handle(self, *args, **options):
     imports = options['imports']
     query = options['query']
     verbose = options['verbose']
     assert imports, 'No imports specified.'
     assert query, 'No query specified.'
     for imp in imports.strip().split('|'):
         imp_parts = tuple(imp.split(','))
         if len(imp_parts) == 1:
             cmd = ('import %s' % imp_parts)
         elif len(imp_parts) == 2:
             cmd = ('from %s import %s' % imp_parts)
         elif len(imp_parts) == 3:
             cmd = ('from %s import %s as %s' % imp_parts)
         else:
             raise Exception('Invalid import: %s' % (imp,))
         if verbose:
             print(cmd)
         six.exec_(cmd)
     if verbose:
         print(query)
     q = eval(query, globals(), locals())
     
     job = get_current_job()
     if job:
         job.monitor_records = q.count()
         job.save()
     
     if q.count():
         print('%i records require attention.' % (q.count(),), file=sys.stderr)
     else:
         print('%i records require attention.' % (q.count(),), file=sys.stdout)
Exemplo n.º 14
0
def execfile_(filepath, _globals, open=open):
    from sphinx.util.osutil import fs_encoding
    # get config source -- 'b' is a no-op under 2.x, while 'U' is
    # ignored under 3.x (but 3.x compile() accepts \r\n newlines)
    f = open(filepath, 'rbU')
    try:
        source = f.read()
    finally:
        f.close()

    # py26 accept only LF eol instead of CRLF
    if sys.version_info[:2] == (2, 6):
        source = source.replace(b'\r\n', b'\n')

    # compile to a code object, handle syntax errors
    filepath_enc = filepath.encode(fs_encoding)
    try:
        code = compile(source, filepath_enc, 'exec')
    except SyntaxError:
        if convert_with_2to3:
            # maybe the file uses 2.x syntax; try to refactor to
            # 3.x syntax using 2to3
            source = convert_with_2to3(filepath)
            code = compile(source, filepath_enc, 'exec')
        else:
            raise
    exec_(code, _globals)
Exemplo n.º 15
0
def get_feature_fn(feat_str, tok, sim):
    if not isinstance(feat_str, six.string_types):
        logger.error('Input feature string is not of type string')
        raise AssertionError('Input feature string is not of type string')

    if not isinstance(tok, dict):
        logger.error('Input tok is not of type dict')
        raise AssertionError('Input tok. is not of type dict')

    if not isinstance(sim, dict):
        logger.error('Input sim is not of type dict')
        raise AssertionError('Input sim. is not of type dict')
    temp = {}
    # update sim
    if len(sim) > 0:
        temp.update(sim)
    if len(tok) > 0:
        temp.update(tok)
    fn = 'def fn(ltuple, rtuple):\n'
    fn += '    '
    fn += 'return ' + feat_str
    d = parse_feat_str(feat_str, tok, sim)
    # six.exec_(fn, temp, temp)
    six.exec_(fn, temp)
    d['function'] = temp['fn']
    d['function_source'] = fn
    return d
Exemplo n.º 16
0
    def __init__(self, pyfilename, **kwargs):
        """Loads configuration from a python source file. Any variables
        defined in that file will be interpreted as configuration
        keys. The class ``Subsection`` is automatically imported into
        the context when the file is executed, and represents a
        subsection of the configuration. Any attribute set on such an
        object is treated as a configuration parameter on that
        subsection.

        .. note::

           The python source file is loaded and interpreted once, when
           creating the PyFile object. If a value is set by
           eg. calling a function, that function will only be called
           at load time, not when accessing the parameter.

        :param pyfile: The name of a file containing valid python code.
        :type pyfile: str

        """
        super(PyFile, self).__init__(**kwargs)
        self.source = Subsection()
        if pyfilename:
            with open(pyfilename) as fp:
                pycode = compile(fp.read(), pyfilename, 'exec')
            six.exec_(pycode, globals(), self.source)
        elif kwargs.get('dict'):
            self.source = kwargs['dict']
Exemplo n.º 17
0
    def load_module(self, fullname):
        if self.mod is None:
            mod = self.mod = imp.new_module(self.name)
        else:
            mod = self.mod

        log_buffer = []
        mod.logging = mod.logger = logging.Logger(self.name)
        handler = SaveLogHandler(log_buffer)
        handler.setFormatter(LogFormatter(color=False))
        mod.logger.addHandler(handler)
        mod.log_buffer = log_buffer
        mod.__file__ = '<%s>' % self.name
        mod.__loader__ = self
        mod.__project__ = self.project
        mod.__package__ = ''

        code = self.get_code(fullname)
        six.exec_(code, mod.__dict__)
        linecache.clearcache()

        if '__handler_cls__' not in mod.__dict__:
            BaseHandler = mod.__dict__.get('BaseHandler', base_handler.BaseHandler)
            for each in list(six.itervalues(mod.__dict__)):
                if inspect.isclass(each) and each is not BaseHandler \
                        and issubclass(each, BaseHandler):
                    mod.__dict__['__handler_cls__'] = each

        return mod
Exemplo n.º 18
0
def update_wrapper(wrapper, wrapped):
    """Preserves the argspec for a wrapped function so that testing tools such
    as pytest can continue to use their fixture injection.

    :param wrapper: the wrapper function to update
    :param wrapped: the decorated test function
    """
    newargspec = inspect.getargspec(wrapped)
    need_self = len(newargspec[0]) > 0 and newargspec[0][0] == 'self'

    if need_self:
        newargspec = (newargspec[0],) + newargspec[1:]

    signature = inspect.formatargspec(*newargspec)[1:-1]
    ctx = {'signature': signature, 'tgt_func': 'tgt_func'}

    evaldict = {'tgt_func': wrapper}

    six.exec_(_wrapper_template % ctx, evaldict)

    wrapper = evaldict['_wrapper_']
    if hasattr(wrapped, 'func_defaults'):
        wrapper.func_defaults = wrapped.func_defaults
    _update_wrapper(wrapper, wrapped)
    return wrapper
Exemplo n.º 19
0
def execfile_(filepath, _globals):
    # get config source -- 'b' is a no-op under 2.x, while 'U' is
    # ignored under 3.x (but 3.x compile() accepts \r\n newlines)
    f = open(filepath, 'rbU')
    try:
        source = f.read()
    finally:
        f.close()

    # py25,py26,py31 accept only LF eol instead of CRLF
    if sys.version_info[:2] in ((2, 5), (2, 6), (3, 1)):
        source = source.replace(b('\r\n'), b('\n'))

    # compile to a code object, handle syntax errors
    filepath_enc = filepath.encode(FS_ENCODING)
    try:
        code = compile(source, filepath_enc, 'exec')
    except SyntaxError:
        if convert_with_2to3:
            # maybe the file uses 2.x syntax; try to refactor to
            # 3.x syntax using 2to3
            source = convert_with_2to3(filepath)
            code = compile(source, filepath_enc, 'exec')
        else:
            raise

    exec_(code, _globals)
Exemplo n.º 20
0
def __profile__(filename, code, globals_, profiler_factory,
                pickle_protocol=remote.PICKLE_PROTOCOL, dump_filename=None,
                mono=False):
    frame = sys._getframe()
    profiler = profiler_factory(base_frame=frame, base_code=code)
    profiler.start()
    try:
        exec_(code, globals_)
    except:
        # don't profile print_exc().
        profiler.stop()
        traceback.print_exc()
    else:
        profiler.stop()
    # discard this __profile__ function from the result.
    profiler.stats.discard_child(frame.f_code)
    if dump_filename is None:
        try:
            profiler.run_viewer(get_title(filename), mono=mono)
        except KeyboardInterrupt:
            pass
    else:
        result = profiler.result()
        with open(dump_filename, 'wb') as f:
            pickle.dump((profiler.__class__, result), f, pickle_protocol)
        click.echo('To view statistics:')
        click.echo('  $ profiling view ', nl=False)
        click.secho(dump_filename, underline=True)
Exemplo n.º 21
0
def remote_profile(script, argv, profiler_factory, interval, spawn, signum,
                   pickle_protocol, endpoint, verbose):
    """Launch a server to profile continuously.  The default endpoint is
    127.0.0.1:8912.
    """
    filename, code, globals_ = script
    sys.argv[:] = [filename] + list(argv)
    # create listener.
    listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    listener.bind(endpoint)
    listener.listen(1)
    # be verbose or quiet.
    if verbose:
        log = lambda x: click.echo(click.style('> ', fg='cyan') + x)
        bound_addr = listener.getsockname()
        log('Listening on {0}:{1} for profiling...'.format(*bound_addr))
    else:
        log = noop
    # start profiling server.
    frame = sys._getframe()
    profiler = profiler_factory(base_frame=frame, base_code=code)
    profiler_trigger = BackgroundProfiler(profiler, signum)
    profiler_trigger.prepare()
    server_args = (interval, log, pickle_protocol)
    server = SelectProfilingServer(listener, profiler_trigger, *server_args)
    spawn(server.serve_forever)
    # exec the script.
    try:
        exec_(code, globals_)
    except KeyboardInterrupt:
        pass
Exemplo n.º 22
0
    def OnSetPropertyValues(self,event):
        try:
            d = self.pg.GetPropertyValues(inc_attributes=True)

            ss = []
            for k,v in d.items():
                v = repr(v)
                if not v or v[0] != '<':
                    if k.startswith('@'):
                        ss.append('setattr(obj, "%s", %s)'%(k,v))
                    else:
                        ss.append('obj.%s = %s'%(k,v))

            with MemoDialog(self,
                    "Enter Content for Object Used in SetPropertyValues",
                    '\n'.join(ss)) as dlg:  # default_object_content1

                if dlg.ShowModal() == wx.ID_OK:
                    import datetime
                    sandbox = {'obj':ValueObject(),
                               'wx':wx,
                               'datetime':datetime}
                    exec_(dlg.tc.GetValue(), sandbox)
                    t_start = time.time()
                    #print(sandbox['obj'].__dict__)
                    self.pg.SetPropertyValues(sandbox['obj'])
                    t_end = time.time()
                    self.log.write('SetPropertyValues finished in %.0fms\n' %
                                   ((t_end-t_start)*1000.0))
        except:
            import traceback
            traceback.print_exc()
Exemplo n.º 23
0
def run_rc_file(editor, rc_file):
    """
    Run rc file.
    """
    assert isinstance(editor, Editor)
    assert isinstance(rc_file, six.text_type)

    # Expand tildes.
    rc_file = os.path.expanduser(rc_file)

    # Check whether this file exists.
    if not os.path.exists(rc_file):
        print('Impossible to read %r' % rc_file)
        _press_enter_to_continue()
        return

    # Run the rc file in an empty namespace.
    try:
        namespace = {}

        with open(rc_file, 'r') as f:
            code = compile(f.read(), rc_file, 'exec')
            six.exec_(code, namespace, namespace)

        # Now we should have a 'configure' method in this namespace. We call this
        # method with editor as an argument.
        if 'configure' in namespace:
            namespace['configure'](editor)

    except Exception as e:
        # Handle possible exceptions in rc file.
        traceback.print_exc()
        _press_enter_to_continue()
Exemplo n.º 24
0
def run_strategy(source_code, strategy_filename, start_date, end_date, init_cash, data_bundle_path, show_progress):
    scope = {
        "logger": user_log,
        "print": user_print,
    }
    scope.update({export_name: getattr(api, export_name) for export_name in api.__all__})
    code = compile(source_code, strategy_filename, 'exec')
    exec_(code, scope)

    try:
        data_proxy = LocalDataProxy(data_bundle_path)
    except FileNotFoundError:
        print_("data bundle might crash. Run `%s update_bundle` to redownload data bundle." % sys.argv[0])
        sys.exit()

    trading_cal = data_proxy.get_trading_dates(start_date, end_date)
    Scheduler.set_trading_dates(data_proxy.get_trading_dates(start_date, datetime.date.today()))
    trading_params = TradingParams(trading_cal, start_date=start_date.date(), end_date=end_date.date(),
                                   init_cash=init_cash, show_progress=show_progress)

    executor = StrategyExecutor(
        init=scope.get("init", dummy_func),
        before_trading=scope.get("before_trading", dummy_func),
        handle_bar=scope.get("handle_bar", dummy_func),

        trading_params=trading_params,
        data_proxy=data_proxy,
    )

    results_df = executor.execute()

    return results_df
Exemplo n.º 25
0
    def __load_from_path(cls, conf, path):
        if isdir(path):
            conf.config_folder = path
            files = sorted(os.listdir(path))

            for file in files:
                if file.endswith('.conf'):
                    filepath = path + os.sep + file
                    conf = Config.__load_from_path(conf, filepath)

            return conf

        with open(path) as config_file:
            name = 'configuration'
            code = config_file.read()
            module = imp.new_module(name)

            six.exec_(code, module.__dict__)

            conf.config_file = path

            for name, value in module.__dict__.items():
                if name.upper() == name:
                    conf._items[name] = value
                    setattr(conf, name, value)

            return conf
Exemplo n.º 26
0
def compile_strategy(source_code, strategy, scope):
    try:
        code = compile(source_code, strategy, 'exec')
        six.exec_(code, scope)
        return scope
    except Exception as e:
        exc_type, exc_val, exc_tb = sys.exc_info()
        exc_val = patch_user_exc(exc_val, force=True)
        try:
            msg = str(exc_val)
        except Exception as e1:
            msg = ""
            six.print_(e1)

        error = CustomError()
        error.set_msg(msg)
        error.set_exc(exc_type, exc_val, exc_tb)
        stackinfos = list(traceback.extract_tb(exc_tb))

        if isinstance(e, (SyntaxError, IndentationError)):
            error.add_stack_info(exc_val.filename, exc_val.lineno, "", exc_val.text)
        else:
            for item in stackinfos:
                filename, lineno, func_name, code = item
                if strategy == filename:
                    error.add_stack_info(*item)
            # avoid empty stack
            if error.stacks_length == 0:
                error.add_stack_info(*item)

        raise CustomException(error)
Exemplo n.º 27
0
def execute_python_code(code):
    try:
        c = compile(code, '<string>', 'exec')
    except Exception:
        print('On execute_python_code with code=\n{}'.format(code))
        raise
    six.exec_(c)
Exemplo n.º 28
0
    def verify(cls, path):
        if path is None:
            return []

        if not exists(path):
            raise ConfigurationError('Configuration file not found at path %s' % path)

        with open(path) as config_file:
            name = 'configuration'
            code = config_file.read()
            module = imp.new_module(name)

            six.exec_(code, module.__dict__)

            conf = cls(defaults=[])

            for name, value in module.__dict__.items():
                if name.upper() == name:
                    setattr(conf, name, value)

            not_found = []

            for key, value in cls.class_defaults.items():
                if key not in conf.__dict__:
                    not_found.append((key, value))

            return not_found
Exemplo n.º 29
0
    def _test(self, modname):
        self.modname = modname
        six.exec_("import %s" % modname, {})
        self.module = sys.modules[modname]

        self.check_all()

        self.__implements__ = getattr(self.module, '__implements__', None)
        self.__imports__ = getattr(self.module, '__imports__', [])
        self.__extensions__ = getattr(self.module, '__extensions__', [])

        self.stdlib_name = MAPPING.get(modname)
        self.stdlib_module = None

        if self.stdlib_name is not None:
            try:
                self.stdlib_module = __import__(self.stdlib_name)
            except ImportError:
                pass

        self.check_implements_presence_justified()

        if self.stdlib_module is None:
            return

        # use __all__ as __implements__
        if self.__implements__ is None:
            self.__implements__ = sorted(self.module.__all__)

        self.set_stdlib_all()
        self.check_implements_subset_of_stdlib_all()
        self.check_implements_actually_implements()
        self.check_imports_actually_imports()
        self.check_extensions_actually_extend()
        self.check_completeness()
Exemplo n.º 30
0
def main(script_name, stored_testinfo, snapshots_dir):
    # do interceptions and other setup task here
    # ...
    sys.path.insert(0, os.getcwd())
    module_name = script_name[:script_name.rfind('.py')]
    print('module name:', module_name)
    s = "import %s as script_module"%module_name
    six.exec_(s, globals())

    if stored_testinfo != script_module.testinfo:
        sys.stderr.write("Es01 - received testinfo doesn't match script testinfo. (db outdated?)\n")
        sys.exit(1)

    screen_sampler, diagnostic = st.ScreenSampler.sampler(stored_testinfo,
                                    script_name,
                                    fn_quit=quit_pyglet_app,
                                    fn_take_snapshot=take_snapshot_cocos_app,
                                    snapshots_dir=snapshots_dir)
    assert diagnostic == ''
    clock = cc.get_autotest_clock(screen_sampler)
    cocos.custom_clocks.set_app_clock(clock)
    
    set_init_interceptor()
    #sys.stderr.write('\nafter interceptor')
    if hasattr(script_module, 'autotest'):
        # allows the script to know if running through autotest
        script_module.autotest = 1
    script_module.main()
Exemplo n.º 31
0
def _exec(cmd):
    '''Prints the time it takes to execute 'cmd'.'''
    if os.environ.get('X', None):
        start = time.time()
        exec_(cmd)
        _print('(%.3fs)' % (time.time() - start))
Exemplo n.º 32
0
def _build_preprocessed_function(func, processors, args_defaults, varargs,
                                 varkw):
    """
    Build a preprocessed function with the same signature as `func`.

    Uses `exec` internally to build a function that actually has the same
    signature as `func.
    """
    format_kwargs = {'func_name': func.__name__}

    def mangle(name):
        return 'a' + uuid4().hex + name

    format_kwargs['mangled_func'] = mangled_funcname = mangle(func.__name__)

    def make_processor_assignment(arg, processor_name):
        template = "{arg} = {processor}({func}, '{arg}', {arg})"
        return template.format(
            arg=arg,
            processor=processor_name,
            func=mangled_funcname,
        )

    exec_globals = {mangled_funcname: func, 'wraps': wraps}
    defaults_seen = 0
    default_name_template = 'a' + uuid4().hex + '_%d'
    signature = []
    call_args = []
    assignments = []
    star_map = {
        varargs: '*',
        varkw: '**',
    }

    def name_as_arg(arg):
        return star_map.get(arg, '') + arg

    for arg, default in args_defaults:
        if default is NO_DEFAULT:
            signature.append(name_as_arg(arg))
        else:
            default_name = default_name_template % defaults_seen
            exec_globals[default_name] = default
            signature.append('='.join([name_as_arg(arg), default_name]))
            defaults_seen += 1

        if arg in processors:
            procname = mangle('_processor_' + arg)
            exec_globals[procname] = processors[arg]
            assignments.append(make_processor_assignment(arg, procname))

        call_args.append(name_as_arg(arg))

    exec_str = dedent("""\
        @wraps({wrapped_funcname})
        def {func_name}({signature}):
            {assignments}
            return {wrapped_funcname}({call_args})
        """).format(
        func_name=func.__name__,
        signature=', '.join(signature),
        assignments='\n    '.join(assignments),
        wrapped_funcname=mangled_funcname,
        call_args=', '.join(call_args),
    )
    compiled = compile(
        exec_str,
        func.__code__.co_filename,
        mode='exec',
    )

    exec_locals = {}
    exec_(compiled, exec_globals, exec_locals)
    new_func = exec_locals[func.__name__]

    code = new_func.__code__
    args = {
        attr: getattr(code, attr)
        for attr in dir(code) if attr.startswith('co_')
    }
    # Copy the firstlineno out of the underlying function so that exceptions
    # get raised with the correct traceback.
    # This also makes dynamic source inspection (like IPython `??` operator)
    # work as intended.
    try:
        # Try to get the pycode object from the underlying function.
        original_code = func.__code__
    except AttributeError:
        try:
            # The underlying callable was not a function, try to grab the
            # `__func__.__code__` which exists on method objects.
            original_code = func.__func__.__code__
        except AttributeError:
            # The underlying callable does not have a `__code__`. There is
            # nothing for us to correct.
            return new_func

    args['co_firstlineno'] = original_code.co_firstlineno
    new_func.__code__ = CodeType(*map(getitem(args), _code_argorder))
    return new_func
Exemplo n.º 33
0
def main():
    # TODO: more options, refactor this into somewhere shared
    # between tornado.autoreload and auto2to3
    parser = argparse.ArgumentParser(description="Plop: Python Low-Overhead Profiler",
                                     prog="python -m plop.collector",
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--format", "-f", help="Output format",
                        choices=["plop", "flamegraph"], default="plop")
    parser.add_argument("--module", "-m", help="Execute target as a module",
                        action="store_const", const=True, default=False)
    parser.add_argument("--mode", help="Interval timer mode to use, see `man 2 setitimer`",
                        choices=["prof", "real", "virtual"], default="prof")
    parser.add_argument("--interval", help="Timer interval in seconds", default=0.01, type=float)
    parser.add_argument("--duration", help="Profiling duration in seconds", default=3600,
                        type=int)
    parser.add_argument("--max-stacks", help=("Number of most frequent stacks to store."
                        " Ignored for Flamegraph output."), type=int, default=50)
    parser.add_argument("target", help="Module or script to run")
    parser.add_argument("arguments", nargs=argparse.REMAINDER,
                        help="Pass-through arguments for the profiled application")
    args = parser.parse_args()
    sys.argv = [args.target] + args.arguments

    if args.format == "flamegraph":
        extension = "flame"
        formatter = FlamegraphFormatter()
    elif args.format == "plop":
        extension = "plop"
        formatter = PlopFormatter(max_stacks=args.max_stacks)
    else:
        sys.stderr.write("Unhandled output format: %s" % args.format)
        sys.stderr.flush()
        sys.exit(1)

    try:  # This is equivalent to os.makedirs('profiles', exist_ok=True)
        os.mkdir('profiles')
    except OSError as exc:
        if exc.errno != errno.EEXIST:
            raise
    filename = os.path.join(
        'profiles',
        '%s-%s.%s' % (os.path.basename(args.target),
                      time.strftime('%Y%m%d-%H%M-%S'),
                      extension))

    collector = Collector(mode=args.mode, interval=args.interval)
    collector.start(duration=args.duration)
    exit_code = 0
    try:
        if args.module:
            import runpy
            runpy.run_module(args.target, run_name="__main__", alter_sys=True)
        else:
            with open(args.target) as f:
                # Execute the script in our namespace instead of creating
                # a new one so that something that tries to import __main__
                # (e.g. the unittest module) will see names defined in the
                # script instead of just those defined in this module.
                global __file__
                __file__ = args.target
                # If __package__ is defined, imports may be incorrectly
                # interpreted as relative to this module.
                global __package__
                del __package__
                six.exec_(f.read(), globals(), globals())
    except SystemExit as e:
        exit_code = e.code
    collector.stop()
    if collector.samples_taken:
        formatter.store(collector, filename)
        print("profile output saved to %s" % filename)
        overhead = float(collector.sample_time) / collector.samples_taken
        print("overhead was %s per sample (%s%%)" % (
            overhead, overhead / collector.interval))
    else:
        print("no samples collected; program was too fast")
    sys.exit(exit_code)
Exemplo n.º 34
0
 def byte_EXEC_STMT(self):
     stmt, globs, locs = self.popn(3)
     six.exec_(stmt, globs, locs)
Exemplo n.º 35
0
    def compile(self, c1, c2, c3, c4):
        """
        Compile native python rule-matching function
        and install it as .match() instance method
        """
        def pyq(s):
            return s.replace("\\", "\\\\").replace('"', '\\"')

        e_vars_used = c2 or c3 or c4
        c = []
        if c1:
            cc = " and ".join(["(%s)" % x for x in c1])
            c += ["if not (%s):" % cc]
            c += ["    return None"]
        if e_vars_used:
            c += ["e_vars = {}"]
        if c2:
            for k in c2:
                c += ["# %s" % self.rxp[c2[k]]]
                c += ["match = self.rx_%s.search(vars['%s'])" % (c2[k], k)]
                c += ["if not match:"]
                c += ["    return None"]
                c += ["e_vars.update(match.groupdict())"]
        if c3:
            for rx, v in c3:
                c += ["found = False"]
                c += ["for k in vars:"]
                c += ["    # %s" % self.rxp[rx]]
                c += ["    match = self.rx_%s.search(k)" % rx]
                c += ["    if match:"]
                c += ["        if vars[k] == '%s':" % v]
                c += ["            e_vars.update(match.groupdict())"]
                c += ["            found = True"]
                c += ["            break"]
                c += ["        else:"]
                c += ["            return None"]
                c += ["if not found:"]
                c += ["    return None"]
        if c4:
            for rxk, rxv in c4:
                c += ["found = False"]
                c += ["for k in vars:"]
                c += ["    # %s" % self.rxp[rxk]]
                c += ["    match_k = self.rx_%s.search(k)" % rxk]
                c += ["    if match_k:"]
                c += ["        # %s" % self.rxp[rxv]]
                c += ["        match_v = self.rx_%s.search(vars[k])" % rxv]
                c += ["        if match_v:"]
                c += ["            e_vars.update(match_k.groupdict())"]
                c += ["            e_vars.update(match_v.groupdict())"]
                c += ["            found = True"]
                c += ["            break"]
                c += ["        else:"]
                c += ["            return None"]
                c += ["if not found:"]
                c += ["    return None"]
        # Vars binding
        if self.vars:
            has_expressions = any(v for v in six.itervalues(self.vars)
                                  if not isinstance(v, six.string_types))
            if has_expressions:
                # Calculate vars context
                c += ["var_context = {'event': event}"]
                c += ["var_context.update(e_vars)"]
            for k, v in six.iteritems(self.vars):
                if isinstance(v, six.string_types):
                    c += ['e_vars["%s"] = "%s"' % (k, pyq(v))]
                else:
                    c += [
                        'e_vars["%s"] = eval(self.vars["%s"], {}, var_context)'
                        % (k, k)
                    ]
        if e_vars_used:
            # c += ["return self.fixup(e_vars)"]
            for name in self.fixups:
                r = name.split("__")
                if len(r) == 2:
                    if r[1] in ("ifindex", ):
                        # call fixup with managed object
                        c += [
                            'e_vars["%s"] = self.fixup_%s(event.managed_object, fm_unescape(e_vars["%s"]))'
                            % (r[0], r[1], name)
                        ]
                    else:
                        c += [
                            'e_vars["%s"] = self.fixup_%s(fm_unescape(e_vars["%s"]))'
                            % (r[0], r[1], name)
                        ]
                else:
                    c += [
                        'args = [%s, fm_unescape(e_vars["%s"])]' %
                        (", ".join(['"%s"' % x for x in r[2:]]), name)
                    ]
                    c += ['e_vars["%s"] = self.fixup_%s(*args)' % (r[0], r[1])]
                c += ['del e_vars["%s"]' % name]
            c += ["return e_vars"]
        else:
            c += ["return {}"]
        c = ["    " + l for l in c]

        cc = ["# %s" % self.name]
        cc += ["def match(self, event, vars):"]
        cc += c
        cc += ["rule.match = new.instancemethod(match, rule, rule.__class__)"]
        self.code = "\n".join(cc)
        code = compile(self.code, "<string>", "exec")
        six.exec_(
            code, {
                "rule": self,
                "new": new,
                "logging": logging,
                "fm_unescape": fm_unescape
            })
Exemplo n.º 36
0
def run(user_ns=None):
    a = docopt.docopt(__doc__)

    vi_mode = bool(a['--vi'])
    config_dir = os.path.expanduser(a['--config-dir']
                                    or os.path.join('~', '.ptpython'))

    # Create config directory.
    if not os.path.isdir(config_dir) and not os.path.islink(config_dir):
        os.mkdir(config_dir)

    # If IPython is not available, show message and exit here with error status
    # code.
    try:
        import IPython
    except ImportError:
        print(
            'IPython not found. Please install IPython (pip install ipython).')
        sys.exit(1)
    else:
        from ptpython.ipython import embed
        from ptpython.repl import run_config, enable_deprecation_warnings

    # Add the current directory to `sys.path`.
    if sys.path[0] != '':
        sys.path.insert(0, '')

    # When a file has been given, run that, otherwise start the shell.
    if a['<arg>'] and not a['--interactive']:
        sys.argv = a['<arg>']
        six.exec_(
            compile(open(a['<arg>'][0], "rb").read(), a['<arg>'][0], 'exec'))
    else:
        enable_deprecation_warnings()

        # Create an empty namespace for this interactive shell. (If we don't do
        # that, all the variables from this function will become available in
        # the IPython shell.)
        if user_ns is None:
            user_ns = {}

        # Startup path
        startup_paths = []
        if 'PYTHONSTARTUP' in os.environ:
            startup_paths.append(os.environ['PYTHONSTARTUP'])

        # --interactive
        if a['--interactive']:
            startup_paths.append(a['--interactive'])
            sys.argv = [a['--interactive']] + a['<arg>']

        # exec scripts from startup paths
        for path in startup_paths:
            if os.path.exists(path):
                with open(path, 'r') as f:
                    code = compile(f.read(), path, 'exec')
                    six.exec_(code, user_ns, user_ns)
            else:
                print('File not found: {}\n\n'.format(path))
                sys.exit(1)

        # Apply config file
        def configure(repl):
            path = os.path.join(config_dir, 'config.py')
            if os.path.exists(path):
                run_config(repl, path)

        # Run interactive shell.
        embed(vi_mode=vi_mode,
              history_filename=os.path.join(config_dir, 'history'),
              configure=configure,
              user_ns=user_ns,
              title='IPython REPL (ptipython)')
Exemplo n.º 37
0
def run_code(code,
             code_path=None,
             ns=None,
             function_name=None,
             workdir=None,
             pre_code=None,
             raises=None):
    """
    Run `code` from file at `code_path` in namespace `ns`

    Parameters
    ----------
    code : str
        code to run.
    code_path : str
        Filename containing the code.
    ns : None or dict, optional
        Python namespace in which to execute code.  If None, make fresh
        namespace.
    function_name : None or str, optional
        If non-empty string, name of function to execute after executing
        `code`.
    workdir : None or str, optional
        Working directory in which to run code.  Defaults to current working
        directory.
    pre_code : None or str, optional
        Any code to run before `code`.
    raises : None or Exception class
        An exception that the run code should raise.

    Returns
    -------
    ns : dict
        Namespace, filled from execution of `code`.
    """
    # Change the working directory to the directory of the example, so
    # it can get at its data files, if any.  Add its path to sys.path
    # so it can import any helper modules sitting beside it.
    if six.PY2:
        pwd = os.getcwdu()
    else:
        pwd = os.getcwd()
    old_sys_path = list(sys.path)
    workdir = os.getcwd() if workdir is None else workdir
    os.chdir(workdir)
    sys.path.insert(0, workdir)

    # Reset sys.argv
    old_sys_argv = sys.argv
    sys.argv = [code_path]

    # Redirect stdout
    stdout = sys.stdout
    sys.stdout = io.StringIO() if six.PY3 else io.BytesIO()

    # Assign a do-nothing print function to the namespace.  There
    # doesn't seem to be any other way to provide a way to (not) print
    # that works correctly across Python 2 and 3.
    def _dummy_print(*arg, **kwarg):
        pass

    ns = {} if ns is None else ns
    try:
        try:
            code = unescape_doctest(code)
            if pre_code and not ns:
                six.exec_(six.text_type(pre_code), ns)
            ns['print'] = _dummy_print
            if "__main__" in code:
                six.exec_("__name__ = '__main__'", ns)
            code = remove_coding(code)
            if raises is None:
                six.exec_(code, ns)
            else:  # Code should raise exception
                try:
                    six.exec_(code, ns)
                except raises:
                    pass
            if function_name:
                six.exec_(function_name + "()", ns)
        except (Exception, SystemExit):
            raise PlotError(traceback.format_exc())
    finally:
        os.chdir(pwd)
        sys.argv = old_sys_argv
        sys.path[:] = old_sys_path
        sys.stdout = stdout
    return ns
Exemplo n.º 38
0
import sys
import re

import contextlib2 as contextlib
import fixtures
import linecache2 as linecache
import six
from six import b, text_type, u
try:
    from six import raise_from
except ImportError:
    # support raise_from on 3.x:
    # submitted to six: https://bitbucket.org/gutworth/six/issue/102/raise-foo-from-bar-is-a-syntax-error-on-27
    if sys.version_info[:2] > (3, 1):
        six.exec_("""def raise_from(value, from_value):
        raise value from from_value
    """)
    else:

        def raise_from(value, from_value):
            raise value


import unittest2 as unittest
import testtools
from testtools.matchers import DocTestMatches, Equals, MatchesAny

import traceback2 as traceback


@contextlib.contextmanager
Exemplo n.º 39
0
def raises(expected_exception, *args, **kwargs):
    r"""
    Assert that a code block/function call raises ``expected_exception``
    and raise a failure exception otherwise.

    :arg message: if specified, provides a custom failure message if the
        exception is not raised
    :arg match: if specified, asserts that the exception matches a text or regex

    This helper produces a ``ExceptionInfo()`` object (see below).

    You may use this function as a context manager::

        >>> with raises(ZeroDivisionError):
        ...    1/0

    .. versionchanged:: 2.10

    In the context manager form you may use the keyword argument
    ``message`` to specify a custom failure message::

        >>> with raises(ZeroDivisionError, message="Expecting ZeroDivisionError"):
        ...    pass
        Traceback (most recent call last):
          ...
        Failed: Expecting ZeroDivisionError

    .. note::

       When using ``pytest.raises`` as a context manager, it's worthwhile to
       note that normal context manager rules apply and that the exception
       raised *must* be the final line in the scope of the context manager.
       Lines of code after that, within the scope of the context manager will
       not be executed. For example::

           >>> value = 15
           >>> with raises(ValueError) as exc_info:
           ...     if value > 10:
           ...         raise ValueError("value must be <= 10")
           ...     assert exc_info.type == ValueError  # this will not execute

       Instead, the following approach must be taken (note the difference in
       scope)::

           >>> with raises(ValueError) as exc_info:
           ...     if value > 10:
           ...         raise ValueError("value must be <= 10")
           ...
           >>> assert exc_info.type == ValueError


    Since version ``3.1`` you can use the keyword argument ``match`` to assert that the
    exception matches a text or regex::

        >>> with raises(ValueError, match='must be 0 or None'):
        ...     raise ValueError("value must be 0 or None")

        >>> with raises(ValueError, match=r'must be \d+$'):
        ...     raise ValueError("value must be 42")

    **Legacy forms**

    The forms below are fully supported but are discouraged for new code because the
    context manager form is regarded as more readable and less error-prone.

    It is possible to specify a callable by passing a to-be-called lambda::

        >>> raises(ZeroDivisionError, lambda: 1/0)
        <ExceptionInfo ...>

    or you can specify an arbitrary callable with arguments::

        >>> def f(x): return 1/x
        ...
        >>> raises(ZeroDivisionError, f, 0)
        <ExceptionInfo ...>
        >>> raises(ZeroDivisionError, f, x=0)
        <ExceptionInfo ...>

    It is also possible to pass a string to be evaluated at runtime::

        >>> raises(ZeroDivisionError, "f(0)")
        <ExceptionInfo ...>

    The string will be evaluated using the same ``locals()`` and ``globals()``
    at the moment of the ``raises`` call.

    .. currentmodule:: _pytest._code

    Consult the API of ``excinfo`` objects: :class:`ExceptionInfo`.

    .. note::
        Similar to caught exception objects in Python, explicitly clearing
        local references to returned ``ExceptionInfo`` objects can
        help the Python interpreter speed up its garbage collection.

        Clearing those references breaks a reference cycle
        (``ExceptionInfo`` --> caught exception --> frame stack raising
        the exception --> current frame stack --> local variables -->
        ``ExceptionInfo``) which makes Python keep all objects referenced
        from that cycle (including all local variables in the current
        frame) alive until the next cyclic garbage collection run. See the
        official Python ``try`` statement documentation for more detailed
        information.

    """
    __tracebackhide__ = True
    for exc in filterfalse(isclass,
                           always_iterable(expected_exception, BASE_TYPE)):
        msg = ("exceptions must be old-style classes or"
               " derived from BaseException, not %s")
        raise TypeError(msg % type(exc))

    message = "DID NOT RAISE {}".format(expected_exception)
    match_expr = None

    if not args:
        if "message" in kwargs:
            message = kwargs.pop("message")
        if "match" in kwargs:
            match_expr = kwargs.pop("match")
        if kwargs:
            msg = "Unexpected keyword arguments passed to pytest.raises: "
            msg += ", ".join(kwargs.keys())
            raise TypeError(msg)
        return RaisesContext(expected_exception, message, match_expr)
    elif isinstance(args[0], str):
        code, = args
        assert isinstance(code, str)
        frame = sys._getframe(1)
        loc = frame.f_locals.copy()
        loc.update(kwargs)
        # print "raises frame scope: %r" % frame.f_locals
        try:
            code = _pytest._code.Source(code).compile()
            six.exec_(code, frame.f_globals, loc)
            # XXX didn't mean f_globals == f_locals something special?
            #     this is destroyed here ...
        except expected_exception:
            return _pytest._code.ExceptionInfo()
    else:
        func = args[0]
        try:
            func(*args[1:], **kwargs)
        except expected_exception:
            return _pytest._code.ExceptionInfo()
    fail(message)
Exemplo n.º 40
0
    __safe_for_unpickling__ = True

    def __call__(self, *args, **kw):
        ffc = getattr(__import__(self.module, self.globals), self.object)
        ffc.description = _description
        return apply(ffc, args, kw)


def _description(self):
    return 'ForceFields.' + self.__class__.__name__ + self.arguments


ff_list = resource_stream(__name__, 'force_fields').readlines()
for line in ff_list:
    if six.PY3:
        line = line.decode()
    line = (line).split()
    six.exec_(line[0] + "=_ForceFieldLoader(line[1], line[2])")

try:
    default_energy_threads = int(os.environ['MMTK_ENERGY_THREADS'])
except KeyError:
    default_energy_threads = 1

del os
del string
del sys
del ff_list
del line
Exemplo n.º 41
0
 def f():
     l = []
     six.exec_("l.append(1)")
     assert l == [1]
Exemplo n.º 42
0
def run_application(application,
                    patch_stdout=False,
                    return_asyncio_coroutine=False,
                    true_color=False,
                    refresh_interval=0,
                    eventloop=None):
    """
    Run a prompt toolkit application.

    :param patch_stdout: Replace ``sys.stdout`` by a proxy that ensures that
            print statements from other threads won't destroy the prompt. (They
            will be printed above the prompt instead.)
    :param return_asyncio_coroutine: When True, return a asyncio coroutine. (Python >3.3)
    :param true_color: When True, use 24bit colors instead of 256 colors.
    :param refresh_interval: (number; in seconds) When given, refresh the UI
        every so many seconds.
    """
    assert isinstance(application, Application)

    if return_asyncio_coroutine:
        eventloop = create_asyncio_eventloop()
    else:
        eventloop = eventloop or create_eventloop()

    # Create CommandLineInterface.
    cli = CommandLineInterface(application=application,
                               eventloop=eventloop,
                               output=create_output(true_color=true_color))

    # Set up refresh interval.
    if refresh_interval:
        done = [False]

        def start_refresh_loop(cli):
            def run():
                while not done[0]:
                    time.sleep(refresh_interval)
                    cli.request_redraw()

            t = threading.Thread(target=run)
            t.daemon = True
            t.start()

        def stop_refresh_loop(cli):
            done[0] = True

        cli.on_start += start_refresh_loop
        cli.on_stop += stop_refresh_loop

    # Replace stdout.
    patch_context = cli.patch_stdout_context(
        raw=True) if patch_stdout else DummyContext()

    # Read input and return it.
    if return_asyncio_coroutine:
        # Create an asyncio coroutine and call it.
        exec_context = {
            'patch_context': patch_context,
            'cli': cli,
            'Document': Document
        }
        exec_(
            textwrap.dedent('''
        def prompt_coro():
            # Inline import, because it slows down startup when asyncio is not
            # needed.
            import asyncio

            @asyncio.coroutine
            def run():
                with patch_context:
                    result = yield from cli.run_async()

                if isinstance(result, Document):  # Backwards-compatibility.
                    return result.text
                return result
            return run()
        '''), exec_context)

        return exec_context['prompt_coro']()
    else:
        try:
            with patch_context:
                result = cli.run()

            if isinstance(result, Document):  # Backwards-compatibility.
                return result.text
            return result
        finally:
            eventloop.close()
Exemplo n.º 43
0
def pymain(argv):
    import sys, code, runpy, six
    from os.path import dirname
    from six.moves import input as raw_input

    # interactive console
    if not argv:
        sys.argv = ['']
        sys.path.insert(0, '')  # cwd

        # like code.interact() but with overridden console.raw_input _and_
        # readline imported (code.interact mutually excludes those two).
        try:
            import readline  # enable interactive editing
        except ImportError:
            pass

        console = code.InteractiveConsole()

        def _(prompt):
            # python behaviour: don't print '>>>' if stdin is not a tty
            # (builtin raw_input always prints prompt)
            if not sys.stdin.isatty():
                prompt = ''
            return raw_input(prompt)

        console.raw_input = _

        console.interact()
        return

    # -c command
    if argv[0] == '-c':
        sys.argv = argv[0:1] + argv[2:]  # python leaves '-c' as argv[0]
        sys.path.insert(0, '')  # cwd

        # exec with the same globals `python -c ...` does
        g = {'__name__': '__main__', '__doc__': None, '__package__': None}
        six.exec_(argv[1], g)

    # -m module
    elif argv[0] == '-m':
        # search sys.path for module and run corresponding .py file as script
        sys.argv = argv[1:]
        sys.path.insert(0, '')  # cwd
        runpy.run_module(sys.argv[0],
                         init_globals={'__doc__': None},
                         run_name='__main__',
                         alter_sys=True)

    elif argv[0].startswith('-'):
        print("unknown option: '%s'" % argv[0], file=sys.stderr)
        sys.exit(2)

    # file
    else:
        sys.argv = argv
        filepath = argv[0]
        sys.path.insert(0, dirname(filepath))

        # exec with same globals `python file.py` does
        # XXX use runpy.run_path() instead?
        g = {
            '__name__': '__main__',
            '__file__': filepath,
            '__doc__': None,
            '__package__': None
        }
        _execfile(filepath, g)

    return
Exemplo n.º 44
0
def run(statements, expression, run_globals, _shouldprint, _quiet):
    if _debug:
        _debugp("in run()")
    try:
        for statement in statements:
            if _debug:
                _debugp("exec statement:", statement)
            six.exec_(statement, run_globals)
        if not expression.strip():
            if _debug:
                _debugp("no expression to run")
            _result = None
        else:
            if _debug:
                _debugp("running expression:", expression)
            _result = eval("(%s)" % expression, run_globals)
        if _debug:
            try:
                _debugp("result: repr={!r} str={}".format(_result, _result))
            except:
                import traceback
                _debugp("error printing result:")
                traceback.print_exc()
                _debugp("----------------")

        if "tensorflow" in sys.modules:
            import tensorflow
            if _debug:
                _debugp("tensorflow was imported, checking if tf result")
            if isinstance(_result, tensorflow.python.framework.ops.Tensor):
                if _debug:
                    _debugp("tensorflow result detected")
                if "session" not in run_globals:
                    if _debug:
                        _debugp(
                            "no session detected, creating interactivesession as 'session'"
                        )
                    run_globals["session"] = tensorflow.InteractiveSession()
                if _debug:
                    _debugp("run tf _result with", run_globals["session"])
                _result = run_globals["session"].run(_result)
                if _debug:
                    _debugp("tf result", _result)
    except KeyboardInterrupt:
        if not _quiet:
            sys.stderr.write("@ killed (ctrl+d to close cleanly)")
        return fail
    except BrokenPipeError:
        raise
    except BaseException as e:
        import traceback
        if _debug:
            traceback.print_exc()
        else:
            x = traceback.format_exc().split("\n")
            y = "\n".join(x[4:])
            sys.stderr.write(y)
        sys.stderr.flush()
        return fail

    if _result is None:
        if _debug:
            _debugp("converting result of None to result of True")
        _result = True

    if not (isinstance(_result, six.string_types)
            or isinstance(_result, _LazyString)):
        if _debug:
            _debugp("result is not a string, attempting iteration")
        try:
            iterator = iter(_result)
        except TypeError as e:
            if getattr(_result, "__iter__", None):
                print(
                    "Tried to run as iterator, but it failed with typeerror, despite having an __iter__:"
                )
                print(repr(_result.__iter__))
                raise
            if _debug:
                _debugp("result doesn't seem iterable")
        else:
            if _shouldprint:
                for x in iterator:
                    if _debug:
                        print("printed iterator:", x)
                    else:
                        print(x)
            else:
                result2 = list(iterator)
                try:
                    # lol hax
                    is_repeatable_iterable = ("numpy" in str(
                        type(_result)) or (iterator is not _result
                                           and result2 == list(iter(_result))))
                except ValueError:
                    # assume yes, because annoying
                    is_repeatable_iterable = True
                if is_repeatable_iterable:  # check for repeatability
                    if _debug:
                        print("repeatable iterable:", _result, result2)
                    else:
                        print(_result)
                elif any(x != None for x in result2):
                    if _debug:
                        print("listed iterable with at least one non-none:",
                              result2)
                    else:
                        print(result2)
                elif _debug:
                    _debugp("nothing to print")
            return succeed

    if not isinstance(_result, bool) or _shouldprint:
        if _debug:
            _debugp("hasdoc:", _hasdoc(_result), "repr and str equal:",
                    repr(_result) == str(_result), "uses object str:",
                    type(_result).__str__ == object.__str__)
        if (_hasdoc(_result) and repr(_result)
                == str(_result)) or type(_result).__str__ == object.__str__:
            if _debug:
                print("printed docstring:", _result.__doc__)
            else:
                #print(_result.__doc__)
                show(_result)
        else:
            if _debug:
                print("primary print:", _result)
            else:
                print(_result)

    if isinstance(_result, bool):
        if _debug:
            _debugp("bool result, returning exit code:", 0 if _result else 1,
                    _result)

        if _result:
            return succeed
        else:
            return fail
    else:
        if _debug:
            _debugp("non-bool result, returning exit code 0 (true)")
        return succeed
Exemplo n.º 45
0
    def from_reflection(cls, asname, actinfo, connection):
        '''
        Construct a CASAction class from reflection information

        Parameters
        ----------
        asname : string
            The action set name
        actinfo : dict
            The reflection information for the action
        connection : CAS object
            The connection to associate with the CASAction
        defaults : dict
            Default parameters for the action

        Returns
        -------
        CASAction class

        '''
        _globals = globals()
        _locals = locals()

        name = actinfo['name'].split('.', 1)[-1]

        clsname = name.title()

        # Create call signatures
        params = actinfo.get('params', [])
        results = actinfo.get('results', [])
        params = [x for x in params if not x['name'].startswith('_')]
        params = sorted(params, key=lambda x: (int(not x.get('isRequired', 0))))
        pkeys = [param['name'] for param in params]
        sig = ', '.join([('%s=None' % dekeywordify(x)) for x in pkeys] + ['**kwargs'])
        callargs = ', '.join([('%s: %s' % (repr(x), dekeywordify(x))) for x in pkeys])

        # Save these for testing valid default_params
        param_names = [prm.lower() for prm in pkeys]

        # __init__ and action methods
        funcargs = ('**mergedefined(_self_._get_default_params(), '
                    + '{%s}, kwargs)') % callargs
        six.exec_(('''def __init__(_self_, %s):'''
                   + '''    CASAction.__init__(_self_, %s)''')
                     % (sig, funcargs), _globals, _locals)
        six.exec_(('''def __call__(_self_, %s):'''
                   + '''    return CASAction.__call__(_self_, %s)''')
                     % (sig, funcargs), _globals, _locals)

        # Generate documentation
        all_params = []
        setget_doc = format_params(params, connection,
                                   suppress_subparams=['table.importoptions'],
                                   param_names=all_params).rstrip()
        action_doc = cls._format_action_doc(actinfo, setget_doc).rstrip()
        if results:
            results_doc = '\n\nResults Keys\n------------\n' + \
                          format_params(results, connection, results_format=True).rstrip()
        else:
            results_doc = ''

        # Generate set/del methods for scalar parameters
        def set_params(_self_, *args, **kwargs):
            ''' Set parameters '''
            return CASAction.set_params(_self_, *args, **kwargs)

        def set_param(_self_, *args, **kwargs):
            ''' Set parameter '''
            return CASAction.set_param(_self_, *args, **kwargs)

        def get_params(_self_, *keys):
            ''' Get parameters '''
            return CASAction.get_params(_self_, *keys)

        def get_param(_self_, key):
            ''' Get parameter '''
            return CASAction.get_param(_self_, key)

        # Set docstrings
        set_params.__doc__ = SET_PARAMS_DOCSTRING % setget_doc
        set_param.__doc__ = SET_PARAM_DOCSTRING % setget_doc
        get_params.__doc__ = GET_PARAMS_DOCSTRING % setget_doc
        get_param.__doc__ = GET_PARAM_DOCSTRING % setget_doc
        _locals['__call__'].__doc__ = re.sub(r'\w+ object$',
                                             r'CASResults object%s' % results_doc,
                                             action_doc.rstrip())
        _locals['__init__'].__doc__ = action_doc.rstrip()

        for name in list(param_names):
            if keyword.iskeyword(name):
                param_names.append(dekeywordify(name))

        # CASAction members and methods
        actmembers = {
            '_connection': weakref.ref(connection),
            '__init__': _locals['__init__'],
            '__call__': _locals['__call__'],
            '__doc__': action_doc,
            'set_params': set_params,
            'set_param': set_param,
            'get_params': get_params,
            'get_param': get_param,
            'param_names': param_names,
            'all_params': set(all_params)
        }

        # Generate action class
        actcls = type(str(asname + '.' + clsname), (CASAction,), actmembers)

        return actcls
def immutable(members='', name='Immutable', verbose=False):
    """
    Produces a class that either can be used standalone or as a base class for persistent classes.

    This is a thin wrapper around a named tuple.

    Constructing a type and using it to instantiate objects:

    >>> Point = immutable('x, y', name='Point')
    >>> p = Point(1, 2)
    >>> p2 = p.set(x=3)
    >>> p
    Point(x=1, y=2)
    >>> p2
    Point(x=3, y=2)

    Inheriting from a constructed type. In this case no type name needs to be supplied:

    >>> class PositivePoint(immutable('x, y')):
    ...     __slots__ = tuple()
    ...     def __new__(cls, x, y):
    ...         if x > 0 and y > 0:
    ...             return super(PositivePoint, cls).__new__(cls, x, y)
    ...         raise Exception('Coordinates must be positive!')
    ...
    >>> p = PositivePoint(1, 2)
    >>> p.set(x=3)
    PositivePoint(x=3, y=2)
    >>> p.set(y=-3)
    Traceback (most recent call last):
    Exception: Coordinates must be positive!

    The persistent class also supports the notion of frozen members. The value of a frozen member
    cannot be updated. For example it could be used to implement an ID that should remain the same
    over time. A frozen member is denoted by a trailing underscore.

    >>> Point = immutable('x, y, id_', name='Point')
    >>> p = Point(1, 2, id_=17)
    >>> p.set(x=3)
    Point(x=3, y=2, id_=17)
    >>> p.set(id_=18)
    Traceback (most recent call last):
    AttributeError: Cannot set frozen members id_
    """

    if isinstance(members, six.string_types):
        members = members.replace(',', ' ').split()

    def frozen_member_test():
        frozen_members = ["'%s'" % f for f in members if f.endswith('_')]
        if frozen_members:
            return """
        frozen_fields = fields_to_modify & set([{frozen_members}])
        if frozen_fields:
            raise AttributeError('Cannot set frozen members %s' % ', '.join(frozen_fields))
            """.format(frozen_members=', '.join(frozen_members))

        return ''

    verbose_string = ""
    if sys.version_info < (3, 7):
        # Verbose is no longer supported in Python 3.7
        verbose_string = ", verbose={verbose}".format(verbose=verbose)

    quoted_members = ', '.join("'%s'" % m for m in members)
    template = """
class {class_name}(namedtuple('ImmutableBase', [{quoted_members}]{verbose_string})):
    __slots__ = tuple()

    def __repr__(self):
        return super({class_name}, self).__repr__().replace('ImmutableBase', self.__class__.__name__)

    def set(self, **kwargs):
        if not kwargs:
            return self

        fields_to_modify = set(kwargs.keys())
        if not fields_to_modify <= {member_set}:
            raise AttributeError("'%s' is not a member" % ', '.join(fields_to_modify - {member_set}))

        {frozen_member_test}

        return self.__class__.__new__(self.__class__, *map(kwargs.pop, [{quoted_members}], self))
""".format(quoted_members=quoted_members,
           member_set="set([%s])" %
           quoted_members if quoted_members else 'set()',
           frozen_member_test=frozen_member_test(),
           verbose_string=verbose_string,
           class_name=name)

    if verbose:
        print(template)

    from collections import namedtuple
    namespace = dict(namedtuple=namedtuple, __name__='pyrsistent_immutable')
    try:
        six.exec_(template, namespace)
    except SyntaxError as e:
        raise SyntaxError(e.message + ':\n' + template)

    return namespace[name]
Exemplo n.º 47
0
class AbstractProxy(object):
    """Delegates all operations (except ``.__subject__``) to another object"""
    __slots__ = ()

    def __call__(self, *args, **kw):
        return self.__subject__(*args, **kw)

    def __getattribute__(self, attr, oga=object.__getattribute__):
        subject = oga(self, '__subject__')
        if attr == '__subject__':
            return subject
        return getattr(subject, attr)

    def __setattr__(self, attr, val, osa=object.__setattr__):
        if attr == '__subject__':
            osa(self, attr, val)
        else:
            setattr(self.__subject__, attr, val)

    def __delattr__(self, attr, oda=object.__delattr__):
        if attr == '__subject__':
            oda(self, attr)
        else:
            delattr(self.__subject__, attr)

    if _is_py2:

        def __nonzero__(self):
            return bool(self.__subject__)
    else:

        def __bool__(self):
            return bool(self.__subject__)

    def __getitem__(self, arg):
        return self.__subject__[arg]

    def __setitem__(self, arg, val):
        self.__subject__[arg] = val

    def __delitem__(self, arg):
        del self.__subject__[arg]

    def __getslice__(self, i, j):
        return self.__subject__[i:j]

    def __setslice__(self, i, j, val):
        self.__subject__[i:j] = val

    def __delslice__(self, i, j):
        del self.__subject__[i:j]

    def __contains__(self, ob):
        return ob in self.__subject__

    for name in 'repr str hash len abs complex int long float iter oct hex'.split(
    ):
        exec_("def __%s__(self): return %s(self.__subject__)" % (name, name))

    for name in 'cmp', 'coerce', 'divmod':
        exec_("def __%s__(self, ob): return %s(self.__subject__, ob)" %
              (name, name))

    for name, op in [('lt', '<'), ('gt', '>'), ('le', '<='), ('ge', '>='),
                     ('eq', '=='), ('ne', '!=')]:
        exec_("def __%s__(self, ob): return self.__subject__ %s ob" %
              (name, op))

    for name, op in [('neg', '-'), ('pos', '+'), ('invert', '~')]:
        exec_("def __%s__(self): return %s self.__subject__" % (name, op))

    for name, op in [('or', '|'), ('and', '&'), ('xor', '^'), ('lshift', '<<'),
                     ('rshift', '>>'), ('add', '+'),
                     ('sub', '-'), ('mul', '*'), ('div', '/'), ('mod', '%'),
                     ('truediv', '/'), ('floordiv', '//')]:
        exec_(("def __%(name)s__(self, ob):\n"
               "    return self.__subject__ %(op)s ob\n"
               "\n"
               "def __r%(name)s__(self, ob):\n"
               "    return ob %(op)s self.__subject__\n"
               "\n"
               "def __i%(name)s__(self, ob):\n"
               "    self.__subject__ %(op)s=ob\n"
               "    return self\n") % locals())

    del name, op

    # Oddball signatures

    if not _is_py2:

        def __index__(self):
            return self.__subject__.__index__()

    def __rdivmod__(self, ob):
        return divmod(ob, self.__subject__)

    def __pow__(self, *args):
        return pow(self.__subject__, *args)

    def __ipow__(self, ob):
        self.__subject__ **= ob
        return self

    def __rpow__(self, ob):
        return pow(ob, self.__subject__)
Exemplo n.º 48
0
def _execfile(path, globals=None, locals=None):
    import six
    with open(path, "rb") as f:
        src = f.read()
    code = compile(src, path, 'exec')
    six.exec_(code, globals, locals)
Exemplo n.º 49
0
    def generate_func(self, as_global=False):
        """
        Generate function for specific method and using specific api

        :param as_global: if set, will use the global function name, instead of
            the class method (usually {resource}_{class_method}) when defining
            the function
        """
        keywords = []
        params_def = []
        params_doc = ""
        original_names = {}

        params = dict(
            (param['name'], param)
            for param in self.params
        )

        # parse the url required params, as sometimes they are skipped in the
        # parameters list of the definition
        for param in self.url_params:
            if param not in params:
                param = {
                    'name': param,
                    'required': True,
                    'description': '',
                    'validator': '',
                }
                params[param['name']] = param
            else:
                params[param]['required'] = True

        # split required and non-required params for the definition
        req_params = []
        nonreq_params = []
        for param in six.itervalues(params):
            if param['required']:
                req_params.append(param)
            else:
                nonreq_params.append(param)

        for param in req_params + nonreq_params:
            params_doc += self.create_param_doc(param) + "\n"
            local_name = param['name']
            # some params collide with python keywords, that's why we do
            # this switch (and undo it inside the function we generate)
            if param['name'] == 'except':
                local_name = 'except_'
            original_names[local_name] = param['name']
            keywords.append(local_name)
            if param['required']:
                params_def.append("%s" % local_name)
            else:
                params_def.append("%s=None" % local_name)

        func_head = 'def {0}(self, {1}):'.format(
            as_global and self.get_global_method_name() or self.name,
            ', '.join(params_def)
        )
        code_body = (
            '   _vars_ = locals()\n'
            '   _url = self._fill_url("{url}", _vars_, {url_params})\n'
            '   _original_names = {original_names}\n'
            '   _kwargs = dict((_original_names[k], _vars_[k])\n'
            '                   for k in {keywords} if _vars_[k])\n'
            '   return self._foreman.do_{http_method}(_url, _kwargs)')
        code_body = code_body.format(
            http_method=self.http_method.lower(),
            url=self.url,
            url_params=self.url_params,
            keywords=keywords,
            original_names=original_names,
        )

        code = [
            func_head,
            '   """',
            self.short_desc,
            '',
            params_doc,
            '   """',
            code_body,
        ]

        code = '\n'.join(code)

        six.exec_(code)

        function = locals()[self.name]
        # to ease debugging, all the funcs have the definitions attached
        setattr(function, 'defs', self)
        return function
Exemplo n.º 50
0
 def exec_handler():
     six.exec_(args.eval[0], vars(entry_module.namespace))
Exemplo n.º 51
0
class CommandLineInterface(object):
    """
    Wrapper around all the other classes, tying everything together.

    Typical usage::

        application = Application(...)
        cli = CommandLineInterface(application, eventloop)
        result = cli.run()
        print(result)

    :param application: :class:`~prompt_toolkit.application.Application` instance.
    :param eventloop: The :class:`~prompt_toolkit.eventloop.base.EventLoop` to
                      be used when `run` is called.
    :param input: :class:`~prompt_toolkit.input.Input` instance.
    :param output: :class:`~prompt_toolkit.output.Output` instance. (Probably
                   Vt100_Output or Win32Output.)
    """
    def __init__(self, application, eventloop=None, input=None, output=None):
        assert isinstance(application, Application)
        assert eventloop is None or isinstance(eventloop, EventLoop)
        assert output is None or isinstance(output, Output)
        assert input is None or isinstance(input, Input)

        from .shortcuts import create_output, create_eventloop

        self.application = application
        self.eventloop = eventloop or create_eventloop()
        self._is_running = False

        # Inputs and outputs.
        self.output = output or create_output()
        self.input = input or StdinInput(sys.stdin)

        #: The input buffers.
        self.buffers = {
            # For the 'search' and 'system' buffers, 'returnable' is False, in
            # order to block normal Enter/ControlC behaviour.
            DEFAULT_BUFFER: (application.buffer or Buffer(accept_action=AcceptAction.RETURN_DOCUMENT)),
            SEARCH_BUFFER: Buffer(history=InMemoryHistory(), accept_action=AcceptAction.IGNORE),
            SYSTEM_BUFFER: Buffer(history=InMemoryHistory(), accept_action=AcceptAction.IGNORE),
            DUMMY_BUFFER: Buffer(read_only=True),
        }
        self.buffers.update(application.buffers)

        #: The `Renderer` instance.
        # Make sure that the same stdout is used, when a custom renderer has been passed.
        self.renderer = Renderer(
            self.application.style,
            self.output,
            use_alternate_screen=application.use_alternate_screen,
            mouse_support=application.mouse_support)

        # Invalidate flag. When 'True', a repaint has been scheduled.
        self._invalidated = False

        #: The `InputProcessor` instance.
        self.input_processor = InputProcessor(application.key_bindings_registry, weakref.ref(self))

        self._async_completers = {}  # Map buffer name to completer function.

        # Pointer to sub CLI. (In chain of CLI instances.)
        self._sub_cli = None  # None or other CommandLineInterface instance.

        # Call `add_buffer` for each buffer.
        for name, b in self.buffers.items():
            self.add_buffer(name, b)

        self.reset()

        # Trigger initialize callback.
        self.application.on_initialize.fire(self)

    @property
    def layout(self):
        return self.application.layout

    @property
    def clipboard(self):
        return self.application.clipboard

    @property
    def focus_stack(self):
        return self.application.focus_stack

    def add_buffer(self, name, buffer, focus=False):
        """
        Insert a new buffer.
        """
        assert isinstance(buffer, Buffer)
        self.buffers[name] = buffer

        if focus:
            self.focus_stack.replace(name)

        # Create asynchronous completer / auto suggestion.
        auto_suggest_function = self._create_auto_suggest_function(buffer)
        completer_function = self._create_async_completer(buffer)
        self._async_completers[name] = completer_function

        # Complete/suggest on text insert.
        def create_on_insert_handler():
            """
            Wrapper around the asynchronous completer and auto suggestion, that
            ensures that it's only called while typing if the
            `complete_while_typing` filter is enabled.
            """
            def on_text_insert():
                # Only complete when "complete_while_typing" is enabled.
                if buffer.completer and buffer.complete_while_typing():
                    completer_function()

                # Call auto_suggest.
                if buffer.auto_suggest:
                    auto_suggest_function()

                # Trigger on_buffer_changed when text in this buffer changes.
                self.application.on_buffer_changed.fire(self)

            return on_text_insert

        buffer.on_text_insert += create_on_insert_handler()

    def start_completion(self, buffer_name=None, select_first=False,
                         select_last=False, insert_common_part=False,
                         complete_event=None):
        """
        Start asynchronous autocompletion of this buffer.
        (This will do nothing if a previous completion was still in progress.)
        """
        buffer_name = buffer_name or self.current_buffer_name
        completer = self._async_completers.get(buffer_name)

        if completer:
            completer(select_first=select_first,
                      select_last=select_last,
                      insert_common_part=insert_common_part,
                      complete_event=CompleteEvent(completion_requested=True))

    @property
    def current_buffer_name(self):
        """
        The name of the current  :class:`.Buffer`. (Or `None`.)
        """
        return self.focus_stack.current

    @property
    def current_buffer(self):
        """
        The currently focussed :class:`~.Buffer`.

        (This returns a dummy :class:`.Buffer` when none of the actual buffers has the
        focus. In this case, it's really not practical to check for `None`
        values or catch exceptions every time.)
        """
        name = self.focus_stack.current
        if name is not None:
            return self.buffers[self.focus_stack.current]
        else:
            return self.buffers[DUMMY_BUFFER]

    @property
    def terminal_title(self):
        """
        Return the current title to be displayed in the terminal.
        When this in `None`, the terminal title remains the original.
        """
        result = self.application.get_title()

        # Make sure that this function returns a unicode object,
        # and not a byte string.
        assert result is None or isinstance(result, six.text_type)
        return result

    @property
    def is_searching(self):
        """
        True when we are searching.
        """
        return self.current_buffer_name == SEARCH_BUFFER

    def reset(self, reset_current_buffer=False):
        """
        Reset everything, for reading the next input.

        :param reset_current_buffer: If True, also reset the focussed buffer.
        """
        # Notice that we don't reset the buffers. (This happens just before
        # returning, and when we have multiple buffers, we clearly want the
        # content in the other buffers to remain unchanged between several
        # calls of `run`. (And the same is true for the `focus_stack`.)

        self._exit_flag = False
        self._abort_flag = False

        self._return_value = None

        self.renderer.reset()
        self.input_processor.reset()
        self.layout.reset()

        if reset_current_buffer:
            self.current_buffer.reset()

        # Search new search state. (Does also remember what has to be
        # highlighted.)
        self.search_state = SearchState(ignore_case=Condition(lambda: self.is_ignoring_case))

        # Trigger reset event.
        self.application.on_reset.fire(self)

    @property
    def in_paste_mode(self):
        """ True when we are in paste mode. """
        return self.application.paste_mode(self)

    @property
    def is_ignoring_case(self):
        """ True when we currently ignore casing. """
        return self.application.ignore_case(self)

    def invalidate(self):
        """
        Thread safe way of sending a repaint trigger to the input event loop.
        """
        if self.eventloop is not None:
            # Never schedule a second redraw, when a previous one has not yet been
            # executed. (This should protect against other threads calling
            # 'invalidate' many times, resulting in 100% CPU.)
            if self._invalidated:
                return

            self._invalidated = True

            def redraw():
                self._invalidated = False
                self._redraw()

            self.eventloop.call_from_executor(redraw)

    # Depracated alias for 'invalidate'.
    request_redraw = invalidate

    def _redraw(self):
        """
        Render the command line again. (Not thread safe!) (From other threads,
        or if unsure, use :meth:`.CommandLineInterface.invalidate`.)
        """
        # Only draw when no sub application was started.
        if self._is_running and self._sub_cli is None:
            self.renderer.render(self, self.layout, is_done=self.is_done)

    def _on_resize(self):
        """
        When the window size changes, we erase the current output and request
        again the cursor position. When the CPR answer arrives, the output is
        drawn again.
        """
        # Erase, request position (when cursor is at the start position)
        # and redraw again. -- The order is important.
        self.renderer.erase()
        self.renderer.request_absolute_cursor_position()
        self._redraw()

    def run(self, reset_current_buffer=True, pre_run=None):
        """
        Read input from the command line.
        This runs the eventloop until a return value has been set.

        :param reset_current_buffer: Reset content of current buffer.
        :param pre_run: Callable that is called right after the reset has taken
            place. This allows custom initialisation.
        """
        assert pre_run is None or callable(pre_run)

        try:
            self._is_running = True

            self.application.on_start.fire(self)
            self.reset(reset_current_buffer=reset_current_buffer)

            # Call pre_run.
            if pre_run:
                pre_run()

            # Run eventloop in raw mode.
            with self.input.raw_mode():
                self.renderer.request_absolute_cursor_position()
                self._redraw()

                self.eventloop.run(self.input, self.create_eventloop_callbacks())
        finally:
            # Clean up renderer. (This will leave the alternate screen, if we use
            # that.)
            self.renderer.reset()
            self.application.on_stop.fire(self)
            self._is_running = False

        # Return result.
        return self.return_value()

    try:
        # The following `run_async` function is compiled at runtime
        # because it contains syntax which is not supported on older Python
        # versions. (A 'return' inside a generator.)
        six.exec_(textwrap.dedent('''
        def run_async(self, reset_current_buffer=True, pre_run=None):
            """
            Same as `run`, but this returns a coroutine.

            This is only available on Python >3.3, with asyncio.
            """
            assert pre_run is None or callable(pre_run)

            try:
                self._is_running = True

                self.application.on_start.fire(self)
                self.reset(reset_current_buffer=reset_current_buffer)

                # Call pre_run.
                if pre_run:
                    pre_run()

                with self.input.raw_mode():
                    self.renderer.request_absolute_cursor_position()
                    self._redraw()

                    yield from self.eventloop.run_as_coroutine(
                            self.input, self.create_eventloop_callbacks())

                return self.return_value()
            finally:
                self.renderer.reset()
                self.application.on_stop.fire(self)
                self._is_running = False

        try:
            import asyncio
        except ImportError:
            pass
        else:
            run_async = asyncio.coroutine(run_async)
        '''))
    except SyntaxError:
        # Python2, or early versions of Python 3.
        def run_async(self, reset_current_buffer=True, pre_run=None):
            """
            Same as `run`, but this returns a coroutine.

            This is only available on Python >3.3, with asyncio.
            """
            raise NotImplementedError

    def run_sub_application(self, application, done_callback=None):
        """
        Run a sub :class:`~prompt_toolkit.application.Application`.

        This will suspend the main application and display the sub application
        until that one returns a value. The value is returned by calling
        `done_callback` with the result.

        The sub application will share the same I/O of the main application.
        That means, it uses the same input and output channels and it shares
        the same event loop.

        .. note:: Technically, it gets another Eventloop instance, but that is
            only a proxy to our main event loop. The reason is that calling
            'stop' --which returns the result of an application when it's
            done-- is handled differently.
        """
        assert isinstance(application, Application)
        assert done_callback is None or callable(done_callback)

        if self._sub_cli is not None:
            raise RuntimeError('Another sub application started already.')

        # Erase current application.
        self.renderer.erase()

        # Callback when the sub app is done.
        def done():
            # Redraw sub app in done state.
            # and reset the renderer. (This reset will also quit the alternate
            # screen, if the sub application used that.)
            sub_cli._redraw()
            sub_cli.renderer.reset()
            sub_cli._is_running = False  # Don't render anymore.

            self._sub_cli = None

            # Restore main application.
            self.renderer.request_absolute_cursor_position()
            self._redraw()

            # Deliver result.
            if done_callback:
                done_callback(sub_cli.return_value())

        # Create sub CommandLineInterface.
        sub_cli = CommandLineInterface(
            application=application,
            eventloop=_SubApplicationEventLoop(self, done),
            input=self.input,
            output=self.output)
        sub_cli._is_running = True  # Allow rendering of sub app.

        sub_cli._redraw()
        self._sub_cli = sub_cli

    def set_exit(self):
        """
        Set exit. When Control-D has been pressed.
        """
        on_exit = self.application.on_exit

        if on_exit != AbortAction.IGNORE:
            self._exit_flag = True
            self._redraw()

        if on_exit == AbortAction.RAISE_EXCEPTION:
            def eof_error():
                raise EOFError()
            self._set_return_callable(eof_error)

        elif on_exit == AbortAction.RETRY:
            self.reset()
            self.renderer.request_absolute_cursor_position()
            self.current_buffer.reset()

        elif on_exit == AbortAction.RETURN_NONE:
            self.set_return_value(None)

    def set_abort(self):
        """
        Set abort. When Control-C has been pressed.
        """
        on_abort = self.application.on_abort

        if on_abort != AbortAction.IGNORE:
            self._abort_flag = True
            self._redraw()

        if on_abort == AbortAction.RAISE_EXCEPTION:
            def keyboard_interrupt():
                raise KeyboardInterrupt()
            self._set_return_callable(keyboard_interrupt)

        elif on_abort == AbortAction.RETRY:
            self.reset()
            self.renderer.request_absolute_cursor_position()
            self.current_buffer.reset()

        elif on_abort == AbortAction.RETURN_NONE:
            self.set_return_value(None)

    def set_return_value(self, document):
        """
        Set a return value. The eventloop can retrieve the result it by calling
        `return_value`.
        """
        self._set_return_callable(lambda: document)
        self._redraw()  # Redraw in "done" state, after the return value has been set.

    def _set_return_callable(self, value):
        assert callable(value)
        self._return_value = value

        if self.eventloop:
            self.eventloop.stop()

    def run_in_terminal(self, func, render_cli_done=False):
        """
        Run function on the terminal above the prompt.

        What this does is first hiding the prompt, then running this callable
        (which can safely output to the terminal), and then again rendering the
        prompt which causes the output of this function to scroll above the
        prompt.

        :param func: The callable to execute.
        :param render_cli_done: When True, render the interface in the
                'Done' state first, then execute the function. If False,
                erase the interface first.

        :returns: the result of `func`.
        """
        # Draw interface in 'done' state, or erase.
        if render_cli_done:
            self._return_value = True
            self._redraw()
            self.renderer.reset()  # Make sure to disable mouse mode, etc...
        else:
            self.renderer.erase()

        # Run system command.
        with self.input.cooked_mode():
            result = func()
        self._return_value = None

        # Redraw interface again.
        self.renderer.reset()
        self.renderer.request_absolute_cursor_position()
        self._redraw()

        return result

    def run_system_command(self, command):
        """
        Run system command (While hiding the prompt. When finished, all the
        output will scroll above the prompt.)

        :param command: Shell command to be executed.
        """
        def run():
            if is_windows():
                os.system(command)  # Needs to be unicode for win32
            else:
                os.system(command.encode('utf-8'))

            try:
                six.moves.input('\nPress ENTER to continue...')
            except EOFError:
                pass

        self.run_in_terminal(run)

    def suspend_to_background(self):
        """
        (Not thread safe -- to be called from inside the key bindings.)
        Suspend process.
        """
        # Only suspend when the opperating system supports it.
        # (Not on Windows.)
        if hasattr(signal, 'SIGTSTP'):
            def run():
                # Send `SIGSTP` to own process.
                # This will cause it to suspend.
                os.kill(os.getpid(), signal.SIGTSTP)

            self.run_in_terminal(run)

    def print_tokens(self, tokens, style=None):
        """
        Print a list of (Token, text) tuples to the output.
        (When the UI is running, this method has to be called through
        `run_in_terminal`, otherwise it will destroy the UI.)

        :param style: Style class to use. Defaults to the active style in the CLI.
        """
        print_tokens(self.output, tokens, style or self.application.style)

    @property
    def is_exiting(self):
        """
        ``True`` when the exit flag as been set.
        """
        return self._exit_flag

    @property
    def is_aborting(self):
        """
        ``True`` when the abort flag as been set.
        """
        return self._abort_flag

    @property
    def is_returning(self):
        """
        ``True`` when a return value has been set.
        """
        return self._return_value is not None

    def return_value(self):
        """
        Get the return value. Not that this method can throw an exception.
        """
        # Note that it's a method, not a property, because it can throw
        # exceptions.
        if self._return_value:
            return self._return_value()

    @property
    def is_done(self):
        return self.is_exiting or self.is_aborting or self.is_returning

    def _create_async_completer(self, buffer):
        """
        Create function for asynchronous autocompletion.
        (Autocomplete in other thread.)
        """
        complete_thread_running = [False]  # By ref.

        def async_completer(select_first=False, select_last=False,
                            insert_common_part=False, complete_event=None):
            document = buffer.document
            complete_event = complete_event or CompleteEvent(text_inserted=True)

            # Don't start two threads at the same time.
            if complete_thread_running[0]:
                return

            # Don't complete when we already have completions.
            if buffer.complete_state or not buffer.completer:
                return

            # Otherwise, get completions in other thread.
            complete_thread_running[0] = True

            def run():
                completions = list(buffer.completer.get_completions(document, complete_event))
                complete_thread_running[0] = False

                def callback():
                    """
                    Set the new complete_state in a safe way. Don't replace an
                    existing complete_state if we had one. (The user could have
                    pressed 'Tab' in the meantime. Also don't set it if the text
                    was changed in the meantime.
                    """
                    # Set completions if the text was not yet changed.
                    if buffer.text == document.text and \
                            buffer.cursor_position == document.cursor_position and \
                            not buffer.complete_state:

                        set_completions = True
                        select_first_anyway = False

                        # When the commond part has to be inserted, and there
                        # is a common part.
                        if insert_common_part:
                            common_part = get_common_complete_suffix(document, completions)
                            if common_part:
                                # Insert + run completer again.
                                buffer.insert_text(common_part)
                                async_completer()
                                set_completions = False
                            else:
                                # When we were asked to insert the "common"
                                # prefix, but there was no common suffix but
                                # still exactly one match, then select the
                                # first. (It could be that we have a completion
                                # which does * expension, like '*.py', with
                                # exactly one match.)
                                if len(completions) == 1:
                                    select_first_anyway = True

                        if set_completions:
                            buffer.set_completions(
                                completions=completions,
                                go_to_first=select_first or select_first_anyway,
                                go_to_last=select_last)
                        self.invalidate()
                    else:
                        # Otherwise, restart thread.
                        async_completer()

                if self.eventloop:
                    self.eventloop.call_from_executor(callback)

            self.eventloop.run_in_executor(run)
        return async_completer

    def _create_auto_suggest_function(self, buffer):
        """
        Create function for asynchronous auto suggestion.
        (AutoSuggest in other thread.)
        """
        suggest_thread_running = [False]  # By ref.

        def async_suggestor():
            document = buffer.document

            # Don't start two threads at the same time.
            if suggest_thread_running[0]:
                return

            # Don't suggest when we already have a suggestion.
            if buffer.suggestion or not buffer.auto_suggest:
                return

            # Otherwise, get completions in other thread.
            suggest_thread_running[0] = True

            def run():
                suggestion = buffer.auto_suggest.get_suggestion(self, buffer, document)
                suggest_thread_running[0] = False

                def callback():
                    # Set suggestion only if the text was not yet changed.
                    if buffer.text == document.text and \
                            buffer.cursor_position == document.cursor_position:

                        # Set suggestion and redraw interface.
                        buffer.suggestion = suggestion
                        self.invalidate()
                    else:
                        # Otherwise, restart thread.
                        async_suggestor()

                if self.eventloop:
                    self.eventloop.call_from_executor(callback)

            self.eventloop.run_in_executor(run)
        return async_suggestor

    def stdout_proxy(self):
        """
        Create an :class:`_StdoutProxy` class which can be used as a patch for
        sys.stdout. Writing to this proxy will make sure that the text appears
        above the prompt, and that it doesn't destroy the output from the
        renderer.
        """
        return _StdoutProxy(self)

    def patch_stdout_context(self):
        """
        Return a context manager that will replace ``sys.stdout`` with a proxy
        that makes sure that all printed text will appear above the prompt, and
        that it doesn't destroy the output from the renderer.
        """
        return _PatchStdoutContext(self.stdout_proxy())

    def create_eventloop_callbacks(self):
        return _InterfaceEventLoopCallbacks(self)
Exemplo n.º 52
0
# -*- coding: utf-8 -*-
"""This submodule contains helpers for exception handling."""

__author__ = 'Jens Finkhaeuser'
__copyright__ = 'Copyright (c) 2018 Jens Finkhaeuser'
__license__ = 'MIT +no-false-attribs'
__all__ = ()

# Raise the given exception class from the caught exception, preserving
# stack trace and message as much as possible.
import six
if six.PY3:  # pragma: nocover
    six.exec_("""def raise_from(klass, from_value):
  try:
    if from_value is None:
      raise klass()
    raise klass(*from_value.args) from from_value
  finally:
    klass = None
""")
elif six.PY2:  # pragma: nocover
    six.exec_("""def raise_from(klass, from_value):
  try:
    if from_value is None:
      raise klass()
    import sys
    exc_info = sys.exc_info()
    raise klass, klass(*from_value.args), exc_info[2]
  finally:
    klass = None
""")
Exemplo n.º 53
0
# coding: utf-8
import sys
from six import exec_

PY2 = sys.version_info[0] == 2
WIN = sys.platform.startswith('win')

if PY2:
    # because PY# fails syntax here
    exec_('def reraise(tp, value, tb=None):\n raise tp, value, tb')
else:

    def reraise(tp, value, tb=None):
        if value.__traceback__ is not tb:
            raise value.with_traceback(tb)
        raise value


def import_string(import_name, silent=False):
    """Imports an object based on a string.  This is useful if you want to
    use import paths as endpoints or something similar.  An import path can
    be specified either in dotted notation (``xml.sax.saxutils.escape``)
    or with a colon as object delimiter (``xml.sax.saxutils:escape``).
    If `silent` is True the return value will be `None` if the import fails.
    :param import_name: the dotted name for the object to import.
    :param silent: if set to `True` import errors are ignored and
                   `None` is returned instead.
    :return: imported object
    """
    # force the import name to automatically convert to strings
    # __import__ is not able to handle unicode strings in the fromlist
Exemplo n.º 54
0
def run_code(code, code_path, ns=None, function_name=None):
    """
    Import a Python module from a path, and run the function given by
    name, if function_name is not None.
    """

    # Change the working directory to the directory of the example, so
    # it can get at its data files, if any.  Add its path to sys.path
    # so it can import any helper modules sitting beside it.
    if six.PY2:
        pwd = os.getcwdu()
    else:
        pwd = os.getcwd()
    old_sys_path = list(sys.path)
    if setup.config.plot_working_directory is not None:
        try:
            os.chdir(setup.config.plot_working_directory)
        except OSError as err:
            raise OSError(
                str(err) + '\n`plot_working_directory` option in'
                'Sphinx configuration file must be a valid '
                'directory path')
        except TypeError as err:
            raise TypeError(
                str(err) + '\n`plot_working_directory` option in '
                'Sphinx configuration file must be a string or '
                'None')
        sys.path.insert(0, setup.config.plot_working_directory)
    elif code_path is not None:
        dirname = os.path.abspath(os.path.dirname(code_path))
        os.chdir(dirname)
        sys.path.insert(0, dirname)

    # Reset sys.argv
    old_sys_argv = sys.argv
    sys.argv = [code_path]

    # Redirect stdout
    stdout = sys.stdout
    if six.PY3:
        sys.stdout = io.StringIO()
    else:
        sys.stdout = cStringIO.StringIO()

    # Assign a do-nothing print function to the namespace.  There
    # doesn't seem to be any other way to provide a way to (not) print
    # that works correctly across Python 2 and 3.
    def _dummy_print(*arg, **kwarg):
        pass

    try:
        try:
            code = unescape_doctest(code)
            if ns is None:
                ns = {}
            if not ns:
                if setup.config.plot_pre_code is None:
                    six.exec_(
                        six.text_type(
                            "import numpy as np\n" +
                            "from matplotlib import pyplot as plt\n"), ns)
                else:
                    six.exec_(six.text_type(setup.config.plot_pre_code), ns)
            ns['print'] = _dummy_print
            if "__main__" in code:
                six.exec_("__name__ = '__main__'", ns)
            code = remove_coding(code)
            six.exec_(code, ns)
            if function_name is not None:
                six.exec_(function_name + "()", ns)
        except (Exception, SystemExit) as err:
            raise PlotError(traceback.format_exc())
    finally:
        os.chdir(pwd)
        sys.argv = old_sys_argv
        sys.path[:] = old_sys_path
        sys.stdout = stdout
    return ns
Exemplo n.º 55
0
class CommandLineInterface(object):
    """
    Wrapper around all the other classes, tying everything together.

    Typical usage::

        application = Application(...)
        cli = CommandLineInterface(application, eventloop)
        result = cli.run()
        print(result)

    :param application: :class:`~prompt_toolkit.application.Application` instance.
    :param eventloop: The :class:`~prompt_toolkit.eventloop.base.EventLoop` to
                      be used when `run` is called. The easiest way to create
                      an eventloop is by calling
                      :meth:`~prompt_toolkit.shortcuts.create_eventloop`.
    :param input: :class:`~prompt_toolkit.input.Input` instance.
    :param output: :class:`~prompt_toolkit.output.Output` instance. (Probably
                   Vt100_Output or Win32Output.)
    """
    def __init__(self, application, eventloop=None, input=None, output=None):
        assert isinstance(application, Application)
        assert isinstance(eventloop,
                          EventLoop), 'Passing an eventloop is required.'
        assert output is None or isinstance(output, Output)
        assert input is None or isinstance(input, Input)

        from .shortcuts import create_output

        self.application = application
        self.eventloop = eventloop
        self._is_running = False

        # Inputs and outputs.
        self.output = output or create_output()
        self.input = input or StdinInput(sys.stdin)

        #: The input buffers.
        assert isinstance(application.buffers, BufferMapping)
        self.buffers = application.buffers

        #: EditingMode.VI or EditingMode.EMACS
        self.editing_mode = application.editing_mode

        #: Quoted insert. This flag is set if we go into quoted insert mode.
        self.quoted_insert = False

        #: Vi state. (For Vi key bindings.)
        self.vi_state = ViState()

        #: The `Renderer` instance.
        # Make sure that the same stdout is used, when a custom renderer has been passed.
        self.renderer = Renderer(
            self.application.style,
            self.output,
            use_alternate_screen=application.use_alternate_screen,
            mouse_support=application.mouse_support)

        #: Render counter. This one is increased every time the UI is rendered.
        #: It can be used as a key for caching certain information during one
        #: rendering.
        self.render_counter = 0

        #: When there is high CPU, postpone the renderering max x seconds.
        #: '0' means: don't postpone. '.5' means: try to draw at least twice a second.
        self.max_render_postpone_time = 0  # E.g. .5

        # Invalidate flag. When 'True', a repaint has been scheduled.
        self._invalidated = False

        #: The `InputProcessor` instance.
        self.input_processor = InputProcessor(
            application.key_bindings_registry, weakref.ref(self))

        self._async_completers = {}  # Map buffer name to completer function.

        # Pointer to sub CLI. (In chain of CLI instances.)
        self._sub_cli = None  # None or other CommandLineInterface instance.

        # Call `add_buffer` for each buffer.
        for name, b in self.buffers.items():
            self.add_buffer(name, b)

        # Events.
        self.on_buffer_changed = Event(self, application.on_buffer_changed)
        self.on_initialize = Event(self, application.on_initialize)
        self.on_input_timeout = Event(self, application.on_input_timeout)
        self.on_invalidate = Event(self, application.on_invalidate)
        self.on_render = Event(self, application.on_render)
        self.on_reset = Event(self, application.on_reset)
        self.on_start = Event(self, application.on_start)
        self.on_stop = Event(self, application.on_stop)

        # Trigger initialize callback.
        self.reset()
        self.on_initialize += self.application.on_initialize
        self.on_initialize.fire()

    @property
    def layout(self):
        return self.application.layout

    @property
    def clipboard(self):
        return self.application.clipboard

    @property
    def pre_run_callables(self):
        return self.application.pre_run_callables

    def add_buffer(self, name, buffer, focus=False):
        """
        Insert a new buffer.
        """
        assert isinstance(buffer, Buffer)
        self.buffers[name] = buffer

        if focus:
            self.buffers.focus(name)

        # Create asynchronous completer / auto suggestion.
        auto_suggest_function = self._create_auto_suggest_function(buffer)
        completer_function = self._create_async_completer(buffer)
        self._async_completers[name] = completer_function

        # Complete/suggest on text insert.
        def create_on_insert_handler():
            """
            Wrapper around the asynchronous completer and auto suggestion, that
            ensures that it's only called while typing if the
            `complete_while_typing` filter is enabled.
            """
            def on_text_insert(_):
                # Only complete when "complete_while_typing" is enabled.
                if buffer.completer and buffer.complete_while_typing():
                    completer_function()

                # Call auto_suggest.
                if buffer.auto_suggest:
                    auto_suggest_function()

            return on_text_insert

        buffer.on_text_insert += create_on_insert_handler()

        def buffer_changed(_):
            """
            When the text in a buffer changes.
            (A paste event is also a change, but not an insert. So we don't
            want to do autocompletions in this case, but we want to propagate
            the on_buffer_changed event.)
            """
            # Trigger on_buffer_changed.
            self.on_buffer_changed.fire()

        buffer.on_text_changed += buffer_changed

    def start_completion(self,
                         buffer_name=None,
                         select_first=False,
                         select_last=False,
                         insert_common_part=False,
                         complete_event=None):
        """
        Start asynchronous autocompletion of this buffer.
        (This will do nothing if a previous completion was still in progress.)
        """
        buffer_name = buffer_name or self.current_buffer_name
        completer = self._async_completers.get(buffer_name)

        if completer:
            completer(select_first=select_first,
                      select_last=select_last,
                      insert_common_part=insert_common_part,
                      complete_event=CompleteEvent(completion_requested=True))

    @property
    def current_buffer_name(self):
        """
        The name of the current  :class:`.Buffer`. (Or `None`.)
        """
        return self.buffers.current_name(self)

    @property
    def current_buffer(self):
        """
        The currently focussed :class:`~.Buffer`.

        (This returns a dummy :class:`.Buffer` when none of the actual buffers
        has the focus. In this case, it's really not practical to check for
        `None` values or catch exceptions every time.)
        """
        return self.buffers.current(self)

    def focus(self, buffer_name):
        """
        Focus the buffer with the given name on the focus stack.
        """
        self.buffers.focus(self, buffer_name)

    def push_focus(self, buffer_name):
        """
        Push to the focus stack.
        """
        self.buffers.push_focus(self, buffer_name)

    def pop_focus(self):
        """
        Pop from the focus stack.
        """
        self.buffers.pop_focus(self)

    @property
    def terminal_title(self):
        """
        Return the current title to be displayed in the terminal.
        When this in `None`, the terminal title remains the original.
        """
        result = self.application.get_title()

        # Make sure that this function returns a unicode object,
        # and not a byte string.
        assert result is None or isinstance(result, six.text_type)
        return result

    @property
    def is_searching(self):
        """
        True when we are searching.
        """
        return self.current_buffer_name == SEARCH_BUFFER

    def reset(self, reset_current_buffer=False):
        """
        Reset everything, for reading the next input.

        :param reset_current_buffer: XXX: not used anymore. The reason for
            having this option in the past was when this CommandLineInterface
            is run multiple times, that we could reset the buffer content from
            the previous run. This is now handled in the AcceptAction.
        """
        # Notice that we don't reset the buffers. (This happens just before
        # returning, and when we have multiple buffers, we clearly want the
        # content in the other buffers to remain unchanged between several
        # calls of `run`. (And the same is true for the focus stack.)

        self._exit_flag = False
        self._abort_flag = False

        self._return_value = None

        self.renderer.reset()
        self.input_processor.reset()
        self.layout.reset()
        self.vi_state.reset()

        # Search new search state. (Does also remember what has to be
        # highlighted.)
        self.search_state = SearchState(
            ignore_case=Condition(lambda: self.is_ignoring_case))

        # Trigger reset event.
        self.on_reset.fire()

    @property
    def in_paste_mode(self):
        """ True when we are in paste mode. """
        return self.application.paste_mode(self)

    @property
    def is_ignoring_case(self):
        """ True when we currently ignore casing. """
        return self.application.ignore_case(self)

    def invalidate(self):
        """
        Thread safe way of sending a repaint trigger to the input event loop.
        """
        # Never schedule a second redraw, when a previous one has not yet been
        # executed. (This should protect against other threads calling
        # 'invalidate' many times, resulting in 100% CPU.)
        if self._invalidated:
            return
        else:
            self._invalidated = True

        # Trigger event.
        self.on_invalidate.fire()

        if self.eventloop is not None:

            def redraw():
                self._invalidated = False
                self._redraw()

            # Call redraw in the eventloop (thread safe).
            # Usually with the high priority, in order to make the application
            # feel responsive, but this can be tuned by changing the value of
            # `max_render_postpone_time`.
            if self.max_render_postpone_time:
                _max_postpone_until = time.time(
                ) + self.max_render_postpone_time
            else:
                _max_postpone_until = None

            self.eventloop.call_from_executor(
                redraw, _max_postpone_until=_max_postpone_until)

    # Depracated alias for 'invalidate'.
    request_redraw = invalidate

    def _redraw(self):
        """
        Render the command line again. (Not thread safe!) (From other threads,
        or if unsure, use :meth:`.CommandLineInterface.invalidate`.)
        """
        # Only draw when no sub application was started.
        if self._is_running and self._sub_cli is None:
            self.render_counter += 1
            self.renderer.render(self, self.layout, is_done=self.is_done)

            # Fire render event.
            self.on_render.fire()

    def _on_resize(self):
        """
        When the window size changes, we erase the current output and request
        again the cursor position. When the CPR answer arrives, the output is
        drawn again.
        """
        # Erase, request position (when cursor is at the start position)
        # and redraw again. -- The order is important.
        self.renderer.erase(leave_alternate_screen=False, erase_title=False)
        self.renderer.request_absolute_cursor_position()
        self._redraw()

    def _load_next_buffer_indexes(self):
        for buff, index in self._next_buffer_indexes.items():
            if buff in self.buffers:
                self.buffers[buff].working_index = index

    def _pre_run(self, pre_run=None):
        " Called during `run`. "
        if pre_run:
            pre_run()

        # Process registered "pre_run_callables" and clear list.
        for c in self.pre_run_callables:
            c()
        del self.pre_run_callables[:]

    def run(self, reset_current_buffer=False, pre_run=None):
        """
        Read input from the command line.
        This runs the eventloop until a return value has been set.

        :param reset_current_buffer: XXX: Not used anymore.
        :param pre_run: Callable that is called right after the reset has taken
            place. This allows custom initialisation.
        """
        assert pre_run is None or callable(pre_run)

        try:
            self._is_running = True

            self.on_start.fire()
            self.reset()

            # Call pre_run.
            self._pre_run(pre_run)

            # Run eventloop in raw mode.
            with self.input.raw_mode():
                self.renderer.request_absolute_cursor_position()
                self._redraw()

                self.eventloop.run(self.input,
                                   self.create_eventloop_callbacks())
        finally:
            # Clean up renderer. (This will leave the alternate screen, if we use
            # that.)

            # If exit/abort haven't been called set, but another exception was
            # thrown instead for some reason, make sure that we redraw in exit
            # mode.
            if not self.is_done:
                self._exit_flag = True
                self._redraw()

            self.renderer.reset()
            self.on_stop.fire()
            self._is_running = False

        # Return result.
        return self.return_value()

    try:
        # The following `run_async` function is compiled at runtime
        # because it contains syntax which is not supported on older Python
        # versions. (A 'return' inside a generator.)
        six.exec_(
            textwrap.dedent('''
        def run_async(self, reset_current_buffer=True, pre_run=None):
            """
            Same as `run`, but this returns a coroutine.

            This is only available on Python >3.3, with asyncio.
            """
            # Inline import, because it slows down startup when asyncio is not
            # needed.
            import asyncio

            @asyncio.coroutine
            def run():
                assert pre_run is None or callable(pre_run)

                try:
                    self._is_running = True

                    self.on_start.fire()
                    self.reset()

                    # Call pre_run.
                    self._pre_run(pre_run)

                    with self.input.raw_mode():
                        self.renderer.request_absolute_cursor_position()
                        self._redraw()

                        yield from self.eventloop.run_as_coroutine(
                                self.input, self.create_eventloop_callbacks())

                    return self.return_value()
                finally:
                    if not self.is_done:
                        self._exit_flag = True
                        self._redraw()

                    self.renderer.reset()
                    self.on_stop.fire()
                    self._is_running = False

            return run()
        '''))
    except SyntaxError:
        # Python2, or early versions of Python 3.
        def run_async(self, reset_current_buffer=True, pre_run=None):
            """
            Same as `run`, but this returns a coroutine.

            This is only available on Python >3.3, with asyncio.
            """
            raise NotImplementedError

    def run_sub_application(self,
                            application,
                            done_callback=None,
                            erase_when_done=False,
                            _from_application_generator=False):
        # `erase_when_done` is deprecated, set Application.erase_when_done instead.
        """
        Run a sub :class:`~prompt_toolkit.application.Application`.

        This will suspend the main application and display the sub application
        until that one returns a value. The value is returned by calling
        `done_callback` with the result.

        The sub application will share the same I/O of the main application.
        That means, it uses the same input and output channels and it shares
        the same event loop.

        .. note:: Technically, it gets another Eventloop instance, but that is
            only a proxy to our main event loop. The reason is that calling
            'stop' --which returns the result of an application when it's
            done-- is handled differently.
        """
        assert isinstance(application, Application)
        assert done_callback is None or callable(done_callback)

        if self._sub_cli is not None:
            raise RuntimeError('Another sub application started already.')

        # Erase current application.
        if not _from_application_generator:
            self.renderer.erase()

        # Callback when the sub app is done.
        def done():
            # Redraw sub app in done state.
            # and reset the renderer. (This reset will also quit the alternate
            # screen, if the sub application used that.)
            sub_cli._redraw()
            if erase_when_done or application.erase_when_done:
                sub_cli.renderer.erase()
            sub_cli.renderer.reset()
            sub_cli._is_running = False  # Don't render anymore.

            self._sub_cli = None

            # Restore main application.
            if not _from_application_generator:
                self.renderer.request_absolute_cursor_position()
                self._redraw()

            # Deliver result.
            if done_callback:
                done_callback(sub_cli.return_value())

        # Create sub CommandLineInterface.
        sub_cli = CommandLineInterface(application=application,
                                       eventloop=_SubApplicationEventLoop(
                                           self, done),
                                       input=self.input,
                                       output=self.output)
        sub_cli._is_running = True  # Allow rendering of sub app.

        sub_cli._redraw()
        self._sub_cli = sub_cli

    def exit(self):
        """
        Set exit. When Control-D has been pressed.
        """
        on_exit = self.application.on_exit
        self._exit_flag = True
        self._redraw()

        if on_exit == AbortAction.RAISE_EXCEPTION:

            def eof_error():
                raise EOFError()

            self._set_return_callable(eof_error)

        elif on_exit == AbortAction.RETRY:
            self.reset()
            self.renderer.request_absolute_cursor_position()
            self.current_buffer.reset()

        elif on_exit == AbortAction.RETURN_NONE:
            self.set_return_value(None)

    def abort(self):
        """
        Set abort. When Control-C has been pressed.
        """
        on_abort = self.application.on_abort
        self._abort_flag = True
        self._redraw()

        if on_abort == AbortAction.RAISE_EXCEPTION:

            def keyboard_interrupt():
                raise KeyboardInterrupt()

            self._set_return_callable(keyboard_interrupt)

        elif on_abort == AbortAction.RETRY:
            self.reset()
            self.renderer.request_absolute_cursor_position()
            self.current_buffer.reset()

        elif on_abort == AbortAction.RETURN_NONE:
            self.set_return_value(None)

    # Deprecated aliase for exit/abort.
    set_exit = exit
    set_abort = abort

    def set_return_value(self, document):
        """
        Set a return value. The eventloop can retrieve the result it by calling
        `return_value`.
        """
        self._set_return_callable(lambda: document)
        self._redraw(
        )  # Redraw in "done" state, after the return value has been set.

    def _set_return_callable(self, value):
        assert callable(value)
        self._return_value = value

        if self.eventloop:
            self.eventloop.stop()

    def run_in_terminal(self, func, render_cli_done=False, cooked_mode=True):
        """
        Run function on the terminal above the prompt.

        What this does is first hiding the prompt, then running this callable
        (which can safely output to the terminal), and then again rendering the
        prompt which causes the output of this function to scroll above the
        prompt.

        :param func: The callable to execute.
        :param render_cli_done: When True, render the interface in the
                'Done' state first, then execute the function. If False,
                erase the interface first.
        :param cooked_mode: When True (the default), switch the input to
                cooked mode while executing the function.

        :returns: the result of `func`.
        """
        # Draw interface in 'done' state, or erase.
        if render_cli_done:
            self._return_value = True
            self._redraw()
            self.renderer.reset()  # Make sure to disable mouse mode, etc...
        else:
            self.renderer.erase()
        self._return_value = None

        # Run system command.
        if cooked_mode:
            with self.input.cooked_mode():
                result = func()
        else:
            result = func()

        # Redraw interface again.
        self.renderer.reset()
        self.renderer.request_absolute_cursor_position()
        self._redraw()

        return result

    def run_application_generator(self, coroutine, render_cli_done=False):
        """
        EXPERIMENTAL
        Like `run_in_terminal`, but takes a generator that can yield Application instances.

        Example:

            def f():
                yield Application1(...)
                print('...')
                yield Application2(...)
            cli.run_in_terminal_async(f)

        The values which are yielded by the given coroutine are supposed to be
        `Application` instances that run in the current CLI, all other code is
        supposed to be CPU bound, so except for yielding the applications,
        there should not be any user interaction or I/O in the given function.
        """
        # Draw interface in 'done' state, or erase.
        if render_cli_done:
            self._return_value = True
            self._redraw()
            self.renderer.reset()  # Make sure to disable mouse mode, etc...
        else:
            self.renderer.erase()
        self._return_value = None

        # Loop through the generator.
        g = coroutine()
        assert isinstance(g, types.GeneratorType)

        def step_next(send_value=None):
            " Execute next step of the coroutine."
            try:
                # Run until next yield, in cooked mode.
                with self.input.cooked_mode():
                    result = g.send(send_value)
            except StopIteration:
                done()
            except:
                done()
                raise
            else:
                # Process yielded value from coroutine.
                assert isinstance(result, Application)
                self.run_sub_application(result,
                                         done_callback=step_next,
                                         _from_application_generator=True)

        def done():
            # Redraw interface again.
            self.renderer.reset()
            self.renderer.request_absolute_cursor_position()
            self._redraw()

        # Start processing coroutine.
        step_next()

    def run_system_command(self, command):
        """
        Run system command (While hiding the prompt. When finished, all the
        output will scroll above the prompt.)

        :param command: Shell command to be executed.
        """
        def wait_for_enter():
            """
            Create a sub application to wait for the enter key press.
            This has two advantages over using 'input'/'raw_input':
            - This will share the same input/output I/O.
            - This doesn't block the event loop.
            """
            from .shortcuts import create_prompt_application

            registry = Registry()

            @registry.add_binding(Keys.ControlJ)
            @registry.add_binding(Keys.ControlM)
            def _(event):
                event.cli.set_return_value(None)

            application = create_prompt_application(
                message='Press ENTER to continue...',
                key_bindings_registry=registry)
            self.run_sub_application(application)

        def run():
            # Try to use the same input/output file descriptors as the one,
            # used to run this application.
            try:
                input_fd = self.input.fileno()
            except AttributeError:
                input_fd = sys.stdin.fileno()
            try:
                output_fd = self.output.fileno()
            except AttributeError:
                output_fd = sys.stdout.fileno()

            # Run sub process.
            # XXX: This will still block the event loop.
            p = Popen(command, shell=True, stdin=input_fd, stdout=output_fd)
            p.wait()

            # Wait for the user to press enter.
            wait_for_enter()

        self.run_in_terminal(run)

    def suspend_to_background(self, suspend_group=True):
        """
        (Not thread safe -- to be called from inside the key bindings.)
        Suspend process.

        :param suspend_group: When true, suspend the whole process group.
            (This is the default, and probably what you want.)
        """
        # Only suspend when the opperating system supports it.
        # (Not on Windows.)
        if hasattr(signal, 'SIGTSTP'):

            def run():
                # Send `SIGSTP` to own process.
                # This will cause it to suspend.

                # Usually we want the whole process group to be suspended. This
                # handles the case when input is piped from another process.
                if suspend_group:
                    os.kill(0, signal.SIGTSTP)
                else:
                    os.kill(os.getpid(), signal.SIGTSTP)

            self.run_in_terminal(run)

    def print_tokens(self, tokens, style=None):
        """
        Print a list of (Token, text) tuples to the output.
        (When the UI is running, this method has to be called through
        `run_in_terminal`, otherwise it will destroy the UI.)

        :param style: Style class to use. Defaults to the active style in the CLI.
        """
        print_tokens(self.output, tokens, style or self.application.style)

    @property
    def is_exiting(self):
        """
        ``True`` when the exit flag as been set.
        """
        return self._exit_flag

    @property
    def is_aborting(self):
        """
        ``True`` when the abort flag as been set.
        """
        return self._abort_flag

    @property
    def is_returning(self):
        """
        ``True`` when a return value has been set.
        """
        return self._return_value is not None

    def return_value(self):
        """
        Get the return value. Not that this method can throw an exception.
        """
        # Note that it's a method, not a property, because it can throw
        # exceptions.
        if self._return_value:
            return self._return_value()

    @property
    def is_done(self):
        return self.is_exiting or self.is_aborting or self.is_returning

    def _create_async_completer(self, buffer):
        """
        Create function for asynchronous autocompletion.
        (Autocomplete in other thread.)
        """
        complete_thread_running = [False]  # By ref.

        def completion_does_nothing(document, completion):
            """
            Return `True` if applying this completion doesn't have any effect.
            (When it doesn't insert any new text.
            """
            text_before_cursor = document.text_before_cursor
            replaced_text = text_before_cursor[len(text_before_cursor) +
                                               completion.start_position:]
            return replaced_text == completion.text

        def async_completer(select_first=False,
                            select_last=False,
                            insert_common_part=False,
                            complete_event=None):
            document = buffer.document
            complete_event = complete_event or CompleteEvent(
                text_inserted=True)

            # Don't start two threads at the same time.
            if complete_thread_running[0]:
                return

            # Don't complete when we already have completions.
            if buffer.complete_state or not buffer.completer:
                return

            # Otherwise, get completions in other thread.
            complete_thread_running[0] = True

            def run():
                completions = list(
                    buffer.completer.get_completions(document, complete_event))

                def callback():
                    """
                    Set the new complete_state in a safe way. Don't replace an
                    existing complete_state if we had one. (The user could have
                    pressed 'Tab' in the meantime. Also don't set it if the text
                    was changed in the meantime.
                    """
                    complete_thread_running[0] = False

                    # When there is only one completion, which has nothing to add, ignore it.
                    if (len(completions) == 1 and completion_does_nothing(
                            document, completions[0])):
                        del completions[:]

                    # Set completions if the text was not yet changed.
                    if buffer.text == document.text and \
                            buffer.cursor_position == document.cursor_position and \
                            not buffer.complete_state:

                        set_completions = True
                        select_first_anyway = False

                        # When the common part has to be inserted, and there
                        # is a common part.
                        if insert_common_part:
                            common_part = get_common_complete_suffix(
                                document, completions)
                            if common_part:
                                # Insert the common part, update completions.
                                buffer.insert_text(common_part)
                                if len(completions) > 1:
                                    # (Don't call `async_completer` again, but
                                    # recalculate completions. See:
                                    # https://github.com/ipython/ipython/issues/9658)
                                    completions[:] = [
                                        c.new_completion_from_position(
                                            len(common_part))
                                        for c in completions
                                    ]
                                else:
                                    set_completions = False
                            else:
                                # When we were asked to insert the "common"
                                # prefix, but there was no common suffix but
                                # still exactly one match, then select the
                                # first. (It could be that we have a completion
                                # which does * expansion, like '*.py', with
                                # exactly one match.)
                                if len(completions) == 1:
                                    select_first_anyway = True

                        if set_completions:
                            buffer.set_completions(completions=completions,
                                                   go_to_first=select_first
                                                   or select_first_anyway,
                                                   go_to_last=select_last)
                        self.invalidate()
                    elif not buffer.complete_state:
                        # Otherwise, restart thread.
                        async_completer()

                if self.eventloop:
                    self.eventloop.call_from_executor(callback)

            self.eventloop.run_in_executor(run)

        return async_completer

    def _create_auto_suggest_function(self, buffer):
        """
        Create function for asynchronous auto suggestion.
        (AutoSuggest in other thread.)
        """
        suggest_thread_running = [False]  # By ref.

        def async_suggestor():
            document = buffer.document

            # Don't start two threads at the same time.
            if suggest_thread_running[0]:
                return

            # Don't suggest when we already have a suggestion.
            if buffer.suggestion or not buffer.auto_suggest:
                return

            # Otherwise, get completions in other thread.
            suggest_thread_running[0] = True

            def run():
                suggestion = buffer.auto_suggest.get_suggestion(
                    self, buffer, document)

                def callback():
                    suggest_thread_running[0] = False

                    # Set suggestion only if the text was not yet changed.
                    if buffer.text == document.text and \
                            buffer.cursor_position == document.cursor_position:

                        # Set suggestion and redraw interface.
                        buffer.suggestion = suggestion
                        self.invalidate()
                    else:
                        # Otherwise, restart thread.
                        async_suggestor()

                if self.eventloop:
                    self.eventloop.call_from_executor(callback)

            self.eventloop.run_in_executor(run)

        return async_suggestor

    def stdout_proxy(self, raw=False):
        """
        Create an :class:`_StdoutProxy` class which can be used as a patch for
        `sys.stdout`. Writing to this proxy will make sure that the text
        appears above the prompt, and that it doesn't destroy the output from
        the renderer.

        :param raw: (`bool`) When True, vt100 terminal escape sequences are not
                    removed/escaped.
        """
        return _StdoutProxy(self, raw=raw)

    def patch_stdout_context(self,
                             raw=False,
                             patch_stdout=True,
                             patch_stderr=True):
        """
        Return a context manager that will replace ``sys.stdout`` with a proxy
        that makes sure that all printed text will appear above the prompt, and
        that it doesn't destroy the output from the renderer.

        :param patch_stdout: Replace `sys.stdout`.
        :param patch_stderr: Replace `sys.stderr`.
        """
        return _PatchStdoutContext(self.stdout_proxy(raw=raw),
                                   patch_stdout=patch_stdout,
                                   patch_stderr=patch_stderr)

    def create_eventloop_callbacks(self):
        return _InterfaceEventLoopCallbacks(self)
Exemplo n.º 56
0
 def run(self):
     try:
         logger.debug('Executing script: {}'.format(self.script))
         six.exec_(self.script, globals(), None)
     except Exception as e:
         logger.error('Error executing script: {}'.format(e))
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import six

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup
from os.path import abspath, dirname, join

CURDIR = dirname(abspath(__file__))

#from AristaLibrary import __version__, __author__
fileloc = join(CURDIR, 'AristaLibrary', 'version.py')
six.exec_(compile(open(fileloc).read(), fileloc, 'exec'))
with open(join(CURDIR, 'README.rst')) as readme:
    README = readme.read()

setup(
    name='robotframework-aristalibrary',
    version=VERSION,
    description='Python Robot Framework Library for EOS devices',
    long_description=README,
    author='Arista EOS+ Consulting Services',
    author_email='*****@*****.**',
    url='https://aristanetworks.github.io/robotframework-aristalibrary/',
    download_url=
    'https://github.com/aristanetworks/robotframework-aristalibrary/tarball/%s'
    % VERSION,
    license='BSD-3',
Exemplo n.º 58
0
    def test_namespace_plus_model_should_be_unique(self):
        MainBike = Bike

        def sub_test():
            with self.assertRaises(ImplementationError):

                class Bike(TestRedisModel):
                    name = fields.StringField()

            class Bike2(TestRedisModel):
                name = fields.StringField()
                namespace = 'sub-tests'

            self.assertNotEqual(MainBike._name, Bike2._name)

            with self.assertRaises(ImplementationError):

                class Bike2(TestRedisModel):
                    name = fields.StringField()
                    namespace = 'sub-tests'

        sub_test()

        # check also with metaclass inheritance
        class TestMetaClass(model.MetaRedisModel):
            def __new__(mcs, name, base, attrs):
                return super(TestMetaClass,
                             mcs).__new__(mcs, name, base, attrs)

        # normal class what will work

        # in python2 it should fail using `__metaclass__`
        if six.PY2:

            class Bike3(TestRedisModel):
                __metaclass__ = TestMetaClass
                name = fields.StringField()
                namespace = 'sub-tests2'

            with self.assertRaises(ImplementationError):

                class Bike3(TestRedisModel):
                    __metaclass__ = TestMetaClass
                    name = fields.StringField()
                    namespace = 'sub-tests2'

        # in python3 it should fail using `metaclass=`
        if six.PY3:
            # we use exec else it will be a syntax error in python 2
            with self.assertRaises(ImplementationError):
                six.exec_("""\
class Bike4(TestRedisModel, metaclass=TestMetaClass):
    name = fields.StringField()
    namespace = 'sub-tests2'
class Bike4(TestRedisModel, metaclass=TestMetaClass):
    name = fields.StringField()
    namespace = 'sub-tests2'
                """)

        # it should fail using six.with_metaclass
        class Bike5(six.with_metaclass(TestMetaClass, TestRedisModel)):
            name = fields.StringField()
            namespace = 'sub-tests2'

        with self.assertRaises(ImplementationError):

            class Bike5(six.with_metaclass(TestMetaClass, TestRedisModel)):
                name = fields.StringField()
                namespace = 'sub-tests2'

        # it should fail using future.utils.with_metaclass
        class Bike6(future.utils.with_metaclass(TestMetaClass,
                                                TestRedisModel)):
            name = fields.StringField()
            namespace = 'sub-tests2'

        with self.assertRaises(ImplementationError):

            class Bike6(
                    future.utils.with_metaclass(TestMetaClass,
                                                TestRedisModel)):
                name = fields.StringField()
                namespace = 'sub-tests2'