opengui.Fields¶
- class opengui.Fields(values: dict = None, originals: dict = None, fields: 'list[dict]' = None, errors: 'list[str]' = None, valid: bool = None, validation: <built-in function callable> = None, ready: bool = None)¶
Class for creating and manipulating fields
- Parameters:
values (dict) – Field values to use, key by name
originals (dict) – Field orginal values to use, key by name
fields (list[dict]) – Field to use in dict form, not instances
errors (list[str]) – Overall errors
valid (bool) – Whether valid overall
validation (callable) – Function to use to validate across fields
ready (bool) – Whether ready overall
- errors: list[str]¶
Overall errors
- names: dict[str, opengui.Field]¶
Fields by name
- order: list[opengui.Field]¶
Fields in order
- originals: dict¶
Field orginal values to use, key by name
- ready: bool¶
Whether ready overall
- valid: bool¶
Whether valid overall
- validation: callable¶
Function to use to validate across fields
- values: dict¶
Field values to use, key by name
- __contains__(key: str) bool¶
Check to see if field exists
- Parameters:
key (str) – field name
- Return type:
bool
Usage
fields = Fields(fields=[{"name": "foo"}]) "foo" in fields # True "bar" in fields # False
- __getitem__(key: str) 'opengui.Field'¶
Retrieve a field by name or number
- Parameters:
key (str) – field name
- Return type:
Usage
fields = opengui.Fields(fields=[ {"name": "a"}, { "name": "b", "fields": [ { "name": "c" } ] } ]) fields[0].name # "a" fields[1][0].name # "c" fields["a"].name # "a" fields["b"]["c"].name # "c"
- __iter__()¶
Allows iteration over fields
Usage
fields = Fields(fields=[{"name": "foo"},{"name": "bar"}]) for field in fields: field.name # "foo" # "bar"
- __len__() int¶
Returns the number of fields
- Return type:
int
Usage
fields = Fields(fields=[{"name": "foo"},{"name": "bar"}]) len(fields) # 2
- append(*args, **kwargs)¶
Adds a field (as dict) to these Fields
- Parameters:
args – single arg dict to use as kwargs
kwargs – kwargs to use in
Fieldcreation
- Raises:
DuplicateName – if name is already used
MissingName – if no name is sent
Usage
fields = opengui.Fields(values={"a": 1}, originals={"a": 2}) fields.append({"name": "a", "label": "A"}) fields.order[0].name # a fields.order[0].content["label"] # "A" fields.order[0].value # 1 fields.order[0].original # 2
- cli() dict¶
Returns dict of values from getting input from the cli
- Return type:
dict
Usage
Taken from its unittest:
@unittest.mock.patch("builtins.print") @unittest.mock.patch("builtins.input") def test_cli(self, mock_input, mock_print): fields = opengui.Fields( fields=[ { "name": "basic", "description": "be basic", "default": "badass", "validation": "^bitch$" }, { "name": "single", "options": ["yin", "yang"], "labels": { "yin": "Yin", "yang": "Yang" }, "default": "yon" }, { "name": "multiple", "multi": True, "options": ["fee", "fie", "foe", "fum"], "default": ["fun"] }, { "name": "yah", "bool": True, "default": True }, { "name": "sure", "bool": True }, { "name": "nah", "bool": True } ] ) mock_input.side_effect = [ "", "bitch", "fish", "0", "3", "", "1", "fish 0 6", "", "1 3", "", "y", "n" ] self.assertEqual(fields.cli(), { "basic": "bitch", "single": "yin", "multiple": ["fee", "foe"], "yah": True, "sure": True, "nah": False }) mock_print.assert_has_calls([ unittest.mock.call("basic:"), unittest.mock.call(" be basic"), unittest.mock.call(" default: badass"), unittest.mock.call("enter value: "), unittest.mock.call("must match '^bitch$'"), unittest.mock.call("enter value: "), unittest.mock.call("single:"), unittest.mock.call(" default: yon"), unittest.mock.call("[1] Yin"), unittest.mock.call("[2] Yang"), unittest.mock.call("enter value: "), unittest.mock.call("invalid choice: fish"), unittest.mock.call("[1] Yin"), unittest.mock.call("[2] Yang"), unittest.mock.call("enter value: "), unittest.mock.call("invalid choice: 0"), unittest.mock.call("[1] Yin"), unittest.mock.call("[2] Yang"), unittest.mock.call("enter value: "), unittest.mock.call("invalid choice: 3"), unittest.mock.call("[1] Yin"), unittest.mock.call("[2] Yang"), unittest.mock.call("enter value: "), unittest.mock.call("invalid value 'yon'"), unittest.mock.call("[1] Yin"), unittest.mock.call("[2] Yang"), unittest.mock.call("enter value: "), unittest.mock.call("multiple:"), unittest.mock.call(" default: ['fun']"), unittest.mock.call("[1] fee"), unittest.mock.call("[2] fie"), unittest.mock.call("[3] foe"), unittest.mock.call("[4] fum"), unittest.mock.call("enter multiple values, separated by spaces: "), unittest.mock.call("invalid choices: ['fish', '0', '6']"), unittest.mock.call("[1] fee"), unittest.mock.call("[2] fie"), unittest.mock.call("[3] foe"), unittest.mock.call("[4] fum"), unittest.mock.call("enter multiple values, separated by spaces: "), unittest.mock.call("invalid values ['fun']"), unittest.mock.call("[1] fee"), unittest.mock.call("[2] fie"), unittest.mock.call("[3] foe"), unittest.mock.call("[4] fum"), unittest.mock.call("enter multiple values, separated by spaces: "), ])
- extend(fields: 'list[dict]')¶
Adds a list of fields
- Parameters:
fields (list[dict]) – List of field dicts (not instances)
Usage
fields = opengui.Fields() fields.extend([ {"name": "a"}, {"name": "b"} ]) len(fields.order) # 2 fields.order[0].name # "a" fields.order[1].name # "b"
- to_dict() dict¶
Returns dict representation of fields
- Return type:
dict
Usage
fields = opengui.Fields( fields=[ {"name": "a", "label": "A"}, {"name": "b"} ], errors=['boo'], valid=True, ready=False )) fields.to_dict() # { # "fields": [ # {"name": "a", "label": "A"}, # {"name": "b"} # ], # "errors": [ # "boo" # ], # "valid": True, # "ready": False # }
- to_list() 'list[dict]'¶
Returns list of field dicts
- Return type:
list[dict]
Usage
fields = opengui.Fields(fields=[ {"name": "a", "label": "A"}, {"name": "b"} ]) fields.to_list() # [ # {"name": "a", "label": "A"}, # {"name": "b"} # ]
- update(*args, **kwargs)¶
If exists, updates field, adds field if not
- Parameters:
args – single arg dict to use as kwargs
kwargs – kwargs to use in
Fieldupdate
- Raises:
MissingName – if no name is sent
Usage
fields = opengui.Fields(values={"a": 1}, originals={"a": 2}) fields.update({"name": "a", "label": "A"}) fields.order[0].name # a fields.order[0].content["label"] # "A" fields.order[0].value # 1 fields.order[0].original # 2 fields.update({"name": "a", "more": "B"}) fields.order[0].content["more"] # "B"
- validate(store=True) bool¶
Validates the data in all fields, even if validation isn’t set. Returns errors.
If if a key is values doesn’t matcha a field, adds ‘unknown field’ to errors
Calls validate on all fields
If validation is set, calls the function with this and errors
- Parameters:
store – whether to store the errors (if any) and valid
- Returns:
Whether everything is valid
- Return type:
bool