Code¶
This script provides search for an importing path of a module in the PYTHON PATH of the currently activated virtual environment.
In used idea, it is a some analog of whatprovides command of yum package manager, but works with python modules instead of packages.
- Author:
- shmakovpn <shmakovpn@yandex.ru>
- Date:
- 2020-06-21
-
class
whatprovides.Declaration(declaration_type: whatprovides.DeclarationType, name: str, module_path: str)[source]¶ Declaration of a name in python
Parameters: - declaration_type (DeclarationType) – a type of a declaration (variable, def, class …)
- name (str) – a name of a variable or a function or a class
- module_path (str) – a path to python_module where the declaration was found
-
class
whatprovides.DeclarationType(name: str, pattern: Pattern[AnyStr])[source]¶ Python declaration type
- Declaration type can be one of this:
- var: some_variable = some_value
- def: def some_func(arg, ):
- class: class SomeClass(SomeParent):
Parameters: - name (str) – a name of a declaration (var, def, class)
- pattern (Pattern) – a pattern to match a declaration in a string
-
search(line: str) → str[source]¶ Search in a line using self.pattern, returns the name matching group or an empty string, if search was failed
Parameters: line (str) – a line to search Returns: the name matching group or an empty string if search was failed Rtype str: Raises: KeyError – This exception raises if search result does not contains the ‘name’ group
-
class
whatprovides.FileLine(file_path: str, line_number: int, line: str)[source]¶ A line of a file
Parameters: - file_path (str) – A path to a file contains this line
- line_number (int) – A number of this line
- line (str) – a content of this line
-
whatprovides.declaration_types= [<whatprovides.DeclarationType object>, <whatprovides.DeclarationType object>, <whatprovides.DeclarationType object>]¶ types of declaration used in python code (e.g. variables, function, classes)
-
whatprovides.filter_declaration(search: str, declarations: Iterator[whatprovides.Declaration]) → Iterator[whatprovides.Declaration][source]¶ This generator will filter declarations by name of declaration case sensitive
Parameters: - search (str) – A part of a name for case sensitive search
- declarations (Iterator[Declaration]) – An iterable of declarations
Returns: generator of filtered declarations
Return type: Iterator[Declaration]
-
whatprovides.filter_delaration_type(declarations: Iterator[whatprovides.Declaration], remained_types: List[whatprovides.DeclarationType]) → Iterator[whatprovides.Declaration][source]¶ This generator filters instances of Declaration by a list of declaration types
Parameters: - declarations (Iterator[Declarations]) – an iterable of Declarations to filter
- remained_types (List[DeclarationType]) – only declarations of type from this list will remain.
Returns: filtered declarations
Return type: Iterator[Declarations]
-
whatprovides.get_declarations(lines: Iterator[whatprovides.FileLine]) → Iterator[whatprovides.Declaration][source]¶ This generator creates instances of declaration from lines of code which contains declaration of variables or functions or classes
Parameters: lines (Iterator[FileLine]) – an iterable of lines of code Returns: a generator of instances of declaration Return type: Iterator[Declaration]
-
whatprovides.get_file_lines(file_path: str, encoding: str) → Iterator[whatprovides.FileLine][source]¶ This generator creates instances of a line of a file
Parameters: - file_path (str) – A path to a file
- encoding (str) – An encoding of a file
Returns: a generator of instances of lines of a file
Return type: Iterator[FileLine]
-
whatprovides.get_files_lines(file_paths: Iterator[str]) → Iterator[whatprovides.FileLine][source]¶ This generator creates instances of a line of file from paths to files
Parameters: file_paths (Iterator[str]) – An iterable of file paths Returns: a generator of instances of lines of files Return type: Iterator[FileLine]
-
whatprovides.get_paths(paths: Iterator[str]) → Iterator[str][source]¶ This generator filters an iterable of paths, remaining only python libraries folders paths
Parameters: paths (Iterator[str]) – An iterable of paths Returns: a generator of paths of python libraries folders Return type: Iterator[str]
-
whatprovides.get_python_files(search_paths: Iterator[str]) → Iterator[str][source]¶ This generator yields paths of python files from search paths
Parameters: search_paths (Iterator[str]) – An iterable of search paths Returns: a generator of paths of python files Return type: Iterator[str]
-
whatprovides.ifilter_declaration(search: str, declarations: Iterator[whatprovides.Declaration]) → Iterator[whatprovides.Declaration][source]¶ This generator will filter declarations by name of declaration case insensitive :param search: A part of a name of case insensitive search :type search: str :param declarations: An iterable of declarations :type declarations: Iterator[Declaration] :return: generator of filtered declarations :rtype: Iterator[Declaration]
-
whatprovides.re_filter_declaration(search: Pattern[AnyStr], declarations: Iterator[whatprovides.Declaration]) → Iterator[whatprovides.Declaration][source]¶ This generator will filter declarations by name if declaration using the compiled regular expression :param search: a pattern to match name of declaration :type search: Pattern :param declarations: An iterable of declarations :type declarations: Iterator[Declaration] :return: generator of filtered declarations :rtype: Iterator[Declaration]