Allow a pathlib.PurePath object as a input to open_filename (#492)
* open_filename accepts a pathlib.PurePath object * Add test for open_filename with pathlib * Fix a wrong function name * Cast a pathlib object to string for py3.4/3.5 * Add link to the PR * Raise an exception when open_filename gets an unsupported type * Add tests for open_filename * Update CHANGELOG.md * Documentation Co-authored-by: Pieter Marsman <pietermarsman@gmail.com>pull/479/head^2
parent
b4054ff4cf
commit
f03657e5c4
|
@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Support for `pathlib.PurePath` in `open_filename` ([#491](https://github.com/pdfminer/pdfminer.six/issues/491))
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- Pass caching parameter to PDFResourceManager in `high_level` functions ([#475](https://github.com/pdfminer/pdfminer.six/pull/475))
|
- Pass caching parameter to PDFResourceManager in `high_level` functions ([#475](https://github.com/pdfminer/pdfminer.six/pull/475))
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
"""
|
"""
|
||||||
Miscellaneous Routines.
|
Miscellaneous Routines.
|
||||||
"""
|
"""
|
||||||
|
import io
|
||||||
|
import pathlib
|
||||||
import struct
|
import struct
|
||||||
from html import escape
|
from html import escape
|
||||||
|
|
||||||
|
@ -13,16 +15,21 @@ INF = (1 << 31) - 1
|
||||||
|
|
||||||
class open_filename(object):
|
class open_filename(object):
|
||||||
"""
|
"""
|
||||||
Context manager that allows opening a filename and closes it on exit,
|
Context manager that allows opening a filename
|
||||||
|
(str or pathlib.PurePath type is supported) and closes it on exit,
|
||||||
(just like `open`), but does nothing for file-like objects.
|
(just like `open`), but does nothing for file-like objects.
|
||||||
"""
|
"""
|
||||||
def __init__(self, filename, *args, **kwargs):
|
def __init__(self, filename, *args, **kwargs):
|
||||||
|
if isinstance(filename, pathlib.PurePath):
|
||||||
|
filename = str(filename)
|
||||||
if isinstance(filename, str):
|
if isinstance(filename, str):
|
||||||
self.file_handler = open(filename, *args, **kwargs)
|
self.file_handler = open(filename, *args, **kwargs)
|
||||||
self.closing = True
|
self.closing = True
|
||||||
else:
|
elif isinstance(filename, io.IOBase):
|
||||||
self.file_handler = filename
|
self.file_handler = filename
|
||||||
self.closing = False
|
self.closing = False
|
||||||
|
else:
|
||||||
|
raise TypeError('Unsupported input type: %s' % type(filename))
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
return self.file_handler
|
return self.file_handler
|
||||||
|
|
|
@ -1,7 +1,30 @@
|
||||||
from nose.tools import assert_equal
|
from nose.tools import assert_equal, assert_raises
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
from helpers import absolute_sample_path
|
||||||
from pdfminer.layout import LTComponent
|
from pdfminer.layout import LTComponent
|
||||||
from pdfminer.utils import Plane, shorten_str
|
from pdfminer.utils import open_filename, Plane, shorten_str
|
||||||
|
|
||||||
|
|
||||||
|
class TestOpenFilename:
|
||||||
|
def test_string_input(self):
|
||||||
|
filename = absolute_sample_path("simple1.pdf")
|
||||||
|
opened = open_filename(filename)
|
||||||
|
assert_equal(opened.closing, True)
|
||||||
|
|
||||||
|
def test_pathlib_input(self):
|
||||||
|
filename = pathlib.Path(absolute_sample_path("simple1.pdf"))
|
||||||
|
opened = open_filename(filename)
|
||||||
|
assert_equal(opened.closing, True)
|
||||||
|
|
||||||
|
def test_file_input(self):
|
||||||
|
filename = absolute_sample_path("simple1.pdf")
|
||||||
|
with open(filename, "rb") as in_file:
|
||||||
|
opened = open_filename(in_file)
|
||||||
|
assert_equal(opened.file_handler, in_file)
|
||||||
|
|
||||||
|
def test_unsupported_input(self):
|
||||||
|
assert_raises(TypeError, open_filename, 0)
|
||||||
|
|
||||||
|
|
||||||
class TestPlane:
|
class TestPlane:
|
||||||
|
|
Loading…
Reference in New Issue