forked from labrad/servers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_vault.py
104 lines (89 loc) · 3.38 KB
/
data_vault.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
# Copyright (C) 2007 Matthew Neeley
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
### BEGIN NODE INFO
[info]
name = Data Vault
version = 3.0.1
description = Store and retrieve numeric data
[startup]
cmdline = %PYTHON% %FILE%
timeout = 20
[shutdown]
message = 987654321
timeout = 5
### END NODE INFO
"""
from __future__ import absolute_import
import os
import sys
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks, returnValue
import labrad.util
import labrad.wrappers
from datavault import SessionStore
from datavault.server import DataVault
@inlineCallbacks
def load_settings(cxn, name):
"""Load settings from registry with fallback to command line if needed.
Attempts to load the data vault configuration for this node from the
registry. If not configured, we instead prompt the user to enter a path
to use for storing data, and save this config into the registry to be
used later.
"""
path = ['', 'Servers', name, 'Repository']
nodename = labrad.util.getNodeName()
reg = cxn.registry
yield reg.cd(path, True)
(dirs, keys) = yield reg.dir()
if nodename in keys:
datadir = yield reg.get(nodename)
elif '__default__' in keys:
datadir = yield reg.get('__default__')
else:
default_datadir = os.path.expanduser('~/.labrad/vault')
print 'Could not load repository location from registry.'
print 'Please enter data storage directory or hit enter to use'
print 'the default directory ({}):'.format(default_datadir)
datadir = os.path.expanduser(raw_input('>>>'))
if datadir == '':
datadir = default_datadir
if not os.path.exists(datadir):
os.makedirs(datadir)
# set as default and for this node
yield reg.set(nodename, datadir)
yield reg.set('__default__', datadir)
print 'Data location configured in the registry at {}: {}'.format(
path + [nodename], datadir)
print 'To change this, edit the registry keys and restart the server.'
returnValue(datadir)
def main(argv=sys.argv):
@inlineCallbacks
def start():
opts = labrad.util.parseServerOptions(name=DataVault.name)
cxn = yield labrad.wrappers.connectAsync(
host=opts['host'], port=int(opts['port']), password=opts['password'])
datadir = yield load_settings(cxn, opts['name'])
yield cxn.disconnect()
session_store = SessionStore(datadir, hub=None)
server = DataVault(session_store)
session_store.hub = server
# Run the server. We do not need to start the reactor, but we will
# stop it after the data_vault shuts down.
labrad.util.runServer(server, run_reactor=False, stop_reactor=True)
_ = start()
reactor.run()
if __name__ == '__main__':
main()