Exemplo n.º 1
0
    def run(self, debugger, record=None, print_result=False):
        try:
            with open(self.path) as f:
                self.script = medi.Script(f.read(), path=self.path)
            kwargs = {}
            if self.operation == 'goto':
                kwargs['follow_imports'] = random.choice([False, True])

            self.objects = getattr(self.script,
                                   self.operation)(self.line, self.column,
                                                   **kwargs)
            if print_result:
                print("{path}: Line {line} column {column}".format(
                    **self.__dict__))
                self.show_location(self.line, self.column)
                self.show_operation()
        except Exception:
            self.traceback = traceback.format_exc()
            if record is not None:
                call_args = (self.operation, self.path, self.line, self.column,
                             self.traceback)
                with open(record, 'w') as f:
                    json.dump(call_args, f)
            self.show_errors()
            if debugger:
                einfo = sys.exc_info()
                pdb = __import__(debugger)
                if debugger == 'pudb':
                    pdb.post_mortem(einfo[2], einfo[0], einfo[1])
                else:
                    pdb.post_mortem(einfo[2])
            exit(1)
Exemplo n.º 2
0
def test_follow_import_incomplete(Script, environment):
    """
    Completion on incomplete imports should always take the full completion
    to do any type inference.
    """
    datetime = check_follow_definition_types(Script, "import itertool")
    assert datetime == ['module']

    # empty `from * import` parts
    itert = medi.Script("from itertools import ").complete()
    definitions = [d for d in itert if d.name == 'chain']
    assert len(definitions) == 1
    assert [d.type for d in definitions[0].infer()] == ['class']

    # incomplete `from * import` part
    datetime = check_follow_definition_types(Script,
                                             "from datetime import datetim")
    if environment.version_info.major == 2:
        assert datetime == ['class']
    else:
        assert set(datetime) == {'class', 'instance'
                                 }  # py3: builtin and pure py version
    # os.path check
    ospath = check_follow_definition_types(Script,
                                           "from os.path import abspat")
    assert set(ospath) == {'function'}

    # alias
    alias = check_follow_definition_types(Script, "import io as abcd; abcd")
    assert alias == ['module']
Exemplo n.º 3
0
 def refactor(self, environment):
     project = medi.Project(os.path.join(test_dir, 'refactor'))
     script = medi.Script(self._code,
                          path=self._path,
                          project=project,
                          environment=environment)
     refactor_func = getattr(script, self.refactor_type)
     return refactor_func(self._line_nr, self._index, **self._kwargs)
Exemplo n.º 4
0
def run(code, index, infer=False):
    start = time.time()
    script = medi.Script(code)
    if infer:
        result = script.infer()
    else:
        result = script.complete()
    print('Used %ss for the %sth run.' % (time.time() - start, index + 1))
    return result
Exemplo n.º 5
0
def run():
    start = time.time()
    print('Process Memory before: %skB' % process_memory())
    # After this the module should be cached.
    # Need to invent a path so that it's really cached.
    medi.Script(wx_core, path='foobar.py').complete()

    gc.collect()  # make sure that it's all fair and the gc did its job.
    print('Process Memory after: %skB' % process_memory())

    print(objgraph.most_common_types(limit=50))
    print('\nIt took %s seconds to parse the file.' % (time.time() - start))
Exemplo n.º 6
0
    def run(self, compare_cb, environment):
        def typ_str(inst):
            return 'warning ' if isinstance(inst, Warning) else ''

        analysis = medi.Script(
            self._source,
            path=self._path,
            environment=environment,
        )._analysis()
        analysis = [(r.line, r.column, typ_str(r) + r.name)
                    for r in analysis]
        compare_cb(self, analysis, self.collect_comparison())
Exemplo n.º 7
0
def test_demo():
    code = "first: abc + 'ss'\nsecond: a"
    demo = medi.Script(code, language="demo")

    project = demo._inference_state.project
    project.save()
    path = project._path
    p2 = medi.api.Project.load(path)
    assert project.language == p2.language

    arr = demo.complete()
    assert len(arr) == 1
    assert arr[0].type == 'keyword'
    assert arr[0].name == 'ademo'
Exemplo n.º 8
0
def _complete():
    import medi
    import pdb

    if '-d' in sys.argv:
        sys.argv.remove('-d')
        medi.set_debug_function()

    try:
        completions = medi.Script(sys.argv[2]).complete()
        for c in completions:
            c.docstring()
            c.type
    except Exception as e:
        print(repr(e))
        pdb.post_mortem()
    else:
        print(completions)
Exemplo n.º 9
0
 def check(self, source, desired):
     script = medi.Script(textwrap.dedent(source),
                          environment=self.environment)
     definitions = script.get_names()
     full_names = [d.full_name for d in definitions]
     self.assertEqual(full_names, desired)
Exemplo n.º 10
0
 def script(self, environment):
     return medi.Script(self.source, path=self.path, environment=environment)