Internal API

pypicontents

pypicontents is just black magic.

PyPIContents is a package that studies the codebase of your project in search for internal and external imports. It then discards the imports that are satisfied with internal code or with the standard library and finally searches the PyPIContents index to list which packages satisfy your imports.

pypicontents.cli module

pypicontents.cli is a module for handling the command line interface.

This module handles the commands for using PyPIContents. It also parses parameters, show help, version and controls the logging level.

pypicontents.cli.commandline(argv=None)[source]

Configure ArgumentParser to accept custom arguments and commands.

Parameters:argv

a list of commandline arguments like sys.argv. For example:

['-v', '--loglevel', 'INFO']
Returns:a Namespace object.

New in version 0.1.0.

pypicontents.cli.main(argv=None)[source]

Handle arguments and commands.

Parameters:argv

a list of commandline arguments like sys.argv. For example:

['-v', '--loglevel', 'INFO']
Returns:an exit status.

New in version 0.1.0.

pypicontents.wrapper module

pypicontents.api package

pypicontents.api is a package containing all the cli commands.

This package contains each command of PyPIContents organized in modules.

pypicontents.api.pypi module

pypicontents.api.report is a module implementing the report command.

This module contains the logic to examine your source code, extract internal and external imports, and finally determine which external PyPI packages you need to install in order to satisfy dependencies.

class pypicontents.api.pypi.PyPIParser[source]
__init__()[source]

Initialize and reset this instance.

If convert_charrefs is True (the default), all character references are automatically converted to the corresponding Unicode characters.

pypicontents.api.stdlib module

pypicontents.api.stdlib.fetch_inventory(uri, inv)[source]

Fetch, parse and return an intersphinx inventory file.

pypicontents.api.errors module

pypicontents.api.stats module

pypicontents.api.merge module

pypicontents.core package

pypicontents.core contains the core functions of the application.

This package contains functions for managing the download of files, exploring and obtaining imported modules, logging and other minor file operations.

pypicontents.core.logger module

pypicontents.core.logger is the global application logging module.

All modules use the same global logging object. No messages will be emitted until the logger is started.

class pypicontents.core.logger.ControlableLogger(name=None)[source]

This class represents a logger object that can be started and stopped.

It has a start method which allows you to specify a logging level. The stop method halts output.

__init__(name=None)[source]

Initialize this ControlableLogger.

The name defaults to the application name. Loggers with the same name refer to the same underlying object. Names are hierarchical, e.g. ‘parent.child’ defines a logger that is a descendant of ‘parent’.

Parameters:name – a string containing the logger name.
Returns:a ControlableLogger instance.

New in version 0.1.0.

disabled = None

Attribute disabled (boolean): Stores the current status of the logger.

formatstring = None

Attribute formatstring (string): Stores the string that will be used to format the logger output.

loglevel(level='INFO')[source]

Set the log level for this logger.

Messages less than the given priority level will be ignored. The default level is ‘INFO’, which conforms to the *nix convention that a successful run should produce no diagnostic output. Available levels and their suggested meanings:

  • NOTSET: all messages are processed.
  • DEBUG: output useful for developers.
  • INFO: trace normal program flow, especially external
    interactions.
  • WARNING: an abnormal condition was detected that might need
    attention.
  • ERROR: an error was detected but execution continued.
  • CRITICAL: an error was detected and execution was halted.
Parameters:level – a string containing the desired logging level.

New in version 0.1.0.

start(filename=None)[source]

Start logging with this logger.

Until the logger is started, no messages will be emitted. This applies to all loggers with the same name and any child loggers.

New in version 0.1.0.

stop()[source]

Stop logging with this logger.

Remove available handlers and set disabled attribute to True.

New in version 0.1.0.

pypicontents.core.patches module

pypicontents.core.utils module

pypicontents.core.utils is a utility module.

This module contains several utilities to process information coming from the other modules.

pypicontents.core.utils.chunk_read(response, chunk_size=8192, report_hook=None)[source]

Download a file by chunks.

