-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbhelpers.py
158 lines (109 loc) · 3.23 KB
/
dbhelpers.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
"""
HipparchiaSQLoader: archive and restore a database of Greek and Latin texts
Copyright: E Gunderson 2016-18
License: GNU GENERAL PUBLIC LICENSE 3
(see LICENSE in the top level directory of the distribution)
"""
import configparser
import re
from collections import deque
from multiprocessing import Value, Process
from os import name as osname
from connection import setconnection, ConnectionObject
config = configparser.ConfigParser()
config.read('config.ini')
sqltemplateversion = 10082019
def icanpickleconnections():
result = True
c = (ConnectionObject(),)
j = Process(target=type, args=c)
try:
j.start()
j.join()
except TypeError:
# can't pickle psycopg2.extensions.connection objects
result = False
c[0].connectioncleanup()
return result
def templatetest():
"""
make sure that HipparchiaSQLoader knows how to deal with the databases that are coming its way
:return:
"""
dbc = setconnection()
cur = dbc.cursor()
q = 'SELECT templateversion FROM builderversion'
cur.execute(q)
versions = cur.fetchall()
versions = set([x[0] for x in versions])
compatible = 1
version = sqltemplateversion
for v in versions:
if v != sqltemplateversion:
version = v
compatible = 0
testresults = {compatible: version}
dbc.connectioncleanup()
return testresults
def knowncorpora():
"""
test for what is available
return list of available dbs
:return:
"""
dbc = setconnection()
cur = dbc.cursor()
q = 'SELECT universalid FROM authors'
cur.execute(q)
ids = cur.fetchall()
ids = [x[0] for x in ids]
prefixes = set([id[0:2] for id in ids])
dbc.connectioncleanup()
return prefixes
def loadschemafromfile(tablename, templatetablename, filename):
"""
:param schemaname:
:param filename:
:return:
"""
with open(filename, 'rt') as s:
fullschema = s.readlines()
# do not name every work table 'gr0001' or every wordcount table 'wordcounts_0'
fullschema = [re.sub(templatetablename, tablename, x) for x in fullschema]
fullschema = [s.strip() for s in fullschema]
return fullschema
def loadcolumnsfromfile(filename):
"""
find the core column structure from a set of schema lines
return a set of tuples that associate the name of the column with its structure:
... ('accented_line', 'text'), ('stripped_line', 'text'), ...
:param schemalines:
:return:
"""
with open(filename) as s:
schemalines = s.readlines()
schemalines = deque([s.strip() for s in schemalines])
columnfinder = re.compile(r'CREATE TABLE', re.MULTILINE)
# prune away the stuff that is on either side of 'CREATE TABLE ... ( ... );
thisline = ''
while re.search(columnfinder, thisline) is None:
thisline = schemalines.popleft()
columns = list()
while re.search(r'^\);', thisline) is None:
thisline = schemalines.popleft()
columns.append(thisline)
columns = columns[:-1]
# now you have the core a list of items like 'wkuniversalid character varying(10),'
columns = [re.sub(r',$', '', c) for c in columns]
columns = [(c.split(' ')[0], ' '.join(c.split(' ')[1:])) for c in columns]
return columns
class MPCounter(object):
def __init__(self):
self.val = Value('i', 0)
def increment(self, n=1):
with self.val.get_lock():
self.val.value += n
@property
def value(self):
return self.val.value