Guest User

Scripts

a guest
Apr 29th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | None | 0 0
  1. import subprocess
  2. import json
  3. import os
  4.  
  5. # All scripts must be in the same directory as the ui directory, as they need to search through it.
  6. #
  7. # First script:
  8. #
  9. # This script takes each key from the English json and searches for that snippet in all the JS, VUE and HTML files in
  10. # the UI folder, saving in another file all the keys that are used.
  11.  
  12. walk_dir = 'ui'
  13. keyArray = []
  14. with open('./keylist.txt', 'w') as keylist:
  15.     for root, subdirs, files in os.walk(walk_dir):
  16.         for filename in files:
  17.             if filename.endswith('.js') or filename.endswith('.vue') or filename.endswith('html'):
  18.                 file_path = os.path.join(root, filename)
  19.                 print(file_path)
  20.                 with open(file_path, 'r') as f:
  21.                     with open("ui/public/locales/en.json", 'r') as j:
  22.                         en = json.loads(j.read())
  23.                         for key, value in en.items():
  24.                             f.seek(0)
  25.                             for line in f:
  26.                                 if key.lstrip('label.') in line:
  27.                                     if key not in keyArray:
  28.                                         keyArray.append(key)
  29.                                         keylist.write(key)
  30.                                         keylist.write(' ')
  31.  
  32. # Second script:
  33. #
  34. # This script takes all the keys in en.json that aren't in the first script's output and saves them
  35. # to another file. Which therefore contains all the keys that are not being used according to the first script.
  36.  
  37. with open('./keylist.txt', 'r') as keylist:
  38.     with open('./unused.txt', 'w') as unused:
  39.         with open("ui/public/locales/en.json", 'r') as j:
  40.             en = json.loads(j.read())
  41.             for key in en.keys():
  42.                 keylist.seek(0)
  43.                 if key not in keylist.readline().split(' '):
  44.                     unused.write(key)
  45.                     unused.write(' ')
  46.  
  47. # Third script:
  48. #
  49. # This script was made to ensure that all the keys discovered in the second script were not really being used,
  50. # for that purpose it takes each of the keys discovered in the second script and calls grep -r -l -i '<key>',
  51. # saving in another file all the results.
  52.  
  53. with open('./unused.txt', 'r') as un:
  54.     with open('./verifyManually.txt', 'w') as ver:
  55.         for key in un.readline().split(' '):
  56.             result = subprocess.getoutput("grep -r -l -i {keyvalue}".format(keyvalue=key))
  57.             print('\n', file=ver)
  58.             print(key, file=ver)
  59.  
  60.             for line in result.split('\n'):
  61.                 if (not line.endswith('.json')) and (not line.endswith('.txt')):
  62.                     print(line, file=ver)
  63.  
  64.             if 'label.' in key:
  65.                 result = subprocess.getoutput("grep -r -l -i {keyvalue}".format(keyvalue=key.lstrip('label.')))
  66.  
  67.                 for line in result.split('\n'):
  68.                     if (not line.endswith('.json')) and (not line.endswith('.txt')):
  69.                         print(line, file=ver)
  70.  
  71. # Fourth script:
  72. #
  73. # In order for this one to work, there needs to be a manual step between this and the third script.
  74. # One must search through the verifyManually.txt file and delete all the labels that have any result from the "grep
  75. # -r -l -i" alongside it's results, and then use a tool to remove all linebreaks and paragraphs from the file so
  76. # that all the keys are in one line, and separated by one whitespace. This is the site I used:
  77. # https://www.textfixer.com/tools/remove-line-breaks.php
  78.  
  79. with open('./verifyManually.txt', 'r') as f:
  80.     with open('ui/public/locales/en.json', encoding='utf8') as j:
  81.         data = json.load(j)
  82.         for label in f.readline().split(' '):
  83.             for element in list(data):
  84.                 if label.__eq__(element):
  85.                     data.pop(element, None)
  86.         with open('./updated-en.json', 'w', encoding='utf8') as nj:
  87.             json.dump(data, nj, indent=0)
  88.  
  89. # Fifth script:
  90. #
  91. # This script simply removes any keys from the other languages' json that weren't in the updated English file.
  92. #
  93. # Ps: in order for this to work properly, one must replace the ui/public/locales/en.json with the updated-en.json
  94. # generated by the last script.
  95.  
  96. walk_dir = 'ui/public/locales'
  97. keyArray = []
  98. for root, subdirs, files in os.walk(walk_dir):
  99.     for filename in files:
  100.         if filename == 'en.json':
  101.             continue
  102.         file_path = os.path.join(root, filename)
  103.         print(file_path)
  104.         with open("ui/public/locales/en.json", 'r') as j:
  105.             en = json.loads(j.read())
  106.             enKeys = en.keys()
  107.             with open(file_path, 'r+') as f:
  108.                 lang = json.loads(f.read())
  109.                 for key, value in list(lang.items()):
  110.                     if key not in enKeys:
  111.                         lang.pop(key, None)
  112.                 f.seek(0)
  113.                 json.dump(lang, f, indent=0)
  114.                 f.truncate()
  115.  
  116. # Final script: Since not all the jsons are encoded in utf8, in order to maintain their original encoding,
  117. # this script was used manually in each of the jsons that had their encodings changed.
  118.  
  119. with open('ui/public/locales/hi.json', 'r+') as f:
  120.     lang = json.loads(f.read())
  121.     f.seek(0)
  122.     json.dump(lang, f, indent=0, ensure_ascii=False)
  123.  
Advertisement
Add Comment
Please, Sign In to add comment