Esempio n. 1
0
class TestTempDirectory:
    @st.composite
    def file_paths(draw):
        file_path = draw(st.lists(elements=st.text()))
        return '/'.join(file_path)

    @st.composite
    def temp_directory_data(draw, file_paths=file_paths()):
        old_file_path = draw(file_paths)
        new_file_path = os.path.join(old_file_path,
                                     util.default_subdirectory())
        return dict(old_path=old_file_path, new_path=new_file_path)

    @given(temp_directory_data())
    @example(
        dict(old_path='/foo/bar/baz/quux/',
             new_path=f'/foo/bar/baz/quux/{util.default_subdirectory()}'))
    @example(dict(old_path='/', new_path=f'/{util.default_subdirectory()}'))
    @example(dict(old_path='', new_path=util.default_subdirectory()))
    def test_temp_directory(self, file_dict):
        """
        Given any file directory, ``temp_directory`` should return the correct
        path to the temporary directory. Also, that directory should exist
        inside the input directory.
        """
        result = util.temp_directory(file_dict['old_path'])
        expected = file_dict['new_path']

        assert result == expected
Esempio n. 2
0
 def arg_dict(self):
     return dict(
         input = sample.SAMPLE_PDF,
         output = os.path.join(
             sample.SAMPLE_ROOT, util.default_subdirectory()
         )
     )
Esempio n. 3
0
def make(source_pdf, target_dir=''):
    """
    The ``make`` factory method unpacks a PDF file into a collection of TIFF 
    files--one per page--into the target directory. If a target directory is
    not specified, a default one is used in the directory of the source pdf
    file.
    """
    if not target_dir:
        # Use a default directory.
        new_target_dir = os.path.join(
            os.path.dirname(source_pdf), util.default_subdirectory()
        )
        return UnpackPDF(source_pdf, new_target_dir)
    else:
        # use the target directory
        return UnpackPDF(source_pdf, target_dir)
Esempio n. 4
0
def process_args(arg_dict):
    """
    The ``process_args`` factory method parses the command line arguments in
    ``arg_dict`` and uses them to construct a page command.
    """
    try:
        input = arg_dict['input']
        dimensions = arg_dict['dimensions']
    except KeyError as e:
        raise e

    try:
        width = dimensions[0]
        height = dimensions[1]
    except TypeError as e:
        raise e
    except ValueError as e:
        raise e

    if width <= 0 or height <= 0:
        raise ValueError(
            f'Dimensions must be positive integers: Got {width}x{height}')

    if os.path.isfile(input):
        try:
            output = arg_dict['output']
        except KeyError as e:
            # Use input as the target file.
            output = input

        return make(width, height, input, output)

    elif os.path.isdir(input):
        try:
            output = arg_dict['output']
        except KeyError as e:
            # Derive output directory from input directory
            output = os.path.join(input, util.default_subdirectory())

        files_dict = {'path': input, 'files': os.listdir(input)}
        new_files_dict = util.with_extension('.tiff', files_dict)

        return multi_expand_page(width, height, new_files_dict['path'],
                                 new_files_dict['files'], output)

    else:
        raise FileNotFoundError(f'File or directory does not exist: {input}')
Esempio n. 5
0
 def temp_directory_data(draw, file_paths=file_paths()):
     old_file_path = draw(file_paths)
     new_file_path = os.path.join(old_file_path,
                                  util.default_subdirectory())
     return dict(old_path=old_file_path, new_path=new_file_path)
Esempio n. 6
0
 def temp_directory_data(draw, file_paths=file_paths()):
     old_file_path = draw(file_paths)
     new_file_path = os.path.join(
         old_file_path, util.default_subdirectory()
     )
     return dict(old_path = old_file_path, new_path = new_file_path)
Esempio n. 7
0
 def arg_dict(self):
     return dict(input=sample.SAMPLE_PDF,
                 output=os.path.join(sample.SAMPLE_ROOT,
                                     util.default_subdirectory()))