PDA

View Full Version : Problems with my first program


El Piranna
2007-05-01, 01:29
Really, first and a half :p

Well, i tried what Nuka1195 and Asteron suggested me (http://xbmc.org/forum/showthread.php?p=133724) with almost no results... but at least i could be able to found some bugs (i has had some classes def ended with '()' that XBMC Phyton 2.4.3 doesn't tolerated so well as PC Phyton 2.5) and another interesting things, as that XBMC Python console is auto-updated :nod: but it doesn't work (fully), so let's go with it:

I'm aiming to do XUT, Xbox Unix Tools. This is a set of python apps to allow to have a shell on the XBox to be able to use it by us the old-school guys ;) It have PySH (Python SHell, a bask-like/Windows shell mixture written in phyton. Yes, i know there are similar proyects and in fact there is another PySH, but they want python compatibility, I don't: just bash), xTerm (xbox Terminal, a local console) and xTTY (xbox TTY, a remote console).

xTTY is the most incomplete since by now only work almost correctly in a unidireccional way and sending raw data, but i aim to make it telnet compatible (or integrate it into medusa, who would be the better opcion...), but at least it write to the XBMC Python console, so i know it work at the same state as PC version :p

PySH work correctly in PC and after repair some little problems compounding paths in XBMC too (i know it thanks to xTTY). It doesn't have too much commands, but at least it can navigate over the directory tree and show you things.

The more problematic and the real point for this message is xTerm. I can launch it, i can be able to input data with the onscreen keyboard... but it doesn't show me nothing. It should show a big controlLabel at all over the screen and update its content with the console text, but it only shows me the content of scripts folder as background. Yes, i know: labels background is transparent, but i should be able to see letters written over it, isn't it?

Here i have uploaded the 'XUT 0.1 alpha dangerous.it.beat.you' code if someone can help me with that ;)

Download XUT.zip (http://putstuff.putfile.com/74700/6887368)

Asteron
2007-05-01, 06:36
In regard to your question from the other thread
At the readme (http://xbmc.svn.sourceforge.net/view...xt?view=markup) it says that modules as xbmc.py and xbmcgui.py should be installed on Lib directory, but that directory doesn't exist. I have been looking for all the XBMC source tree without success, so where is that code? Where i can go to see it and try to expand it?

The xbmc and xbmcgui modules are implemented only in the cpp code. The objects are programmed into the interpreter programmaticly using the C interface. To see where this is done look here: http://xbmc.svn.sourceforge.net/viewvc/xbmc/trunk/XBMC/xbmc/lib/libPython/xbmcmodule/

Also if you are ever on irc feel free to stop by irc://irc.freenode.net/xbmc-scripting as lots of us hang out there.

In the future please just paste the code (with the "CODE" tag) or use a pastebin like http://www.pastebin.ca .. That putstuff website is kinda annoying...

EDIT:
This code is actually kinda fun... For a 'first program' its very nice :) I cant tell you exactly whats wrong with it yet but a few things caught my eye

def onAction(self, action):
if action == ACTION_PREVIOUS_MENU:
self.close()
if action == ACTION_SELECT_ITEM:
sys.stdin.showKeyboard()
I cant remember if sys.stdin.showKeyboard() is safe here... you might want to be careful.

if car == '\n':
self.column_ = 0
else:
self.column_ = self.column_+1
I think you want to increment self.row_ here as well

El Piranna
2007-05-01, 17:33
In regard to your question from the other thread

The xbmc and xbmcgui modules are implemented only in the cpp code. The objects are programmed into the interpreter programmaticly using the C interface. To see where this is done look here: http://xbmc.svn.sourceforge.net/viewvc/xbmc/trunk/XBMC/xbmc/lib/libPython/xbmcmodule/

Also if you are ever on irc feel free to stop by irc://irc.freenode.net/xbmc-scripting as lots of us hang out there.

In the future please just paste the code (with the "CODE" tag) or use a pastebin like http://www.pastebin.ca .. That putstuff website is kinda annoying...

Thanks, remembered that i have see a "windowsXML.cpp" somewhere... Ok: are hardcoded inside libPython, not an external module. Now i know where to start... telling it on the wiki :p

IRC... well, i hate chat ;) I must to use it at the ARDE (http://www.webdearde.com) directives reunions but i preffer e-mail or foums. In any case, thanks for the offer. And yes, i'll try to use PasteBin or similar, or at least not put urls on the fly ;)

EDIT:
This code is actually kinda fun... For a 'first program' its very nice :)

