Documentation for pdfebc_core

compress

This module contains the PDF manipulation functions of the pdfebc program. Ghostscript is used to compress PDF files.

pdfebc_core.compress.compress_multiple_pdfs(source_directory, output_directory, ghostscript_binary)[source]

Compress all PDF files in the current directory and place the output in the given output directory. This is a generator function that first yields the amount of files to be compressed, and then yields the output path of each file.

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.
Returns:

paths to outputs.

Return type:

list(str)

pdfebc_core.compress.compress_pdf(filepath, output_path, ghostscript_binary)[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.
Raises:

config_utils

Module containing util functions relating to the configuration of pdfebc.

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

Requires a config file called ‘config.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>
exception pdfebc_core.config_utils.ConfigurationError(source=None, filename=None)[source]

Error thrown whenever something is wrong with the configuration file.

pdfebc_core.config_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_core.config_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_core.config_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_core.config_utils.get_attribute_from_config(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_core.config_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_core.config_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_core.config_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_core.config_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.

email_utils

Module containing email util functions for the pdfebc program.

The SMTP server and port are configured in the config.cnf file, see the config_utils module for more information.

pdfebc_core.email_utils.send_files_preconf(filepaths, config_path='/home/docs/.config/pdfebc/config.cnf')[source]

Send files using the config.ini settings.

Parameters:filepaths (list(str)) – A list of filepaths.
pdfebc_core.email_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.

misc_utils

Module containing miscellaneous util functions for pdfebc.

The SMTP server and port are configured in the config.cnf file, see the config_utils module for more information.

pdfebc_core.misc_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