Skip to content

Commit

Permalink
PR Review - move open_filename to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jstockwin committed Mar 26, 2020
1 parent ffecd5e commit abd72c4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
23 changes: 1 addition & 22 deletions pdfminer/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,7 @@
from .pdfdevice import TagExtractor
from .pdfinterp import PDFResourceManager, PDFPageInterpreter
from .pdfpage import PDFPage


class open_filename(object):
"""
Context manager that allows opening a filename and closes it on exit,
(just like `open`), but does nothing for file-like objects.
"""
def __init__(self, filename, *args, **kwargs):
if isinstance(filename, str):
self.file_handler = open(filename, *args, **kwargs)
self.closing = True
else:
self.file_handler = filename
self.closing = False

def __enter__(self):
return self.file_handler

def __exit__(self, exc_type, exc_val, exc_tb):
if self.closing:
self.file_handler.close()
return False
from .utils import open_filename


def extract_text_to_fp(inf, outfp, output_type='text', codec='utf-8',
Expand Down
22 changes: 22 additions & 0 deletions pdfminer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@
INF = (1 << 31) - 1


class open_filename(object):
"""
Context manager that allows opening a filename and closes it on exit,
(just like `open`), but does nothing for file-like objects.
"""
def __init__(self, filename, *args, **kwargs):
if isinstance(filename, str):
self.file_handler = open(filename, *args, **kwargs)
self.closing = True
else:
self.file_handler = filename
self.closing = False

def __enter__(self):
return self.file_handler

def __exit__(self, exc_type, exc_val, exc_tb):
if self.closing:
self.file_handler.close()
return False


def make_compat_bytes(in_str):
"Converts to bytes, encoding to unicode."
assert isinstance(in_str, str), str(type(in_str))
Expand Down

0 comments on commit abd72c4

Please sign in to comment.