Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for JSON #13

Merged
merged 6 commits into from
Jul 30, 2014
Merged
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
4 changes: 2 additions & 2 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ Quick start
<https://github.com/deanmalmgren/flo/blob/master/CONTRIBUTING.md>`__ and send
pull requests; your help is greatly appreciated!

.. |Build Status| image:: https://travis-ci.org/deanmalmgren/flo.png
:target: https://travis-ci.org/deanmalmgren/flo
.. |Build Status| image:: https://travis-ci.org/deanmalmgren/textract.png
:target: https://travis-ci.org/deanmalmgren/textract

11 changes: 11 additions & 0 deletions tests/json/json_is_my_best_friend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"hey":"hello world this is some text from a json document",
"should_ignore_this_number": 42,
"should_extract_one_two_three": {
"1": "one",
"2": "two",
"3": "three",
"should_be_abcdef": ["a", "b", "c", ["d","e"], {"dict":"f"}]
},
"finished": "fine"
}
1 change: 1 addition & 0 deletions tests/run_functional_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ validate_example ${BASEDIR}/txt/little_bo_peep.txt 1c5fb4478d84c3b3296746e491895
validate_example ${BASEDIR}/html/snow-fall.html acc2d8c49094e56474006cab3d3768eb
validate_example ${BASEDIR}/html/what-we-do.html 1fb0263bf62317365cb30246d9e094be
validate_example ${BASEDIR}/eml/example.eml cb59a5fad8ed8b849e15d53449b1de3f
validate_example ${BASEDIR}/json/json_is_my_best_friend.json 00a080452bbafa6dfbf81e2a369a4430

# exit with the sum of the status
exit ${EXIT_CODE}
5 changes: 4 additions & 1 deletion textract/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ def process(filename, **kwargs):
# is a relative import so the name of the package is necessary
root, ext = os.path.splitext(filename)
ext = ext.lower()

# cannot call module json.py, conflicts with built-in json library
module = '.json_parser' if ext == '.json' else ext
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting way of handling this. Maybe we should rename all of the parser modules to something like {{ext}}_parser to avoid conflicts with globally installed packages.

We run into this problem in the docx and pptx modules too and we use the utils.non_local_import function to deal with it. While clever, I'm not terribly happy with that implementation and think you've got a better way of handling this. I'll make some changes to how those are incorporated after merging.

try:
filetype_module = importlib.import_module(ext, 'textract.parsers')
filetype_module = importlib.import_module(module, 'textract.parsers')
except ImportError, e:
raise exceptions.ExtensionNotSupported(ext)

Expand Down
26 changes: 26 additions & 0 deletions textract/parsers/json_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json


def extract(filename, **kwargs):
f = open(filename, 'r')
deserialized_json = json.load(f)
return get_text(deserialized_json)


def get_text(deserialized_json):
if isinstance(deserialized_json, dict):
result = ''
for key in deserialized_json:
result += get_text(deserialized_json[key]) + ' '
return result

if isinstance(deserialized_json, list):
result = ''
for item in deserialized_json:
result += get_text(item) + ' '
return result

if isinstance(deserialized_json, basestring):
return deserialized_json
else:
return ''