ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am playing around with the idea of a front end in python using nethack as the backend.
Anybody know how to capture signals from nethack and return commands in python?
]$ python nethackPython.py
posix
total 4
-rw-rw-r-- 1 gillesg gillesg 235 Feb 11 12:28 nethackPython.py
0
/usr/games/nethack-3.4.3/nethack: error while loading shared libraries: libXaw.so.7: cannot open shared object file: No such file or directory
32512
The error message is ok. Nethack must be missing a lib, which is ok for now since it quits and I don't have to kill it.
I'm wondering what the two numbers mean: 0 and 32512. Are they handles to programs? Could I use them to feed commands to the programs? On second thought, it must be an error code that nethack is returning. Nuts!
Last edited by S. Chapelin; 02-11-2012 at 11:44 AM.
Reason: second thoughts
I'm wondering what the two numbers mean: 0 and 32512. Are they handles to programs? Could I use them to feed commands to the programs? On second thought, it must be an error code that nethack is returning. Nuts!
Those are process exit codes, they're meant to indicate if a process finished successfully (0) or not (any other number). Programs can use different numbers to indicate different errors.
To communicate with a child process, you should use stdin and stdout. You should also use subprocess module instead of os.system(), it's better suited for subprocess handling. There are quite a few usage examples in its documentation.
$ python nethackPython.py
sortie = ('\x1b[?1049h\x1b[H\x1b[2J\x1b[H\x1b[2;1HNetHack, Copyright 1985-2003\r\x1b[3;1H By Stichting Mathematisch Centrum and M. Stephenson.\r\x1b[4;1H See license for details.\r\x1b[5;1H\x1b[H\x1b[2J\x1b[H\x1b[?1049lYou must play from a terminal.\n', 'NetHack (gettty): Invalid argument\nNetHack (settty): Invalid argument\n')
nh.poll() = 1
Traceback (most recent call last):
File "nethackPython.py", line 12, in <module>
nh.kill()
File "/usr/lib/python2.7/subprocess.py", line 1446, in kill
self.send_signal(signal.SIGKILL)
File "/usr/lib/python2.7/subprocess.py", line 1436, in send_signal
os.kill(self.pid, sig)
OSError: [Errno 3] No such process
Hmmm. Curiouser and curiouser.
1. Nethack wants a terminal.
2. nh.kill() doesn't work.
I used to be able to read c files.
They used to begin with the calling of main(argc,argv).
I guess that was a long time ago.
Now I can't find the point of entry in the nethack src files.
I would like to see where the check for the tty, the console.
I seem to remember vaguely that nethack, or was it some other program, had a slave mode that you could turn on with some switch. Going to have to look that up, but for now I've had enough for one day.
Last edited by S. Chapelin; 02-11-2012 at 08:21 PM.
Reason: afterthought
Does anyone know how to simulate a terminal call from within python?
ex:
python script --> simulated shell --> nethack
I haven't been able to google anything useful.
Works, but it seems a waste of resources to have a shell constantly between python and nethack.
Anyone have a better solution?
Now to try having python open sh open nethack.
Here's a bit of text I found on the internet from Fredrik Lundh (the guy behind effbot.org website you have mentioned in this thread):
Quote:
the subprocess module is designed to deal with Unix-style subprocesses in general, not interactive tty-based applications.
you can fake it with some applications, if you make sure to flush your buffers and make sure you don't deadlock, but the right way to run an interactive tty-based application on remote is to attach it to a pseudo-tty.
Is there any way to use nethack non-interactively?
(verrry cautiously)Seems to work.
I get the following response:
Code:
'Cannot find any current entries for you.\r\nUsage: /usr/games/nethack-3.4.3/nethack -s [-v] <playertypes> [maxrank] [playernames]\r\nPlayer types are: [-p role] [-r race]'
which is typical when you open up nethack.
Here is the code I copied from the previous link:
Code:
import os, pty, time, string
class pty_Popen:
def __init__ (self, command, args, delay=0.1):
self.delay = delay
# Clone this process in a separate thread.
self.pid, self.child = pty.fork ()
# In the child process, pid will contain 0. In the parent,
# it will contain the pid of the child.
if self.pid == 0: # In the child process, so replace the python
# process with the requested command.
os.execv (command, [''] + args)
else: # In the parent process, which stays live
pass
def read (self, max_read):
time.sleep (self.delay)
return os.read (self.child, max_read)
def write (self, text):
time.sleep (self.delay)
return os.write (self.child, text)
process = pty_Popen ('/usr/bin/nethack', ['-s'])
response = '{', string.strip (process.read (1024)), '}'
print response
#process.write (text + '\n')
You might wish to take a look at a Python terminal emulator next, perhaps pyte (documentation), to take care of those strings and especially the ANSI escape sequences (for clearing the screen, writing characters to specific locations on screen, and so on). In particular, it maintains and gives you access to a screen buffer, with the library (when supplied with the input from nethack via the pseudoterminal) taking care of updating it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.