aedev.pythonanywhere

web api for www.pyanywhere.com and eu.pyanywhere.com

a similar package can be found at `https://gitlab.com/texperience/pythonanywhereapiclient`__

Classes

PythonanywhereApi(web_domain, web_user, ...)

remote host api to a project package on the web hosts eu.pythonanywhere.com and pythonanywhere.com.

class PythonanywhereApi(web_domain, web_user, web_token, project_name)[source]

Bases: object

remote host api to a project package on the web hosts eu.pythonanywhere.com and pythonanywhere.com.

__init__(web_domain, web_user, web_token, project_name)[source]

initialize web host api and the deployed project package name.

Parameters:
  • web_domain (str) – remote web host domain.

  • web_user (str) – remote connection username.

  • web_token (str) – personal user credential token string on remote host.

  • project_name (str) – name of the web project package.

property error_message: str

error message string if an error occurred or an empty string if not.

Getter:

return the accumulated error message of the recently occurred error(s).

Setter:

any assigned error message will be accumulated to recent error messages. pass an empty string to reset the error message.

property project_name: str

project main package name string property.

Getter:

return the currently connected/configured project package name of the web host server.

Setter:

set/change the currently connected/configured project name of the web host server.

_folder_items(folder_path)[source]
Return type:

Optional[list[dict[str, str]]]

_from_json(response)[source]

convert json in response to python type (list/dict).

Parameters:

response (Response) – response from requests to convert into python data type.

Return type:

Union[list[dict[str, Any]], dict[str, dict[str, Any]], None]

Returns:

[list of] dictionaries converted from the response content or None on error.

_prepare_collector(skipper)[source]
_request(url_path, task, method=<function get>, success_codes=(200, 201), **request_kwargs)[source]

send a https request specified via method and return the response.

Parameters:
  • url_path (str) – sub url path to send request to.

  • task (str) – string describing the task to archive (used to compile an error message).

  • method (Callable) – requests method (get, post, push, delete, patch, …).

  • success_codes (Sequence) – sequence of response.status_code success codes

  • request_kwargs – additional request method arguments.

Return type:

Response

Returns:

request response. if on error occurred then the instance string attribute error_message contains an error message. if the caller is not checking for errors and not resetting the error message string, then this function will accumulate further errors to error_message, separated by two new line characters.

available_consoles()[source]

determine the available consoles.

Return type:

Optional[list[dict[str, Any]]]

Returns:

python dictionary with available consoles or None if an error occurred.

deployed_code_files(path_masks, skip_file_path=<function PythonanywhereApi.<lambda>>)[source]

determine all deployed code files of given package name deployed to the pythonanywhere server.

Parameters:
  • path_masks (Sequence[str]) – root package paths with glob wildcards to collect deployed code files from.

  • skip_file_path (Callable[[str], bool]) – called for each found file/folder with the path_mask relative to the package root folder as argument, returning True to exclude the specified item from the returned result set. calls of a folder have a prefix of a slash character followed by a dot (“/.”) and help to minimize the number of calls against the web server api.

Return type:

Optional[set[str]]

Returns:

set of file paths of the package deployed on the web, relative to the project root or None if an error occurred.

deployed_file_content(file_path)[source]

determine the file content of a file deployed to a web server.

Parameters:

file_path (str) – path of a deployed file relative to the project root.

Return type:

Optional[bytes]

Returns:

file content as bytes or None if error occurred (check self.error_message).

deployed_version()[source]

determine the version of a deployed django project package.

Return type:

str

Returns:

version string of the package deployed to the web host/server or empty string if package version file or version-in-file not found.

deploy_file(file_path, file_content)[source]

add or update a project file to the web server.

Parameters:
  • file_path (str) – path relative to the project root of the file to be deployed (added or updated).

  • file_content (bytes) – file content to deploy/upload.

Return type:

str

Returns:

error message if update/add failed else on success an empty string.

delete_file_or_folder(file_path)[source]

delete a file or folder on the web server.

Parameters:

file_path (str) – path relative to the project root of the file to be deleted.

Return type:

str

Returns:

error message if deletion failed else on success an empty string.

files_iterator(path_mask, level_index=0)[source]

find files matching the path mask string passed as the paramref:.path_mask argument.

Parameters:
  • path_mask (str) – file path pattern/mask with optional wildcards. passing an empty string will return the files of the project/package root directory, as well as passing ‘.’ or ‘*’. also absolute path masks will be relative to the project root directory. file path mask matches are case-sensitive (done with the function fnmatch.fnmatchcase()).

  • level_index (int) – folder level depth in passed file path mask to start searching (only specified in recursive call).

Return type:

Iterable[Any]

Returns:

iterator/generator yielding dicts. each dict has a file_path key containing the path string of the found file relative to the project root folder and a type key containing the string ‘directory’ or `’file’

find_project_files(path_mask='', skip_file_path=<function PythonanywhereApi.<lambda>>, collector=None)[source]

determine the files matching the glob pattern provided in path_mask at the app/web server.

not using the files tree api endpoints/function (f”files/tree/?path=/home/{self.web_user}/{project_name}”) because their response is limited to 1000 files (see https://help.pythonanywhere.com/pages/API#apiv0userusernamefilestreepathpath) and e.g. kairos has more than 5300 files in its package folder (mainly for django filer and the static files).

Parameters:
  • path_mask (str) – file mask including relative path to the package project root to be searched. passing an empty string (the default) returns all files in the package root directory.

  • collector (Optional[Collector]) – file collector callable.

  • skip_file_path (Callable[[str], bool]) – called for each found file/folder with the path_mask relative to the package root folder as argument, returning True to exclude the specified item from the returned result set. calls of a folder have a prefix of a slash character followed by a dot (“/.”) and help to minimize the number of calls against the web server api.

Return type:

Optional[set[str]]

Returns:

set of file paths of the package deployed on the web, relative to the project root or None if an error occurred. all files underneath a