def spTypeVal(val): subType = spType(type(val)) name = "{0}, default={1}".format(subType.name, js.dumps(val)) _doc = doc.TypeDoc( name, "A value of type {0}. Default value = {1}.".format(subType.name, val)) return SpType(name, subType.handleVal, lambda: val, _doc)
def const(constVal): name = js.dumps(constVal) _doc = doc.TypeDoc(name, "The constant value {0}.".format(name)) return SpType( name, lambda val: val if val == constVal else error("Trying to assign a value to const val"), lambda: constVal, _doc)
def maybe(val): subType = spType(val) valT = subType.handleVal name = "Optional({0})".format(subType.name) _doc = doc.TypeDoc("Optional", "If present must be of type {0}".format(subType.name), deps=[subType.spDoc]) return SpType(name, valT, lambda: None, _doc)
def internal(val): subType = spType(val) valT = subType.handleVal name = "Internal({0})".format(subType.name) _doc = doc.TypeDoc( "Internal", "An internal attribute used only for debugging. We strongly recommend that you do not use this attribute in any kind of automation.", deps=[subType.spDoc]) return SpType(name, valT, lambda: None, _doc)
def spTypeFun(argName, validator, argDoc): return SpType( argName, validator, lambda: error("No default value for {argName}", argName=argName), doc.TypeDoc(argName, argDoc))
error("The value does not match any type") return SpType(name, handleVal, lambda: error("No default value for either type"), _doc) eitherOr = lambda type, default: either(const(default), type) spTypes = { list: spList, set: spSet, dict: spDict, } spDocTypes = { bool: doc.TypeDoc("bool", "true or false."), int: doc.TypeDoc("int", "An integer value."), long: doc.TypeDoc("long", "A long integer value."), str: doc.TypeDoc("string", "A string value."), } def spTypeVal(val): subType = spType(type(val)) name = "{0}, default={1}".format(subType.name, js.dumps(val)) _doc = doc.TypeDoc( name, "A value of type {0}. Default value = {1}.".format(subType.name, val)) return SpType(name, subType.handleVal, lambda: val, _doc)