mfarragher / obsidiantools

Obsidian tools - a Python package for analysing an Obsidian.md vault
Other
402 stars 28 forks source link

Fix get_md_relpaths_from_dir() error on MacOS #11

Closed ja14000 closed 2 years ago

ja14000 commented 2 years ago

Running the example from the readme on MacOS gives the below error:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    vault = otools.Vault(VAULT_DIRECTORY).connect().gather()
  File "api.py", line 89, in __init__
    self._file_index = self._get_md_relpaths_by_name(
  File "/obsidiantools/obsidiantools/api.py", line 434, in _get_md_relpaths_by_name
    return {f.stem: f for f in self._get_md_relpaths(**kwargs)}
  File "/obsidiantools/obsidiantools/api.py", line 424, in _get_md_relpaths
    return get_md_relpaths_matching_subdirs(self._dirpath, **kwargs)
  File "/obsidiantools/obsidiantools/md_utils.py", line 62, in get_md_relpaths_matching_subdirs
    return get_md_relpaths_from_dir(dir_path)
  File "/obsidiantools/obsidiantools/md_utils.py", line 29, in get_md_relpaths_from_dir
    for p in glob(str(dir_path / '**/*.md'), recursive=True)]
TypeError: unsupported operand type(s) for /: 'str' and 'str'

The error is due to oddities in the glob module on MacOS, it can be fixed by passing an f'string to the glob function call in get_md_relpaths_from_dir(), rather than performing a type conversion using the str() function:

Original:

return [Path(p).relative_to(dir_path)
        for p in glob(str(dir_path / '**/*.md'), recursive=True)]

Changed:

return [Path(p).relative_to(dir_path)
        for p in glob(f"{dir_path}/**/*.md", recursive=True)]
stepsal commented 2 years ago

This needs a fix for Ubuntu also https://github.com/mfarragher/obsidiantools/issues/12

mfarragher commented 2 years ago

Thanks for the PR suggestion. I've had a busy few months of studying but will have time in August to look at outstanding issues & PRs now, and start pulling things into the dev branch.