c-w / gutenberg

A simple interface to the Project Gutenberg corpus.
Apache License 2.0
320 stars 60 forks source link

getting and setting the location of the cache #121

Closed ericleasemorgan closed 5 years ago

ericleasemorgan commented 5 years ago

How can I use the gutenberg module get and set the location of the cache?

From what I can tell, the location of the cache defaults to my home directory through the use of local_path() and defining _DB_PATH. Is there a way to programmatically change the location of the cache to something like /usr/local/gutenberg? Something like this does not work because it returns "'str' object has no attribute 'populate'":"

# configure
CACHE = '/usr/local/gutenberg'

# require
from gutenberg.acquire import get_metadata_cache
from gutenberg.acquire import set_metadata_cache

# initialize
set_metadata_cache( CACHE )
cache = get_metadata_cache()

# do the work and done
cache.populate()
exit()
c-w commented 5 years ago

The simplest way to set the cache location is via the GUTENBERG_DATA environment variable:

export GUTENBERG_DATA=/usr/local/gutenberg
python your_script.py

Alternatively, to set the location in code, does the snippet in the docs work for you?

# first, set up gutenberg to use a custom database location

from gutenberg.acquire import set_metadata_cache
from gutenberg.acquire.metadata import SleepycatMetadataCache

cache = SleepycatMetadataCache('/path/to/metadata.db')
cache.populate()
set_metadata_cache(cache)

# now you can use the library as normal

from gutenberg.query import get_metadata

print(get_metadata('title', 2701))

Let me know if this helps!

ericleasemorgan commented 5 years ago

Setting the environment variable works quite well. Thank you for the prompt reply.