コード例 #1
0
def get_module_name(filename, pythonpath):
  """Get the module name, or None if we can't determine it."""
  if filename:
    filename = os.path.normpath(filename)
    # Keep path '' as is; infer_module will handle it.
    pythonpath = [path and os.path.normpath(path) for path in pythonpath]
    return module_utils.infer_module(filename, pythonpath).name
コード例 #2
0
ファイル: pytype_runner.py プロジェクト: TheBenPayton/pytype
 def yield_sorted_modules(self):
     """Yield modules from our sorted source files."""
     for files in self.sorted_source_files:
         modules = []
         for f in files:
             # Report errors for files we are analysing directly.
             report_errors = f in self.filenames
             # We'll use this function to report skipped files.
             report = logging.warning if report_errors else logging.info
             if not f.endswith('.py'):
                 report('Skipping non-Python file: %s', f)
                 continue
             module = module_utils.infer_module(f,
                                                self.pythonpath,
                                                preserve_init=True)
             if not any(module.path.startswith(d) for d in self.pythonpath):
                 report('Skipping file not in pythonpath: %s', f)
                 continue
             modules.append((module, report_errors))
         if len(modules) == 1:
             yield modules[0]
         else:
             # If we have a cycle we run pytype over the files twice, ignoring errors
             # the first time so that we don't fail on missing dependencies.
             for module, _ in modules:
                 yield module, False
             for module_and_report_errors in modules:
                 yield module_and_report_errors
コード例 #3
0
 def test_not_found(self):
     mod = module_utils.infer_module(expand("bar/baz.py"), ["foo"])
     expected_target = expand("bar/baz.py")
     expected_name, _ = os.path.splitext(
         expected_target.replace(os.sep, "."))
     self.assert_module_equal(mod, "", expected_target, expected_name)
コード例 #4
0
 def test_multiple_paths(self):
     pythonpath = [expand("foo"), expand("bar/baz"), expand("bar")]
     mod = module_utils.infer_module(expand("bar/baz/qux.py"), pythonpath)
     self.assert_module_equal(mod, expand("bar/baz"), "qux.py", "qux")
     mod = module_utils.infer_module(expand("bar/qux.py"), pythonpath)
     self.assert_module_equal(mod, expand("bar"), "qux.py", "qux")
コード例 #5
0
 def test_name_in_package(self):
     mod = module_utils.infer_module(expand("foo/bar/baz.py"),
                                     [expand("foo")])
     self.assert_module_equal(mod, expand("foo"), "bar/baz.py", "bar.baz")
コード例 #6
0
 def test_simple_name(self):
     mod = module_utils.infer_module(expand("foo/bar.py"), [expand("foo")])
     self.assert_module_equal(mod, expand("foo"), "bar.py", "bar")