An alternative to sufreplace

Adriaan de Groot <adridg-FlD2LfDziEhmR6Xm/[email protected]>
Newsgroups gmane.comp.tools.aap.devel
Message-ID <[email protected]>
The attached patch adds suffixmangle() to the library of AAP-specific Python 
functions. It acts like a kind of list-lifted sufreplace() - you give it a 
list of replacements and it does them. The calling form is

	suffixmangle(recdict,variablename,replacementlist)

typically, recdict will be _recipe when calling suffixmangle. variablename is 
the _name_ of a variable - not it's value. So use "source", not source, when 
calling this. Actually, I don't even know if "source" is in there - use 
"SOURCES", or whatever you named the variable that stores the list of 
interesting files, instead. replacementlist is a list of pairs (suffix,new 
suffix), and if a suffix matches, that suffix is replaced by the new suffix. 
The first match wins.

Possible use is, say, to deal with "fake sources", or to change some filenames 
on the way:

	suffixmangle(_recipe,"SOURCES",[ (".y","_"+OSNAME+".y") ])

this would replace all .y by _OSNAME.y in your list of sources, which would 
let you list one source name but have it replaced at AAP runtime by another.

A previous version I mentioned removed attributes. As Bram pointed out, using 
var2dictlist fixes that. This version preserves attributes.

-- 
pub  1024D/FEA2A3FE 2002-06-18 Adriaan de Groot <[email protected]>
     Key fingerprint = 934E 31AA 80A7 723F 54F9  50ED 76AC EE01 FEA2 A3FE
aap-suffix.diff (text/x-diff, 844 B)
--- RecPython.py.orig	Tue Aug 12 21:33:05 2003
+++ RecPython.py	Wed Aug 13 13:14:30 2003
@@ -100,6 +100,22 @@
         recipe_error([], _('sufreplace() failed: ') + str(e))
         return None     # not reached
 
+def suffixmangle(recdict,variable,replacelist=[]):
+    """Replace suffixes in replacelist.
+       Variable must be the name of a variable (ie. "SOURCES").
+       Replacelist must be a list of pairs (suffix,replacement)."""
+    r=[]
+    # Look up variable and get its entries with no attributes as a list.
+    dl = var2dictlist(recdict[variable])
+    for i in dl:
+	if not i.has_key("name"):
+	    continue
+	rsrc=i["name"]
+	for j in replacelist:
+	    if rsrc.endswith(j[0]):
+		i["name"]=rsrc[:-len(j[0])] + j[1]
+		break
+    return dictlist2str(dl)
 
 def aap_abspath(var):
     """Obsolete name for var_abspath()."""
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.