module.session module

class module_dependencies.module.session.ModuleSession[source]

Bases: Session

__init__(token: str | None = None) None[source]
Parameters:

token (str, optional) – Sourcegraph API token to avoid rate-limiting 429 error

Return type:

None

fetch_and_parse(module: str, count: int | str = 'all', timeout: int | str = '10s', verbose: bool = True, languages: Tuple[str] | None = None) Dict[source]

Return the parsed data from the SourceGraph API. This method loads the data once upon request and then parses it using Source(…).dependencies().

Parameters:
  • module (str) – The name of a Python module of which to find the frequently used objects, e.g. “nltk”.

  • count (Union[int, str], optional) – The maximum number of times an import of module should be fetched. Roughly equivalent to the number of fetched files. Either an integer, a string representing an integer, or “all”, defaults to “all”.

  • timeout (Union[int, str], optional) – Timeout for the source code API. Does not correspond to the timeout for any of the functions as a whole. The timeout can be an integer or a string with some digits and then a time unit, e.g. “10s”, “100ms”. If an integer instead, then parsed as number of milliseconds. Cannot exceed 1 minute. Defaults to “10s”.

  • verbose (bool, optional) – If True, display a progress bar for parsing files. Defaults to True.

  • languages (Tuple[str] | None) –

Returns:

The parsed SourceGraph API data.

Return type:

Dict

parse_raw_response(results: Dict, module: str, language: str, verbose: bool = True)[source]

Strip content from the raw input data, and replace it with dependencies and parse_error.

Parameters:
  • results (Dict) – Raw output from session.post(self.module, …)

  • module (str) – String of the module we are interested in, e.g. “nltk” or “nltk.tokenize”.

  • language (str) – The name of the language that is being parsed, e.g. “Python” or “Jupyter Notebook”.

  • verbose (bool) –

Returns:

Modified output of results, with file content stripped, and dependencies and parse_error added.

Return type:

Dict

construct_payload(module: str, count: int | str = 'all', timeout: int | str = '10s', language: str = 'Python') Dict[source]

Construct the payload to send to SourceGraph. The payload is a dictionary with “variables” and “query” keys.

Parameters:
  • module (str) – String of the module we are interested in, e.g. “nltk” or “nltk.tokenize”.

  • count (Union[int, str], optional) – The maximum number of results. Either an integer, a string representing an integer, or “all”, defaults to “all”.

  • timeout (Union[int, str], optional) – Timeout as parsed by the Go time package “ParseDuration” function, e.g. “10s”, “100ms”. If an integer instead, then parsed as number of milliseconds. Cannot exceed 1 minute. Defaults to “10s”.

  • language (str) –

Returns:

Mapping of “variable” and “query” to a dictionary and a string, respectively.

Return type:

Dict

TODO: Verify that module is valid

post(module: str, count: int | str = 'all', timeout: int | str = '10s', language: str = 'Python') Response[source]

Sends a POST request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • json – (optional) json to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

  • module (str) –

  • count (int | str) –

  • timeout (int | str) –

  • language (str) –

Return type:

requests.Response

close()

Closes all adapters and as such the session

delete(url, **kwargs)

Sends a DELETE request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

get(url, **kwargs)

Sends a GET request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

get_adapter(url)

Returns the appropriate connection adapter for the given URL.

Return type:

requests.adapters.BaseAdapter

get_redirect_target(resp)

Receives a Response. Returns a redirect URI or None

head(url, **kwargs)

Sends a HEAD request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

merge_environment_settings(url, proxies, stream, verify, cert)

Check the environment and merge it with some settings.

Return type:

dict

mount(prefix, adapter)

Registers a connection adapter to a prefix.

Adapters are sorted in descending order by prefix length.

options(url, **kwargs)

Sends a OPTIONS request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

patch(url, data=None, **kwargs)

Sends a PATCH request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

prepare_request(request)

Constructs a PreparedRequest for transmission and returns it. The PreparedRequest has settings merged from the Request instance and those of the Session.

Parameters:

requestRequest instance to prepare with this session’s settings.

Return type:

requests.PreparedRequest

put(url, data=None, **kwargs)

Sends a PUT request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

rebuild_auth(prepared_request, response)

When being redirected we may want to strip authentication from the request to avoid leaking credentials. This method intelligently removes and reapplies authentication where possible to avoid credential loss.

rebuild_method(prepared_request, response)

When being redirected we may want to change the method of the request based on certain specs or browser behavior.

rebuild_proxies(prepared_request, proxies)

This method re-evaluates the proxy configuration by considering the environment variables. If we are redirected to a URL covered by NO_PROXY, we strip the proxy configuration. Otherwise, we set missing proxy keys for this URL (in case they were stripped by a previous redirect).

This method also replaces the Proxy-Authorization header where necessary.

Return type:

dict

request(method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None)

Constructs a Request, prepares it and sends it. Returns Response object.

Parameters:
  • method – method for the new Request object.

  • url – URL for the new Request object.

  • params – (optional) Dictionary or bytes to be sent in the query string for the Request.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • json – (optional) json to send in the body of the Request.

  • headers – (optional) Dictionary of HTTP Headers to send with the Request.

  • cookies – (optional) Dict or CookieJar object to send with the Request.

  • files – (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.

  • auth – (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth.

  • timeout (float or tuple) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.

  • allow_redirects (bool) – (optional) Set to True by default.

  • proxies – (optional) Dictionary mapping protocol or protocol and hostname to the URL of the proxy.

  • stream – (optional) whether to immediately download the response content. Defaults to False.

  • verify – (optional) Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to True. When set to False, requests will accept any TLS certificate presented by the server, and will ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks. Setting verify to False may be useful during local development or testing.

  • cert – (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.

Return type:

requests.Response

resolve_redirects(resp, req, stream=False, timeout=None, verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs)

Receives a Response. Returns a generator of Responses or Requests.

send(request, **kwargs)

Send a given PreparedRequest.

Return type:

requests.Response

should_strip_auth(old_url, new_url)

Decide whether Authorization header should be removed when redirecting

headers

A case-insensitive dictionary of headers to be sent on each Request sent from this Session.

auth

Default Authentication tuple or object to attach to Request.

proxies

Dictionary mapping protocol or protocol and host to the URL of the proxy (e.g. {‘http’: ‘foo.bar:3128’, ‘http://host.name’: ‘foo.bar:4012’}) to be used on each Request.

hooks

Event-handling hooks.

params

Dictionary of querystring data to attach to each Request. The dictionary values may be lists for representing multivalued query parameters.

stream

Stream response content default.

verify

SSL Verification default. Defaults to True, requiring requests to verify the TLS certificate at the remote end. If verify is set to False, requests will accept any TLS certificate presented by the server, and will ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks. Only set this to False for testing.

cert

SSL client certificate default, if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.

max_redirects

Maximum number of redirects allowed. If the request exceeds this limit, a TooManyRedirects exception is raised. This defaults to requests.models.DEFAULT_REDIRECT_LIMIT, which is 30.

trust_env

Trust environment settings for proxy configuration, default authentication and similar.

cookies

A CookieJar containing all currently outstanding cookies set on this session. By default it is a RequestsCookieJar, but may be any other cookielib.CookieJar compatible object.