• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

AlexandreDecan / sismic / 6689847414

30 Oct 2023 07:57AM UTC coverage: 91.427% (+0.05%) from 91.376%
6689847414

push

github

AlexandreDecan
Prepare 1.6.6

1781 of 1948 relevant lines covered (91.43%)

4.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/sismic/io/yaml.py
1
import ruamel.yaml as yaml
5✔
2
import schema
5✔
3

4
from io import StringIO
5✔
5

6
from ..exceptions import StatechartError
5✔
7
from ..model import Statechart
5✔
8

9
from .datadict import export_to_dict, import_from_dict
5✔
10

11
__all__ = ['import_from_yaml', 'export_to_yaml']
5✔
12

13

14
class SCHEMA:
5✔
15
    contract = {schema.Or('before', 'after', 'always'): schema.Use(str)}
5✔
16

17
    transition = {
5✔
18
        schema.Optional('target'): schema.Use(str),
19
        schema.Optional('event'): schema.Use(str),
20
        schema.Optional('guard'): schema.Use(str),
21
        schema.Optional('action'): schema.Use(str),
22
        schema.Optional('contract'): [contract],
23
        schema.Optional('priority'): schema.Or(schema.Use(int), 'high', 'low'),
24
    }
25

26
    state = dict()  # type: ignore
5✔
27
    state.update({
5✔
28
        'name': schema.Use(str),
29
        schema.Optional('type'): schema.Or('final', 'shallow history', 'deep history'),
30
        schema.Optional('on entry'): schema.Use(str),
31
        schema.Optional('on exit'): schema.Use(str),
32
        schema.Optional('transitions'): [transition],
33
        schema.Optional('contract'): [contract],
34
        schema.Optional('initial'): schema.Use(str),
35
        schema.Optional('parallel states'): [state],
36
        schema.Optional('states'): [state],
37
        schema.Optional('memory'): schema.Use(str),
38
    })
39

40
    statechart = {
5✔
41
        'statechart': {
42
            'name': schema.Use(str),
43
            schema.Optional('description'): schema.Use(str),
44
            schema.Optional('preamble'): schema.Use(str),
45
            'root state': state,
46
        }
47
    }
48

49

50
def import_from_yaml(
5✔
51
        text: str = None, filepath: str = None, *, ignore_schema: bool = False,
52
        ignore_validation: bool = False) -> Statechart:
53
    """
54
    Import a statechart from a YAML representation (first argument) or a YAML file (filepath
55
    argument).
56

57
    Unless specified, the structure contained in the YAML is validated against a predefined
58
    schema (see *sismic.io.SCHEMA*), and the resulting statechart is validated using its
59
    *validate()* method.
60

61
    :param text: A YAML text. If not provided, filepath argument has to be provided.
62
    :param filepath: A path to a YAML file.
63
    :param ignore_schema: set to *True* to disable yaml validation.
64
    :param ignore_validation: set to *True* to disable statechart validation.
65
    :return: a *Statechart* instance
66
    """
67
    if not text and not filepath:
5✔
68
        raise TypeError(
5✔
69
            'A YAML must be provided, either using first argument or filepath argument.')
70
    elif text and filepath:
5✔
71
        raise TypeError('Either provide first argument or filepath argument, not both.')
5✔
72
    elif filepath:
5✔
73
        with open(filepath, 'r') as f:
5✔
74
            text = f.read()
5✔
75

76
    yml = yaml.YAML(typ='safe', pure=True)
5✔
77
    data = yml.load(text)
5✔
78

79
    if not ignore_schema:
5✔
80
        try:
5✔
81
            data = schema.Schema(SCHEMA.statechart).validate(data)
5✔
82
        except schema.SchemaError as e:
×
83
            raise StatechartError('YAML validation failed') from e
×
84

85
    sc = import_from_dict(data)
5✔
86

87
    if not ignore_validation:
5✔
88
        sc.validate()
5✔
89
    return sc
5✔
90

91

92
def export_to_yaml(statechart: Statechart, filepath: str = None) -> str:
5✔
93
    """
94
    Export given *Statechart* instance to YAML. Its YAML representation is returned by
95
    this function. Automatically save the output to filepath, if provided.
96

97
    :param statechart: statechart to export
98
    :param filepath: save output to given filepath, if provided
99
    :return: A textual YAML representation
100
    """
101
    output = StringIO()
5✔
102

103
    yml = yaml.YAML(typ='safe', pure=True)
5✔
104
    yml.dump(export_to_dict(statechart), output)
5✔
105

106
    if filepath:
5✔
107
        with open(filepath, 'w') as f:
×
108
            f.write(output.getvalue())
×
109

110
    return output.getvalue()
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc