Skip to content

Refactor Duplicate Code in _identify_path Function #382

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 32 additions & 35 deletions pypandoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,45 +209,42 @@ def convert_file(source_file:Union[list, str, Path, Generator], to:str, format:U
cworkdir=cworkdir, sort_files=sort_files)


def resolve_path(source) -> bool:
# Check if a file path is valid, handling unicode and wildcards
try:
if os.path.exists(source):
return True
except UnicodeEncodeError:
if os.path.exists(source.encode('utf-8')):
return True

# Handle wildcards
try:
matches = glob.glob(source)
if matches:
return True
except UnicodeEncodeError:
matches = glob.glob(source.encode('utf-8'))
if matches:
return True

# If no valid path is found
return False


def _identify_path(source) -> bool:
# Determine if the source is a valid file path or a network path (URL).
if isinstance(source, list):
for single_source in source:
if not _identify_path(single_source):
return False
return all(_identify_path(item) for item in source)

if resolve_path(source):
return True
is_path = False
try:
is_path = os.path.exists(source)
except UnicodeEncodeError:
is_path = os.path.exists(source.encode('utf-8'))
except: # noqa
# still false
pass

if not is_path:
try:
is_path = len(glob.glob(source)) >= 1
except UnicodeEncodeError:
is_path = len(glob.glob(source.encode('utf-8'))) >= 1
except: # noqa
# still false
pass

if not is_path:
try:
# check if it's an URL
result = urlparse(source)
if result.scheme in ["http", "https"]:
is_path = True
elif result.scheme and result.netloc and result.path:
# complete uri including one with a network path
is_path = True
elif result.scheme == "file" and result.path:
is_path = os.path.exists(url2path(source))
except AttributeError:
pass
if _is_network_path(source):
return True

return False

return is_path

def _is_network_path(source):
try:
Expand Down Expand Up @@ -849,4 +846,4 @@ def clean_pandocpath_cache():


__version = None
__pandoc_path = None
__pandoc_path = None
Loading