Skip to content

Commit

Permalink
Improve parsec validate
Browse files Browse the repository at this point in the history
Single validator with simple dispatch table for coercers.
Cleaner specs that reflect actual usages for type, default, options.
  • Loading branch information
matthewrmshin committed Apr 27, 2018
1 parent d6a3535 commit 4fdb388
Show file tree
Hide file tree
Showing 26 changed files with 1,014 additions and 1,111 deletions.
8 changes: 4 additions & 4 deletions bin/cylc-broadcast
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ from cylc.network.httpclient import SuiteRuntimeServiceClient
from cylc.print_tree import print_tree
from cylc.task_id import TaskID
from cylc.cfgspec.suite import SPEC, upg
from parsec.config import config
from parsec.validate import validate
from parsec.config import ParsecConfig
from parsec.validate import ParsecValidator


REC_ITEM = re.compile(r'^\[([^\]]*)\](.*)$')
Expand Down Expand Up @@ -135,13 +135,13 @@ def get_rdict(left, right=None):
cur_dict[tail.strip()] = right
tail = None
upg({'runtime': {'__MANY__': rdict}}, 'test')
validate(rdict, SPEC['runtime']['__MANY__'])
ParsecValidator().validate(rdict, SPEC['runtime']['__MANY__'])
return rdict


