forked from chunfuchen/qiskit-chemistry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preferences.py
170 lines (138 loc) · 5.97 KB
/
preferences.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
# -*- coding: utf-8 -*-
# Copyright 2018 IBM.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
import os
import json
import re
import qiskit_acqua
class Preferences(object):
_FILENAME = '.qiskit_acqua_chemistry'
_VERSION = '1.0'
_QCONFIG_NAME = 'Qconfig'
URL = 'https://quantumexperience.ng.bluemix.net/api'
def __init__(self):
"""Create Preferences object."""
self._preferences = {
'version' : Preferences._VERSION
}
self._qconfig_changed = False
self._logging_config_changed = False
self._token = None
self._url = Preferences.URL
self._hub = None
self._group = None
self._project = None
template_file = os.path.join(os.path.dirname(__file__), 'Qconfig_template.txt')
self._qconfig_template = []
with open(template_file, 'r') as stream:
for line in stream:
self._qconfig_template.append(line)
qconfig = qiskit_acqua.get_qconfig()
if qconfig is not None:
self._token = qconfig.APItoken
if 'url' in qconfig.config:
self._url = qconfig.config['url']
if 'hub' in qconfig.config:
self._hub = qconfig.config['hub']
if 'group' in qconfig.config:
self._group = qconfig.config['group']
if 'project' in qconfig.config:
self._project = qconfig.config['project']
home = os.path.expanduser("~")
self._filepath = os.path.join(home,Preferences._FILENAME)
try:
with open(self._filepath) as json_pref:
self._preferences = json.load(json_pref)
except:
pass
def save(self):
if self._qconfig_changed:
token = "'" + self._token + "'" if self._token is not None else 'None'
url = "'" + self._url + "'" if self._url is not None else 'None'
hub = "'" + self._hub + "'" if self._hub is not None else 'None'
group = "'" + self._group + "'" if self._group is not None else 'None'
project = "'" + self._project + "'" if self._project is not None else 'None'
qconfig_content = [re.sub('&APItoken', token, l) for l in self._qconfig_template]
qconfig_content = [re.sub('&url', url, l) for l in qconfig_content]
qconfig_content = [re.sub('&hub', hub, l) for l in qconfig_content]
qconfig_content = [re.sub('&group', group, l) for l in qconfig_content]
qconfig_content = [re.sub('&project', project, l) for l in qconfig_content]
path = self.get_qconfig_path(os.path.abspath(os.path.join(os.getcwd(),Preferences._QCONFIG_NAME + '.py')))
with open(path, 'w') as stream:
stream.write(''.join(qconfig_content))
self._qconfig_changed = False
qconfig = qiskit_acqua.discover_qconfig(os.getcwd())
if qconfig is not None:
qiskit_acqua.set_qconfig(qconfig)
if self._logging_config_changed:
with open(self._filepath, 'w') as fp:
json.dump(self._preferences, fp, sort_keys=True, indent=4)
self._logging_config_changed = False
def get_version(self):
if 'version' in self._preferences:
return self._preferences['version']
return None
def get_qconfig_path(self,default_value=None):
qconfig = qiskit_acqua.get_qconfig()
if qconfig is not None:
return os.path.abspath(qconfig.__file__)
return default_value
def get_token(self,default_value=None):
if self._token is not None:
return self._token
return default_value
def set_token(self,token):
if self._token != token:
self._qconfig_changed = True
self._token = token
def get_url(self,default_value=None):
if self._url is not None:
return self._url
return default_value
def set_url(self,url):
if self._url != url:
self._qconfig_changed = True
self._url = url
def get_hub(self,default_value=None):
if self._hub is not None:
return self._hub
return default_value
def set_hub(self,hub):
if self._hub != hub:
self._qconfig_changed = True
self._hub = hub
def get_group(self,default_value=None):
if self._group is not None:
return self._group
return default_value
def set_group(self,group):
if self._group != group:
self._qconfig_changed = True
self._group = group
def get_project(self,default_value=None):
if self._project is not None:
return self._project
return default_value
def set_project(self,project):
if self._project != project:
self._qconfig_changed = True
self._project = project
def get_logging_config(self,default_value=None):
if 'logging_config' in self._preferences:
return self._preferences['logging_config']
return default_value
def set_logging_config(self,logging_config):
self._logging_config_changed = True
self._preferences['logging_config'] = logging_config