changeset 25167:6f7048cc2419

ignore: move readpatternfile to match.py In preparation for adding 'include:' rule support to match.py, let's move the pattern file reader function to match.py
author Durham Goode <durham@fb.com>
date Sat, 16 May 2015 15:46:54 -0700
parents 7f53f305d4a6
children 4dfd4d3b9b93
files mercurial/ignore.py mercurial/match.py
diffstat 2 files changed, 47 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/ignore.py	Sat May 16 15:45:46 2015 -0700
+++ b/mercurial/ignore.py	Sat May 16 15:46:54 2015 -0700
@@ -7,54 +7,6 @@
 
 from i18n import _
 import util, match
-import re
-
-_commentre = None
-
-def readpatternfile(filepath, warn):
-    '''parse a pattern file, returning a list of
-    patterns. These patterns should be given to compile()
-    to be validated and converted into a match function.'''
-    syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
-    syntax = 'relre:'
-    patterns = []
-
-    fp = open(filepath)
-    for line in fp:
-        if "#" in line:
-            global _commentre
-            if not _commentre:
-                _commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
-            # remove comments prefixed by an even number of escapes
-            line = _commentre.sub(r'\1', line)
-            # fixup properly escaped comments that survived the above
-            line = line.replace("\\#", "#")
-        line = line.rstrip()
-        if not line:
-            continue
-
-        if line.startswith('syntax:'):
-            s = line[7:].strip()
-            try:
-                syntax = syntaxes[s]
-            except KeyError:
-                warn(_("%s: ignoring invalid syntax '%s'\n") % (filepath, s))
-            continue
-
-        linesyntax = syntax
-        for s, rels in syntaxes.iteritems():
-            if line.startswith(rels):
-                linesyntax = rels
-                line = line[len(rels):]
-                break
-            elif line.startswith(s+':'):
-                linesyntax = rels
-                line = line[len(s) + 1:]
-                break
-        patterns.append(linesyntax + line)
-
-    fp.close()
-    return patterns
 
 def readpats(root, files, warn):
     '''return a dict mapping ignore-file-name to list-of-patterns'''
@@ -64,7 +16,7 @@
         if f in pats:
             continue
         try:
-            pats[f] = readpatternfile(f, warn)
+            pats[f] = match.readpatternfile(f, warn)
         except IOError, inst:
             warn(_("skipping unreadable ignore file '%s': %s\n") %
                  (f, inst.strerror))
--- a/mercurial/match.py	Sat May 16 15:45:46 2015 -0700
+++ b/mercurial/match.py	Sat May 16 15:46:54 2015 -0700
@@ -498,3 +498,49 @@
     for kind, pat in kindpats:
         if kind in ('glob', 're', 'relglob', 'relre', 'set'):
             return True
+
+_commentre = None
+
+def readpatternfile(filepath, warn):
+    '''parse a pattern file, returning a list of
+    patterns. These patterns should be given to compile()
+    to be validated and converted into a match function.'''
+    syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
+    syntax = 'relre:'
+    patterns = []
+
+    fp = open(filepath)
+    for line in fp:
+        if "#" in line:
+            global _commentre
+            if not _commentre:
+                _commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
+            # remove comments prefixed by an even number of escapes
+            line = _commentre.sub(r'\1', line)
+            # fixup properly escaped comments that survived the above
+            line = line.replace("\\#", "#")
+        line = line.rstrip()
+        if not line:
+            continue
+
+        if line.startswith('syntax:'):
+            s = line[7:].strip()
+            try:
+                syntax = syntaxes[s]
+            except KeyError:
+                warn(_("%s: ignoring invalid syntax '%s'\n") % (filepath, s))
+            continue
+
+        linesyntax = syntax
+        for s, rels in syntaxes.iteritems():
+            if line.startswith(rels):
+                linesyntax = rels
+                line = line[len(rels):]
+                break
+            elif line.startswith(s+':'):
+                linesyntax = rels
+                line = line[len(s) + 1:]
+                break
+        patterns.append(linesyntax + line)
+    fp.close()
+    return patterns