Permalink
Browse files

initial commit

  • Loading branch information...
temoto committed Jan 7, 2010
0 parents commit d2c48040e7ce531da8374c340cc8447fb83864fb
Showing with 696 additions and 0 deletions.
  1. +16 −0 LICENSE
  2. +42 −0 Makefile
  3. +71 −0 README.rst
  4. +24 −0 demo.py
  5. +26 −0 setup.py
  6. +3 −0 termkey/__init__.py
  7. +435 −0 termkey/mod_termkey.c
  8. +32 −0 termkey/mod_termkey.h
  9. +47 −0 test_memleak.c
16 LICENSE
@@ -0,0 +1,16 @@
+Copyright (c) 2009 Sergey Shepelev <temotor@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+and associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute,
+sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or
+substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
+PYTHON = python
+#PYTHON = $(HOME)/bin/python2.6
+CC = gcc
+CFLAGS = -std=c99 $(shell $(PYTHON)-config --cflags)
+#LD_LIBRARY_PATH = $(HOME)/lib
+#export LD_LIBRARY_PATH
+LDFLAGS = -L$(LD_LIBRARY_PATH) $(shell $(PYTHON)-config --ldflags)
+PYTHONPATH +=.:termkey
+export PYTHONPATH
+
+
+all: clean build run_memleak run_tests
+
+clean:
+ @rm -rf build
+ @rm -f ./test_memleak
+ @find . -name '_termkey*so' -exec rm "{}" \;
+ @find . -name '*.pyc' -exec rm "{}" \;
+ @echo "Cleaned."
+
+build_ext: clean
+ $(PYTHON) setup.py build_ext -fi
+ #mv termkey/termkey/_termkey.so termkey/
+
+build_memleak: clean
+ $(CC) -o test_memleak test_memleak.c $(CFLAGS) $(LDFLAGS)
+
+build: build_ext build_memleak
+
+run_memleak: build_ext build_memleak
+ ./test_memleak
+ #LD_LIBRARY_PATH= gdb
+
+grind_memleak: build_ext build_memleak
+ valgrind --tool=memcheck --leak-check=full --show-reachable=yes --track-origins=yes \
+ ./test_memleak
+
+run_tests: build_ext
+ $(PYTHON) ~/bin/nosetests $* || $(PYTHON)
+
+run_stress: build_ext
+ $(PYTHON) test_memleak.py
@@ -0,0 +1,71 @@
+What
+====
+
+Python C extension bindings for libtermkey, one excellent terminal key handling library.
+
+This binding is similar to ctypes_termkey, but in form of a C extension.
+Meaning, you will need a C compiler and libtermkey headers to build the binding, but it will work
+faster than ctypes version.
+
+
+Demo
+====
+
+See demo.py for a simple demo (to exit press C-c, that is Control+C).
+
+
+Usage
+=====
+
+To use bindings, you must first install `libtermkey <http://www.leonerd.org.uk/code/libtermkey/>`_.
+
+Then, you can import `TermKey` from termkey::
+
+ from termkey import TermKey
+
+if all goes ok, you'll see no errors here.
+
+You can now instantiate `TermKey` class to start using libtermkey::
+
+ tk = TermKey(0, 0)
+
+Usage of class is meaningful here, because it holds a state (an opaque handle to libtermkey) and you can create
+more than one `TermKey` to different file descriptors, etc.
+
+You can pass two parameters to constructor: file descriptor and flags.
+
+*TODO document parameters*
+
+Now you can start collecting keys, i.e. in wait mode::
+
+ result, key = tk.waitkey()
+
+to check result compare it to `termkey` attributes `RES_NONE`, `RES_KEY`, `RES_EOF` and `RES_AGAIN`::
+
+ from termkey import RES_KEY
+ if result == RES_KEY:
+ print "got key"
+
+key is a `termkey.Key` instance.
+
+To get string key name from key, you can call `str(key)` on it::
+
+ print str(key)
+
+which could print, i.e., q.
+
+Look at termkey documentation to learn how to use it.
+
+
+Credits
+=======
+
+Bindings by Sergey Shepelev <temotor@gmail.com>
+
+libtermkey by Paul LeoNerd Evans <leonerd@leonerd.org.uk>
+
+
+License
+=======
+
+pytermkey is under MIT license. Text is included in file LICENSE.
24 demo.py
@@ -0,0 +1,24 @@
+import termkey
+
+
+result_str_map = {
+ termkey.RES_NONE: 'None',
+ termkey.RES_KEY: 'Key',
+ termkey.RES_EOF: 'EOF',
+ termkey.RES_AGAIN: 'AGAIN',
+}
+
+
+tk = termkey.TermKey(0, 0)
+tk.flags = termkey.FLAG_RAW
+tk.waittime = tk.waittime
+
+keystr = ''
+while keystr != 'q':
+ result, key = tk.waitkey()
+ keystr = str(key)
+ print "*** result", result_str_map[result]
+ print " keystr", repr(keystr)
+# print " utf8", key.utf8
+
+tk.close()
@@ -0,0 +1,26 @@
+from distutils.core import setup, Extension
+import os
+
+
+PACKAGE = 'termkey'
+SOURCES = [
+ 'termkey/mod_termkey.c',
+]
+
+README = open('README.rst').read().strip() if os.path.isfile('README.rst') else ''
+
+termkey_module = Extension('termkey._termkey', SOURCES,
+ libraries=['termkey'],
+ language='c')
+
+setup(name='pytermkey',
+ version='0.1',
+ author='Sergey Shepelev',
+ author_email='temotor@gmail.com',
+ url='http://github.com/temoto/pytermkey',
+ packages=[PACKAGE],
+ description="pytermkey is python binding to libtermkey library by Paul Evans.",
+ long_description=README,
+ ext_modules=[termkey_module],
+ license='MIT License',
+ )
@@ -0,0 +1,3 @@
+from ._termkey import TermKey, Key
+from ._termkey import RES_NONE, RES_KEY, RES_EOF, RES_AGAIN
+from ._termkey import FLAG_NOINTERPRET, FLAG_CONVERTKP, FLAG_RAW, FLAG_UTF8, FLAG_NOTERMIOS
Oops, something went wrong.

0 comments on commit d2c4804

Please sign in to comment.