Commits

Alexander Solovyov committed 99c22e8

be more accurate with files

  • Participants
  • Parent commits 6bd7115

Comments (0)

Files changed (1)

  • +12 -8
    sr
     eliminate = config.eliminate and config.eliminate.search(line) or False
     return config.search.search(line) and not eliminate
 
-def search(path):
+def search(path, f):
     """
     Search pattern in each line of file.
     """
 
     started = False
 
-    for count, line in enumerate(file(path)):
+    for count, line in enumerate(f):
         number = count + 1
         if search_line(line):
             if not started:
         print
 
 
-def search_replace(path):
+def search_replace(path, f):
     """
     Search and replace patterns in file.
 
         if not os.path.exists(tmp_path):
             break
 
-    data = file(path).read()
+    data = f.read()
 
     if search_line(data):
         print path
     TODO: write docstring.
     """
 
-    if config.mode == 'search':
-        return search(path)
-    elif config.mode == 'replace':
-        return search_replace(path)
+    try:
+        with file(path) as f:
+            if config.mode == 'search':
+                return search(path, f)
+            elif config.mode == 'replace':
+                return search_replace(path, f)
+    except IOError, e:
+        print 'Error: %s' % e
 
 
 def walk_files():