Parameters:
  • response – a file object as returned by urlopen.
  • chunk_size – an integer representing the size of the chunks to be downloaded at a time.
  • report_hook – a function to report the progress of the download.
Returns:

a blob containing the downloaded file.

New in version 0.1.0.

pypicontents.core.utils.chunk_report(downloaded, total)[source]

Print the progress of a download.

Parameters:
  • downloaded – an integer representing the size (in bytes) of data downloaded so far.
  • total – an integer representing the total size (in bytes) of data that needs to be downloaded.

New in version 0.1.0.

pypicontents.core.utils.custom_sys_path(new_sys_path)[source]

Context manager to momentarily change sys.path.

Parameters:new_sys_path – a list of paths to overwrite sys.path.

New in version 0.1.0.

pypicontents.core.utils.find_files(path=None, pattern='*')[source]

Locate all the files matching the supplied pattern in path.

Locate all the files matching the supplied filename pattern in and below the supplied root directory. If no pattern is supplied, all files will be returned.

Parameters:
  • path – a string containing a path where the files will be looked for.
  • pattern – a string containing a regular expression.
Returns:

a list of files matching the pattern within path (recursive).

New in version 0.1.

pypicontents.core.utils.get_modules(pkgdata)[source]

List modules inside packages provided in pkgdata.

Parameters:pkgdata – a list of tuples containing the name of a package and the directory where its located.
Returns:a list of the modules according to the list of packages provided in pkgdata.

New in version 0.1.0.

pypicontents.core.utils.get_package_dirs(path)[source]

List directories containing python packages on path.

Parameters:path – a path pointing to a directory containing python code.
Returns:a list containing directories of packages.

New in version 0.1.0.

pypicontents.core.utils.get_packages(path)[source]

List packages living in path with its directory.

Parameters:path – a path pointing to a directory containing python code.
Returns:a list of tuples containing the name of the package and the package directory. For example:
[
   ('package_a', '/path/to/package_a'),
   ('package_b.module_b', '/path/to/package_b/module_b'),
   ('package_c.module_c', '/path/to/package_c/module_c')
]

New in version 0.1.0.

pypicontents.core.utils.human2bytes(s)[source]

Attempts to guess the string format based on default symbols set and return the corresponding bytes as an integer. When unable to recognize the format ValueError is raised.

>>> human2bytes('0 B')
0
>>> human2bytes('1 K')
1024
>>> human2bytes('1 M')
1048576
>>> human2bytes('1 Gi')
1073741824
>>> human2bytes('1 tera')
1099511627776
>>> human2bytes('0.5kilo')
512
>>> human2bytes('0.1  byte')
0
>>> human2bytes('1 k')  # k is an alias for K
1024
>>> human2bytes('12 foo')
Traceback (most recent call last):
    ...
ValueError: can't interpret '12 foo'
pypicontents.core.utils.is_valid_path(path)[source]

Test if path is a valid python path.

Parameters:path – a string containing a path.
Returns:True if path is a valid python path. False otherwise.

New in version 0.1.0.

pypicontents.core.utils.list_files(path=None, pattern='*')[source]

List files on path (non-recursively).

Locate all the files matching the supplied filename pattern in the first level of the supplied path. If no pattern is supplied, all files will be returned.

Parameters:
  • path – a string containing a path where the files will be looked for.
  • pattern – a string containing a regular expression.
Returns:

a list of files matching the pattern within the first level of path (non-recursive).

New in version 0.1.0.

pypicontents.core.utils.remove_sys_modules(remove)[source]

Context manager to momentarily remove modules from sys.modules.

Parameters:remove – a list of modules to remove from sys.modules.

New in version 0.1.0.

pypicontents.core.utils.s(s_string)[source]

Convert a byte stream to string working on both python 2 and 3.

Parameters:s_string – a byte stream to convert to string.

New in version 0.1.5.

pypicontents.core.utils.u(u_string)[source]

Convert a string to unicode working on both python 2 and 3.

Parameters:u_string – a string to convert to unicode.

New in version 0.1.5.