def dotted_path_in_sys_path(sys_path, module_path): """ Returns the dotted path inside a sys.path as a list of names. """ # First remove the suffix. for suffix in all_suffixes(): if module_path.endswith(suffix): module_path = module_path[:-len(suffix)] break else: # There should always be a suffix in a valid Python file on the path. return None if module_path.startswith(os.path.sep): # The paths in sys.path most of the times don't end with a slash. module_path = module_path[1:] for p in sys_path: if module_path.startswith(p): rest = module_path[len(p):] if rest: split = rest.split(os.path.sep) for string in split: if not string or '.' in string: return None return split return None
def transform_path_to_dotted(sys_path, module_path): """ Returns the dotted path inside a sys.path as a list of names. e.g. >>> transform_path_to_dotted(["/foo"], '/foo/bar/baz.py') ['bar', 'baz'] Returns None if the path doesn't really resolve to anything. """ # First remove the suffix. for suffix in all_suffixes(): if module_path.endswith(suffix): module_path = module_path[:-len(suffix)] break # Once the suffix was removed we are using the files as we know them. This # means that if someone uses an ending like .vim for a Python file, .vim # will be part of the returned dotted part. for p in sys_path: if module_path.startswith(p): rest = module_path[len(p):] if rest.startswith(os.path.sep): # Remove a slash in cases it's still there. rest = rest[1:] if rest: split = rest.split(os.path.sep) for string in split: if not string: return None return split return None
def transform_path_to_dotted(sys_path, module_path): """ Returns the dotted path inside a sys.path as a list of names. e.g. >>> transform_path_to_dotted(["/foo"], '/foo/bar/baz.py') ['bar', 'baz'] Returns None if the path doesn't really resolve to anything. """ # First remove the suffix. for suffix in all_suffixes(): if module_path.endswith(suffix): module_path = module_path[:-len(suffix)] break # Once the suffix was removed we are using the files as we know them. This # means that if someone uses an ending like .vim for a Python file, .vim # will be part of the returned dotted part. for p in sys_path: if module_path.startswith(p): rest = module_path[len(p):] if rest.startswith(os.path.sep): # Remove a slash in cases it's still there. rest = rest[1:] if rest: split = rest.split(os.path.sep) for string in split: if not string: return None return split return None
def dotted_path_in_sys_path(sys_path, module_path): """ Returns the dotted path inside a sys.path. """ # First remove the suffix. for suffix in all_suffixes(): if module_path.endswith(suffix): module_path = module_path[:-len(suffix)] break else: # There should always be a suffix in a valid Python file on the path. return None if module_path.startswith(os.path.sep): # The paths in sys.path most of the times don't end with a slash. module_path = module_path[1:] for p in sys_path: if module_path.startswith(p): rest = module_path[len(p):] if rest: split = rest.split(os.path.sep) for string in split: if not string or '.' in string: return None return '.'.join(split) return None
def _get_init_path(directory_path): """ The __init__ file can be searched in a directory. If found return it, else None. """ for suffix in all_suffixes(): path = os.path.join(directory_path, '__init__' + suffix) if os.path.exists(path): return path return None
def _get_init_path(directory_path): """ The __init__ file can be searched in a directory. If found return it, else None. """ for suffix in all_suffixes(): path = os.path.join(directory_path, '__init__' + suffix) if os.path.exists(path): return path return None
def _get_init_directory(self): """ :return: The path to the directory of a package. None in case it's not a package. """ for suffix in all_suffixes(): ending = '__init__' + suffix py__file__ = self.py__file__() if py__file__ is not None and py__file__.endswith(ending): # Remove the ending, including the separator. return self.py__file__()[:-len(ending) - 1] return None
def _get_init_directory(self): """ :return: The path to the directory of a package. None in case it's not a package. """ for suffix in all_suffixes(): ending = '__init__' + suffix py__file__ = self.py__file__() if py__file__ is not None and py__file__.endswith(ending): # Remove the ending, including the separator. return self.py__file__()[:-len(ending) - 1] return None
def remove_python_path_suffix(path): for suffix in all_suffixes(): if path.endswith(suffix): path = path[:-len(suffix)] break return path