1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
# stateMachine.py
#
# module to define .pystate import handler
#
# import imputil
import keyword
import sys
import os
import types
import importlib
import importlib.machinery
from urllib.parse import urlparse
DEBUG = False
import pyparsing as pp
# define basic exception for invalid state transitions - state machine classes will subclass to
# define their own specific exception type
class InvalidTransitionException(Exception):
pass
ident = pp.Word(pp.alphas + "_", pp.alphanums + "_$")
# add parse-time condition to make sure we do not allow any Python keywords to be used as
# statemachine identifiers
def no_keywords_allowed(s, l, t):
wd = t[0]
return not keyword.iskeyword(wd)
ident.addCondition(
no_keywords_allowed,
message="cannot use a Python keyword for state or transition identifier",
)
stateTransition = ident("from_state") + "->" + ident("to_state")
stateMachine = (
pp.Keyword("statemachine")
+ ident("name")
+ ":"
+ pp.OneOrMore(pp.Group(stateTransition))("transitions")
)
namedStateTransition = (
ident("from_state") + "-(" + ident("transition") + ")->" + ident("to_state")
)
namedStateMachine = (
pp.Keyword("statemachine")
+ ident("name")
+ ":"
+ pp.OneOrMore(pp.Group(namedStateTransition))("transitions")
)
def expand_state_definition(source, loc, tokens):
"""
Parse action to convert statemachine to corresponding Python classes and methods
"""
indent = " " * (pp.col(loc, source) - 1)
statedef = []
# build list of states
states = set()
fromTo = {}
for tn in tokens.transitions:
states.add(tn.from_state)
states.add(tn.to_state)
fromTo[tn.from_state] = tn.to_state
# define base class for state classes
baseStateClass = tokens.name
statedef.extend(
[
f"class {baseStateClass}(object):",
" def __str__(self):",
" return self.__class__.__name__",
" @classmethod",
" def states(cls):",
" return list(cls.__subclasses__())",
" def next_state(self):",
" return self._next_state_class()",
]
)
# define all state classes
statedef.extend("class {}({}): pass".format(s, baseStateClass) for s in states)
# define state->state transitions
statedef.extend(
"{}._next_state_class = {}".format(s, fromTo[s]) for s in states if s in fromTo
)
statedef.extend(
[
"class {baseStateClass}Mixin:".format(baseStateClass=baseStateClass),
" def __init__(self):",
" self._state = None",
" def initialize_state(self, init_state):",
" if issubclass(init_state, {baseStateClass}):".format(
baseStateClass=baseStateClass
),
" init_state = init_state()",
" self._state = init_state",
" @property",
" def state(self):",
" return self._state",
" # get behavior/properties from current state",
" def __getattr__(self, attrname):",
" attr = getattr(self._state, attrname)",
" return attr",
" def __str__(self):",
" return '{0}: {1}'.format(self.__class__.__name__, self._state)",
]
)
return ("\n" + indent).join(statedef) + "\n"
stateMachine.setParseAction(expand_state_definition)
def expand_named_state_definition(source, loc, tokens):
"""
Parse action to convert statemachine with named transitions to corresponding Python
classes and methods
"""
indent = " " * (pp.col(loc, source) - 1)
statedef = []
# build list of states and transitions
states = set()
transitions = set()
baseStateClass = tokens.name
fromTo = {}
for tn in tokens.transitions:
states.add(tn.from_state)
states.add(tn.to_state)
transitions.add(tn.transition)
if tn.from_state in fromTo:
fromTo[tn.from_state][tn.transition] = tn.to_state
else:
fromTo[tn.from_state] = {tn.transition: tn.to_state}
# add entries for terminal states
for s in states:
if s not in fromTo:
fromTo[s] = {}
# define state transition class
statedef.extend(
[
"class {baseStateClass}Transition:".format(baseStateClass=baseStateClass),
" def __str__(self):",
" return self.transitionName",
]
)
statedef.extend(
"{tn_name} = {baseStateClass}Transition()".format(
tn_name=tn, baseStateClass=baseStateClass
)
for tn in transitions
)
statedef.extend(
"{tn_name}.transitionName = '{tn_name}'".format(tn_name=tn)
for tn in transitions
)
# define base class for state classes
statedef.extend(
[
f"class {baseStateClass}(object):",
" from statemachine import InvalidTransitionException as BaseTransitionException",
" class InvalidTransitionException(BaseTransitionException): pass",
" def __str__(self):",
" return self.__class__.__name__",
" @classmethod",
" def states(cls):",
" return list(cls.__subclasses__())",
" @classmethod",
" def next_state(cls, name):",
" try:",
" return cls.tnmap[name]()",
" except KeyError:",
" raise cls.InvalidTransitionException(f'{cls.__name__} does not support transition {name!r}'",
" def __bad_tn(name):",
" def _fn(cls):",
" raise cls.InvalidTransitionException(f'{cls.__name__} does not support transition {name!r}'",
" _fn.__name__ = name",
" return _fn",
]
)
# define default 'invalid transition' methods in base class, valid transitions will be implemented in subclasses
statedef.extend(
" {tn_name} = classmethod(__bad_tn({tn_name!r}))".format(tn_name=tn)
for tn in transitions
)
# define all state classes
statedef.extend("class {}({}): pass".format(s, baseStateClass) for s in states)
# define state transition methods for valid transitions from each state
for s in states:
trns = list(fromTo[s].items())
# statedef.append(f"{s}.tnmap = {{{', '.join('%s:%s' % tn for tn in trns)}}}")
statedef.extend(
f"{s}.{tn_} = classmethod(lambda cls: {to_}())"
for tn_, to_ in trns
)
statedef.extend(
[
"{baseStateClass}.transitions = classmethod(lambda cls: [{transition_class_list}])".format(
baseStateClass=baseStateClass,
transition_class_list=", ".join(
"cls.{}".format(tn) for tn in transitions
),
),
"{baseStateClass}.transition_names = [tn.__name__ for tn in {baseStateClass}.transitions()]".format(
baseStateClass=baseStateClass
),
]
)
# define <state>Mixin class for application classes that delegate to the state
statedef.extend(
[
"class {baseStateClass}Mixin:".format(baseStateClass=baseStateClass),
" def __init__(self):",
" self._state = None",
" def initialize_state(self, init_state):",
" if issubclass(init_state, {baseStateClass}):".format(
baseStateClass=baseStateClass
),
" init_state = init_state()",
" self._state = init_state",
" @property",
" def state(self):",
" return self._state",
" # get behavior/properties from current state",
" def __getattr__(self, attrname):",
" attr = getattr(self._state, attrname)",
" return attr",
" def __str__(self):",
" return '{0}: {1}'.format(self.__class__.__name__, self._state)",
]
)
# define transition methods to be delegated to the _state instance variable
statedef.extend(
" def {tn_name}(self): self._state = self._state.{tn_name}()".format(
tn_name=tn
)
for tn in transitions
)
return ("\n" + indent).join(statedef) + "\n"
namedStateMachine.setParseAction(expand_named_state_definition)
# ======================================================================
# NEW STUFF - Matt Anderson, 2009-11-26
# ======================================================================
class SuffixImporter:
"""An importer designed using the mechanism defined in :pep:`302`. I read
the PEP, and also used Doug Hellmann's PyMOTW article `Modules and
Imports`_, as a pattern.
.. _`Modules and Imports`: http://www.doughellmann.com/PyMOTW/sys/imports.html
Define a subclass that specifies a :attr:`suffix` attribute, and
implements a :meth:`process_filedata` method. Then call the classmethod
:meth:`register` on your class to actually install it in the appropriate
places in :mod:`sys`."""
scheme = "suffix"
suffix = None
path_entry = None
@classmethod
def trigger_url(cls):
if cls.suffix is None:
raise ValueError(f"{cls.__name__}.suffix is not set")
return f"suffix:{cls.suffix}"
@classmethod
def register(cls):
sys.path_hooks.append(cls)
sys.path.append(cls.trigger_url())
def __init__(self, path_entry):
pr = urlparse(str(path_entry))
if pr.scheme != self.scheme or pr.path != self.suffix:
raise ImportError()
self.path_entry = path_entry
self._found = {}
def checkpath_iter(self, fullname):
for dirpath in sys.path:
# if the value in sys.path_importer_cache is None, then this
# path *should* be imported by the builtin mechanism, and the
# entry is thus a path to a directory on the filesystem;
# if it's not None, then some other importer is in charge, and
# it probably isn't even a filesystem path
finder = sys.path_importer_cache.get(dirpath)
if isinstance(finder, (type(None), importlib.machinery.FileFinder)):
checkpath = os.path.join(dirpath, "{}.{}".format(fullname, self.suffix))
yield checkpath
def find_module(self, fullname, path=None):
for checkpath in self.checkpath_iter(fullname):
if os.path.isfile(checkpath):
self._found[fullname] = checkpath
return self
return None
def load_module(self, fullname):
assert fullname in self._found
if fullname in sys.modules:
module = sys.modules[fullname]
else:
sys.modules[fullname] = module = types.ModuleType(fullname)
data = None
with open(self._found[fullname]) as f:
data = f.read()
module.__dict__.clear()
module.__file__ = self._found[fullname]
module.__name__ = fullname
module.__loader__ = self
self.process_filedata(module, data)
return module
def process_filedata(self, module, data):
pass
class PystateImporter(SuffixImporter):
suffix = "pystate"
def process_filedata(self, module, data):
# MATT-NOTE: re-worked :func:`get_state_machine`
# convert any statemachine expressions
stateMachineExpr = (stateMachine | namedStateMachine).ignore(
pp.pythonStyleComment
)
generated_code = stateMachineExpr.transformString(data)
if DEBUG:
print(generated_code)
# compile code object from generated code
# (strip trailing spaces and tabs, compile doesn't like
# dangling whitespace)
COMPILE_MODE = "exec"
codeobj = compile(generated_code.rstrip(" \t"), module.__file__, COMPILE_MODE)
exec(codeobj, module.__dict__)
PystateImporter.register()
if DEBUG:
print("registered {!r} importer".format(PystateImporter.suffix))
|