Home | Reviews | GUIpedia | Forum | Fun500
pharoah | New version of PQMLI've released a new version of PQML, revision Charlie. It has a much more robust URL opening system that is able to follow HTTP redirects and doesn't die when it can't find a page. A nasty bug in the text embed functionality was fixed, manual support for the refresh tag was added, and there is now a primitive history system that lets you visit previous pages. I put together a little site for PQML at http://pharoah.xetaspace.net/pqml that is optimized for display using monochrome, low ASCII characters. Here's the code: [code] #!/usr/bin/env python # # pqml.py - Revision Charlie # # Copyright 2010 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 FancyURLopener class urlOpen(FancyURLopener): version = 'PQML/Charlie' 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 = [] autoRefresh = "" 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 ==""": data = urlopen(line[1:]).read().splitlines() if len(data) > 0: disp[len(disp)-1] += data[0] if len(data) > 1: disp.extend(data[1:]) elif tag == "~": autoRefresh = line[1:] final = [] for line in disp: if len(line) > 80: final.extend(wrap([line])) else: final.append(line) return (final,links,forms,downloads,autoRefresh) 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 global history try: pagedata = urlopen(url).read() except: raw_input("There was a problem loading the page. Press [ENTER] to be taken back.") return history.pop() history.append(url) (display, hyperlinks, forms, downloads, autoRefresh) = render(pagedata) page(display, 1) curpage = 1 if autoRefresh != "": print "~" * 80 print "This page automatically refreshes to:" print autoRefresh print "To accept this redirect, use the 'a' command." 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 " a - Accept auto refresh" print " p - Visit previous location" 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" print " q - Quit PQML" 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) elif cmd == "p": if len(history) > 1: history.pop() return history.pop() else: print "No pages in history." elif cmd == "a": if autoRefresh != "": return autoRefresh else: print "No automatic redirect set." return "" url = "http://pharoah.xetaspace.net/pqml/start.qml" history = [] while True: url = interface(url) if url == "": break [/code] | 2010-01-23 | 12:09 AM |
2021 Brandon Cornell