diff options
author | jerous <jerous@gmail.com> | 2008-10-31 00:31:08 +0100 |
---|---|---|
committer | jerous <jerous@gmail.com> | 2008-10-31 00:31:08 +0100 |
commit | 30c157d5a16e8fae9e63335d7beaa444e48529ee (patch) | |
tree | 679d7b7f859e0e55cf70d8d9a75fedf8dde135f0 /format.py | |
parent | 581ba6116c5b9ff887fc252b7eea03715a4ea7c1 (diff) |
format has new function: dirname
Diffstat (limited to 'format.py')
-rw-r--r-- | format.py | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -1,4 +1,6 @@ import re +import os + def compile(string): """Compile a string into a function which fills in correct values.""" @@ -20,7 +22,7 @@ def merge(list): ret="%s%s, "%(ret, l) return ret[:-2] -func=re.compile('\$[a-z]+', re.IGNORECASE) +func=re.compile('\$[a-z_]+', re.IGNORECASE) def format_parse_rec(string, start): """Compiles a function. Returns [special str, params].""" match=func.search(string, start) @@ -33,7 +35,8 @@ def format_parse_rec(string, start): ret1+=string[start:match.start()] name=string[match.start()+1:match.end()] - if name in ['if', 'ifn']: + # here are all functions defined + if name in ['if', 'ifn', 'dirname']: try: end=match_bracket(string, string.find("(", match.start())) except: @@ -42,6 +45,9 @@ def format_parse_rec(string, start): raise Exception("No matching brackets") if name=='if' or name=='ifn': + # print if something is true/false + # $if($expr-true, $output) + # $ifn($expr-false, $output) body=string[match.end():end] comma=body.find(',') s1,p1=format_parse_rec(body[1:comma], 0) # get the if-part @@ -51,11 +57,16 @@ def format_parse_rec(string, start): ret2.append("('%s' %% (%s) if (%s) else '')"%(s2, merge(p2), merge(p1))) elif name=='ifn': ret2.append("('%s' %% (%s) if not(%s) else '')"%(s2, merge(p2), merge(p1))) + elif name=='dirname': + # get the dir of a path + # $dirname(path) + s,p=format_parse_rec(string[match.end()+1:end-1], 0) + ret1+="%s" + ret2.append('os.path.dirname("%s"%%(%s))'%(s, merge(p))) else: end=match.end() # variable ret1+="%s" - #ret2.append("params['%s']"%(name)) ret2.append('params["%s"]'%(name)) ret=format_parse_rec(string, end) @@ -103,6 +114,7 @@ def params(song, overwrite={}, ensure={"title":"", "artist":""\ #f=compile("$if($date, ($date) )") #f=compile("a$if($title,$title)b$if($artist,$artist)c") +#f=compile('abc$dirname($title/$artist/$artist$state)ghi') #print f({'state':'playing', 'title':'martha', 'artist':'waits'}) #f=compile("$artist $if($title, titeltje:$artist, $artist $title!) $album") #print f({'artist':'artist', 'title':'title', 'album':'album'}) |