Commits

Konstantine Rybnikov  committed d119137

make groups as classes

  • Participants
  • Parent commits c2eab67

Comments (0)

Files changed (1)

 log = logging.getLogger(__name__)
 log.setLevel(logging.INFO)
 
-class HgFileMerge():
+class MergeGroup(object):
+    def __init__(self):
+        self.name = ''
+        self.files = []
+        self.prefix = ''
+    
+    def get_files(self):
+        return self.files
+    
+    def set_files(self, possible_files, base_path='.'):
+        for file_ in possible_files:
+            file_path = os.path.join(base_path, file_)
+            if not os.path.exists(file_path):
+                log.warning(u'File %s does not exist' % file_path)
+            elif not os.path.isfile(file_path):
+                log.warning(u'File %s is not a file (directory maybe?)' % file_path)
+            else:
+                self.files.append(file_)
+
+class HgFileMerge(object):
     
     def __init__(self):
         self.base_path = '.'
             self.base_path = d['base_path']
         if 'merge_groups' in d:
             for group in d['merge_groups']:
-                valid_group = {}
-                valid_group['name'] = group['name']
-                valid_group['files'] = []
-                for file_ in group['files']:
-                    file_path = os.path.join(self.base_path, file_)
-                    if not os.path.exists(file_path):
-                        log.warning(u'File %s does not exist' % file_path)
-                    elif not os.path.isfile(file_path):
-                        log.warning(u'File %s is not a file (directory maybe?)' % file_path)
-                    else:
-                        valid_group['files'].append(file_)
-                self.merge_groups[valid_group['name']] = valid_group['files']
+                m_group = MergeGroup()
+                m_group.name = group['name']
+                m_group.prefix = group.get('prefix', '')
+                m_group.set_files(group['files'])
+                self.merge_groups[m_group.name] = m_group
     
     def configure_from_file(self, path):
         import codecs
 
     def save(self, group_name=None):
         import codecs
-
+        
         if group_name is None:
             for group_name in self.get_merge_groups():
                 self.save(group_name)
         
         lines_to_save = self.get_merged_lines(group_name)
-        with codecs.open(os.path.join(self.base_path, group_name), 'w', 'utf-8') as f:
+        with (codecs.open(
+                os.path.join(
+                    self.base_path,
+                    os.path.join(self.merge_groups[group_name].prefix,
+                                 self.merge_groups[group_name].name)),
+                'w', 'utf-8')) as f:
             f.writelines(lines_to_save)