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

make it possibile to set encoding #471

Merged
merged 3 commits into from Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Expand Up @@ -26,9 +26,12 @@ Here's the process to make changes to the codebase:
2. Review [open pull requests](https://github.com/spdx/tools-python/pulls) before committing time to a substantial
revision. Work along similar lines may already be in progress.

3. Create a new branch:
3. Create a new branch and set up environment:
```sh
git checkout -b fix-or-improve-something
python -m venv ./venv
./venv/bin/activate
pip install -r requirements.txt
chrisdecker1201 marked this conversation as resolved.
Show resolved Hide resolved
```
4. Make some changes and commit them to the branch:
```sh
Expand Down
7 changes: 7 additions & 0 deletions requirements.txt
@@ -0,0 +1,7 @@
click
ply
pytest
pyyaml
rdflib
uritools
xmltodict
chrisdecker1201 marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 5 additions & 2 deletions spdx/parsers/parse_anything.py
Expand Up @@ -21,10 +21,13 @@
from spdx.parsers.builderexceptions import FileTypeError


def parse_file(fn):
def parse_file(fn, encoding="utf-8"):
in_mode = "r"
builder_module = jsonyamlxmlbuilders
read_data = False
if fn.endswith(".rdf") or fn.endswith(".rdf.xml"):
in_mode = "rb"
chrisdecker1201 marked this conversation as resolved.
Show resolved Hide resolved
encoding = None
parsing_module = rdf
builder_module = rdfbuilders
elif fn.endswith(".tag") or fn.endswith(".spdx"):
Expand All @@ -43,7 +46,7 @@ def parse_file(fn):
p = parsing_module.Parser(builder_module.Builder(), StandardLogger())
if hasattr(p, "build"):
p.build()
with open(fn) as f:
with open(fn, in_mode, encoding=encoding) as f:
if read_data:
data = f.read()
return p.parse(data)
Expand Down
5 changes: 3 additions & 2 deletions spdx/writers/write_anything.py
Expand Up @@ -18,11 +18,12 @@
from spdx.parsers.builderexceptions import FileTypeError


def write_file(doc, fn, validate=True):
def write_file(doc, fn, validate=True, encoding="utf-8"):
out_mode = "w"
if fn.endswith(".rdf") or fn.endswith(".rdf.xml"):
writer_module = rdf
out_mode = "wb"
encoding = None
elif fn.endswith(".tag") or fn.endswith(".spdx"):
writer_module = tagvalue
elif fn.endswith(".json"):
Expand All @@ -34,5 +35,5 @@ def write_file(doc, fn, validate=True):
else:
raise FileTypeError("FileType Not Supported")

with open(fn, out_mode) as out:
with open(fn, out_mode, encoding=encoding) as out:
p = writer_module.write_document(doc, out, validate)