Example #1
0
def main():
    """Main entry point"""
    parser = argparse.ArgumentParser(
        description=
        'Tool to merge single-sided PDF scans of the same page stack into a double sided scan'
    )
    pdf_completer = argcomplete.FilesCompleter(allowednames=('pdf',))
    parser.add_argument("front_file",
                        type=Path,
                        help="Path to PDF with fronts of pages (1,3,...)").completer = pdf_completer
    parser.add_argument("back_file",
                        type=Path,
                        help="Path to PDF with backs of pages (2,4,...)").completer = pdf_completer
    parser.add_argument("output_file",
                        type=Path,
                        help="Path of combined output file").completer = pdf_completer
    argcomplete.autocomplete(parser)
    args = parser.parse_args()

    output = PdfFileWriter()
    # Load the PDFs
    with args.front_file.open(mode='rb') as front_file, \
            args.back_file.open(mode='rb') as back_file:
        input_front = PdfFileReader(front_file)
        input_back = PdfFileReader(back_file)
        # Generate combined sequence of pages from both PDFs
        combined = filter(
            bool,
            interleave_longest(input_front.pages, reversed(input_back.pages)))
        for page in combined:
            output.addPage(page)
        with args.output_file.open(mode='wb') as output_file:
            output.write(output_file)
Example #2
0
def configure_parser(description, log_params=True):
    """
    Configure an argument parser with some common options and return it.
    """
    parser = ArgParser(
        description=description,
        add_config_file_help=False,
        add_env_var_help=False,
        default_config_files=[
            '/etc/piwheels.conf',
            '/usr/local/etc/piwheels.conf',
            '~/.config/piwheels/piwheels.conf'
        ],
        ignore_unknown_config_file_keys=True
    )
    parser.add_argument(
        '--version', action='version', version=__version__)
    parser.add_argument(
        '-c', '--configuration', metavar='FILE', default=None,
        is_config_file=True, help='Specify a configuration file to load')
    if log_params:
        parser.set_defaults(log_level=logging.WARNING)
        parser.add_argument(
            '-q', '--quiet', dest='log_level', action='store_const',
            const=logging.ERROR, help='Produce less console output')
        parser.add_argument(
            '-v', '--verbose', dest='log_level', action='store_const',
            const=logging.INFO, help='Produce more console output')
        arg = parser.add_argument(
            '-l', '--log-file', metavar='FILE',
            help='Log messages to the specified file')
        if argcomplete is not None:
            arg.completer = argcomplete.FilesCompleter(['*.log', '*.txt'])
    return parser
Example #3
0
 def __init__(self,
              version,
              description=None,
              config_files=None,
              config_section=None,
              config_bools=None):
     super(TerminalApplication, self).__init__()
     if description is None:
         description = self.__doc__
     self.parser = argparse.ArgumentParser(description=description,
                                           fromfile_prefix_chars='@')
     self.parser.add_argument('--version',
                              action='version',
                              version=version)
     if config_files:
         self.config = configparser.ConfigParser(interpolation=None)
         self.config_files = config_files
         self.config_section = config_section
         self.config_bools = config_bools
         self.parser.add_argument(
             '-c',
             '--config',
             metavar='FILE',
             help='specify the configuration file to load')
     else:
         self.config = None
     self.parser.set_defaults(log_level=logging.WARNING)
     self.parser.add_argument('-q',
                              '--quiet',
                              dest='log_level',
                              action='store_const',
                              const=logging.ERROR,
                              help='produce less console output')
     self.parser.add_argument('-v',
                              '--verbose',
                              dest='log_level',
                              action='store_const',
                              const=logging.INFO,
                              help='produce more console output')
     arg = self.parser.add_argument(
         '-l',
         '--log-file',
         metavar='FILE',
         help='log messages to the specified file')
     if argcomplete:
         arg.completer = argcomplete.FilesCompleter(['*.log', '*.txt'])
     self.parser.add_argument('-P',
                              '--pdb',
                              dest='debug',
                              action='store_true',
                              default=False,
                              help='run under PDB (debug mode)')
"""Hijack the ArgComplete's bash completion handler to return AutoCompleter results"""

try:
    # check if argcomplete is installed
    import argcomplete
except ImportError:  # pragma: no cover
    # not installed, skip the rest of the file
    DEFAULT_COMPLETER = None
else:
    # argcomplete is installed

    # Newer versions of argcomplete have FilesCompleter at top level, older versions only have it under completers
    try:
        DEFAULT_COMPLETER = argcomplete.FilesCompleter()
    except AttributeError:
        DEFAULT_COMPLETER = argcomplete.completers.FilesCompleter()

    from cmd2.argparse_completer import ACTION_ARG_CHOICES, ACTION_SUPPRESS_HINT
    from contextlib import redirect_stdout
    import copy
    from io import StringIO
    import os
    import shlex
    import sys
    from typing import List, Tuple, Union

    from . import constants
    from . import utils

    def tokens_for_completion(
        line: str, endidx: int