[Tutor] bind line-oriented device output?

Wayne Werner waynejwerner at gmail.com
Mon Mar 29 14:03:02 CEST 2010


On Sun, Mar 28, 2010 at 6:51 PM, Tom Roche <Tom_Roche at pobox.com> wrote:

>
> I'd like to learn to pythonically redirect the output from a
> line-oriented character device to a particular file or process,
> regardless of focus, on a generic graphical OS. But I don't want to
> redirect stdin entirely. Here's the usecase:
>
> My school has a seminar for which we record attendance by scanning the
> student ID card, or rather the barcode on its back, with a handheld
> USB scanner. This is pretty straightforward, except that the barcode
> scanner is like a keyboard writing the ASCII equivalent of the barcode
> (== the student's ID#) to stdin. FWIW, the scanner writes lines: I'm
> not sure if EOL is \r, \n, or \r\n, but I suspect the latter.
>
> Since the scanner is connected to an ordinary multiprocessing laptop on
> which one will likely be doing other things while scanning (notably
> setting up to record the presenter), it sometimes happens (especially
> until one learns to pay attention to this) that one writes to a frame
> other than the text file into which we want record attendee ID#s. This
> semester recurs every {fall, spring}, so someone faces this {pitfall,
> learning curve} at regular intervals.
>
> How to prevent writing the wrong target? One trivial solution--shlep a
> dedicated USB host for the scanner--is deprecated. An OS-specific
> solution (e.g. relying on a linux raw device) is also undesirable: I
> use ubuntu, but others will probably use mac or windows.
>
> Rather, It Would Be Nice, and useful for this seminar's mechanics, to
> be able to run some code to which one could say, see this device? and
> this file? Make the device's output go only to that file. For extra
> credit, don't let anything else write that file while this code is
> running.
>
> Can python do that? Or does one need to get closer to the metal?
>

It's certainly possible. Technically you can do /anything/, it's just a
matter of knowing how to do it. Whether or not it's worth doing is something
completely beside the point.

OTOH, if you can assure that the target computer will be running a certain
set of python features (i.e. Tkinter/pyGTK+/wxPython, etc) you can fairly
easily write a program that will run fullscreen and continually grab focus
to whatever widget (in this case a text entry field) you want.

with pyGTK you could do something like this:

gobject.timeout_add(100,  myentrywidget.grab_focus)

the function at the end may be different - perhaps you write your own
function to comply with the timeout_add requirements - but it it will
continually (every 100ms) grab the "keyboard" focus.

OTOH, if the device is something like the CueCat and reports a control
character (I think it's alt+f7 for the cuecat), then at least on
linux/windows I think it's much more trivial. You just have to register a
global hotkey handler that will send the focus to that particular window.

There are probably a few other approaches to this problem, but those are two
of the "simplest".

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100329/cd9d2ac6/attachment.html>


More information about the Tutor mailing list