def get_default_project(path=None): """ If a project is not defined by the user, Jedi tries to define a project by itself as well as possible. Jedi traverses folders until it finds one of the following: 1. A ``.jedi/config.json`` 2. One of the following files: ``setup.py``, ``.git``, ``.hg``, ``requirements.txt`` and ``MANIFEST.in``. """ if path is None: path = os.getcwd() check = os.path.realpath(path) probable_path = None first_no_init_file = None for dir in traverse_parents(check, include_current=True): try: return Project.load(dir) except (FileNotFoundError, IsADirectoryError, PermissionError): pass except NotADirectoryError: continue if first_no_init_file is None: if os.path.exists(os.path.join(dir, '__init__.py')): # In the case that a __init__.py exists, it's in 99% just a # Python package and the project sits at least one level above. continue else: first_no_init_file = dir if _is_django_path(dir): project = Project(dir) project._django = True return project if probable_path is None and _is_potential_project(dir): probable_path = dir if probable_path is not None: # TODO search for setup.py etc return Project(probable_path) if first_no_init_file is not None: return Project(first_no_init_file) curdir = path if os.path.isdir(path) else os.path.dirname(path) return Project(curdir)
def _get_sys_path(self, inference_state, add_parent_paths=True, add_init_paths=False): """ Keep this method private for all users of jedi. However internally this one is used like a public method. """ suffixed = list(self.added_sys_path) prefixed = [] if self._sys_path is None: sys_path = list(self._get_base_sys_path(inference_state)) else: sys_path = list(self._sys_path) if self._smart_sys_path: prefixed.append(self._path) if inference_state.script_path is not None: suffixed += discover_buildout_paths( inference_state, inference_state.script_path) if add_parent_paths: # Collect directories in upward search by: # 1. Skipping directories with __init__.py # 2. Stopping immediately when above self._path traversed = [] for parent_path in traverse_parents( inference_state.script_path): if parent_path == self._path or not parent_path.startswith( self._path): break if not add_init_paths \ and os.path.isfile(os.path.join(parent_path, "__init__.py")): continue traversed.append(parent_path) # AFAIK some libraries have imports like `foo.foo.bar`, which # leads to the conclusion to by default prefer longer paths # rather than shorter ones by default. suffixed += reversed(traversed) if self._django: prefixed.append(self._path) path = prefixed + sys_path + suffixed return list(_force_unicode_list(_remove_duplicates_from_path(path)))
def _get_parent_dir_with_file(path, filename): for parent in traverse_parents(path): if os.path.isfile(os.path.join(parent, filename)): return parent return None