Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

added sorting option by using list #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* Chris Hager (https://github.com/metachris)
* Taranjeet Singh (https://github.com/staranjeet)
* Jaton Justice (https://github.com/masterjx9)
12 changes: 6 additions & 6 deletions pdfx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class PDFx(object):
reader = None # ReaderBackend
summary = {}

def __init__(self, uri):
def __init__(self, uri, references_data_structure="set"):
"""
Open PDF handle and parse PDF metadata
- `uri` can bei either a filename or an url
Expand Down Expand Up @@ -125,7 +125,7 @@ def __init__(self, uri):

# Create ReaderBackend instance
try:
self.reader = PDFMinerBackend(self.stream)
self.reader = PDFMinerBackend(self.stream, references_data_structure=references_data_structure)
except PDFSyntaxError as e:
raise PDFInvalidError("Invalid PDF (%s)" % unicode(e))

Expand Down Expand Up @@ -159,13 +159,13 @@ def get_text(self):
def get_metadata(self):
return self.reader.get_metadata()

def get_references(self, reftype=None, sort=False):
def get_references(self, reftype=None):
""" reftype can be `None` for all, `pdf`, etc. """
return self.reader.get_references(reftype=reftype, sort=sort)
return self.reader.get_references(reftype=reftype)

def get_references_as_dict(self, reftype=None, sort=False):
def get_references_as_dict(self, reftype=None):
""" reftype can be `None` for all, `pdf`, etc. """
return self.reader.get_references_as_dict(reftype=reftype, sort=sort)
return self.reader.get_references_as_dict(reftype=reftype)

def get_references_count(self, reftype=None):
""" reftype can be `None` for all, `pdf`, etc. """
Expand Down
27 changes: 16 additions & 11 deletions pdfx/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ class ReaderBackend(object):

text = ""
metadata = {}
references = set()

def __init__(self):
def __init__(self, references_data_structure="set"):
self.text = ""
self.metadata = {}
self.references = set()
if references_data_structure == "list":
self.references = list()
else: # default to set
self.references = set()

def get_metadata(self):
return self.metadata
Expand Down Expand Up @@ -163,18 +165,18 @@ def metadata_cleanup(self):
def get_text(self):
return self.text

def get_references(self, reftype=None, sort=False):
def get_references(self, reftype=None):
refs = self.references
if reftype:
refs = set([ref for ref in refs if ref.reftype == "pdf"])
return sorted(refs) if sort else refs
return refs

def get_references_as_dict(self, reftype=None, sort=False):
def get_references_as_dict(self, reftype=None):
ret = {}
refs = self.references
if reftype:
refs = set([ref for ref in refs if ref.reftype == "pdf"])
for r in sorted(refs) if sort else refs:
refs = [ref for ref in refs if ref.reftype == reftype]
for r in refs:
if r.reftype in ret:
ret[r.reftype].append(r.ref)
else:
Expand All @@ -183,8 +185,8 @@ def get_references_as_dict(self, reftype=None, sort=False):


class PDFMinerBackend(ReaderBackend):
def __init__(self, pdf_stream, password="", pagenos=[], maxpages=0): # noqa: C901
ReaderBackend.__init__(self)
def __init__(self, pdf_stream, password="", pagenos=[], maxpages=0, references_data_structure="set"): # noqa: C901
ReaderBackend.__init__(self, references_data_structure)
self.pdf_stream = pdf_stream

# Extract Metadata
Expand Down Expand Up @@ -238,7 +240,10 @@ def __init__(self, pdf_stream, password="", pagenos=[], maxpages=0): # noqa: C9
if isinstance(refs, list):
for ref in refs:
if ref:
self.references.add(ref)
if references_data_structure == "list":
self.references.append(ref)
else:
self.references.add(ref)
elif isinstance(refs, Reference):
self.references.add(refs)

Expand Down