Skip to content

Commit

Permalink
Lazy loading of global.rc
Browse files Browse the repository at this point in the history
Load expensive module only if required.
Allow reload.
  • Loading branch information
matthewrmshin committed Mar 19, 2018
1 parent 94693de commit 27fcc46
Show file tree
Hide file tree
Showing 39 changed files with 214 additions and 198 deletions.
24 changes: 12 additions & 12 deletions bin/cylc-cat-log
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ from cylc.option_parsers import CylcOptionParser as COP
from cylc.rundb import CylcSuiteDAO
from cylc.hostuserutil import is_remote
from cylc.suite_logging import get_logs
from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.task_id import TaskID


Expand Down Expand Up @@ -148,7 +148,7 @@ def get_option_parser():

def get_suite_log_path(options, suite):
"""Return file name of a suite log, given the options."""
log_dir = GLOBAL_CFG.get_derived_host_item(suite, "suite log directory")
log_dir = glbl_cfg().get_derived_host_item(suite, "suite log directory")
if options.list_mode:
basename = "."
else:
Expand Down Expand Up @@ -186,7 +186,7 @@ def get_task_job_log_path(
if submit_num != "NN":
submit_num = "%02d" % submit_num
return os.path.normpath(os.path.join(
GLOBAL_CFG.get_derived_host_item(
glbl_cfg().get_derived_host_item(
suite, "suite job log directory", host, owner),
point, task, submit_num, basename))

Expand All @@ -204,7 +204,7 @@ def get_task_job_attrs(options, suite, point, task, submit_num):
return (None, None)
suite_dao = CylcSuiteDAO(
os.path.join(
GLOBAL_CFG.get_derived_host_item(suite, "suite run directory"),
glbl_cfg().get_derived_host_item(suite, "suite run directory"),
"log", CylcSuiteDAO.DB_FILE_BASE_NAME),
is_public=True)
task_job_data = suite_dao.select_task_job(None, point, task, submit_num)
Expand Down Expand Up @@ -242,7 +242,7 @@ def get_task_job_attrs(options, suite, point, task, submit_num):
key = "out viewer"
else:
key = "err viewer"
conf = GLOBAL_CFG.get_host_item("batch systems", host, owner)
conf = glbl_cfg().get_host_item("batch systems", host, owner)
command0_tmpl = conf[str(task_job_data["batch_sys_name"])][key]
except (KeyError, TypeError):
return (user_at_host, None)
Expand Down Expand Up @@ -291,7 +291,7 @@ def main():
else:
owner, host = (None, user_at_host)

cylc_tmpdir = GLOBAL_CFG.get_tmpdir()
cylc_tmpdir = glbl_cfg().get_tmpdir()

# Construct the shell command
commands = []
Expand All @@ -311,20 +311,20 @@ def main():
elif options.tail:
if user_at_host:
# Replace 'cat' with the remote tail command.
cmd_tmpl = str(GLOBAL_CFG.get_host_item(
cmd_tmpl = str(glbl_cfg().get_host_item(
"remote tail command template", host, owner))
commands.append(shlex.split(cmd_tmpl % {"filename": filename}))
else:
# Replace 'cat' with the local tail command.
cmd_tmpl = str(GLOBAL_CFG.get_host_item(
cmd_tmpl = str(glbl_cfg().get_host_item(
"local tail command template"))
commands.append(shlex.split(cmd_tmpl % {"filename": filename}))
elif options.geditor or options.editor:
# Copy local or remote job file to a local temp file.
viewfile = mkstemp(dir=cylc_tmpdir)[1]
if user_at_host:
cmd = shlex.split(
GLOBAL_CFG.get_host_item('scp command', host, owner)) + [
glbl_cfg().get_host_item('scp command', host, owner)) + [
'%s:%s' % (user_at_host, filename), viewfile]
else:
cmd = ['cp', filename, viewfile]
Expand All @@ -336,9 +336,9 @@ def main():
sys.stderr.write(err)
sys.exit(ret_code)
if options.geditor:
editor = GLOBAL_CFG.get(['editors', 'gui'])
editor = glbl_cfg().get(['editors', 'gui'])
elif options.editor:
editor = GLOBAL_CFG.get(['editors', 'terminal'])
editor = glbl_cfg().get(['editors', 'terminal'])

command_list = shlex.split(editor)
command_list.append(viewfile)
Expand All @@ -351,7 +351,7 @@ def main():

# Deal with [user@]host.
if user_at_host and editor is None:
ssh = str(GLOBAL_CFG.get_host_item("ssh command", host, owner))
ssh = str(glbl_cfg().get_host_item("ssh command", host, owner))
for i, command in enumerate(commands):
commands[i] = shlex.split(ssh) + ["-n", user_at_host] + command

Expand Down
6 changes: 3 additions & 3 deletions bin/cylc-cat-state
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import re
import sqlite3
import traceback

from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.dump import dump_to_stdout, get_stop_state_summary
import cylc.flags
from cylc.option_parsers import CylcOptionParser as COP
Expand Down Expand Up @@ -78,7 +78,7 @@ def _get_state_lines(suite):
"""Get state lines from suite runtime DB."""
dao = CylcSuiteDAO(
os.path.join(
GLOBAL_CFG.get_derived_host_item(suite, 'suite run directory'),
glbl_cfg().get_derived_host_item(suite, 'suite run directory'),
'log', CylcSuiteDAO.DB_FILE_BASE_NAME),
is_public=True)
data = {
Expand Down Expand Up @@ -149,7 +149,7 @@ def _callback_task_pool(data, row):
def _get_state_lines_compat(suite):
"""Read old state file from normal location, for backward compat."""
state_file_path = os.path.join(
GLOBAL_CFG.get_derived_host_item(suite, 'suite run directory'),
glbl_cfg().get_derived_host_item(suite, 'suite run directory'),
"state", "state")
lines = []
for line in open(state_file_path, 'rb').readlines():
Expand Down
14 changes: 7 additions & 7 deletions bin/cylc-documentation
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ from subprocess import call
from optparse import OptionParser

import cylc.flags
from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.run_get_stdout import run_get_stdout


Expand Down Expand Up @@ -93,13 +93,13 @@ def main():
(options, args) = parser.parse_args()
cylc.flags.debug = options.debug

intranet_url = GLOBAL_CFG.get(['documentation', 'urls', 'local index'])
internet_url = GLOBAL_CFG.get(['documentation', 'urls',
intranet_url = glbl_cfg().get(['documentation', 'urls', 'local index'])
internet_url = glbl_cfg().get(['documentation', 'urls',
'internet homepage'])
html_file = GLOBAL_CFG.get(['documentation', 'files', 'html index'])
html_viewer = GLOBAL_CFG.get(['document viewers', 'html'])
pdf_file = GLOBAL_CFG.get(['documentation', 'files', 'pdf user guide'])
pdf_viewer = GLOBAL_CFG.get(['document viewers', 'pdf'])
html_file = glbl_cfg().get(['documentation', 'files', 'html index'])
html_viewer = glbl_cfg().get(['document viewers', 'html'])
pdf_file = glbl_cfg().get(['documentation', 'files', 'pdf user guide'])
pdf_viewer = glbl_cfg().get(['document viewers', 'pdf'])
if len(args) == 0:
# Cylc documentation.
if options.pdf:
Expand Down
6 changes: 3 additions & 3 deletions bin/cylc-edit
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ from subprocess import call
from shutil import copy

import cylc.flags
from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.option_parsers import CylcOptionParser as COP
from cylc.suite_srv_files_mgr import SuiteSrvFilesManager
from parsec.include import inline, \
Expand Down Expand Up @@ -97,9 +97,9 @@ def main():
suiterc = SuiteSrvFilesManager().parse_suite_arg(options, args[0])[1]

if options.geditor:
editor = GLOBAL_CFG.get(['editors', 'gui'])
editor = glbl_cfg().get(['editors', 'gui'])
else:
editor = GLOBAL_CFG.get(['editors', 'terminal'])
editor = glbl_cfg().get(['editors', 'terminal'])

suitedir = os.path.dirname(suiterc)

Expand Down
10 changes: 5 additions & 5 deletions bin/cylc-get-site-config
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ def main():
if len(args) != 0:
parser.error("ERROR: wrong number of arguments")

# import GLOBAL_CFG here to avoid aborting before command help is printed
from cylc.cfgspec.globalcfg import GLOBAL_CFG
# import glbl_cfg here to avoid aborting before command help is printed
from cylc.cfgspec.glbl_cfg import glbl_cfg
if options.run_dir:
print GLOBAL_CFG.get_host_item('run directory')
print glbl_cfg().get_host_item('run directory')
elif options.site_dir:
print GLOBAL_CFG.SITE_CONF_DIR
print glbl_cfg().SITE_CONF_DIR
else:
GLOBAL_CFG.idump(
glbl_cfg().idump(
options.item, sparse=options.sparse, pnative=options.pnative)


Expand Down
4 changes: 2 additions & 2 deletions bin/cylc-gscan
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ gtk.settings_get_default().set_long_property(
gtk.settings_get_default().set_long_property(
"gtk-menu-images", True, "main")

from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.gui.gscan import ScanApp
from cylc.option_parsers import CylcOptionParser as COP

Expand Down Expand Up @@ -81,7 +81,7 @@ def main():
options, args = parser.parse_args()

if options.all_ports:
args.extend(GLOBAL_CFG.get(["suite host scanning", "hosts"]))
args.extend(glbl_cfg().get(["suite host scanning", "hosts"]))
scan_app = ScanApp(
hosts=args,
patterns_name=options.patterns_name,
Expand Down
6 changes: 3 additions & 3 deletions bin/cylc-jobscript
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ __END__
editor () {
python -c "
import sys
from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
if sys.argv[1] in ['-g', '--gedit']:
print GLOBAL_CFG.get(['editors', 'gui'])
print glbl_cfg().get(['editors', 'gui'])
elif sys.argv[1] in ['-e', '--edit']:
print GLOBAL_CFG.get(['editors', 'terminal'])
print glbl_cfg().get(['editors', 'terminal'])
" $1
}

Expand Down
4 changes: 2 additions & 2 deletions bin/cylc-ls-checkpoints
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if remrun():

import os

from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.option_parsers import CylcOptionParser as COP
from cylc.rundb import CylcSuiteDAO

Expand Down Expand Up @@ -97,7 +97,7 @@ def list_checkpoints(suite, callback):
def _get_dao(suite):
"""Return the DAO (public) for suite."""

suite_log_dir = GLOBAL_CFG.get_derived_host_item(
suite_log_dir = glbl_cfg().get_derived_host_item(
suite, 'suite log directory')
pub_db_path = os.path.join(os.path.dirname(suite_log_dir),
CylcSuiteDAO.DB_FILE_BASE_NAME)
Expand Down
4 changes: 2 additions & 2 deletions bin/cylc-monitor
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ from parsec.OrderedDict import OrderedDict
from cylc.option_parsers import CylcOptionParser as COP
from cylc.network.httpclient import SuiteRuntimeServiceClient, ClientError
from cylc.wallclock import get_time_string_from_unix_time
from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.task_state import (
TASK_STATUS_RUNAHEAD, TASK_STATUSES_ORDERED,
TASK_STATUSES_RESTRICTED)
Expand Down Expand Up @@ -73,7 +73,7 @@ The USER_AT_HOST argument allows suite selection by 'cylc scan' output:
"The state summary line still reflects all task proxies.",
action="store_true", default=False, dest="restricted")

def_sort_order = GLOBAL_CFG.get(["monitor", "sort order"])
def_sort_order = glbl_cfg().get(["monitor", "sort order"])

self.parser.add_option(
"-s", "--sort", metavar="ORDER",
Expand Down
6 changes: 3 additions & 3 deletions bin/cylc-ping
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import cylc.flags
from cylc.option_parsers import CylcOptionParser as COP
from cylc.task_id import TaskID
from cylc.network.httpclient import SuiteRuntimeServiceClient
from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg


def main():
Expand All @@ -48,8 +48,8 @@ def main():
(options, args) = parser.parse_args()

if options.print_ports:
base = GLOBAL_CFG.get(['comms', 'base port'])
range = GLOBAL_CFG.get(['comms', 'maximum number of ports'])
base = glbl_cfg().get(['comms', 'base port'])
range = glbl_cfg().get(['comms', 'maximum number of ports'])
print base, '<= port <=', base + range
sys.exit(0)

Expand Down
4 changes: 2 additions & 2 deletions bin/cylc-report-timings
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import contextlib
import os

import cylc.flags
from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.option_parsers import CylcOptionParser as COP
from cylc.rundb import CylcSuiteDAO

Expand Down Expand Up @@ -158,7 +158,7 @@ def format_rows(header, rows):

def _get_dao(suite):
"""Return the DAO (public) for suite."""
suite_log_dir = GLOBAL_CFG.get_derived_host_item(
suite_log_dir = glbl_cfg().get_derived_host_item(
suite, 'suite log directory'
)
pub_db_path = os.path.join(
Expand Down
12 changes: 6 additions & 6 deletions bin/cylc-scan
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ if "--use-ssh" in sys.argv[1:]:
import re
import json

from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.hostuserutil import get_user
from cylc.network.port_scan import scan_many, get_scan_items_from_fs
from cylc.option_parsers import CylcOptionParser as COP
Expand Down Expand Up @@ -160,8 +160,8 @@ def main():
options, args = parser.parse_args()

if options.print_ports:
base = GLOBAL_CFG.get(["communication", "base port"])
max_num_ports = GLOBAL_CFG.get(
base = glbl_cfg().get(["communication", "base port"])
max_num_ports = glbl_cfg().get(
["communication", "maximum number of ports"])
print base, "<= port <=", base + max_num_ports
sys.exit(0)
Expand Down Expand Up @@ -220,7 +220,7 @@ def main():
parser.error(
'--%s=%s: bad regular expression' % (key, pattern))
if options.all_ports:
args.extend(GLOBAL_CFG.get(["suite host scanning", "hosts"]))
args.extend(glbl_cfg().get(["suite host scanning", "hosts"]))
if not args:
args = get_scan_items_from_fs(cres['suite-owner'])
if not args:
Expand Down Expand Up @@ -297,8 +297,8 @@ def main():
elif not meta_items["title"]:
print indent + bold("Title:\n") + indent * 2 + "(no title)"
else:
print (indent + bold("Title:\n") + indent * 2 +
'"%s"' % meta_items["title"])
print(indent + bold("Title:\n") + indent * 2 +
'"%s"' % meta_items["title"])
for metaitem, metavalue in meta_items.items():
if metaitem != "title":
if metaitem == "description" or metaitem == "group":
Expand Down
4 changes: 2 additions & 2 deletions bin/cylc-submit
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if remrun():
import os
from time import sleep

from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.config import SuiteConfig
from cylc.cycling.loader import get_point
import cylc.flags
Expand Down Expand Up @@ -100,7 +100,7 @@ def main():
taskdef, get_point(point_str).standardise(), is_startup=True))

# Initialise job submit environment
GLOBAL_CFG.create_cylc_run_tree(suite)
glbl_cfg().create_cylc_run_tree(suite)
task_job_mgr = TaskJobManager(
suite, SuiteProcPool(), SuiteDatabaseManager(), suite_srv_mgr)
task_job_mgr.task_remote_mgr.single_task_mode = True
Expand Down
4 changes: 2 additions & 2 deletions bin/cylc-suite-state
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ if remrun():
import cylc.flags
from cylc.option_parsers import CylcOptionParser as COP
from cylc.dbstatecheck import CylcSuiteDBChecker
from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.command_polling import Poller
from cylc.task_state import TASK_STATUSES_ORDERED

Expand Down Expand Up @@ -239,7 +239,7 @@ def main():
# re-invocation).
run_dir = os.path.expandvars(
os.path.expanduser(
options.run_dir or GLOBAL_CFG.get_host_item('run directory')))
options.run_dir or glbl_cfg().get_host_item('run directory')))

pollargs = {'suite': suite,
'run_dir': run_dir,
Expand Down
6 changes: 3 additions & 3 deletions bin/cylc-trigger
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import cylc.flags
from cylc.prompt import prompt
from cylc.option_parsers import CylcOptionParser as COP
from cylc.network.httpclient import SuiteRuntimeServiceClient
from cylc.cfgspec.globalcfg import GLOBAL_CFG
from cylc.cfgspec.glbl_cfg import glbl_cfg


def main():
Expand Down Expand Up @@ -138,9 +138,9 @@ def main():

# Edit the new job file.
if options.geditor:
editor = GLOBAL_CFG.get(['editors', 'gui'])
editor = glbl_cfg().get(['editors', 'gui'])
else:
editor = GLOBAL_CFG.get(['editors', 'terminal'])
editor = glbl_cfg().get(['editors', 'terminal'])
# The editor command may have options, e.g. 'emacs -nw'.
command_list = re.split(' ', editor)
command_list.append(jobfile_path)
Expand Down
Loading

0 comments on commit 27fcc46

Please sign in to comment.