Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit 47ad6a2

Browse files
committed
Refactor minorly: tabs, settings values
1 parent 9e8d41b commit 47ad6a2

File tree

4 files changed

+55
-42
lines changed

4 files changed

+55
-42
lines changed

tests/berlinium/basic_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sockjs.tornado import SockJSRouter
1010

1111
import set_path
12-
from tilde.core.settings import settings
12+
from tilde.core.settings import settings, GUI_URL_TPL
1313
from tilde.core.api import API
1414
import tilde.core.model as model
1515
from tilde.berlinium import Async_Connection, add_redirection
@@ -60,7 +60,7 @@ def sleep(req, client_id, db_session):
6060
DuplexRouter = SockJSRouter(Connection)
6161

6262
application = web.Application(
63-
add_redirection(DuplexRouter.urls, settings['gui_url']),
63+
add_redirection(DuplexRouter.urls, GUI_URL_TPL % settings['webport']),
6464
debug = True if logging.getLogger().getEffectiveLevel()==logging.DEBUG or settings['debug_regime'] else False
6565
)
6666
application.listen(settings['webport'], address='0.0.0.0')

tilde/core/settings.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Author: Evgeny Blokhin
44

55
import os, sys
6-
import json # using native driver due to indent
6+
import json
77

88
from sqlalchemy import create_engine
99
from sqlalchemy.orm import sessionmaker, scoped_session
@@ -23,6 +23,8 @@
2323
INIT_DATA = os.path.join(DATA_DIR, 'sql/init-data.sql')
2424
TEST_DBS_FILE = os.path.join(DATA_DIR, 'test_dbs.txt')
2525
TEST_DBS_REF_FILE = os.path.join(DATA_DIR, 'test_dbs_ref.txt')
26+
SETTINGS_PATH = DATA_DIR + os.sep + SETTINGS_FILE
27+
GUI_URL_TPL = 'http://tilde-lab.github.io/berlinium/?http://127.0.0.1:%s'
2628

2729
DEFAULT_SETUP = {
2830

@@ -46,18 +48,21 @@
4648

4749
# Server part
4850
'webport': 8070,
49-
'title': "Tilde GUI",
50-
'gui_url': "http://tilde-lab.github.io/berlinium/?https://db.tilde.pro"
51+
'title': "Tilde GUI"
5152
}
5253

5354
def virtualize_path(item):
5455
return item
5556

5657
def connect_url(settings, named=None):
5758
if settings['db']['engine'] == 'sqlite':
58-
if not named: named = settings['db']['default_sqlite_db']
59-
if os.sep in named: named = os.path.realpath(os.path.abspath(named))
60-
else: named = os.path.join(DATA_DIR, named)
59+
if not named:
60+
named = settings['db']['default_sqlite_db']
61+
if os.sep in named:
62+
named = os.path.realpath(os.path.abspath(named))
63+
else:
64+
named = os.path.join(DATA_DIR, named)
65+
6166
return settings['db']['engine'] + ':///' + named
6267

6368
elif settings['db']['engine'] == 'postgresql':
@@ -114,10 +119,10 @@ def write_settings(settings):
114119
'''
115120
if not os.access(DATA_DIR, os.W_OK): return False
116121
try:
117-
f = open(DATA_DIR + os.sep + SETTINGS_FILE, 'w')
122+
f = open(SETTINGS_PATH, 'w')
118123
f.writelines(json.dumps(settings, indent=0))
119124
f.close()
120-
os.chmod(os.path.abspath(DATA_DIR + os.sep + SETTINGS_FILE), 0o777) # to avoid (or create?) IO problems with multiple users
125+
os.chmod(os.path.abspath(SETTINGS_PATH), 0o777) # to avoid (or create?) IO problems with multiple users
121126
except IOError:
122127
return False
123128
else:
@@ -177,17 +182,25 @@ def get_hierarchy(settings):
177182
return hierarchy, hierarchy_groups, hierarchy_values
178183

179184
# DEFAULT ACTIONS: LOAD/SAVE SETTINGS
180-
if not os.path.exists( os.path.abspath( DATA_DIR + os.sep + SETTINGS_FILE ) ):
185+
if not os.path.exists(os.path.abspath(SETTINGS_PATH)):
181186
settings = DEFAULT_SETUP
182187
if not os.path.exists(DATA_DIR):
183-
try: os.makedirs(DATA_DIR)
184-
except IOError: sys.exit('I/O error: failed write ' + DATA_DIR)
185-
if not write_settings(settings): sys.exit('I/O error: failed to save settings in ' + DATA_DIR)
188+
try:
189+
os.makedirs(DATA_DIR)
190+
except IOError:
191+
sys.exit('I/O error: failed write ' + DATA_DIR)
192+
if not write_settings(settings):
193+
sys.exit('I/O error: failed to save settings in ' + DATA_DIR)
194+
186195
try: settings
187196
except NameError:
188-
try: settings = json.loads( open( DATA_DIR + os.sep + SETTINGS_FILE ).read() )
189-
except ValueError: sys.exit('Your '+DATA_DIR + os.sep + SETTINGS_FILE+' seems to be bad-formatted, please, pay attention to commas and quotes!')
190-
except IOError: sys.exit('Your '+DATA_DIR + os.sep + SETTINGS_FILE+' is not accessible!')
197+
try:
198+
settings = json.loads(open(SETTINGS_PATH).read())
199+
except ValueError:
200+
sys.exit('Your ' + SETTINGS_PATH + ' seems to be bad-formatted, please, pay attention to commas and quotes!')
201+
except IOError:
202+
sys.exit('Your ' + SETTINGS_PATH + ' is not accessible!')
203+
191204
DEFAULT_SETUP.update(settings)
192205
settings = DEFAULT_SETUP
193206

utils/entry.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
#!/usr/bin/env python
2-
#
3-
# Entry junction
4-
# Author: Evgeny Blokhin
52
'''
6-
Tilde is general-purpose materials informatics framework
7-
for intelligent organizers of the scientific modeling data.
3+
Entry junction
4+
5+
Tilde is a general-purpose materials informatics framework
6+
for intelligent organizers of the first-principles modeling data.
87
More info: https://tilde.pro
8+
9+
Author: Evgeny Blokhin
910
'''
10-
from __future__ import print_function
1111
import os, sys
1212
import time
1313
import logging
14-
1514
import argparse
15+
1616
from numpy import array
1717
from numpy.linalg import det
1818

1919
import chk_tilde_install
2020

21-
from tilde.core.settings import settings, connect_database, DATA_DIR, DB_SCHEMA_VERSION
21+
from tilde.core.settings import settings, connect_database, DATA_DIR, SETTINGS_PATH, DB_SCHEMA_VERSION
2222
from tilde.core.common import write_cif, num2name
2323
from tilde.core.symmetry import SymmetryFinder
2424
from tilde.core.api import API
@@ -32,22 +32,22 @@
3232
parser = argparse.ArgumentParser(
3333
prog="[this_script]",
3434
usage="%(prog)s [positional / optional arguments]",
35-
epilog="API v%s, DB schema v%s (%s backend)" % (API.version, DB_SCHEMA_VERSION, settings['db']['engine']),
35+
epilog="API v%s, DB schema v%s (%s backend), settings from %s" % (API.version, DB_SCHEMA_VERSION, settings['db']['engine'], SETTINGS_PATH),
3636
argument_default=argparse.SUPPRESS
3737
)
38-
parser.add_argument("path", action="store", help="Scan file(s) / folder(s) / matching-filename(s), divide by space", metavar="PATH(S)/FILE(S)", nargs='*', default=False)
39-
parser.add_argument("-y", dest="symprec", action="store", help="symmetry detecting tolerance (default %.01e)" % SymmetryFinder.accuracy, type=float, metavar="float", nargs='?', const=None, default=None)
40-
parser.add_argument("-r", dest="recursive", action="store", help="scan recursively", type=bool, metavar="", nargs='?', const=True, default=False)
41-
parser.add_argument("-t", dest="terse", action="store", help="terse print during scan", type=bool, metavar="", nargs='?', const=True, default=False)
42-
parser.add_argument("-a", dest="add", action="store", help="add results to the database", type=str, metavar="N if sqlite", nargs='?', const=settings['db']['default_sqlite_db'], default=False)
43-
parser.add_argument("-v", dest="convergence", action="store", help="print calculation convergence", type=bool, metavar="", nargs='?', const=True, default=False)
44-
parser.add_argument("-f", dest="freqs", action="store", help="print phonons", type=bool, metavar="", nargs='?', const=True, default=False)
45-
parser.add_argument("-i", dest="info", action="store", help="print tags", type=bool, metavar="", nargs='?', const=True, default=False)
46-
parser.add_argument("-m", dest="module", action="store", help="invoke a module from the list", nargs='?', const=True, default=False, choices=list(Tilde.Apps.keys()))
47-
parser.add_argument("-s", dest="structures", action="store", help="print the final lattice and the final atomic structure", type=bool, metavar="", nargs='?', const=True, default=False)
48-
parser.add_argument("-c", dest="cif", action="store", help="save i-th CIF structure in \"data\" folder", type=int, metavar="i", nargs='?', const=-1, default=False)
49-
parser.add_argument("-x", dest="service", action="store", help="print total number of items (use to create schema)", type=bool, metavar="", nargs='?', const=True, default=False)
50-
parser.add_argument("-l", dest="targetlist", action="store", help="file with scan targets", type=str, metavar="file", nargs='?', const=None, default=None)
38+
parser.add_argument("path", action="store", help="Scan file(s) / folder(s) / matching-filename(s), divide by space", metavar="PATH(S)/FILE(S)", nargs='*', default=False)
39+
parser.add_argument("-y", dest="symprec", action="store", help="symmetry detecting tolerance (default %.01e)" % SymmetryFinder.accuracy, type=float, metavar="float", nargs='?', const=None, default=None)
40+
parser.add_argument("-r", dest="recursive", action="store", help="scan recursively", type=bool, metavar="", nargs='?', const=True, default=False)
41+
parser.add_argument("-t", dest="terse", action="store", help="terse print during scan", type=bool, metavar="", nargs='?', const=True, default=False)
42+
parser.add_argument("-a", dest="add", action="store", help="add results to the database", type=str, metavar="N if sqlite", nargs='?', const=settings['db']['default_sqlite_db'], default=False)
43+
parser.add_argument("-v", dest="convergence", action="store", help="print calculation convergence", type=bool, metavar="", nargs='?', const=True, default=False)
44+
parser.add_argument("-f", dest="freqs", action="store", help="print phonons", type=bool, metavar="", nargs='?', const=True, default=False)
45+
parser.add_argument("-i", dest="info", action="store", help="print tags", type=bool, metavar="", nargs='?', const=True, default=False)
46+
parser.add_argument("-m", dest="module", action="store", help="invoke a module from the list", nargs='?', const=True, default=False, choices=list(Tilde.Apps.keys()))
47+
parser.add_argument("-s", dest="structures", action="store", help="print the final lattice and the final atomic structure", type=bool, metavar="", nargs='?', const=True, default=False)
48+
parser.add_argument("-c", dest="cif", action="store", help="save i-th CIF structure in \"data\" folder", type=int, metavar="i", nargs='?', const=-1, default=False)
49+
parser.add_argument("-x", dest="service", action="store", help="print total number of items (use to create schema)", type=bool, metavar="", nargs='?', const=True, default=False)
50+
parser.add_argument("-l", dest="targetlist", action="store", help="file with scan targets", type=str, metavar="file", nargs='?', const=None, default=None)
5151
args = parser.parse_args()
5252

5353
session = None

utils/gui_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import chk_tilde_install
2323

24-
from tilde.core.settings import settings
24+
from tilde.core.settings import settings, GUI_URL_TPL
2525
from tilde.core.api import API
2626
from tilde.parsers import HASH_LENGTH
2727
from tilde.core.common import html_formula, extract_chemical_symbols, str2html, num2name, generate_cif
@@ -336,12 +336,12 @@ def settings(req, client_id, db_session):
336336
DuplexRouter = SockJSRouter(Connection)
337337

338338
application = web.Application(
339-
add_redirection(DuplexRouter.urls, settings['gui_url']),
339+
add_redirection(DuplexRouter.urls, GUI_URL_TPL % settings['webport']),
340340
debug = True if logging.getLogger().getEffectiveLevel()==logging.DEBUG or settings['debug_regime'] else False
341341
)
342342
application.listen(settings['webport'], address='0.0.0.0')
343343

344-
logging.warning("%s (%s backend) is on the air on port %s" % (CURRENT_TITLE, settings['db']['engine'], settings['webport']))
344+
logging.warning("%s (%s backend): http://127.0.0.1:%s" % (CURRENT_TITLE, settings['db']['engine'], settings['webport']))
345345
logging.warning("Connections are %s" % Connection.Type)
346346
logging.warning("Press Ctrl+C to quit")
347347

0 commit comments

Comments
 (0)