Skip to content

Commit 31fd7d5

Browse files
Change when we do source file verification (#344)
1 parent b2dcce2 commit 31fd7d5

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

pypandoc/__init__.py

+42-15
Original file line numberDiff line numberDiff line change
@@ -138,34 +138,61 @@ def convert_file(source_file:Union[list, str, Path, Generator], to:str, format:U
138138
:raises OSError: if pandoc is not found; make sure it has been installed and is available at
139139
path.
140140
"""
141+
# check if we have a working directory
142+
# if we don't, we use the current working directory
143+
if cworkdir is None:
144+
cworkdir = os.getcwd()
145+
141146
# TODO: remove 'encoding' parameter and warning
142147
if encoding != "utf-8":
143148
logger.warning("The 'encoding' parameter will be removed in version 1.13. Just remove the parameter, because currently the method does not use it.")
144149

145-
# This if block effectively adds support for pathlib.Path objects
146-
# and generators produced by pathlib.Path().glob().
147-
if not isinstance(source_file, str):
148-
try:
149-
source_file = list(map(str, source_file))
150-
except TypeError:
151-
source_file = str(source_file)
152-
153-
if not _identify_path(source_file):
154-
raise RuntimeError("source_file is not a valid path")
155150
if _is_network_path(source_file): # if the source_file is an url
156151
format = _identify_format_from_path(source_file, format)
157152
return _convert_input(source_file, format, 'path', to, extra_args=extra_args,
158153
outputfile=outputfile, filters=filters,
159154
verify_format=verify_format, sandbox=sandbox,
160155
cworkdir=cworkdir)
161156

162-
discovered_source_files = []
157+
# convert the source file to a path object internally
163158
if isinstance(source_file, str):
164-
discovered_source_files += glob.glob(source_file)
165-
if isinstance(source_file, list): # a list of possibly file or file patterns. Expand all with glob
166-
for filepath in source_file:
167-
discovered_source_files.extend(glob.glob(filepath))
159+
source_file = Path(source_file)
160+
elif isinstance(source_file, list):
161+
source_file = [Path(x) for x in source_file]
162+
elif isinstance(source_file, Generator):
163+
source_file = [Path(x) for x in source_file]
164+
165+
166+
# we are basically interested to figure out if its an absolute path or not
167+
# if it's not, we want to prefix the working directory
168+
# if it's a list, we want to prefix the working directory to each item if it's not an absolute path
169+
# if it is, just use the absolute path
170+
if isinstance(source_file, list):
171+
source_file = [x if x.is_absolute() else Path(cworkdir, x) for x in source_file]
172+
elif isinstance(source_file, Generator):
173+
source_file = (x if x.is_absolute() else Path(cworkdir, x) for x in source_file)
174+
# check ifjust a single path was given
175+
elif isinstance(source_file, Path):
176+
source_file = source_file if source_file.is_absolute() else Path(cworkdir, source_file)
168177

178+
179+
discovered_source_files = []
180+
# if we have a list of files, we need to glob them
181+
# if we have a single file, we need to glob it
182+
# remember that we already converted the source_file to a path object
183+
# so for glob.glob use both the dir and file name
184+
if isinstance(source_file, list):
185+
for single_source in source_file:
186+
discovered_source_files.extend(glob.glob(str(single_source)))
187+
if discovered_source_files == []:
188+
discovered_source_files = source_file
189+
else:
190+
discovered_source_files.extend(glob.glob(str(source_file)))
191+
if discovered_source_files == []:
192+
discovered_source_files = [source_file]
193+
194+
if not _identify_path(discovered_source_files):
195+
raise RuntimeError("source_file is not a valid path")
169196
format = _identify_format_from_path(discovered_source_files[0], format)
170197
if len(discovered_source_files) == 1:
171198
discovered_source_files = discovered_source_files[0]

0 commit comments

Comments
 (0)