def files_to_settings(settings, setting_files, cancel_mode=False):
"""Parse setting files, and append to settings."""
cfg = config(SPEC['runtime']['__MANY__'])
cfg = ParsecConfig(SPEC['runtime']['__MANY__'])
for setting_file in setting_files:
if setting_file == '-':
with NamedTemporaryFile() as handle:
Expand Down
6 changes: 4 additions & 2 deletions bin/cylc-get-gui-config
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Multiple items can be specified at once."""

import sys
from optparse import OptionParser

from cylc.cfgspec.gcylc import GcylcConfig
import cylc.flags


Expand Down Expand Up @@ -63,8 +65,8 @@ def main():
parser.error("ERROR: wrong number of arguments")

# Import gcfg here to avoid aborting before command help is printed.
from cylc.cfgspec.gcylc import gcfg
gcfg.idump(options.item, sparse=options.sparse, pnative=options.pnative)
GcylcConfig.get_inst().idump(
options.item, sparse=options.sparse, pnative=options.pnative)


if __name__ == "__main__":
Expand Down
96 changes: 45 additions & 51 deletions lib/cylc/cfgspec/gcylc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from copy import deepcopy, copy

from parsec import ParsecError
from parsec.config import config, ItemNotFoundError, itemstr
from parsec.validate import coercers, validator as vdr
from parsec.config import ParsecConfig, ItemNotFoundError, itemstr
from parsec.validate import ParsecValidator as VDR, DurationFloat
from parsec.upgrade import upgrader
from parsec.util import printcfg
from cylc.gui.view_tree import ControlTree
Expand All @@ -34,57 +34,55 @@
TASK_STATUS_READY, TASK_STATUS_SUBMITTED, TASK_STATUS_SUBMIT_FAILED,
TASK_STATUS_SUBMIT_RETRYING, TASK_STATUS_RUNNING, TASK_STATUS_SUCCEEDED,
TASK_STATUS_FAILED, TASK_STATUS_RETRYING)
from cylc.cfgspec.utils import (coerce_interval, DurationFloat)


coercers['interval'] = coerce_interval
SITE_FILE = os.path.join(
os.environ['CYLC_DIR'], 'conf', 'gcylcrc', 'themes.rc')
USER_FILE = os.path.join(os.environ['HOME'], '.cylc', 'gcylc.rc')
_COLS = [heading for heading in ControlTree.headings if heading] + ['none']

# Nested dict of spec items.
# Spec value is [value_type, default, allowed_2, allowed_3, ...]
# where:
# - value_type: value type (compulsory).
# - default: the default value (optional).
# - allowed_2, ...: the only other allowed values of this setting (optional).
SPEC = {
'dot icon size': vdr(
vtype='string',
default="medium",
options=["small", "medium", "large", "extra large"]),
'initial side-by-side views': vdr(vtype='boolean', default=False),
'initial views': vdr(vtype='string_list', default=["text"]),
'maximum update interval': vdr(
vtype='interval', default=DurationFloat(15)),
'sort by definition order': vdr(vtype='boolean', default=True),
'sort column': vdr(
vtype='string',
default='none',
options=[heading for heading in ControlTree.headings if heading is not
None] + ['none']),
'sort column ascending': vdr(vtype='boolean', default=True),
'task filter highlight color': vdr(vtype='string', default='PowderBlue'),
'task states to filter out': vdr(
vtype='string_list',
default=[TASK_STATUS_RUNAHEAD]),
'dot icon size': [
VDR.V_STRING, "medium", "small", "medium", "large", "extra large"],
'initial side-by-side views': [VDR.V_BOOLEAN],
'initial views': [VDR.V_STRING_LIST, ["text"]],
'maximum update interval': [VDR.V_INTERVAL, DurationFloat(15)],
'sort by definition order': [VDR.V_BOOLEAN, True],
'sort column': [VDR.V_STRING, 'none'] + _COLS,
'sort column ascending': [VDR.V_BOOLEAN, True],
'task filter highlight color': [VDR.V_STRING, 'PowderBlue'],
'task states to filter out': [
VDR.V_STRING_LIST, [TASK_STATUS_RUNAHEAD]],
'themes': {
'__MANY__': {
'inherit': vdr(vtype='string', default="default"),
'defaults': vdr(vtype='string_list'),
TASK_STATUS_WAITING: vdr(vtype='string_list'),
TASK_STATUS_HELD: vdr(vtype='string_list'),
TASK_STATUS_QUEUED: vdr(vtype='string_list'),
TASK_STATUS_READY: vdr(vtype='string_list'),
TASK_STATUS_EXPIRED: vdr(vtype='string_list'),
TASK_STATUS_SUBMITTED: vdr(vtype='string_list'),
TASK_STATUS_SUBMIT_FAILED: vdr(vtype='string_list'),
TASK_STATUS_RUNNING: vdr(vtype='string_list'),
TASK_STATUS_SUCCEEDED: vdr(vtype='string_list'),
TASK_STATUS_FAILED: vdr(vtype='string_list'),
TASK_STATUS_RETRYING: vdr(vtype='string_list'),
TASK_STATUS_SUBMIT_RETRYING: vdr(vtype='string_list'),
TASK_STATUS_RUNAHEAD: vdr(vtype='string_list'),
'inherit': [VDR.V_STRING, "default"],
'defaults': [VDR.V_STRING_LIST],
TASK_STATUS_WAITING: [VDR.V_STRING_LIST],
TASK_STATUS_HELD: [VDR.V_STRING_LIST],
TASK_STATUS_QUEUED: [VDR.V_STRING_LIST],
TASK_STATUS_READY: [VDR.V_STRING_LIST],
TASK_STATUS_EXPIRED: [VDR.V_STRING_LIST],
TASK_STATUS_SUBMITTED: [VDR.V_STRING_LIST],
TASK_STATUS_SUBMIT_FAILED: [VDR.V_STRING_LIST],
TASK_STATUS_RUNNING: [VDR.V_STRING_LIST],
TASK_STATUS_SUCCEEDED: [VDR.V_STRING_LIST],
TASK_STATUS_FAILED: [VDR.V_STRING_LIST],
TASK_STATUS_RETRYING: [VDR.V_STRING_LIST],
TASK_STATUS_SUBMIT_RETRYING: [VDR.V_STRING_LIST],
TASK_STATUS_RUNAHEAD: [VDR.V_STRING_LIST],
},
},
'transpose dot': vdr(vtype='boolean', default=False),
'transpose graph': vdr(vtype='boolean', default=False),
'ungrouped views': vdr(vtype='string_list', default=[]),
'use theme': vdr(vtype='string', default="default"),
'window size': vdr(vtype='integer_list', default=[800, 500]),
'transpose dot': [VDR.V_BOOLEAN],
'transpose graph': [VDR.V_BOOLEAN],
'ungrouped views': [VDR.V_STRING_LIST],
'use theme': [VDR.V_STRING, "default"],
'window size': [VDR.V_INTEGER_LIST, [800, 500]],
}


Expand All @@ -97,7 +95,7 @@ def upg(cfg, descr):
u.upgrade()


class gconfig(config):
class GcylcConfig(ParsecConfig):
"""gcylc user configuration - default view panels, task themes etc."""

_INST = None
Expand Down Expand Up @@ -128,8 +126,8 @@ def get_inst(cls):
cls._INST.transform()
return cls._INST

def __init__(self, *args):
config.__init__(self, *args)
def __init__(self, spec, upg):
ParsecConfig.__init__(self, spec, upg)
self.default_theme = None
self.use_theme = None

Expand Down Expand Up @@ -279,7 +277,7 @@ def inherit(self, target, source):
else:
target[item] = source[item]

def dump(self, keys, sparse=False, pnative=False, prefix='',
def dump(self, keys=None, sparse=False, pnative=False, prefix='',
none_str=''):
"""Override parse.config.dump().
Expand Down Expand Up @@ -309,7 +307,3 @@ def dump(self, keys, sparse=False, pnative=False, prefix='',
print cfg
else:
printcfg(cfg, prefix=prefix, level=len(keys))


# load on import if not already loaded
gcfg = gconfig.get_inst()
Loading

0 comments on commit 4fdb388

Please sign in to comment.