-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathmisc.py
52 lines (48 loc) · 1.82 KB
/
misc.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
import sys
def select_db(sysargs, context=None):
u"""
Create connections to the database and filestore given in sysargs.
:arg sysargs: system arguments, created by OptionParser
:arg context: the context for a \xd8MQ connection, if one is needed
:type context: zmq.Context
:returns: a tuple of the form ``(db, fs)``
"""
import sagecell_config
try:
db=sysargs.db
except:
if hasattr(sagecell_config, 'db'):
db=sagecell_config.db
else:
db='mongo'
try:
fs=sysargs.fs
except:
if hasattr(sagecell_config, 'fs'):
fs=sagecell_config.fs
else:
fs=db
if db=="sqlite":
import db_sqlite
import sqlite3
conn = sqlite3.connect('sqlite.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS cells(device_id BIGINT DEFAULT NULL, input TEXT, output TEXT DEFAULT NULL);")
conn.close()
return db_sqlite.DB('sqlite.db'), None # None should be replaced by the sqlite filestore
elif db=="sqlalchemy":
import db_sqlalchemy
return db_sqlalchemy.DB('sqlalchemy_sqlite.db'), None # None should be replaced by the sqlite filestore
elif db=="mongo":
import pymongo, db_mongo, filestore
mongo_config = sagecell_config.mongo_config
if '@' in mongo_config['mongo_uri']:
# password specified, so we need to include the database in the URI
URI=mongo_config['mongo_uri']+'/'+mongo_config['mongo_db']
else:
URI = mongo_config['mongo_uri']
conn=pymongo.Connection(URI)
return db_mongo.DB(conn), filestore.FileStoreMongo(conn)
elif db=="zmq":
import db_zmq, filestore
return db_zmq.DB(address=sysargs.dbaddress), filestore.FileStoreZMQ(address=sysargs.fsaddress)