This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.py
176 lines (143 loc) · 5.48 KB
/
utils.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import logging
import os
import tempfile
import pandas as pd
import sqlalchemy as sa
from csvkit.utilities.in2csv import In2CSV
from flask import current_app, json
from pandas.io.sql import SQLTable, pandasSQL_builder
from sqlalchemy.ext.automap import automap_base
import config
import sandman2
import swagger
from refresh_log import AutoapiTableRefreshLog
from sandman2 import model
logger = logging.getLogger(__name__)
def get_name(path):
return os.path.splitext(os.path.split(path)[1])[0]
class APIJSONEncoder(json.JSONEncoder):
def default(self, o):
if hasattr(o, 'isoformat'):
return o.isoformat()
return super(APIJSONEncoder, self).default(o)
class Base(model.Model):
__methods__ = {'GET'}
AutomapModel = automap_base(cls=(Base, model.db.Model))
def to_sql(name, engine, frame, chunksize=None, **kwargs):
pandas_sql_engine = pandasSQL_builder(engine)
table = SQLTable(name, pandas_sql_engine, frame=frame, **kwargs)
table.create()
table.insert(chunksize)
def ensure_csv(filename):
"""Ensure that `filename` is a CSV.
:param filename: Name of tabular file
:returns: File pointer to original or converted file
"""
_, ext = os.path.splitext(filename)
if ext == '.csv':
return open(filename)
logger.info('Converting file {0} to CSV'.format(filename))
file = tempfile.NamedTemporaryFile('w')
converter = In2CSV((filename, ))
converter.args.input_path = filename
converter.output_file = file
converter.main()
return file
def clear_tables(metadata=None, engine=None):
metadata = metadata or sa.MetaData()
if not metadata.bind:
metadata.bind = engine or sa.create_engine(config.SQLA_URI)
metadata.reflect()
clearable = [m[1]
for m in metadata.tables.items()
if m[0] != AutoapiTableRefreshLog.__tablename__]
logger.debug('{} clearable tables:'.format(len(clearable)))
logger.debug(str([t.name for t in clearable]))
try:
metadata.drop_all(tables=clearable)
except Exception as e:
logger.info('drop error: {}'.format(str(e)))
logger.info('all clearables dropped')
def load_table(filename,
tablename,
metadata=None,
engine=None,
infer_size=100,
chunksize=1000):
metadata = metadata or sa.MetaData()
engine = engine or sa.create_engine(config.SQLA_URI)
file = ensure_csv(filename)
# Pass data types to iterator to ensure consistent types across chunks
dtypes = pd.read_csv(file.name, nrows=infer_size,
skipinitialspace=True).dtypes
chunks = pd.read_csv(file.name,
chunksize=chunksize,
dtype=dtypes,
skipinitialspace=True)
for idx, chunk in enumerate(chunks):
to_sql(tablename,
engine,
chunk,
chunksize=chunksize,
keys='index',
if_exists='append', )
_index_table(tablename, metadata, engine, config.CASE_INSENSITIVE)
def _index_table(tablename, metadata, engine, case_insensitive=False):
"""Index all columns on `tablename`, optionally using case-insensitive
indexes on string columns when supported by the database.
"""
table = sa.Table(tablename, metadata, autoload_with=engine)
for label, column in table.columns.items():
if label == 'index':
continue
index_name = 'ix_{0}'.format(label.lower())
indexes = [sa.Index(index_name, column)]
if case_insensitive:
indexes.insert(0, sa.Index(index_name, sa.func.upper(column)))
for index in indexes:
try:
index.drop(engine)
except sa.exc.DatabaseError:
pass
try:
index.create(engine)
except sa.exc.DatabaseError:
pass
def drop_table(tablename, metadata=None, engine=None):
logger.info('Dropping table {0}'.format(tablename))
metadata = metadata or sa.MetaData()
engine = engine or sa.create_engine(config.SQLA_URI)
try:
table = sa.Table(tablename, metadata, autoload_with=engine)
table.drop(engine)
except sa.exc.NoSuchTableError:
pass
def get_tables(engine=None):
engine = engine or sa.create_engine(config.SQLA_URI)
inspector = sa.engine.reflection.Inspector.from_engine(sandman2.db.engine)
return set(inspector.get_table_names())
def refresh_tables():
sandman2.db.metadata.clear()
activate()
def activate():
with current_app.app_context():
rules = [
rule
for rule in current_app.url_map._rules
if is_service_rule(rule, current_app)
]
for rule in rules:
current_app.url_map._rules.remove(rule)
current_app.url_map._rules_by_endpoint.pop(rule.endpoint, None)
current_app.view_functions.pop(rule.endpoint, None)
sandman2.AutomapModel.classes.clear()
sandman2.AutomapModel.metadata.clear()
sandman2._reflect_all(Base=AutomapModel)
tables = get_tables()
sandman2.unregister_services(to_keep=tables)
current_app.__spec__ = swagger.make_spec(current_app)
current_app.config['SQLALCHEMY_TABLES'] = tables
def is_service_rule(rule, app):
view = current_app.view_functions[rule.endpoint]
return hasattr(view, 'view_class') and issubclass(view.view_class,
sandman2.Service)