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

fix: issues with string destination handling in {Graph,Result}.serialize #2065

Merged
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
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ and will be removed for release.
<!-- -->
<!-- -->


<!-- -->
<!-- -->
<!-- CHANGE BARRIER: START #2068 -->
<!-- -->
<!-- -->

- Improve file-URI and path handling in `Graph.serialize` and `Result.serialize` to
address problems with windows path handling in `Result.serialize` and to make
the behavior between `Graph.serialize` and `Result.serialie` more consistent.
Closed [issue #2067](https://github.com/RDFLib/rdflib/issues/2067).
[PR #2068](https://github.com/RDFLib/rdflib/pull/2068).
- String values for the `destination` argument will now only be treated as
file URIs if `urllib.parse.urlparse` returns their schema as `file`.
- Simplified file writing to avoid a temporary file.

<!-- -->
<!-- -->
<!-- CHANGE BARRIER: END #2068 -->
<!-- -->
<!-- -->

<!-- -->
<!-- -->
<!-- CHANGE BARRIER: START -->
Expand Down
29 changes: 13 additions & 16 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import logging
import os
import pathlib
import random
import shutil
import tempfile
from io import BytesIO
from typing import (
IO,
Expand Down Expand Up @@ -1201,20 +1198,20 @@ def serialize(
serializer.serialize(stream, base=base, encoding=encoding, **args)
else:
if isinstance(destination, pathlib.PurePath):
location = str(destination)
os_path = str(destination)
else:
location = cast(str, destination)
scheme, netloc, path, params, _query, fragment = urlparse(location)
if netloc != "":
raise ValueError(
f"destination {destination} is not a local file reference"
)
fd, name = tempfile.mkstemp()
stream = os.fdopen(fd, "wb")
serializer.serialize(stream, base=base, encoding=encoding, **args)
stream.close()
dest = url2pathname(path) if scheme == "file" else location
shutil.move(name, dest)
scheme, netloc, path, params, _query, fragment = urlparse(location)
if scheme == "file":
if netloc != "":
raise ValueError(
f"the file URI {location!r} has an authority component which is not supported"
)
os_path = url2pathname(path)
else:
os_path = location
with open(os_path, "wb") as stream:
serializer.serialize(stream, encoding=encoding, **args)
return self

def print(self, format="turtle", encoding="utf-8", out=None):
Expand Down Expand Up @@ -1276,7 +1273,7 @@ def parse(
... </rdf:Description>
... </rdf:RDF>
... '''
>>> import tempfile
>>> import os, tempfile
>>> fd, file_name = tempfile.mkstemp()
>>> f = os.fdopen(fd, "w")
>>> dummy = f.write(my_data) # Returns num bytes written
Expand Down
24 changes: 11 additions & 13 deletions rdflib/query.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import itertools
import os
import shutil
import tempfile
import types
import warnings
from io import BytesIO
from typing import IO, TYPE_CHECKING, List, Optional, Union, cast
from urllib.parse import urlparse
from urllib.request import url2pathname

__all__ = [
"Processor",
Expand Down Expand Up @@ -267,16 +265,16 @@ def serialize(
else:
location = cast(str, destination)
scheme, netloc, path, params, query, fragment = urlparse(location)
if netloc != "":
print(
"WARNING: not saving as location" + "is not a local file reference"
)
return None
fd, name = tempfile.mkstemp()
stream = os.fdopen(fd, "wb")
serializer.serialize(stream, encoding=encoding, **args)
stream.close()
shutil.move(name, path)
if scheme == "file":
if netloc != "":
raise ValueError(
f"the file URI {location!r} has an authority component which is not supported"
)
os_path = url2pathname(path)
else:
os_path = location
with open(os_path, "wb") as stream:
serializer.serialize(stream, encoding=encoding, **args)
return None

def __len__(self):
Expand Down
Loading