Home | Reviews | GUIpedia | Forum | Fun500


pharoahPython QML Reader
For those of you who haven't been reading this forum much lately, I've been working on a little QML reader in Python. The result is pretty limited because it uses only standard libraries in an effort to be cross compatible, so it does not display colors and probably won't display special characters either. Still, it's good enough that Brandon can participate in the chat from his Mac now (although there is no refresh, not my fault but Python's). To run this code, save it as pqml.py and then open up your terminal, find the correct directory, and type: python pqml.py It should run without any need to install python on most Linux and Mac machines. [code] #!/usr/bin/env python # # pqml.py # # Copyright 2009 Pharoah # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. pagesize = 20 import urllib def tab_strip(string): if string != "": if string[0] == chr(9): return tab_strip(string[1:]) else: return string else: return "" def wrap(line): temp = line.pop() line.append(temp[:80]) line.append(temp[80:]) if len(line[len(line)-1]) > 80: return wrap(line) else: return line def render(page): disp = [""] links = [] forms = [] inputs = {} downloads = [] for line in page.splitlines(): line = tab_strip(line) if line != "": tag = line[0] if tag == "$": disp[len(disp)-1] = disp[len(disp)-1] + line[1:] elif tag == "|": for i in range(0, len(line.strip())): disp.append("") elif tag == "-": disp.append(line[1:].center(80)) disp.append("") elif tag == "=": disp.append("="*80) disp.append("") elif tag == "_": (title, url) = line[1:].split(">") links.append(url) disp[len(disp)-1] = disp[len(disp)-1] + title +"("+str(len(links))+")" elif tag == "[": (title, url) = line[1:].split(">") formtarget = url inputs = {} elif tag == "?": inputs[line[1:]] = "" disp[len(disp)-1] = disp[len(disp)-1] + "["+str(len(forms)+1)+"-"+line[1:]+"]" elif tag == "]": disp[len(disp)-1] = disp[len(disp)-1] + "[submit "+str(len(forms)+1)+"]" forms.append({'target': formtarget,'input': inputs,'append': line[1:]}) elif tag == "^": (title, url) = line[1:].split(">") downloads.append(url) disp[len(disp)-1] = disp[len(disp)-1] + title +"{"+str(len(downloads))+"}" final = [] for line in disp: if len(line) > 80: final.extend(wrap([line])) else: final.append(line) return (final,links,forms, downloads) def page(lines, num): global pagesize print "*"*80 print ("Page "+str(num)).center(80) print "*"*80 num = (num - 1) * pagesize for n in range(num, num+pagesize): if n >= len(lines): break print lines[n] def interface(url): global pagesize (display, hyperlinks, forms, downloads) = render(urllib.urlopen(url).read()) page(display, 1) curpage = 1 cmd = "dummy" while cmd != "q": cmds = raw_input("> ").split() if cmds != "": cmd = cmds[0] if cmd == "g": return cmds[1] elif cmd == "l": return hyperlinks[int(cmds[1])-1] elif cmd == "d": num = int(cmds[1]-1) print "You have elected to download the file at: " print downloads[num] print "Enter destination file, or press [RETURN] to cancel:" temp = raw_input() if temp != "": urllib.urlretreive(downloads[num], temp) elif cmd == "e": (num,name) = cmds[1].split("-") if name in forms[int(num) - 1]['input']: forms[int(num) - 1]['input'][name] = raw_input() else: print cmds[1]+" not found." elif cmd == "p": (num,name) = cmds[1].split("-") if name in forms[int(num) - 1]['input']: print forms[int(num) - 1]['input'][name] else: print cmds[1]+" not found." elif cmd == "s": num = int(cmds[1]) num -= 1 temp = forms[num]['target']+"?" for (key, val) in (forms[num]['input']).iteritems(): temp = temp + "&" + key + "=" +urllib.quote(val) #NEED TO ADD URL ENCODING HERE temp = temp + forms[num]['append'] return temp elif cmd == "r": return url elif cmd == "f": if curpage * pagesize < len(display): curpage += 1 page(display, curpage) elif cmd == "b": if curpage > 1: curpage -= 1 page(display, curpage) elif cmd == "c": page(display, curpage) return "" url = "http://pharoah.xetaspace.net/qbrowse/chat/join.qml" while True: url = interface(url) if url == "": break # g - Go # l - Link # d - Download # e - Edit # p - Print # s - Submit # r - Reload # f - Page Forward # b - Page Back # c - Current # q - Quit [/code]
2009-03-145:33 PM

BrandonRe:Python QML Reader
brandon-cornells-macbook:Desktop brandoncornell$ python pqml.py File "pqml.py", line 1 !/usr/bin/env python ^ SyntaxError: invalid syntax
2009-03-146:08 PM

pharoahRe:Python QML Reader
That line should be commented out. the # is a comment...
2009-03-146:09 PM

pharoahRe:Python QML Reader
I've revised the code, changed a few things, and fixed some bugs: [code] #!/usr/bin/env python # # pqml.py - Revision Alpha # # Copyright 2009 Pharoah # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. pagesize = 20 import urllib def tab_strip(string): if string != "": if string[0] == chr(9): return tab_strip(string[1:]) else: return string else: return "" def wrap(line): temp = line.pop() line.append(temp[:80]) line.append(temp[80:]) if len(line[len(line)-1]) > 80: return wrap(line) else: return line def render(page): disp = [""] links = [] forms = [] inputs = {} downloads = [] for line in page.splitlines(): line = tab_strip(line) if line != "": tag = line[0] if tag == "$": disp[len(disp)-1] = disp[len(disp)-1] + line[1:] elif tag == "|": for i in range(0, len(line.strip())): disp.append("") elif tag == "-": disp.append(line[1:].center(80)) disp.append("") elif tag == "=": disp.append("="*80) disp.append("") elif tag == "_": (title, url) = line[1:].split(">") links.append(url) disp[len(disp)-1] = disp[len(disp)-1] + title +"("+str(len(links))+")" elif tag == "[": (title, url) = line[1:].split(">") formtarget = url inputs = {} elif tag == "?": inputs[line[1:]] = "" disp[len(disp)-1] = disp[len(disp)-1] + "["+str(len(forms)+1)+"-"+line[1:]+"]" elif tag == "]": disp[len(disp)-1] = disp[len(disp)-1] + "[submit "+str(len(forms)+1)+"]" forms.append({'target': formtarget,'input': inputs,'append': line[1:]}) elif tag == "^": (title, url) = line[1:].split(">") downloads.append(url) disp[len(disp)-1] = disp[len(disp)-1] + title +"{"+str(len(downloads))+"}" final = [] for line in disp: if len(line) > 80: final.extend(wrap([line])) else: final.append(line) return (final,links,forms, downloads) def page(lines, num): global pagesize print "*"*80 print ("Page "+str(num)).center(80) print "*"*80 num = (num - 1) * pagesize for n in range(num, num+pagesize): if n >= len(lines): break print lines[n] def interface(url): global pagesize pagedata = urllib.urlopen(url).read() (display, hyperlinks, forms, downloads) = render(pagedata) page(display, 1) curpage = 1 cmd = "dummy" while cmd != "q": cmds = raw_input("> ") if cmds != "": cmds = cmds.split() cmd = cmds[0] if cmd == "h": print "Command help:" print " r - Reload" print " g - Go to url" print " l - Follow link(num)" print " d - Download link{num}" print " c - Print current page" print " f - Page forward" print " b - Page back" print " e - Edit [num-input]" print " p - Print [num-input] value" print " s - Submit [submit num]" print " v - View QML" print " h - Display this help" if cmd == "v": print pagedata if cmd == "g": return cmds[1] elif cmd == "l": return hyperlinks[int(cmds[1])-1] elif cmd == "d": num = int(cmds[1])-1 print "You have elected to download the file at: " print downloads[num] print "Enter destination file, or press [RETURN] to cancel:" temp = raw_input() if temp != "": urllib.urlretrieve(downloads[num], temp) elif cmd == "e": (num,name) = cmds[1].split("-") if name in forms[int(num) - 1]['input']: forms[int(num) - 1]['input'][name] = raw_input() else: print cmds[1]+" not found." elif cmd == "p": (num,name) = cmds[1].split("-") if name in forms[int(num) - 1]['input']: print forms[int(num) - 1]['input'][name] else: print cmds[1]+" not found." elif cmd == "s": num = int(cmds[1]) num -= 1 temp = forms[num]['target']+"?" for (key, val) in (forms[num]['input']).iteritems(): temp = temp + "&" + key + "=" +urllib.quote(val) #NEED TO ADD URL ENCODING HERE temp = temp + forms[num]['append'] return temp elif cmd == "r": return url elif cmd == "f": if curpage * pagesize < len(display): curpage += 1 page(display, curpage) elif cmd == "b": if curpage > 1: curpage -= 1 page(display, curpage) elif cmd == "c": page(display, curpage) return "" url = "http://pharoah.xetaspace.net/qbrowse/chat/join.qml" while True: url = interface(url) if url == "": break [/code]
2009-03-153:17 PM

Other


2021 Brandon Cornell