From c093b7b78fe2275999774e447597e74348a43f6e Mon Sep 17 00:00:00 2001 From: Alexandre Duret-Lutz Date: Tue, 16 Feb 2016 12:56:20 +0100 Subject: [PATCH] python: move auxiliary functions in a separate module * python/spot/aux.py: New file, with function extracted from... * python/spot/__init__.py: ... here. * python/.gitignore, python/Makefile.am: Adjust. --- python/.gitignore | 4 +-- python/Makefile.am | 1 + python/spot/__init__.py | 38 ++++--------------------- python/spot/aux.py | 62 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 34 deletions(-) create mode 100644 python/spot/aux.py diff --git a/python/.gitignore b/python/.gitignore index d867b4bc8..7e98f4b06 100644 --- a/python/.gitignore +++ b/python/.gitignore @@ -3,7 +3,6 @@ Makefile Makefile.in *.la -spot_impl.py* buddy.py* *.lo *.loT @@ -11,4 +10,5 @@ buddy.py* *.pyc */spotimg ajax/*.py -spot/*.py +spot/impl.py +spot/ltsmin.py diff --git a/python/Makefile.am b/python/Makefile.am index 25e04f039..a5f9a8fa8 100644 --- a/python/Makefile.am +++ b/python/Makefile.am @@ -37,6 +37,7 @@ SWIGFLAGS = -c++ -python -py3 -O -nofastproxy EXTRA_DIST = buddy.i spot/impl.i spot/ltsmin.i nobase_python_PYTHON = \ spot/__init__.py \ + spot/aux.py \ spot/impl.py \ spot/ltsmin.py \ buddy.py diff --git a/python/spot/__init__.py b/python/spot/__init__.py index 8781678e0..8b62f86ae 100644 --- a/python/spot/__init__.py +++ b/python/spot/__init__.py @@ -26,28 +26,19 @@ if sys.hexversion < 0x03030000: from spot.impl import * +from spot.aux import \ + extend as _extend, \ + str_to_svg as _str_to_svg, \ + ostream_to_svg as _ostream_to_svg import subprocess import os import signal -from functools import lru_cache -def _extend(*classes): - """ - Decorator that extends all the given classes with the contents - of the class currently being defined. - """ - def wrap(this): - for cls in classes: - for (name, val) in this.__dict__.items(): - if name not in ('__dict__', '__weakref__') \ - and not (name == '__doc__' and val is None): - setattr(cls, name, val) - return classes[0] - return wrap - +# The parrameters used by default when show() is called on an automaton. _show_default = None + def setup(**kwargs): """Configure Spot for fancy display. @@ -108,23 +99,6 @@ if 'op_ff' not in globals(): _bdd_dict = make_bdd_dict() -# Add a small LRU cache so that when we display automata into a -# interactive widget, we avoid some repeated calls to dot for -# identical inputs. -@lru_cache(maxsize=64) -def _str_to_svg(str): - dotty = subprocess.Popen(['dot', '-Tsvg'], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE) - dotty.stdin.write(str) - res = dotty.communicate() - return res[0].decode('utf-8') - - -def _ostream_to_svg(ostr): - return _str_to_svg(ostr.str().encode('utf-8')) - - @_extend(twa, ta) class twa: def _repr_svg_(self, opt=None): diff --git a/python/spot/aux.py b/python/spot/aux.py new file mode 100644 index 000000000..d5bc34aaa --- /dev/null +++ b/python/spot/aux.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2016 Laboratoire de Recherche et Développement de l'Epita +# (LRDE). +# +# This file is part of Spot, a model checking library. +# +# Spot is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Spot is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public +# License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +""" +Auxiliary functions for Spot's Python bindings +""" + +from functools import lru_cache +import subprocess + + +def extend(*classes): + """ + Decorator that extends all the given classes with the contents + of the class currently being defined. + """ + def wrap(this): + for cls in classes: + for (name, val) in this.__dict__.items(): + if name not in ('__dict__', '__weakref__') \ + and not (name == '__doc__' and val is None): + setattr(cls, name, val) + return classes[0] + return wrap + + +# Add a small LRU cache so that when we display automata into a +# interactive widget, we avoid some repeated calls to dot for +# identical inputs. +@lru_cache(maxsize=64) +def str_to_svg(str): + """ + Send some text to dot for conversion to SVG. + """ + dot = subprocess.Popen(['dot', '-Tsvg'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE) + res = dot.communicate(str) + return res[0].decode('utf-8') + + +def ostream_to_svg(ostr): + """ + Encode an ostringstream as utf-8 and send it to dot for cocnversion to SVG. + """ + return str_to_svg(ostr.str().encode('utf-8'))