From 765232ac41164e62e792839b4df6fc80c26f4d9e Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 9 Feb 2023 15:10:17 +0100 Subject: [PATCH 1/3] make it possibile to set encoding Signed-off-by: Christian Decker --- spdx/writers/write_anything.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spdx/writers/write_anything.py b/spdx/writers/write_anything.py index 5e479ef6e..d7fdea19d 100644 --- a/spdx/writers/write_anything.py +++ b/spdx/writers/write_anything.py @@ -18,7 +18,7 @@ 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 @@ -34,5 +34,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) From 71607b1886b782371e0ddd35f25259cc0c8af690 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 10 Feb 2023 10:51:29 +0100 Subject: [PATCH 2/3] add requirements.txt Signed-off-by: Christian Decker --- CONTRIBUTING.md | 5 ++++- requirements.txt | 7 +++++++ spdx/parsers/parse_anything.py | 7 +++++-- spdx/writers/write_anything.py | 1 + 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 requirements.txt diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 03e43d245..45d32261f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 ``` 4. Make some changes and commit them to the branch: ```sh diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..c7161f6e2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +click +ply +pytest +pyyaml +rdflib +uritools +xmltodict \ No newline at end of file diff --git a/spdx/parsers/parse_anything.py b/spdx/parsers/parse_anything.py index 329ea6944..d7c4ccdee 100644 --- a/spdx/parsers/parse_anything.py +++ b/spdx/parsers/parse_anything.py @@ -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" + encoding = None parsing_module = rdf builder_module = rdfbuilders elif fn.endswith(".tag") or fn.endswith(".spdx"): @@ -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) diff --git a/spdx/writers/write_anything.py b/spdx/writers/write_anything.py index d7fdea19d..63a4f4c3b 100644 --- a/spdx/writers/write_anything.py +++ b/spdx/writers/write_anything.py @@ -23,6 +23,7 @@ def write_file(doc, fn, validate=True, encoding="utf-8"): 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"): From 789a71e494dc9fcd96505e7e4d499d51e5ed403b Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 10 Feb 2023 13:38:22 +0100 Subject: [PATCH 3/3] use pip install -e . Signed-off-by: Christian Decker --- CONTRIBUTING.md | 2 +- requirements.txt | 7 ------- spdx/parsers/parse_anything.py | 4 +--- 3 files changed, 2 insertions(+), 11 deletions(-) delete mode 100644 requirements.txt diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 45d32261f..4f5fc4ea0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,7 +31,7 @@ Here's the process to make changes to the codebase: git checkout -b fix-or-improve-something python -m venv ./venv ./venv/bin/activate - pip install -r requirements.txt + pip install -e . ``` 4. Make some changes and commit them to the branch: ```sh diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index c7161f6e2..000000000 --- a/requirements.txt +++ /dev/null @@ -1,7 +0,0 @@ -click -ply -pytest -pyyaml -rdflib -uritools -xmltodict \ No newline at end of file diff --git a/spdx/parsers/parse_anything.py b/spdx/parsers/parse_anything.py index d7c4ccdee..906337fff 100644 --- a/spdx/parsers/parse_anything.py +++ b/spdx/parsers/parse_anything.py @@ -22,11 +22,9 @@ 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" encoding = None parsing_module = rdf builder_module = rdfbuilders @@ -46,7 +44,7 @@ def parse_file(fn, encoding="utf-8"): p = parsing_module.Parser(builder_module.Builder(), StandardLogger()) if hasattr(p, "build"): p.build() - with open(fn, in_mode, encoding=encoding) as f: + with open(fn, "r", encoding=encoding) as f: if read_data: data = f.read() return p.parse(data)