Exemplo n.º 1
0
 def _load_file(self, path):
   if self._env_home:
     filename = os.path.join(self._env_home, path)
     with open(filename, "r") as f:
       return filename, f.read()
   else:
     filepath = os.path.join(self._root, path)
     return filepath, pytype_source_utils.load_text_file(filepath)
Exemplo n.º 2
0
 def _file_exists(self, relpath):
     """Checks whether the given path, relative to the typeshed root, exists."""
     if self._env_home:
         return os.path.exists(os.path.join(self._root, relpath))
     try:
         # For a non-par pytype installation, load_text_file will either succeed,
         # raise FileNotFoundError, or raise IsADirectoryError.
         # For a par installation, load_text_file will raise FileNotFoundError for
         # both a nonexistent file and a directory.
         pytype_source_utils.load_text_file(
             os.path.join("typeshed", relpath))
     except FileNotFoundError:
         try:
             # For a non-par installation, we know at this point that relpath does
             # not exist, so _list_files will always raise NoSuchDirectory. For a par
             # installation, we use _list_files to check whether the directory
             # exists; a non-existent directory will produce an empty generator.
             next(self._list_files(relpath))
         except (pytype_source_utils.NoSuchDirectory, StopIteration):
             return False
     except IsADirectoryError:
         return True
     return True
Exemplo n.º 3
0
def GetPredefinedFile(stubs_subdir, module, extension=".pytd",
                      as_package=False):
  """Get the contents of a predefined PyTD, typically with a file name *.pytd.

  Arguments:
    stubs_subdir: the directory, typically "builtins" or "stdlib"
    module: module name (e.g., "sys" or "__builtins__")
    extension: either ".pytd" or ".py"
    as_package: try the module as a directory with an __init__ file
  Returns:
    The contents of the file
  Raises:
    IOError: if file not found
  """
  parts = module.split(".")
  if as_package:
    parts.append("__init__")
  mod_path = os.path.join(*parts) + extension
  path = os.path.join("stubs", stubs_subdir, mod_path)
  return path, pytype_source_utils.load_text_file(path)