There is a lot of Copy&Paste and something about "copy from the book" ;) But yes, i have been programming since some years ago... :p

I cant tell you exactly whats wrong with it yet but a few things caught my eye

def onAction(self, action):
if action == ACTION_PREVIOUS_MENU:
self.close()
if action == ACTION_SELECT_ITEM:
sys.stdin.showKeyboard()
I cant remember if sys.stdin.showKeyboard() is safe here... you might want to be careful.

I'll go to see for it. In any case i'm thinking it was for the first trying, but in any case at this point it is yet redirected to the xTerm class, so there would have no problems...

if car == '\n':
self.column_ = 0
else:
self.column_ = self.column_+1
I think you want to increment self.row_ here as well

Yeah, it's true... I need to put all the routine to add a row (the code to remove it too), so i have created a CR (CarriadgeReturn) function and call it from both sides. Thanks a lot!!!

P.D.: I hate when the forum rules don't allow me to put as smileis as i want...

Nuka1195
2007-05-02, 00:29
add
self.show()
to __init__()

El Piranna
2007-05-02, 02:35
add

to __init__()

Hooray, we g0t r00t!!! :D

Yes, self.show() gave me an error on python scripts emulator but it worked on my XBox :) I have got a little problem with label width showing me three dots (curious: a high demanded solution for others is a problem for me :p), i'll go now to get actual resolution and put it on label size, and maybe i'll think a routine to get an adaptative console size (a constant wide font would be really fine for this...).

Now, the important question is... what commands do you want on it? :p Remember that i have designed xTerm for "theorically" could be able to run terminal programs without adapt them (if you look inside PySH code you wouldn't found any reference to xTerm or xTTY or any "strange" import line...). Maybe add a "real" shell as "authentic" PySH (http://pysh.sourceforge.net/), PyShell (http://pyshell.sourceforge.net/) or IPhyton (http://ipython.scipy.org/moin/About)? I think the best would be a custom made shell fully integrated on XBMC and add some interesting ideas from those others (i will canibalize original PySH since is almost dead but is who has most common ideas with what i want: almost only a bash compatible shell made in python...)

El Piranna
2007-05-02, 05:01
Well, as promised, here is the code :)

PySH
import xbmc, os, string
#import xTerm

def walk_visit(arg, dirname, names):
for i in names:
print(dirname+i+'\n')

class clsShell:
PATH = os.getcwd().replace(";","")


def cat(self, parameters):
if parameters == []:
pass
else:
for location in parameters:
file = open(location, 'r')
for line in file.readlines():
print(line[0:-1])
file.close()

def cd(self, parameters):
if parameters == []:
pass
else:
self.PATH = parameters[0]
print(self.PATH)

def clear(self):
for i in range(0,100):
print('\n')

def cp(self, parameters):
pass

def df(self, parameters):
pass

def du(self, parameters):
pass

def echo(self, parameters):
pass

def eject(self, parameters):
pass

def find(self, parameters):
if parameters == []:
parameters = self.PATH
os.walk(parameters, walk_visit, None)

def help(self):
print('')

def less(self, parameters):
pass

def ln(self, parameters):
pass

def ls(self, parameters):
if parameters == []:
print(os.listdir(self.PATH))
else:
for directory in parameters:
print(os.listdir(directory))

def mkdir(self, parameters):
pass

def more(self, parameters):
pass

def mv(self, parameters):
pass

def ping(self, parameters):
pass

def rm(self, parameters):
pass

def rmdir(self, parameters):
pass

def set(self, parameters):
if parameters == []:
for i in os.environ:
print i,'=', os.environ[i]
else:
pass

def tty(self, parameters):
pass

def vi(self, parameters): # Someone would kill for this... :-P
pass

def wget(self, parameters):
import urllib
urllib.urlretrieve(parameters, basename(parameters))


def getPrompt(self):
return self.PATH + '> '


def __init__(self):
print("PySH running on top of %s" %os.name)
while 1:
orden = []
while orden == []:
orden = string.split(string.strip(raw_input(self.getPrompt ())))
command = orden[0]
parameters = orden[1:]

if command == 'exit':
break

