summaryrefslogtreecommitdiff
path: root/misc.py
diff options
context:
space:
mode:
authorjerous <jerous@gmail.com>2008-09-15 20:34:13 +0200
committerjerous <jerous@gmail.com>2008-09-15 20:34:13 +0200
commit6a3859be88453cb981d797e8223cba407471020d (patch)
treeca5ba19d42d9f311113c77d796fac6ed7f6029c6 /misc.py
parent3ce4779bf1e51b6ae47ce89f712b630ea536774c (diff)
big speedup of grouping by compiling format
Diffstat (limited to 'misc.py')
-rw-r--r--misc.py59
1 files changed, 5 insertions, 54 deletions
diff --git a/misc.py b/misc.py
index 329238e..1d8ea00 100644
--- a/misc.py
+++ b/misc.py
@@ -2,6 +2,9 @@ from PyQt4 import QtCore, QtGui
import re
import urllib2, httplib, cookielib
import socket
+
+import format
+
socket.setdefaulttimeout(8)
appIcon=QtGui.QIcon('gfx/icon.png')
@@ -29,62 +32,10 @@ def unique(seq):
seen = []
return t(c for c in seq if not (c in seen or seen.append(c)))
-def format(string, song=None, overwrite={}, ensure={}):
- """Replace all tags in $str with their respective value."""
- # what tags are available?
- tags={}
- for tag in ensure:
- tags[tag]=ensure[tag]
- if song:
- for tag in song._data:
- tags[tag]=song._data[tag]
- for tag in overwrite:
- tags[tag]=overwrite[tag]
-
- ret=string
- # first perform some functions: $name(value)
- func=re.compile('\$[a-z]{2}\(', re.MULTILINE|re.IGNORECASE)
- start=0
- loops=20 # my stupidity is endless, so we better make sure to never always loop!
- while True and loops>0:
- loops-=1
- match=func.search(ret[start:])
- if not match:
- break
- # we've found a match!
- # look for matching parenthesis!
- start+=match.start() # we have to add, because we start search from $start!
- end=ret.find('(', start)+1
- balance=1
- while balance>0 and end<len(ret):
- if ret[end]==')':balance-=1
- if ret[end]=='(':balance+=1
- end+=1 # can do better
- # whole function is in ret[start:end]
- # find function-name
- name=ret[start+1:ret.find('(', start)]
- result=None # result of this function
- if name=='if':
- # $if(param,if-true)
- comma=ret.find(',',start)
- param1=ret[start+len('$if('):comma]
- param2=ret[comma+1:end-1]
- result=''
- if param1[1:] in tags:
- result=param2
- else:
- start+=1
- if result!=None:
- ret=("%s%s%s")%(ret[0:start], result, ret[end:])
-
- # perform all replacements!
- for tag in tags:
- ret=ret.replace('$%s'%(tag), str(tags[tag]))
- return ret
-
def fetch(SE, sites, song=None, xtra_tags={}):
"""Returns None when nothing found, or [site,source-url]."""
- url=format(SE, song, xtra_tags)
+ f=format.compile(SE)
+ url=f(format.params(song, xtra_tags))
url=url.replace(' ', '+')
request=urllib2.Request(url)