Home | Reviews | GUIpedia | Forum | Fun500


pharoahPQML Revisioin Bravo
Here's a new version of PQML. It adds support for the character and text embed tags, better handling of improper input, and a PQML user agent string: [code] #!/usr/bin/env python # # pqml.py - Revision Bravo # # Copyright 2009 by 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 from urllib import URLopener class urlOpen(URLopener): version = 'PQML/Bravo' urlopen = urlOpen().open urlretrieve = urlOpen().retrieve 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))+"}" elif tag =="c": disp[len(disp)-1] = disp[len(disp)-1] + chr(int(line[1:])) elif tag ==""": disp[len(disp)-1] = disp[len(disp)-1] + urlopen(line[1:]).read() 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 = 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" and len(cmds) == 2: return hyperlinks[int(cmds[1])-1] elif cmd == "d" and len(cmds) == 2: 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 != "": urlretrieve(downloads[num], temp) elif cmd == "e" and len(cmds) == 2: (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" and len(cmds) == 2: (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" and len(cmds) == 2: num = int(cmds[1]) num -= 1 temp = forms[num]['target']+"?" for (key, val) in (forms[num]['input']).iteritems(): temp = temp + "&" + key + "=" +urllib.quote(val) 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-04-018:00 PM

Other


2021 Brandon Cornell