Skip to content

Commit

Permalink
Merge pull request #110 from BramVanroy/windows-compat
Browse files Browse the repository at this point in the history
Encoding generalizations
  • Loading branch information
goodmami authored Sep 1, 2022
2 parents a6030a4 + b505c37 commit d6432fe
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
8 changes: 5 additions & 3 deletions penman/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-

import sys
import os
import argparse
Expand Down Expand Up @@ -170,6 +169,9 @@ def main():
parser.add_argument(
'FILE', nargs='*',
help='read graphs from FILEs instead of stdin')
parser.add_argument(
'--encoding', default=None,
help='encoding to use for input/output. Defaults to system default')
model_group = parser.add_mutually_exclusive_group()
model_group.add_argument(
'--model', metavar='FILE',
Expand Down Expand Up @@ -227,7 +229,7 @@ def main():
if args.quiet:
args.verbosity = 0
sys.stdout.close()
sys.stdout = open(os.devnull, 'w')
sys.stdout = open(os.devnull, 'w', encoding='utf-8')
else:
args.verbosity = min(args.verbosity, 3)

Expand Down Expand Up @@ -264,7 +266,7 @@ def main():

if args.FILE:
for file in args.FILE:
with open(file) as f:
with open(file, encoding=args.encoding) as f:
exitcode = process(
f, model, sys.stdout, sys.stderr, args.check,
normalize_options, format_options, args.triples)
Expand Down
13 changes: 7 additions & 6 deletions penman/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"""
Serialization of PENMAN graphs.
"""

from typing import Union, Iterable, Iterator, List, IO
from typing import Optional, Union, Iterable, Iterator, List, IO
from pathlib import Path

from penman.types import (
Expand Down Expand Up @@ -241,7 +240,8 @@ def _encode(g: Graph,


def _load(source: FileOrFilename,
model: Model = None) -> List[Graph]:
model: Model = None,
encoding: Optional[str] = None) -> List[Graph]:
"""
Deserialize a list of PENMAN-encoded graphs from *source*.
Expand All @@ -253,7 +253,7 @@ def _load(source: FileOrFilename,
"""
codec = PENMANCodec(model=model)
if isinstance(source, (str, Path)):
with open(source) as fh:
with open(source, encoding=encoding) as fh:
return list(codec.iterdecode(fh))
else:
assert hasattr(source, 'read')
Expand All @@ -279,7 +279,8 @@ def _dump(graphs: Iterable[Graph],
file: FileOrFilename,
model: Model = None,
indent: Union[int, bool] = -1,
compact: bool = False) -> None:
compact: bool = False,
encoding: Optional[str] = None) -> None:
"""
Serialize each graph in *graphs* to PENMAN and write to *file*.
Expand All @@ -292,7 +293,7 @@ def _dump(graphs: Iterable[Graph],
"""
codec = PENMANCodec(model=model)
if isinstance(file, (str, Path)):
with open(file, 'w') as fh:
with open(file, 'w', encoding=encoding) as fh:
_dump_stream(fh, graphs, codec, indent, compact)
else:
assert hasattr(file, 'write')
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
long_description = f.read()

about = {}
with open(os.path.join(base_dir, "penman", "__about__.py")) as f:
with open(os.path.join(base_dir, "penman", "__about__.py"), encoding='utf-8') as f:
exec(f.read(), about)

# thanks: https://snarky.ca/clarifying-pep-518/
docs_requirements = os.path.join(base_dir, 'docs', 'requirements.txt')
if os.path.isfile(docs_requirements):
with open(docs_requirements) as f:
with open(docs_requirements, encoding='utf-8') as f:
docs_require = f.readlines()
else:
docs_require = []
Expand Down
8 changes: 4 additions & 4 deletions tests/test_penman.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ def test_loads():

def test_load(tmp_path):
f = tmp_path / 'test_load1'
f.write_text('(a / alpha)(b / beta)')
f.write_text('(a / alpha)(b / beta)', encoding='utf-8')
gs = load(f)
assert len(gs) == 2
assert gs[0].triples == [('a', ':instance', 'alpha')]
assert gs[1].triples == [('b', ':instance', 'beta')]

with f.open() as fh:
with f.open(encoding='utf-8') as fh:
assert load(fh) == gs


Expand All @@ -54,9 +54,9 @@ def test_dump(tmp_path):
f1 = tmp_path / 'test_dump1'
f2 = tmp_path / 'test_dump2'
dump(gs, f1)
with f2.open('w') as fh:
with f2.open('w', encoding='utf-8') as fh:
dump(gs, fh)
assert f1.read_text() == f2.read_text()
assert f1.read_text(encoding='utf-8') == f2.read_text(encoding='utf-8')


def test_parse():
Expand Down

0 comments on commit d6432fe

Please sign in to comment.