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.
This commit is contained in:
Alexandre Duret-Lutz 2016-02-16 12:56:20 +01:00
parent 5d272fd256
commit c093b7b78f
4 changed files with 71 additions and 34 deletions

4
python/.gitignore vendored
View file

@ -3,7 +3,6 @@
Makefile Makefile
Makefile.in Makefile.in
*.la *.la
spot_impl.py*
buddy.py* buddy.py*
*.lo *.lo
*.loT *.loT
@ -11,4 +10,5 @@ buddy.py*
*.pyc *.pyc
*/spotimg */spotimg
ajax/*.py ajax/*.py
spot/*.py spot/impl.py
spot/ltsmin.py

View file

@ -37,6 +37,7 @@ SWIGFLAGS = -c++ -python -py3 -O -nofastproxy
EXTRA_DIST = buddy.i spot/impl.i spot/ltsmin.i EXTRA_DIST = buddy.i spot/impl.i spot/ltsmin.i
nobase_python_PYTHON = \ nobase_python_PYTHON = \
spot/__init__.py \ spot/__init__.py \
spot/aux.py \
spot/impl.py \ spot/impl.py \
spot/ltsmin.py \ spot/ltsmin.py \
buddy.py buddy.py

View file

@ -26,28 +26,19 @@ if sys.hexversion < 0x03030000:
from spot.impl import * 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 subprocess
import os import os
import signal import signal
from functools import lru_cache
def _extend(*classes): # The parrameters used by default when show() is called on an automaton.
"""
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
_show_default = None _show_default = None
def setup(**kwargs): def setup(**kwargs):
"""Configure Spot for fancy display. """Configure Spot for fancy display.
@ -108,23 +99,6 @@ if 'op_ff' not in globals():
_bdd_dict = make_bdd_dict() _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) @_extend(twa, ta)
class twa: class twa:
def _repr_svg_(self, opt=None): def _repr_svg_(self, opt=None):

62
python/spot/aux.py Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
"""
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'))