Advertisement
Guest User

Untitled

a guest
Mar 20th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import dircache, os, re, shutil
  2.  
  3. dir1 = 'english/'
  4. dir2 = 'icelandic/'
  5. dest = 'merged/'
  6.  
  7. # Stole this class from here: http://stackoverflow.com/questions/1165352/fast-comparison-between-two-python-dictionary
  8. class DictDiffer(object):
  9.     """
  10.    Calculate the difference between two dictionaries as:
  11.    (1) items added
  12.    (2) items removed
  13.    (3) keys same in both but changed values
  14.    (4) keys same in both and unchanged values
  15.    """
  16.     def __init__(self, current_dict, past_dict):
  17.         self.current_dict, self.past_dict = current_dict, past_dict
  18.         self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
  19.         self.intersect = self.set_current.intersection(self.set_past)
  20.     def added(self):
  21.         return self.set_current - self.intersect
  22.     def removed(self):
  23.         return self.set_past - self.intersect
  24.     def changed(self):
  25.         return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
  26.     def unchanged(self):
  27.         return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
  28.        
  29. # Gets lines from translation files and creates a dict
  30. def getLines(file):
  31.     fh = open(file, 'r')
  32.     lines = {}
  33.     for l in fh.readlines():
  34.         if l[:1] != '$':
  35.             continue
  36.        
  37.         try:
  38.             v = re.match("\$_\['(.*)'\].* '(.*?)';", l).groups()
  39.             lines[v[0]] = v[1]
  40.         except:
  41.             continue
  42.     fh.close()
  43.     return lines
  44.  
  45. # Merge the contents of the files and return. file2 items will prevail if items exist in both files.
  46. def mergeFiles(file1, file2):
  47.     lines1, lines2 = getLines(file1), getLines(file2)
  48.    
  49.     merged = dict(lines1.items() + lines2.items())
  50.    
  51.     dd = DictDiffer(lines1, lines2)
  52.     # Remove lines that are no longer in the new translation
  53.     for k in dd.removed():
  54.         merged.pop(k)
  55.    
  56.     if len(dd.added()) > 0:
  57.         print "New lines: %s" % dd.added()
  58.    
  59.     return merged
  60.  
  61. # Creates a new language file
  62. def writeFile(filename, contents):
  63.     lines = ["<?php\n"]
  64.    
  65.     for k in contents.keys():
  66.         lines.append("$_['%s'] = '%s';\n" % (k, contents[k]))
  67.    
  68.     lines.append("?>\n")
  69.    
  70.     fh = open(filename, 'w')
  71.     fh.writelines(lines)
  72.     fh.close()
  73.  
  74. # Duplicate the directory tree
  75. shutil.copytree(dir1, dest)
  76. for f in dircache.listdir(dir1):
  77.     if os.path.isdir(dir1+f):
  78.         # Go through subdirectories
  79.         for file in dircache.listdir(dir1 + f):
  80.             filename = str(f) + '/' + str(file)
  81.             print filename
  82.            
  83.             if not os.path.exists(dir2 + filename):
  84.                 # Need to copy dir1 file to dest
  85.                 shutil.copyfile(dir1 + filename, dest + filename)
  86.             else:
  87.                 # Compare contents (translation strings)
  88.                 merged = mergeFiles(dir1 + filename, dir2 + filename)
  89.                 writeFile(dest+filename, merged)
  90.                
  91.     else:
  92.         # Is a file, probably <language>.php in the root folder.
  93.         merged = mergeFiles(dir1 + f, dir2 + dir2[:-1] + '.php')
  94.         writeFile(dest+dir2[:-1] + '.php',merged)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement