summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorBaruch Sterin <baruchs@gmail.com>2011-01-13 22:12:37 +0200
committerBaruch Sterin <baruchs@gmail.com>2011-01-13 22:12:37 +0200
commit811f5631a812968ccdbe157549f2445747053d50 (patch)
treed86691b0af7681aba69935875e5a7d779e24bc36 /scripts
parentc85a763444f696e0125445b5adc675020d736c5d (diff)
downloadabc-811f5631a812968ccdbe157549f2445747053d50.tar.gz
abc-811f5631a812968ccdbe157549f2445747053d50.tar.bz2
abc-811f5631a812968ccdbe157549f2445747053d50.zip
pyabc: reorganize supporting python scripts
Diffstat (limited to 'scripts')
-rw-r--r--scripts/getch.py37
-rw-r--r--scripts/redirect.py94
2 files changed, 0 insertions, 131 deletions
diff --git a/scripts/getch.py b/scripts/getch.py
deleted file mode 100644
index 89e13078..00000000
--- a/scripts/getch.py
+++ /dev/null
@@ -1,37 +0,0 @@
-
-class _Getch:
- """Gets a single character from standard input. Does not echo to the screen."""
- def __init__(self):
- try:
- self.impl = _GetchWindows()
- except ImportError:
- self.impl = _GetchUnix()
-
- def __call__(self): return self.impl()
-
-
-class _GetchUnix:
- def __init__(self):
- import tty, sys
-
- def __call__(self):
- import sys, tty, termios
- fd = sys.stdin.fileno()
- old_settings = termios.tcgetattr(fd)
- try:
- tty.setraw(sys.stdin.fileno())
- ch = sys.stdin.read(1)
- finally:
- termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
- return ch
-
-
-class _GetchWindows:
- def __init__(self):
- import msvcrt
-
- def __call__(self):
- import msvcrt
- return msvcrt.getch()
-
-getch = _Getch()
diff --git a/scripts/redirect.py b/scripts/redirect.py
deleted file mode 100644
index 498fe150..00000000
--- a/scripts/redirect.py
+++ /dev/null
@@ -1,94 +0,0 @@
-"""
-
-A simple context manager for redirecting streams in Python.
-The streams are redirected at the the C runtime level so that the output of C extensions
-that use stdio will also be redirected.
-
-null_file : a stream representing the null device (e.g. /dev/null on Unix)
-redirect: a context manager for redirecting streams
-
-Author: Baruch Sterin (sterin@berkeley.edu)
-
-"""
-
-import os
-import sys
-
-from contextlib import contextmanager
-
-null_file = open( os.devnull, "w" )
-
-@contextmanager
-def _dup( f ):
- fd = os.dup( f.fileno() )
- yield fd
- os.close(fd)
-
-@contextmanager
-def redirect(dst = null_file, src = sys.stdout):
-
- """
- Redirect the src stream into dst.
-
- Example:
- with redirect( open("somefile.txt", sys.stdout ) ):
- do some stuff ...
- """
-
- if src.fileno() == dst.fileno():
- yield
- return
-
- with _dup( src ) as fd_dup_src:
-
- dst.flush()
-
- src.flush()
- os.close( src.fileno() )
- os.dup2( dst.fileno(), src.fileno() )
-
- yield
-
- src.flush()
- os.close( src.fileno() )
- os.dup2( fd_dup_src, src.fileno() )
-
-def start_redirect(dst = null_file, src = sys.stdout):
-
- """
- Start redirection of src stream into dst. Return the duplicated file handle of the source.
-
- Example:
- fd = start_redirect( open("somefile.txt"), sys.stdout )
- ... do some stuff ...
- end_redirect(sys.stdout, fd)
- """
-
- if src.fileno() == dst.fileno():
- return None
-
- fd_dup_src = os.dup( src.fileno() )
-
- dst.flush()
- src.flush()
-
- os.close( src.fileno() )
- os.dup2( dst.fileno(), src.fileno() )
-
- return fd_dup_src
-
-def end_redirect(src, fd_dup_src):
-
- """
- End redirection of stream src.Redirect the src stream into dst. src is the source stream and fd_dup_src is the value returned by
- start_redirect()
- """
-
- if fd_dup_src is None:
- return
-
- src.flush()
- os.close( src.fileno() )
- os.dup2( fd_dup_src, src.fileno() )
-
- os.close(fd_dup_src)