diff options
author | jerous <jerous@gmail.com> | 2008-06-12 21:09:01 +0200 |
---|---|---|
committer | jerous <jerous@gmail.com> | 2008-06-12 21:09:01 +0200 |
commit | 4220e4a383311583ea82de5719d69301d4f875b1 (patch) | |
tree | 5d8fd41d9df1fd854f425c01024ea16e7fc864ed /misc.py | |
parent | 6817bce6a1600c053a4a7789c2f17f31435dfd87 (diff) |
conditional in formatter
Diffstat (limited to 'misc.py')
-rw-r--r-- | misc.py | 38 |
1 files changed, 37 insertions, 1 deletions
@@ -1,4 +1,5 @@ from PyQt4 import QtCore, QtGui +import re appIcon=QtGui.QIcon('gfx/icon.png') @@ -23,7 +24,6 @@ def format(string, song=None, xtra_tags={}): """Replace all tags in $str with their respective value.""" # what tags are available? if song: - print song tags=song._data else: tags={} @@ -31,6 +31,42 @@ def format(string, song=None, xtra_tags={}): tags[tag]=xtra_tags[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 |