Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: heron_load/devdoc/lalib.py @ 0:42ad7288920a

heron-michigan tip
Last change on this file since 0:42ad7288920a was 0:42ad7288920a, checked in by Matt Hoag <mhoag@…>, 6 years ago

Merge with demo_concepts_3800

File size: 1.4 KB
Line 
1'''ladb -- least-authority style database access
2
3.. note: cribbed from pcori_load.py from gpc-pcornet-cdm
4         Mar 17 2b34bc7b71b1
5'''
6
7from contextlib import contextmanager
8from posixpath import basename, splitext, join
9
10
11def maker(wrapped):
12    def make(*args, **kwargs):
13        methods, properties = wrapped(*args, **kwargs)
14        bases = (object,)
15        maker = type(wrapped.__name__, bases,
16                     dict(properties.items() +
17                          [(m.__name__, m) for m in methods]))
18        return maker()
19    return make
20
21
22@maker
23def Rd(path, open_rd, listdir):
24    def __div__(_, sub):
25        fullsub = join(path, sub)
26        if not fullsub.startswith(path):
27            raise IOError('no upward traversal')
28
29        return Rd(fullsub, open_rd, listdir)
30
31    def open(_):
32        return open_rd(path)
33
34    def iterdir(_):
35        return (Rd(join(path, p), open_rd, listdir)
36                for p in listdir(path))
37
38    return [__div__, open, iterdir], dict(
39        name=basename(path),
40        suffix=splitext(path)[1])
41
42
43def dbmgr(connect):
44    '''Make a context manager that yields cursors, given connect access.
45    '''
46    @contextmanager
47    def dbtrx():
48        conn = connect()
49        cur = conn.cursor()
50        try:
51            yield cur
52        except:
53            conn.rollback()
54            raise
55        else:
56            conn.commit()
57        finally:
58            cur.close()
59    return dbtrx
Note: See TracBrowser for help on using the repository browser.