_methodical.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. # -*- test-case-name: automat._test.test_methodical -*-
  2. import collections
  3. from functools import wraps
  4. from itertools import count
  5. try:
  6. # Python 3
  7. from inspect import getfullargspec as getArgsSpec
  8. except ImportError:
  9. # Python 2
  10. from inspect import getargspec as getArgsSpec
  11. import attr
  12. import six
  13. from ._core import Transitioner, Automaton
  14. from ._introspection import preserveName
  15. ArgSpec = collections.namedtuple('ArgSpec', ['args', 'varargs', 'varkw',
  16. 'defaults', 'kwonlyargs',
  17. 'kwonlydefaults', 'annotations'])
  18. def _getArgSpec(func):
  19. """
  20. Normalize inspect.ArgSpec across python versions
  21. and convert mutable attributes to immutable types.
  22. :param Callable func: A function.
  23. :return: The function's ArgSpec.
  24. :rtype: ArgSpec
  25. """
  26. spec = getArgsSpec(func)
  27. return ArgSpec(
  28. args=tuple(spec.args),
  29. varargs=spec.varargs,
  30. varkw=spec.varkw if six.PY3 else spec.keywords,
  31. defaults=spec.defaults if spec.defaults else (),
  32. kwonlyargs=tuple(spec.kwonlyargs) if six.PY3 else (),
  33. kwonlydefaults=(
  34. tuple(spec.kwonlydefaults.items())
  35. if spec.kwonlydefaults else ()
  36. ) if six.PY3 else (),
  37. annotations=tuple(spec.annotations.items()) if six.PY3 else (),
  38. )
  39. def _getArgNames(spec):
  40. """
  41. Get the name of all arguments defined in a function signature.
  42. The name of * and ** arguments is normalized to "*args" and "**kwargs".
  43. :param ArgSpec spec: A function to interrogate for a signature.
  44. :return: The set of all argument names in `func`s signature.
  45. :rtype: Set[str]
  46. """
  47. return set(
  48. spec.args
  49. + spec.kwonlyargs
  50. + (('*args',) if spec.varargs else ())
  51. + (('**kwargs',) if spec.varkw else ())
  52. + spec.annotations
  53. )
  54. def _keywords_only(f):
  55. """
  56. Decorate a function so all its arguments must be passed by keyword.
  57. A useful utility for decorators that take arguments so that they don't
  58. accidentally get passed the thing they're decorating as their first
  59. argument.
  60. Only works for methods right now.
  61. """
  62. @wraps(f)
  63. def g(self, **kw):
  64. return f(self, **kw)
  65. return g
  66. @attr.s(frozen=True)
  67. class MethodicalState(object):
  68. """
  69. A state for a L{MethodicalMachine}.
  70. """
  71. machine = attr.ib(repr=False)
  72. method = attr.ib()
  73. serialized = attr.ib(repr=False)
  74. def upon(self, input, enter, outputs, collector=list):
  75. """
  76. Declare a state transition within the :class:`automat.MethodicalMachine`
  77. associated with this :class:`automat.MethodicalState`:
  78. upon the receipt of the `input`, enter the `state`,
  79. emitting each output in `outputs`.
  80. :param MethodicalInput input: The input triggering a state transition.
  81. :param MethodicalState enter: The resulting state.
  82. :param Iterable[MethodicalOutput] outputs: The outputs to be triggered
  83. as a result of the declared state transition.
  84. :param Callable collector: The function to be used when collecting
  85. output return values.
  86. :raises TypeError: if any of the `outputs` signatures do not match
  87. the `inputs` signature.
  88. :raises ValueError: if the state transition from `self` via `input`
  89. has already been defined.
  90. """
  91. inputArgs = _getArgNames(input.argSpec)
  92. for output in outputs:
  93. outputArgs = _getArgNames(output.argSpec)
  94. if not outputArgs.issubset(inputArgs):
  95. raise TypeError(
  96. "method {input} signature {inputSignature} "
  97. "does not match output {output} "
  98. "signature {outputSignature}".format(
  99. input=input.method.__name__,
  100. output=output.method.__name__,
  101. inputSignature=getArgsSpec(input.method),
  102. outputSignature=getArgsSpec(output.method),
  103. ))
  104. self.machine._oneTransition(self, input, enter, outputs, collector)
  105. def _name(self):
  106. return self.method.__name__
  107. def _transitionerFromInstance(oself, symbol, automaton):
  108. """
  109. Get a L{Transitioner}
  110. """
  111. transitioner = getattr(oself, symbol, None)
  112. if transitioner is None:
  113. transitioner = Transitioner(
  114. automaton,
  115. automaton.initialState,
  116. )
  117. setattr(oself, symbol, transitioner)
  118. return transitioner
  119. def _empty():
  120. pass
  121. def _docstring():
  122. """docstring"""
  123. def assertNoCode(inst, attribute, f):
  124. # The function body must be empty, i.e. "pass" or "return None", which
  125. # both yield the same bytecode: LOAD_CONST (None), RETURN_VALUE. We also
  126. # accept functions with only a docstring, which yields slightly different
  127. # bytecode, because the "None" is put in a different constant slot.
  128. # Unfortunately, this does not catch function bodies that return a
  129. # constant value, e.g. "return 1", because their code is identical to a
  130. # "return None". They differ in the contents of their constant table, but
  131. # checking that would require us to parse the bytecode, find the index
  132. # being returned, then making sure the table has a None at that index.
  133. if f.__code__.co_code not in (_empty.__code__.co_code,
  134. _docstring.__code__.co_code):
  135. raise ValueError("function body must be empty")
  136. def _filterArgs(args, kwargs, inputSpec, outputSpec):
  137. """
  138. Filter out arguments that were passed to input that output won't accept.
  139. :param tuple args: The *args that input received.
  140. :param dict kwargs: The **kwargs that input received.
  141. :param ArgSpec inputSpec: The input's arg spec.
  142. :param ArgSpec outputSpec: The output's arg spec.
  143. :return: The args and kwargs that output will accept.
  144. :rtype: Tuple[tuple, dict]
  145. """
  146. named_args = tuple(zip(inputSpec.args[1:], args))
  147. if outputSpec.varargs:
  148. # Only return all args if the output accepts *args.
  149. return_args = args
  150. else:
  151. # Filter out arguments that don't appear
  152. # in the output's method signature.
  153. return_args = [v for n, v in named_args if n in outputSpec.args]
  154. # Get any of input's default arguments that were not passed.
  155. passed_arg_names = tuple(kwargs)
  156. for name, value in named_args:
  157. passed_arg_names += (name, value)
  158. defaults = zip(inputSpec.args[::-1], inputSpec.defaults[::-1])
  159. full_kwargs = {n: v for n, v in defaults if n not in passed_arg_names}
  160. full_kwargs.update(kwargs)
  161. if outputSpec.varkw:
  162. # Only pass all kwargs if the output method accepts **kwargs.
  163. return_kwargs = full_kwargs
  164. else:
  165. # Filter out names that the output method does not accept.
  166. all_accepted_names = outputSpec.args[1:] + outputSpec.kwonlyargs
  167. return_kwargs = {n: v for n, v in full_kwargs.items()
  168. if n in all_accepted_names}
  169. return return_args, return_kwargs
  170. @attr.s(eq=False, hash=False)
  171. class MethodicalInput(object):
  172. """
  173. An input for a L{MethodicalMachine}.
  174. """
  175. automaton = attr.ib(repr=False)
  176. method = attr.ib(validator=assertNoCode)
  177. symbol = attr.ib(repr=False)
  178. collectors = attr.ib(default=attr.Factory(dict), repr=False)
  179. argSpec = attr.ib(init=False, repr=False)
  180. @argSpec.default
  181. def _buildArgSpec(self):
  182. return _getArgSpec(self.method)
  183. def __get__(self, oself, type=None):
  184. """
  185. Return a function that takes no arguments and returns values returned
  186. by output functions produced by the given L{MethodicalInput} in
  187. C{oself}'s current state.
  188. """
  189. transitioner = _transitionerFromInstance(oself, self.symbol,
  190. self.automaton)
  191. @preserveName(self.method)
  192. @wraps(self.method)
  193. def doInput(*args, **kwargs):
  194. self.method(oself, *args, **kwargs)
  195. previousState = transitioner._state
  196. (outputs, outTracer) = transitioner.transition(self)
  197. collector = self.collectors[previousState]
  198. values = []
  199. for output in outputs:
  200. if outTracer:
  201. outTracer(output._name())
  202. a, k = _filterArgs(args, kwargs, self.argSpec, output.argSpec)
  203. value = output(oself, *a, **k)
  204. values.append(value)
  205. return collector(values)
  206. return doInput
  207. def _name(self):
  208. return self.method.__name__
  209. @attr.s(frozen=True)
  210. class MethodicalOutput(object):
  211. """
  212. An output for a L{MethodicalMachine}.
  213. """
  214. machine = attr.ib(repr=False)
  215. method = attr.ib()
  216. argSpec = attr.ib(init=False, repr=False)
  217. @argSpec.default
  218. def _buildArgSpec(self):
  219. return _getArgSpec(self.method)
  220. def __get__(self, oself, type=None):
  221. """
  222. Outputs are private, so raise an exception when we attempt to get one.
  223. """
  224. raise AttributeError(
  225. "{cls}.{method} is a state-machine output method; "
  226. "to produce this output, call an input method instead.".format(
  227. cls=type.__name__,
  228. method=self.method.__name__
  229. )
  230. )
  231. def __call__(self, oself, *args, **kwargs):
  232. """
  233. Call the underlying method.
  234. """
  235. return self.method(oself, *args, **kwargs)
  236. def _name(self):
  237. return self.method.__name__
  238. @attr.s(eq=False, hash=False)
  239. class MethodicalTracer(object):
  240. automaton = attr.ib(repr=False)
  241. symbol = attr.ib(repr=False)
  242. def __get__(self, oself, type=None):
  243. transitioner = _transitionerFromInstance(oself, self.symbol,
  244. self.automaton)
  245. def setTrace(tracer):
  246. transitioner.setTrace(tracer)
  247. return setTrace
  248. counter = count()
  249. def gensym():
  250. """
  251. Create a unique Python identifier.
  252. """
  253. return "_symbol_" + str(next(counter))
  254. class MethodicalMachine(object):
  255. """
  256. A :class:`MethodicalMachine` is an interface to an `Automaton`
  257. that uses methods on a class.
  258. """
  259. def __init__(self):
  260. self._automaton = Automaton()
  261. self._reducers = {}
  262. self._symbol = gensym()
  263. def __get__(self, oself, type=None):
  264. """
  265. L{MethodicalMachine} is an implementation detail for setting up
  266. class-level state; applications should never need to access it on an
  267. instance.
  268. """
  269. if oself is not None:
  270. raise AttributeError(
  271. "MethodicalMachine is an implementation detail.")
  272. return self
  273. @_keywords_only
  274. def state(self, initial=False, terminal=False,
  275. serialized=None):
  276. """
  277. Declare a state, possibly an initial state or a terminal state.
  278. This is a decorator for methods, but it will modify the method so as
  279. not to be callable any more.
  280. :param bool initial: is this state the initial state?
  281. Only one state on this :class:`automat.MethodicalMachine`
  282. may be an initial state; more than one is an error.
  283. :param bool terminal: Is this state a terminal state?
  284. i.e. a state that the machine can end up in?
  285. (This is purely informational at this point.)
  286. :param Hashable serialized: a serializable value
  287. to be used to represent this state to external systems.
  288. This value should be hashable;
  289. :py:func:`unicode` is a good type to use.
  290. """
  291. def decorator(stateMethod):
  292. state = MethodicalState(machine=self,
  293. method=stateMethod,
  294. serialized=serialized)
  295. if initial:
  296. self._automaton.initialState = state
  297. return state
  298. return decorator
  299. @_keywords_only
  300. def input(self):
  301. """
  302. Declare an input.
  303. This is a decorator for methods.
  304. """
  305. def decorator(inputMethod):
  306. return MethodicalInput(automaton=self._automaton,
  307. method=inputMethod,
  308. symbol=self._symbol)
  309. return decorator
  310. @_keywords_only
  311. def output(self):
  312. """
  313. Declare an output.
  314. This is a decorator for methods.
  315. This method will be called when the state machine transitions to this
  316. state as specified in the decorated `output` method.
  317. """
  318. def decorator(outputMethod):
  319. return MethodicalOutput(machine=self, method=outputMethod)
  320. return decorator
  321. def _oneTransition(self, startState, inputToken, endState, outputTokens,
  322. collector):
  323. """
  324. See L{MethodicalState.upon}.
  325. """
  326. # FIXME: tests for all of this (some of it is wrong)
  327. # if not isinstance(startState, MethodicalState):
  328. # raise NotImplementedError("start state {} isn't a state"
  329. # .format(startState))
  330. # if not isinstance(inputToken, MethodicalInput):
  331. # raise NotImplementedError("start state {} isn't an input"
  332. # .format(inputToken))
  333. # if not isinstance(endState, MethodicalState):
  334. # raise NotImplementedError("end state {} isn't a state"
  335. # .format(startState))
  336. # for output in outputTokens:
  337. # if not isinstance(endState, MethodicalState):
  338. # raise NotImplementedError("output state {} isn't a state"
  339. # .format(endState))
  340. self._automaton.addTransition(startState, inputToken, endState,
  341. tuple(outputTokens))
  342. inputToken.collectors[startState] = collector
  343. @_keywords_only
  344. def serializer(self):
  345. """
  346. """
  347. def decorator(decoratee):
  348. @wraps(decoratee)
  349. def serialize(oself):
  350. transitioner = _transitionerFromInstance(oself, self._symbol,
  351. self._automaton)
  352. return decoratee(oself, transitioner._state.serialized)
  353. return serialize
  354. return decorator
  355. @_keywords_only
  356. def unserializer(self):
  357. """
  358. """
  359. def decorator(decoratee):
  360. @wraps(decoratee)
  361. def unserialize(oself, *args, **kwargs):
  362. state = decoratee(oself, *args, **kwargs)
  363. mapping = {}
  364. for eachState in self._automaton.states():
  365. mapping[eachState.serialized] = eachState
  366. transitioner = _transitionerFromInstance(
  367. oself, self._symbol, self._automaton)
  368. transitioner._state = mapping[state]
  369. return None # it's on purpose
  370. return unserialize
  371. return decorator
  372. @property
  373. def _setTrace(self):
  374. return MethodicalTracer(self._automaton, self._symbol)
  375. def asDigraph(self):
  376. """
  377. Generate a L{graphviz.Digraph} that represents this machine's
  378. states and transitions.
  379. @return: L{graphviz.Digraph} object; for more information, please
  380. see the documentation for
  381. U{graphviz<https://graphviz.readthedocs.io/>}
  382. """
  383. from ._visualize import makeDigraph
  384. return makeDigraph(
  385. self._automaton,
  386. stateAsString=lambda state: state.method.__name__,
  387. inputAsString=lambda input: input.method.__name__,
  388. outputAsString=lambda output: output.method.__name__,
  389. )