elif command == 'cputemp':
print(xbmc.getCpuTemp())
elif command == 'dashboard':
xbmc.dashboard()
elif command == 'dvdstate':
print(xbmc.getDVDState())
elif command == 'free':
print(xbmc.getFreeMem())
elif command == 'ifconfig':
print(xbmc.getIPAddress())
elif command == 'locale':
print(xbmc.getLanguage())
elif command == 'restart':
xbmc.restart()
elif command == 'shutdown':
xbmc.shutdown()
elif command == 'skin':
print(xbmc.getSkinDir())

elif command == 'cat':
self.cat(parameters)
elif command == 'cd':
self.cd(parameters)
elif command == 'clear':
self.clear()
elif command == 'cp':
self.cp(parameters)
elif command == 'find':
self.find(parameters)
elif command == 'ls':
self.ls(parameters)
elif command == 'set':
self.set(parameters)
elif command == 'wget':
self.wget(parameters)

else:
print("Comando %s no encontrado" %command)


pysh = clsShell()
del pysh

xTerm
# xTerm - Xbox Terminal
# virtual console system for Xbox console system
# it's said, a recursive console ;-)

import xbmc, xbmcgui, sys, os

try: Emulating = xbmcgui.Emulating
except: Emulating = False

COLUMNS = 75
ROWS = 24

ROOT = os.getcwd().replace(";","")+os.sep

#get actioncodes from keymap.xml
ACTION_SELECT_ITEM = 7
ACTION_PREVIOUS_MENU = 10


class clsInput:
# def read(self):
# sys.stdout.write('read')
# keyboard = xbmc.Keyboard('')
# keyboard.doModal()
# if (keyboard.isConfirmed()):
# return(keyboard.getText())

def readline(self):
keyboard = xbmc.Keyboard('')
keyboard.doModal()
if (keyboard.isConfirmed()):
text = keyboard.getText()
print text
return(text)
else:
pass # [BUG] It should wait with no keyboard until user action to let see comands answers

def showKeyboard(self):
teclado = 1
while teclado:
sys.stdout.write(self.readline())
# keyboard = xbmc.Keyboard('')
# keyboard.doModal()
# if (keyboard.isConfirmed()):
# sys.stdout.write(keyboard.getText())
# else:
# teclado = 0


class clsXTerm(xbmcgui.Window):
buffer_ = ''
column_ = 0
row_ = 0


def CR(self):
self.column_ = 0
self.row_ = self.row_ +1
if self.row_ >= ROWS:
self.buffer_ = self.buffer_[COLUMNS:]
self.row_ = self.row_ -1

def write(self, string):
if string != []:
for car in string:
self.buffer_ = self.buffer_ + car
if car == '\n':
self.CR()
else:
self.column_ = self.column_+1
if self.column_ >= COLUMNS:
self.buffer_ = self.buffer_ + '\n'
self.CR()
self.output.setLabel(self.buffer_)


def __init__(self):
if Emulating: xbmcgui.Window.__init__(self)

self.output = xbmcgui.ControlLabel(0, 0, self.getWidth(), self.getHeight(), '', 'font13', '0xFF00FF00')
self.addControl(self.output)
self.output.setLabel('')

if not Emulating: self.show()

def onAction(self, action):
if action == ACTION_PREVIOUS_MENU:
self.close()
if action == ACTION_SELECT_ITEM:
sys.stdin.showKeyboard()


def launchShell():
file = open(ROOT+'sh.conf', 'r')
for line in file.readlines():
SHELL = line[0:-1]
file.close()
exec "import " + SHELL[0:-2]


resStdin = sys.stdin # Store the original stdin, stdout and stderr...
resStdout = sys.stdout
resStderr = sys.stderr

sys.stdin = clsInput() # ...and redirect them to that program :-D
sys.stdout = clsXTerm()
#sys.stderr = open('error.log', 'a')

if __name__ == '__main__': # Execute this ONLY if xTerm app is launch directly:
launchShell()

sys.stdin = resStdin # Restore the stdin, stdout and stderr to its originals ("The Good Way" TM ;-) )
sys.stdout = resStdout
sys.stderr = resStderr

I have found a little bug on xTerm on XBMC (not on python emulator) when you send it a lot of data (i have it with "set" command to show all system variables) making that don't make return carriages, goes out by the right and you can see more info. I really don't know why it happens... :no:

Oh, and... err... there is defined a monospace font on XBMC or i have to include it with the terminal? Fosphore green console is not enough 633|<... :p