Commit c093b7b7 authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz

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.
parent 5d272fd2
......@@ -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
......@@ -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
......
......@@ -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):
......
# -*- 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'))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment