Franck Pommereau

full ABCD simulator

......@@ -14,6 +14,8 @@ from snakes.utils.simul.html import json
## error messages
##
options = None
ERR_ARG = 1
ERR_OPT = 2
ERR_IO = 3
......@@ -92,6 +94,10 @@ opt.add_option("--headless",
dest="headless", action="store", default=None,
help="headless code simulator, with saved parameters",
metavar="JSONFILE")
opt.add_option("--port",
dest="port", action="store", default=8000, type=int,
help="port on which the simulator server runs",
metavar="PORT")
opt.add_option("-H", "--html",
dest="html", action="store", default=None,
help="save net as HTML",
......@@ -292,7 +298,7 @@ def main (args=sys.argv[1:], src=None) :
lineno, trace = Checker(net).run()
if options.simul :
try :
simul = Simulator(node, net, draw(net, None))
simul = Simulator(node, net, draw(net, None), options.port)
except :
bug()
simul.start()
......
Well, you can tell by the way I use my walk,
I'm a woman's man: no time to talk.
Music loud and women warm, I've been kicked around
Since I was born.
And now it's all right. it's ok.
And you may look the other way.
We can try to understand
The new york times effect on man.
Whether you're a brother or whether you're a mother,
You're stayin alive, stayin alive.
Feel the city breakin and everybody shakin,
And were stayin alive, stayin alive.
Ah, ha, ha, ha, stayin alive, stayin alive.
Ah, ha, ha, ha, stayin alive.
Well now, I get low and I get high,
And if I can't get either, I really try.
Got the wings of heaven on my shoes.
I'm a dancin man and I just can't lose.
You know it's all right.its ok.
I'll live to see another day.
We can try to understand
The new york times effect on man.
Whether you're a brother or whether you're a mother,
You're stayin alive, stayin alive.
Feel the city breakin and everybody shakin,
And were stayin alive, stayin alive.
Ah, ha, ha, ha, stayin alive, stayin alive.
Ah, ha, ha, ha, stayin alive.
Life goin nowhere.somebody help me.
Somebody help me, yeah.
Life goin nowhere.somebody help me.
Somebody help me, yeah. stayin alive.
Well, you can tell by the way I use my walk,
I'm a woman's man: no time to talk.
Music loud and women warm,
I've been kicked around since I was born.
And now it's all right. it's ok.
And you may look the other way.
We can try to understand
The new york times effect on man.
Whether you're a brother or whether you're a mother,
You're stayin alive, stayin alive.
Feel the city breakin and everybody shakin,
And were stayin alive, stayin alive.
Ah, ha, ha, ha, stayin alive, stayin alive.
Ah, ha, ha, ha, stayin alive.
Life goin nowhere.somebody help me.
Somebody help me, yeah.
Life goin nowhere.somebody help me, yeah.
I'm stayin alive.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff is collapsed. Click to expand it.
This diff could not be displayed because it is too large.
<!DOCTYPE html>
<html>
<head>
<link type="text/css" href="r/css/bootstrap-theme.css" rel="stylesheet"/>
<link type="text/css" href="r/css/bootstrap.css" rel="stylesheet"/>
<link type="text/css" href="r/css/docs.css" rel="stylesheet"/>
<link type="text/css" href="r/simulator.css" rel="stylesheet"/>
<link type="text/css" href="r/model.css" rel="stylesheet"/>
<script src="r/jquery.min.js"></script>
<script src="r/js/bootstrap.min.js"></script>
<script src="r/jquery.periodic.js"></script>
<script src="r/js/bootstrap.file-input.js"></script>
<script src="r/d3.min.js"></script>
<script src="r/js/petri.js"></script>
<script src="r/simulator.js"></script>
</head>
<style>
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
line {
stroke: black;
}
text {
font-family: Arial;
font-size: 9pt;
}
</style>
<body>
<header class="navbar navbar-static-top">
<div class="container">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-6"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Simulation SNAKES</a></div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-6">
<ul id="nav_sim" class="nav navbar-nav">
<li><a href="#" id="ui-reset">Reset Simulation</a></li>
<li><a href="#" id="ui-quit">Stop Simulation</a></li>
<li><a href="#" id="ui-help">Help</a></li>
</ul>
</div>
</div>
</nav>
</div>
</header>
<div class="container">
%(model)s
<div class="modal fade" id="about" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">Petri Net</h4>
</div>
<div class="modal-body">
<p><span class="title">ABCD simulator is part of the SNAKES toolkit</span><br /> (C) 2014 Franck Pommereau</p>
<p><a href="http://www.ibisc.univ-evry.fr/~fpommereau/SNAKES" target="_blank">SNAKES homepage</a></p>
</div>
<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$.simisc({
"nav":{
"about": {
"text": "About",
"attr": {
"id":"ui-about",
"data-toggle": "modal",
"data-target": "#about"
}
},
"net": {
"text": "Petri Net",
"attr": {
"id":"ui-net",
"data-toggle": "modal",
"data-target": "#net"
}
}
},
"graph": {
"activate": false
}
});
});
</script>
</body>
</html>
This diff could not be displayed because it is too large.
/*!
* jQuery periodic plugin
*
* Copyright 2010, Tom Anderson
* Dual licensed under the MIT or GPL Version 2 licenses.
*
*/
$.periodic = function (options, callback) {
// if the first argument is a function then assume the options aren't being passed
if (jQuery.isFunction(options)) {
callback = options;
options = {};
}
// Merge passed settings with default values
var settings = jQuery.extend({}, jQuery.periodic.defaults, {
ajax_complete : ajaxComplete,
increment : increment,
reset : reset,
cancel : cancel
}, options);
// bookkeeping variables
settings.cur_period = settings.period;
settings.tid = false;
var prev_ajax_response = '';
run();
// return settings so user can tweak them externally
return settings;
// run (or restart if already running) the looping construct
function run() {
// clear/stop existing timer (multiple calls to run() won't result in multiple timers)
cancel();
// let it rip!
settings.tid = setTimeout(function() {
// set the context (this) for the callback to the settings object
callback.call(settings);
// compute the next value for cur_period
increment();
// queue up the next run
if(settings.tid)
run();
}, settings.cur_period);
}
// utility function for use with ajax calls
function ajaxComplete(xhr, status) {
if (status === 'success' && prev_ajax_response !== xhr.responseText) {
// reset the period whenever the response changes
prev_ajax_response = xhr.responseText;
reset();
}
}
// compute the next delay
function increment() {
settings.cur_period *= settings.decay;
if (settings.cur_period < settings.period) {
// don't let it drop below the minimum
reset();
} else if (settings.cur_period > settings.max_period) {
settings.cur_period = settings.max_period;
if (settings.on_max !== undefined) {
// call the user-supplied callback if we reach max_period
settings.on_max.call(settings);
}
}
}
function reset() {
settings.cur_period = settings.period;
// restart with the new timeout
run();
}
function cancel() {
clearTimeout(settings.tid);
settings.tid = null;
}
// other functions we might want to implement
function pause() {}
function resume() {}
function log() {}
};
jQuery.periodic.defaults = {
period : 4000, // 4 sec.
max_period : 1800000, // 30 min.
decay : 1.5, // time period multiplier
on_max : undefined // called if max_period is reached
};
\ No newline at end of file
/*
Bootstrap - File Input
======================
This is meant to convert all file input tags into a set of elements that displays consistently in all browsers.
Converts all
<input type="file">
into Bootstrap buttons
<a class="btn">Browse</a>
*/
$(function() {
$.fn.bootstrapFileInput = function() {
this.each(function(i,elem){
var $elem = $(elem);
// Maybe some fields don't need to be standardized.
if (typeof $elem.attr('data-bfi-disabled') != 'undefined') {
return;
}
// Set the word to be displayed on the button
var buttonWord = 'Browse';
if (typeof $elem.attr('title') != 'undefined') {
buttonWord = $elem.attr('title');
}
// Start by getting the HTML of the input element.
// Thanks for the tip http://stackoverflow.com/a/1299069
var input = $('<div>').append( $elem.eq(0).clone() ).html();
var className = '';
if (!!$elem.attr('class')) {
className = ' ' + $elem.attr('class');
}
// Now we're going to replace that input field with a Bootstrap button.
// The input will actually still be there, it will just be float above and transparent (done with the CSS).
$elem.replaceWith('<a class="file-input-wrapper btn btn-default' + className + '">'+buttonWord+input+'</a>');
})
// After we have found all of the file inputs let's apply a listener for tracking the mouse movement.
// This is important because the in order to give the illusion that this is a button in FF we actually need to move the button from the file input under the cursor. Ugh.
.promise().done( function(){
// As the cursor moves over our new Bootstrap button we need to adjust the position of the invisible file input Browse button to be under the cursor.
// This gives us the pointer cursor that FF denies us
$('.file-input-wrapper').mousemove(function(cursor) {
var input, wrapper,
wrapperX, wrapperY,
inputWidth, inputHeight,
cursorX, cursorY;
// This wrapper element (the button surround this file input)
wrapper = $(this);
// The invisible file input element
input = wrapper.find("input");
// The left-most position of the wrapper
wrapperX = wrapper.offset().left;
// The top-most position of the wrapper
wrapperY = wrapper.offset().top;
// The with of the browsers input field
inputWidth= input.width();
// The height of the browsers input field
inputHeight= input.height();
//The position of the cursor in the wrapper
cursorX = cursor.pageX;
cursorY = cursor.pageY;
//The positions we are to move the invisible file input
// The 20 at the end is an arbitrary number of pixels that we can shift the input such that cursor is not pointing at the end of the Browse button but somewhere nearer the middle
moveInputX = cursorX - wrapperX - inputWidth + 20;
// Slides the invisible input Browse button to be positioned middle under the cursor
moveInputY = cursorY- wrapperY - (inputHeight/2);
// Apply the positioning styles to actually move the invisible file input
input.css({
left:moveInputX,
top:moveInputY
});
});
$('.file-input-wrapper input[type=file]').change(function(){
var fileName;
fileName = $(this).val();
// Remove any previous file names
$(this).parent().next('.file-input-name').remove();
if (!!$(this).prop('files') && $(this).prop('files').length > 1) {
fileName = $(this)[0].files.length+' files';
//$(this).parent().after('<span class="file-input-name">'+$(this)[0].files.length+' files</span>');
}
else {
// var fakepath = 'C:\\fakepath\\';
// fileName = $(this).val().replace('C:\\fakepath\\','');
fileName = fileName.substring(fileName.lastIndexOf('\\')+1,fileName.length);
}
$(this).parent().after('<span class="file-input-name">'+fileName+'</span>');
});
});
};
// Add the styles before the first stylesheet
// This ensures they can be easily overridden with developer styles
var cssHtml = '<style>'+
'.file-input-wrapper { overflow: hidden; position: relative; cursor: pointer; z-index: 1; }'+
'.file-input-wrapper input[type=file], .file-input-wrapper input[type=file]:focus, .file-input-wrapper input[type=file]:hover { position: absolute; top: 0; left: 0; cursor: pointer; opacity: 0; filter: alpha(opacity=0); z-index: 99; outline: 0; }'+
'.file-input-name { margin-left: 8px; }'+
'</style>';
$('link[rel=stylesheet]').eq(0).before(cssHtml);
});
\ No newline at end of file
This diff is collapsed. Click to expand it.
$(document).ready(function(){
$(".abcd .action, .abcd .instance").each(function(){
var objet = this;
$(".action, .instance").each(function(){
if ($(this).attr("data-abcd") == "#" + $(objet).attr("id")){
$(this).html($(objet).html());
}
});
});
$(".action, .instance, .buffer, .proto").mouseover(function(){
$(this).addClass("highlight_simul");
$($(this).attr("data-abcd")).addClass("highlight_simul");
$($(this).attr("data-tree")).addClass("highlight_simul");
$($(this).attr("data-net")).addClass("highlight_simul");
}).mouseout(function(){
$(this).removeClass("highlight_simul");
$($(this).attr("data-abcd")).removeClass("highlight_simul");
$($(this).attr("data-tree")).removeClass("highlight_simul");
$($(this).attr("data-net")).removeClass("highlight_simul");
});
});
\ No newline at end of file
/* ABCD source code */
#model .abcd {
border: solid 1px #DDD;
border-radius: 5px;
padding: 5px 10px;
margin: 5px;
background-color: #F4F4F4;
overflow: auto;
float: left;
}
#model .abcd .comment {
color: #888;
}
......@@ -58,22 +48,10 @@
background-color: #B6F8AE;
}
#model .abcd .highlight {
#model .abcd .highlight_simul {
background-color: yellow;
}
/* Objects tree */
#model .tree {
border: solid 1px #DDD;
border-radius: 5px;
padding: 5px 10px;
margin: 5px;
background-color: #F4F4F4;
overflow: auto;
font-family: monospace;
float: left;
}
#model .tree .buffer .kw {
color: #800;
......@@ -127,20 +105,8 @@
background-color: #B6F8AE;
}
#model .tree .highlight {
#model .tree .highlight_simul {
background-color: yellow;
}
/* Petri net picture */
#model .petrinet {
border: solid 1px #DDD;
border-radius: 5px;
padding: 5px 10px;
margin: 5px;
background-color: #FFF;
overflow: auto;
clear: both;
display: none;
}
......
<h1><tt>%(filename)s</tt></h1>
%(abcd)s
%(tree)s
%(net)s
<div class="page-header">
<h1><tt>%(filename)s</tt> <small> powered by Franck</small></h1>
</div>
<div class="row">
<div class="col-md-3">
<div class="row">
<h3>Player</h3>
<div id="player"></div>
</div>
</div>
</div>
<div id="model" class="row">
<div class="col-md-6">
<div class="row">
<h3>ABCD</h3>
<div class="abcd">%(abcd)s</div>
</div>
</div>
<div class=col-md-6>
<div class="row">
<h3>Tree</h3>
<div class="tree">%(tree)s</div>
</div>
</div>
<div class="col-md-12">
<div id="trace_zone"></div>
</div>
</div>
<div class="modal fade" id="net" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" style="width:auto;">
<div class="modal-content" style="overflow: scroll;">
%(net)s
</div>
</div>
</div>
\ No newline at end of file
......
body {
font-family: sans-serif;
}
#alive {
border: solid 1px #AAA;
border-radius: 5px;
padding: 5px 10px;
margin: 5px;
background-color: #DDD;
overflow:auto;
}
#alive .ui {
display: inline;
list-style: none;
margin: 0px;
padding: 0px;
}
#alive .ui li {
display: inline;
border: solid 1px #AAA;
padding: 5px 10px;
margin: 0px 3px;
background-color: #EEE;
}
#alive .ui a {
text-decoration: none;
color: #333;
}
#alive .ui li:hover {
background-color: #FFF;
}
#alive .ui a:hover {
color: #A33;
}
#alive .ping {
color: #DDD;
float: right;
}
#model {
border: solid 1px #AAA;
border-radius: 5px;
padding: 5px 10px;
margin: 5px;
background-color: #EEE;
overflow:auto;
}
#trace {
border: solid 1px #AAA;
border-radius: 5px;
padding: 5px 10px;
margin: 5px;
background-color: #EEE;
overflow:auto;
}
#about {
display: none;
}
#dialog {
display: none;
position: fixed;
z-index: 2;
top: 10%;
left: 20%;
width: 60%;
border: solid 2px #AAA;
border-radius: 5px;
padding: 20px;
background-color: #FFF;
overflow:auto;
}
#dialog-bg {
display: none;
position: fixed;
z-index: 1;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
background-color: #000;
margin: 0px;
}
#dialog-close {
/* TODO: fix position when dialog has a horizontal scrollbar */
display: block;
float: right;
border: solid 1px #AAA;
padding: 5px 10px;
margin: 0px 3px;
background-color: #EEE;
text-decoration: none;
color: #333;
}
#dialog-close:hover {
background-color: #FFF;
color: #A33;
}
.dialog p {
margin: 10px 20px 0px 20px;
}
.dialog .title {
margin: 0px 20px 5px 20px;
font-weight: bold;
text-align: center;
}
.dialog .subtitle {
margin: 0px 20px 5px 20px;
text-align: center;
}
\ No newline at end of file
This diff is collapsed. Click to expand it.
......@@ -78,9 +78,9 @@ class ABCDSimulator (BaseSimulator) :
}
class Simulator (BaseHTTPSimulator) :
def __init__ (self, node, net, gv) :
def __init__ (self, node, net, gv, port) :
simul = ABCDSimulator(node, net, gv)
BaseHTTPSimulator.__init__(self, net, simulator=simul)
BaseHTTPSimulator.__init__(self, net, simulator=simul, port=port)
def init_model (self) :
return self.res["model.html"] % self.simul.info
def init_ui (self) :
......
import snakes
from snakes.utils.simul.httpd import *
from snakes.utils.simul.html import H
from snakes.utils.simul import logger as log
import multiprocessing, time, sys, os.path, signal, inspect, glob
import operator
......@@ -35,10 +36,6 @@ class StateSpace (dict) :
def modes (self, state) :
return self[state].modes
def log (message) :
sys.stderr.write("[simulator] %s\n" % message.strip())
sys.stderr.flush()
shutdown = multiprocessing.Event()
ping = multiprocessing.Event()
......@@ -64,8 +61,7 @@ class WatchDog (multiprocessing.Process) :
if ping.wait(self.timeout) :
ping.clear()
else :
log("client not active - %s\n"
% time.strftime("%d/%b/%Y %H:%M:%S"))
log.info("client has gone", "simul")
break
except KeyboardInterrupt :
pass
......@@ -138,14 +134,12 @@ class BaseHTTPSimulator (Node) :
dirs = {}
for cls in reversed(inspect.getmro(self.__class__)[:-2]) :
path = os.path.dirname(inspect.getsourcefile(cls))
for pattern in respatt + ["resources/*.js",
"resources/*.css",
"resources/*.html",
"resources/alive.txt"] :
for pattern in respatt + ["resources/*/*.*",
"resources/*.*"] :
for res in glob.glob(os.path.join(path, pattern)) :
if os.path.isfile(res) :
with open(res) as infile :
self.res[os.path.basename(res)] = infile.read()
self.res[res[len(path + "resources/")+1:]] = infile.read()
elif os.path.isdir(res) :
dirs[os.path.basename(res)] = DirNode(res)
else :
......@@ -171,7 +165,7 @@ class BaseHTTPSimulator (Node) :
else :
self.simul = simulator
def start (self) :
log("starting at %r" % self.url)
log.info("starting at %r" % self.url, "simul")
shutdown.clear()
ping.clear()
self.server.start()
......@@ -179,11 +173,11 @@ class BaseHTTPSimulator (Node) :
def wait (self) :
try :
shutdown.wait()
log("preparing to shut down...")
log.info("preparing to shut down...", "simul")
time.sleep(2)
except KeyboardInterrupt :
shutdown.set()
log("shuting down...")
log.info("shuting down...", "simul")
sig = getattr(signal, "CTRL_C_EVENT",
getattr(signal, "SIGTERM", None))
if sig is not None :
......@@ -191,7 +185,7 @@ class BaseHTTPSimulator (Node) :
os.kill(self.server.pid, sig)
if self.watchdog.pid :
os.kill(self.watchdog.pid, sig)
log("bye!")
log.info("bye!", "simul")
def init_index (self) :
return {"res" : "%sr" % self.url,
"url" : self.url,
......
import sys, os.path, httplib, cgi, urlparse, functools, mimetypes
import os, signal, traceback, random, base64, inspect
import os, traceback, random, base64, inspect, math
import BaseHTTPServer
from snakes.utils.simul import logger as log
from snakes.utils.simul.html import json, utf8
##
......@@ -156,6 +157,21 @@ class HTTPRequestHandler (BaseHTTPServer.BaseHTTPRequestHandler) :
"<body><p>%s</p></body>" % (v.answer, v.message))
if v.code == 500 :
traceback.print_exception(*v.debug)
def log_request (self, code="-", size="-") :
code = str(code or "-")
method, path, version = self.requestline.split()
if code[0] in "45" :
logger = log.warn
else :
path = path.split("/", 2)[-1]
if len(path) > 28 :
path = "..." + path[-28:]
logger = log.debug
logger("%s %s => %s" % (method, path, code), "httpd")
def log_message (self, format, *args) :
log.info(format % args, "httpd")
def log_error (self, format, *args) :
log.error(format % args, "httpd")
class HTTPServer (BaseHTTPServer.HTTPServer):
def __init__ (self, server_address, root):
......
import sys, time, re
##
## borrowed from http://code.activestate.com/recipes/475116/
##
class TerminalController:
"""
A class that can be used to portably generate formatted output to
a terminal.
`TerminalController` defines a set of instance variables whose
values are initialized to the control sequence necessary to
perform a given action. These can be simply included in normal
output to the terminal:
>>> term = TerminalController()
>>> print 'This is '+term.GREEN+'green'+term.NORMAL
Alternatively, the `render()` method can used, which replaces
'${action}' with the string required to perform 'action':
>>> term = TerminalController()
>>> print term.render('This is ${GREEN}green${NORMAL}')
If the terminal doesn't support a given action, then the value of
the corresponding instance variable will be set to ''. As a
result, the above code will still work on terminals that do not
support color, except that their output will not be colored.
Also, this means that you can test whether the terminal supports a
given action by simply testing the truth value of the
corresponding instance variable:
>>> term = TerminalController()
>>> if term.CLEAR_SCREEN:
... print 'This terminal supports clearning the screen.'
Finally, if the width and height of the terminal are known, then
they will be stored in the `COLS` and `LINES` attributes.
"""
# Cursor movement:
BOL = '' #: Move the cursor to the beginning of the line
UP = '' #: Move the cursor up one line
DOWN = '' #: Move the cursor down one line
LEFT = '' #: Move the cursor left one char
RIGHT = '' #: Move the cursor right one char
# Deletion:
CLEAR_SCREEN = '' #: Clear the screen and move to home position
CLEAR_EOL = '' #: Clear to the end of the line.
CLEAR_BOL = '' #: Clear to the beginning of the line.
CLEAR_EOS = '' #: Clear to the end of the screen
# Output modes:
BOLD = '' #: Turn on bold mode
BLINK = '' #: Turn on blink mode
DIM = '' #: Turn on half-bright mode
REVERSE = '' #: Turn on reverse-video mode
NORMAL = '' #: Turn off all modes
# Cursor display:
HIDE_CURSOR = '' #: Make the cursor invisible
SHOW_CURSOR = '' #: Make the cursor visible
# Terminal size:
COLS = None #: Width of the terminal (None for unknown)
LINES = None #: Height of the terminal (None for unknown)
# Foreground colors:
BLACK = BLUE = GREEN = CYAN = RED = MAGENTA = YELLOW = WHITE = ''
# Background colors:
BG_BLACK = BG_BLUE = BG_GREEN = BG_CYAN = ''
BG_RED = BG_MAGENTA = BG_YELLOW = BG_WHITE = ''
_STRING_CAPABILITIES = """
BOL=cr UP=cuu1 DOWN=cud1 LEFT=cub1 RIGHT=cuf1
CLEAR_SCREEN=clear CLEAR_EOL=el CLEAR_BOL=el1 CLEAR_EOS=ed BOLD=bold
BLINK=blink DIM=dim REVERSE=rev UNDERLINE=smul NORMAL=sgr0
HIDE_CURSOR=cinvis SHOW_CURSOR=cnorm""".split()
_COLORS = """BLACK BLUE GREEN CYAN RED MAGENTA YELLOW WHITE""".split()
_ANSICOLORS = "BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE".split()
def __init__(self, term_stream=sys.stdout):
"""
Create a `TerminalController` and initialize its attributes
with appropriate values for the current terminal.
`term_stream` is the stream that will be used for terminal
output; if this stream is not a tty, then the terminal is
assumed to be a dumb terminal (i.e., have no capabilities).
"""
# Curses isn't available on all platforms
try: import curses
except: return
# If the stream isn't a tty, then assume it has no capabilities.
if not term_stream.isatty(): return
# Check the terminal type. If we fail, then assume that the
# terminal has no capabilities.
try: curses.setupterm()
except: return
# Look up numeric capabilities.
self.COLS = curses.tigetnum('cols')
self.LINES = curses.tigetnum('lines')
# Look up string capabilities.
for capability in self._STRING_CAPABILITIES:
(attrib, cap_name) = capability.split('=')
setattr(self, attrib, self._tigetstr(cap_name) or '')
# Colors
set_fg = self._tigetstr('setf')
if set_fg:
for i,color in zip(range(len(self._COLORS)), self._COLORS):
setattr(self, color, curses.tparm(set_fg, i) or '')
set_fg_ansi = self._tigetstr('setaf')
if set_fg_ansi:
for i,color in zip(range(len(self._ANSICOLORS)), self._ANSICOLORS):
setattr(self, color, curses.tparm(set_fg_ansi, i) or '')
set_bg = self._tigetstr('setb')
if set_bg:
for i,color in zip(range(len(self._COLORS)), self._COLORS):
setattr(self, 'BG_'+color, curses.tparm(set_bg, i) or '')
set_bg_ansi = self._tigetstr('setab')
if set_bg_ansi:
for i,color in zip(range(len(self._ANSICOLORS)), self._ANSICOLORS):
setattr(self, 'BG_'+color, curses.tparm(set_bg_ansi, i) or '')
def _tigetstr(self, cap_name):
# String capabilities can include "delays" of the form "$<2>".
# For any modern terminal, we should be able to just ignore
# these, so strip them out.
import curses
cap = curses.tigetstr(cap_name) or ''
return re.sub(r'\$<\d+>[/*]?', '', cap)
def render(self, template):
"""
Replace each $-substitutions in the given template string with
the corresponding terminal control string (if it's defined) or
'' (if it's not).
"""
return re.sub(r'\$\$|\${\w+}', self._render_sub, template)
def _render_sub(self, match):
s = match.group()
if s == '$$': return s
else: return getattr(self, s[2:-1])
##
## functions
##
_level = {"debug": "MAGENTA",
"info": "BLUE",
"warn": "YELLOW",
"error": "RED"}
term = TerminalController()
def log (level, message, header="", date=True, newline=True, clearline=True) :
text = "%s%s[%s%s%s]${NORMAL} %s%s%s" % (
"${BOL}" if clearline else "",
"${%s}" % _level.get(level.lower(), "NORMAL"),
time.strftime("%Y-%m-%d %H:%M:%S") if date else "",
" - " if date and header else "",
header,
message,
"${CLEAR_EOL}" if clearline else "",
"\n" if newline else "")
sys.stderr.write(term.render(text))
sys.stderr.flush()
def debug (message, header="") :
log("debug", message, header, date=True, newline=False, clearline=True)
def info (message, header="") :
log("info", message, header, date=True, newline=True, clearline=True)
def warn (message, header="") :
log("warn", message, header, date=True, newline=True, clearline=True)
def error (message, header="") :
log("error", message, header, date=True, newline=True, clearline=True)