Documentation for pdfebc

core

This module contains the core functions of the pdfebc program. These consist mostly of functions that manipulate PDF files and the file system.

pdfebc.core.compress_multiple_pdfs(source_directory, output_directory, ghostscript_binary, status_callback=None)[source]

Compress all PDF files in the current directory and place the output in the given output directory.

Parameters:
  • source_directory (str) – Filepath to the source directory.
  • output_directory (str) – Filepath to the output directory.
  • ghostscript_binary (str) – Name of the Ghostscript binary.
  • status_callback (function) – A callback function for passing status messages to a view.
Returns:

paths to outputs.

Return type:

list(str)

pdfebc.core.compress_pdf(filepath, output_path, ghostscript_binary, status_callback=None)[source]

Compress a single PDF file.

Parameters:
  • filepath (str) – Path to the PDF file.
  • output_path (str) – Output path.
  • ghostscript_binary (str) – Name/alias of the Ghostscript binary.
  • status_callback (function) – A callback function for passing status messages to a view.
Raises:

ValueError

pdfebc.core.get_pdf_filenames_at(source_directory)[source]

Find all PDF files in the specified directory.

Parameters:source_directory (str) – The source directory.
Returns:Filepaths to all PDF files in the specified directory.
Return type:list(str)
Raises:ValueError

cli

This module contains the argument parser for the pdfebc program.

pdfebc.cli.create_argparser()[source]
Returns:The argument parser for pdfebc.
Return type:argparse.ArgumentParser
pdfebc.cli.diagnose_config()[source]

Print the results of the configuration diagnostics check.

pdfebc.cli.prompt_for_config_values()[source]

Prompt the user for the user, password and receiver values for the config.

Returns:user e-mail, user password and receiver e-mail (or whatever the user enters when prompted for these).
Return type:str, str, str
pdfebc.cli.status_callback(status)[source]

Callback function for recieving status messages. This one simply prints the message to stdout.

Parameters:status (str) – A status message.

utils

Module containing util functions for the pdfebc program.

The SMTP server and port are configured in the config.cnf file.

Requires a config file called ‘email.cnf’ in the user conf directory specified by appdirs. In the case of Arch Linux, this is ‘$HOME/.config/pdfebc/config.cnf’, but this may vary with distributions. The config file should have the following format:

|[EMAIL] |user = <sender_email> |pass = <password> |receiver = <receiver_email> |smtp_server = <smtp_server> |smtp_port = <smtp_port> | |[DEFAULTS] |gs_binary = <ghostscript_binary> |src = <source_dir> |out = <out_dir>
pdfebc.utils.attach_files(filepaths, email_)[source]

Take a list of filepaths and attach the files to a MIMEMultipart.

Parameters:
  • filepaths (list(str)) – A list of filepaths.
  • email (email.MIMEMultipart) – A MIMEMultipart email_.
pdfebc.utils.check_config(config)[source]

Check that all sections of the config contain the keys that they should.

Parameters:config (defaultdict) – A defaultdict.
Raises:ConfigurationError
pdfebc.utils.config_parser_to_defaultdict(config_parser)[source]

Convert a ConfigParser to a defaultdict.

Parameters:config_parser (ConfigParser) – A ConfigParser.
pdfebc.utils.config_to_string(config)[source]

Nice output string for the config, which is a nested defaultdict.

Parameters:config (defaultdict(defaultdict)) – The configuration information.
Returns:A human-readable output string detailing the contents of the config.
Return type:str
pdfebc.utils.create_config(sections, section_contents)[source]

Create a config file from the provided sections and key value pairs.

Parameters:
  • sections (List[str]) – A list of section keys.
  • key_value_pairs (Dict[str, str]) – A list of of dictionaries. Must be as long as
  • list of sections. That is to say, if there are two sections, there should be two (the) –
  • dicts.
Returns:

A ConfigParser.

Return type:

configparser.ConfigParser

Raises:

ValueError

pdfebc.utils.if_callable_call_with_formatted_string(callback, formattable_string, *args)[source]

If the callback is callable, format the string with the args and make a call. Otherwise, do nothing.

Parameters:
  • callback (function) – May or may not be callable.
  • formattable_string (str) – A string with ‘{}’s inserted.
  • *args – A variable amount of arguments for the string formatting. Must correspond to the
  • of '{}'s in 'formattable_string'. (amount) –
Raises:

ValueError

pdfebc.utils.read_config(config_path='/home/docs/.config/pdfebc/config.cnf')[source]

Read the config information from the config file.

Parameters:config_path (str) – Relative path to the email config file.
Returns:A defaultdict with the config information.
Return type:defaultdict
Raises:IOError
pdfebc.utils.run_config_diagnostics(config_path='/home/docs/.config/pdfebc/config.cnf')[source]

Run diagnostics on the configuration file.

Parameters:config_path (str) – Path to the configuration file.
Returns:The path to the configuration file, a set of missing sections and a dict that maps each section to the entries that have either missing or empty options.
Return type:str, Set[str], dict(str, Set[str])
pdfebc.utils.section_is_healthy(section, expected_keys)[source]

Check that the section contains all keys it should.

Parameters:
  • section (defaultdict) – A defaultdict.
  • expected_keys (Iterable) – A Set of keys that should be contained in the section.
Returns:

True if the section is healthy, false if not.

Return type:

boolean

pdfebc.utils.send_email(email_, config)[source]

Send an email.

Parameters:
  • email (email.MIMEMultipart) – The email to send.
  • config (defaultdict) – A defaultdict.
pdfebc.utils.send_files_preconf(filepaths, config_path='/home/docs/.config/pdfebc/config.cnf', status_callback=None)[source]

Send files using the config.ini settings.

Parameters:filepaths (list(str)) – A list of filepaths.
pdfebc.utils.send_with_attachments(subject, message, filepaths, config)[source]

Send an email from the user (a gmail) to the receiver.

Parameters:
  • subject (str) – Subject of the email.
  • message (str) – A message.
  • filepaths (list(str)) – Filepaths to files to be attached.
  • config (defaultdict) – A defaultdict.
pdfebc.utils.try_get_conf(config, section, attribute)[source]

Try to parse an attribute of the config file.

Parameters:
  • config (defaultdict) – A defaultdict.
  • section (str) – The section of the config file to get information from.
  • attribute (str) – The attribute of the section to fetch.
Returns:

The string corresponding to the section and attribute.

Return type:

str

Raises:

ConfigurationError

pdfebc.utils.valid_config_exists(config_path='/home/docs/.config/pdfebc/config.cnf')[source]

Verify that a valid config file exists.

Parameters:config_path (str) – Path to the config file.
Returns:True if there is a valid config file, false if not.
Return type:boolean
pdfebc.utils.write_config(config, config_path='/home/docs/.config/pdfebc/config.cnf')[source]

Write the config to the output path. Creates the necessary directories if they aren’t there.

Parameters:config (configparser.ConfigParser) – A ConfigParser.