-
Notifications
You must be signed in to change notification settings - Fork 565
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
Autodetect parse() format #1046
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5621485
Use guess_format util to autodetect format
dwinston c793603
Log warning if assumed xml and could not parse
dwinston ae0de5a
refactor imports; fix try block
dwinston f1e4ed6
fix indent -- unconditionally re-raise
dwinston 5c94ae5
Remove 'as' for import
dwinston 9340842
py3.5 compat: stringify path
dwinston c0a87cf
Merge remote-tracking branch 'upstream/master' into autodetect-parse-…
dwinston b5fa8ec
prefer specific LBYL vs less-specific EAFP
dwinston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import unittest | ||
from pathlib import Path | ||
from shutil import copyfile | ||
from tempfile import TemporaryDirectory | ||
|
||
from xml.sax import SAXParseException | ||
|
||
from rdflib import Graph, logger as graph_logger | ||
|
||
|
||
class FileParserGuessFormatTest(unittest.TestCase): | ||
def test_ttl(self): | ||
g = Graph() | ||
self.assertIsInstance(g.parse("test/w3c/turtle/IRI_subject.ttl"), Graph) | ||
|
||
def test_n3(self): | ||
g = Graph() | ||
self.assertIsInstance(g.parse("test/n3/example-lots_of_graphs.n3"), Graph) | ||
|
||
def test_warning(self): | ||
g = Graph() | ||
with TemporaryDirectory() as tmpdirname: | ||
newpath = Path(tmpdirname).joinpath("no_file_ext") | ||
copyfile("test/w3c/turtle/IRI_subject.ttl", str(newpath)) | ||
with self.assertLogs(graph_logger, "WARNING") as log_cm: | ||
with self.assertRaises(SAXParseException): | ||
g.parse(str(newpath)) | ||
self.assertTrue(any("Could not guess format" in msg for msg in log_cm.output)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under what circumstances are you seeing these errors? I'm guessing when
source.file.name is None
for the TypeError and probably ... whennot hasattr(source, 'file')
? If that is the case then an explicit test (if statement) for the presence and non-Noneness of the values would be much clearer here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tgbugs I've adopted your suggestion for more of a look-before-you-leap approach with an explicit if-statement guard, ensuring that
source.file.name
is an accessible path and is a string before attempting a call toguess_format
.