Konstantine Rybnikov avatar Konstantine Rybnikov committed a424594

add filter_files

Comments (0)

Files changed (2)

             else:
                 self.files.append(file_)
 
+    def __contains__(self, filename):
+        return filename in self.files
+
 class HgFileMerge(object):
     
     def __init__(self):
                 m_group = MergeGroup()
                 m_group.name = group['name']
                 m_group.prefix = group.get('prefix', '')
-                m_group.set_files(group['files'])
+                m_group.set_files(group['files'], base_path=self.base_path)
                 self.merge_groups[m_group.name] = m_group
     
     def configure_from_file(self, path):
         import codecs
         
         ret_val = []
-        for filename in self.merge_groups[group_name]:
+        for filename in self.merge_groups[group_name].files:
             with codecs.open(os.path.join(self.base_path, filename), 'r', 'utf-8') as f:
                 ret_val += f.readlines()
         return ret_val
                                  self.merge_groups[group_name].name)),
                 'w', 'utf-8')) as f:
             f.writelines(lines_to_save)
+
+    def get_group_by_filename(self, filename):
+        """Returns None if there's no group for that file
+        """
+
+        for group_name, group in self.merge_groups.iteritems():
+            if filename in group:
+                return group
+        return None
+    
+    def filter_files(self, filenames):
+        """Takes files list and returns list with files in group replaced by
+        group name. Useful for css/js insertion.
+        """
+        ret_val = []
+        groups_taken = set()
+        for file_ in filenames:
+            group = self.get_group_by_filename(file_)
+            if group is not None:
+                if group.name not in groups_taken:
+                    ret_val.append(group.name)
+                    groups_taken.add(group.name)
+            else:
+                ret_val.append(file_)
+        return ret_val

tests/test_main.py

                          'tests/testfiles/test_yml_config_parser/')
     
     def test_simple_yml_merge_groups(self):
-        self.assertEqual(self.merger.merge_groups,
-                         {'merged_most_popular.css': ['1.css', '2.css',],
-                          'merged_site.css': ['3.css',]})
+        self.assertEqual(self.merger.merge_groups['merged_most_popular.css'].files,
+                         ['1.css', '2.css',])
 
 class TestSimple(unittest.TestCase):
     def setUp(self):
                         'tests/testfiles/test_yml_config_parser/3.css',
                         shallow=True),
             True)
+
+    def test_filter_files(self):
+        self.assertEqual(
+            self.merger.filter_files(['1.css', '2.css', '5.css']),
+            ['merged_most_popular.css', '5.css'])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.