mfarragher / obsidiantools

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

Vault initialization fails when KB path is string #2

Closed taivop closed 2 years ago

taivop commented 2 years ago

When I initialize the vault with a string path, it fails:

import obsidiantools.api as otools
import os

path = os.path.expanduser("~/kb")
vault = otools.Vault(path).connect()

gives

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/4t/bqbt3lbs0x7fg8gb6bzvthq00000gn/T/ipykernel_30638/336964736.py in <module>
      3 
      4 path = os.path.expanduser("~/kb")
----> 5 vault = otools.Vault(path).connect()

~/repos/obsidiantools/obsidiantools/api.py in __init__(self, dirpath)
     69         """
     70         self._dirpath = dirpath
---> 71         self._file_index = self._get_md_relpaths_by_name()
     72 
     73         # graph setup

~/repos/obsidiantools/obsidiantools/api.py in _get_md_relpaths_by_name(self)
    323             dict
    324         """
--> 325         return {f.stem: f for f in self._get_md_relpaths()}
    326 
    327     def _get_wikilinks_index(self):

~/repos/obsidiantools/obsidiantools/api.py in _get_md_relpaths(self)
    313             list
    314         """
--> 315         return get_md_relpaths_from_dir(self._dirpath)
    316 
    317     def _get_md_relpaths_by_name(self):

~/repos/obsidiantools/obsidiantools/md_utils.py in get_md_relpaths_from_dir(dir_path)
     23     """
     24     return [Path(p).relative_to(dir_path)
---> 25             for p in glob(str(dir_path / '**/*.md'), recursive=True)]
     26 
     27 

TypeError: unsupported operand type(s) for /: 'str' and 'str'

I would expect the initializer to either work with a string path, or explicitly declare the required type in the function as pathlib.Path, and emphasize that in the documentation.

I'm on macOS, Python 3.9.1, obsidiantools 5c86662.

mfarragher commented 2 years ago

Hi @taivop yes this is specified as being for pathlib Path explicitly in the documentation - in the Vault class __init__.

For example:

A Vault object lets you dig into your Obsidian vault, by giving
you a toolkit for analysing its contents.  Specify a dirpath to
instantiate the class.  This class is intended to support multiple
operating systems so pass a pathlib Path object.
        Args:
            dirpath (pathlib Path): the directory to analyse.  This would
                typically be the vault's directory.  If you have a
                subdirectory of the vault with notes you want to inspect,
                then you could pass that.
    @property
    def dirpath(self):
        """pathlib Path"""
        return self._dirpath

I can make this clearer in the readme and demo. For ease I will stick to just pathlib Path arg, it seems to be becoming the more modern preference.