aboutsummaryrefslogtreecommitdiffstats
path: root/fuzzycat/serials.py
blob: 2f1782d78f65434b4de83e63a57868e6cbe2690a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# coding: utf-8
"""
Serial name matching. Includes names from issn database.
"""

import os
import shelve

__all__ = ["serialsdb"]


class SerialsDatabase:
    """
    Lookup allows to lookup serial names, using a database of real serial names.

        >>> from serials import serialsdb
        >>> serialsdb.get("Philosophica")
        {'1857-9272', '2232-299X', '2232-3007', '2232-3015'}

    """
    def __init__(self, path=None):
        """
        Note that shelve appends "db" to the name automatically. TODO: make this
        auto-download into a cache directory.
        """
        if path is None:
            path = os.path.join(os.path.expanduser("~"), ".cache/fuzzycat/names")
        self.db = shelve.open(path, flag='r')

    def __getitem__(self, v):
        return self.db[v]

    def get(self, v, default=None, cleanup_pipeline=None):
        if not cleanup_pipeline:
            return self.db.get(v, default=default)
        return self.db.get(cleanup_pipeline(v), default=default)

    def close(self):
        self.db.close()


# A singleton.
serialsdb = SerialsDatabase()