]> scripts.mit.edu Git - wizard.git/commitdiff
Refactor code to make regular expression reuse easier.
authorEdward Z. Yang <ezyang@mit.edu>
Thu, 30 Jul 2009 20:35:55 +0000 (16:35 -0400)
committerEdward Z. Yang <ezyang@mit.edu>
Thu, 30 Jul 2009 20:35:55 +0000 (16:35 -0400)
Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
wizard/app/__init__.py
wizard/app/mediawiki.py
wizard/app/php.py

index a4a6ca21f04980b3e5176d284d9aa2f52fea31a6..1e924335a5c2f2cad31f95ea66eac44ce3b4a2fa 100644 (file)
@@ -6,6 +6,10 @@ def filename_regex_extractor(f):
     that takes a name and returns another function (the actual extractor)
     which takes a deployment and returns the value of the extracted variable.
 
+    The regular expression requires a very specific form, essentially ()()()
+    (with the second subgroup being the value we care about), so that we can
+    reuse the regex for other things.
+
     Its Haskell-style type signature would be:
         (String -> (Filename, Regex)) -> (String -> (Deployment -> String))
 
@@ -16,8 +20,7 @@ def filename_regex_extractor(f):
             contents = deployment.read(file) # cached
             match = regex.search(contents)
             if not match: return None
-            # assumes that the first match is the one we want. Hopefully
-            # our regexes can be clever enough to make this work
-            return match.group(1)
+            # assumes that the second match is the one we want.
+            return match.group(2)
         return h
     return g
index 7ae758373be07dbdb314107c320d76b4dd0cef09..15d3e429723e7e838893197aeeba08b0c22aa44f 100644 (file)
@@ -3,9 +3,10 @@ import re
 from wizard import app, deploy, util
 from wizard.app import php
 
-@app.filename_regex_extractor
-def make_extractor(var):
-    return 'LocalSettings.php', re.compile('^\$' + re.escape(var) + r'''\s*=\s*((["\']).*\2);$''', re.M)
+def make_filename_regex(var):
+    return 'LocalSettings.php', re.compile('^\$(' + re.escape(var) + r'''\s*=\s*)(.*)(;)$''', re.M)
+
+make_extractor = app.filename_regex_extractor(make_filename_regex)
 
 class Application(deploy.Application):
     @property
index 9093edf1931772450354c1dbd9e13e7a5c795698..5eb5130adbb74cc4b230b29f719d79750442966d 100644 (file)
@@ -2,9 +2,10 @@ import re
 
 from wizard import app, util
 
-@app.filename_regex_extractor
-def make_extractor(var):
-    return 'php.ini', re.compile('^' + re.escape(var) + r'\s*=\s*(.*)$', re.M)
+def make_filename_regex(var):
+    return 'php.ini', re.compile('^(' + re.escape(var) + r'\s*=\s*)(.*)()$', re.M)
+
+make_extractor = app.filename_regex_extractor(make_filename_regex)
 
 extractors = util.dictmap(make_extractor,
         {'WIZARD_SESSIONNAME': 'session.name'