Skip to content

Commit 45b366e

Browse files
committed
fix regression in orgparse.load method for file-like objects
broken in 3067189 see #32
1 parent ce8f6cc commit 45b366e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

orgparse/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,19 @@
107107
# [[[end]]]
108108

109109
import codecs
110-
from typing import Iterable
110+
from pathlib import Path
111+
from typing import Iterable, Union, Optional, TextIO
111112

112-
from .node import parse_lines, OrgNode # todo basenode??
113+
114+
from .node import parse_lines, OrgEnv, OrgNode # todo basenode??
113115
from .utils.py3compat import basestring
114116

115117
__author__ = 'Takafumi Arakaki, Dmitrii Gerasimov'
116118
__license__ = 'BSD License'
117119
__all__ = ["load", "loads", "loadi"]
118120

119121

120-
def load(path, env=None):
122+
def load(path: Union[str, Path, TextIO], env: Optional[OrgEnv]=None) -> OrgNode:
121123
"""
122124
Load org-mode document from a file.
123125
@@ -127,18 +129,18 @@ def load(path, env=None):
127129
:rtype: :class:`orgparse.node.OrgRootNode`
128130
129131
"""
130-
path = str(path) # in case of pathlib.Path
131-
if isinstance(path, basestring):
132-
orgfile = codecs.open(path, encoding='utf8')
133-
filename = path
132+
orgfile: TextIO
133+
if isinstance(path, (str, Path)):
134+
orgfile = codecs.open(str(path), encoding='utf8')
135+
filename = str(path)
134136
else:
135137
orgfile = path
136138
filename = path.name if hasattr(path, 'name') else '<file-like>'
137139
return loadi((l.rstrip('\n') for l in orgfile.readlines()),
138140
filename=filename, env=env)
139141

140142

141-
def loads(string: str, filename='<string>', env=None) -> OrgNode:
143+
def loads(string: str, filename: str='<string>', env: Optional[OrgEnv]=None) -> OrgNode:
142144
"""
143145
Load org-mode document from a string.
144146
@@ -148,7 +150,7 @@ def loads(string: str, filename='<string>', env=None) -> OrgNode:
148150
return loadi(string.splitlines(), filename=filename, env=env)
149151

150152

151-
def loadi(lines: Iterable[str], filename='<lines>', env=None) -> OrgNode:
153+
def loadi(lines: Iterable[str], filename: str='<lines>', env: Optional[OrgEnv]=None) -> OrgNode:
152154
"""
153155
Load org-mode document from an iterative object.
154156

orgparse/tests/test_misc.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,14 @@ def test_filetags_are_tags() -> None:
158158
assert root.tags == {'f1', 'f2'}
159159
child = root.children[0].children[0]
160160
assert child.tags == {'f1', 'f2', 'h1'}
161+
162+
163+
def test_load_filelike() -> None:
164+
import io
165+
stream = io.StringIO('''
166+
* heading1
167+
* heading 2
168+
''')
169+
root = load(stream)
170+
assert len(root.children) == 2
171+
assert root.env.filename == '<file-like>'

0 commit comments

Comments
 (0)