PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/merge-emojis.py

https://bitbucket.org/mrdaemon/irssi-emojis
Python | 98 lines | 68 code | 4 blank | 26 comment | 2 complexity | cf6cf1c5b044c5fb7d68e0f23ff5414e MD5 | raw file
  1. #!/usr/bin/python
  2. '''
  3. File: merge-emojis.py
  4. Author: Oliver Uvman
  5. Description:
  6. Usage: ./merge-emojis.py <OUTPUT> <INPUT> <INPUT> [...]
  7. Reads any number of input files, each is assumed to be
  8. of emoji-db form. Saves all emoji key-values encountered.
  9. When encountering duplicate keys, overwrites previous value
  10. with the value from the latter db-file. Finally appends all
  11. emojis to output file, sorted by value so any duplicate
  12. values appear next to each other.
  13. Any values that would be overwritten due to key conflicts
  14. are appended at the end of the file, with their previous
  15. key slightly mutated to become unique.
  16. Reports on stdout if there are duplicate values or if
  17. any values had their keys changed.
  18. Guarantees that no value is lost, though some might get
  19. new namess
  20. License: Pubic domain :)
  21. '''
  22. import sys
  23. if (len(sys.argv) < 4):
  24. print r'./merge-emojis.py <OUTPUT> <INPUT> <INPUT> [...]'
  25. sys.exit()
  26. output = open(sys.argv[1], 'a')
  27. emojis = {}
  28. overwrittenEmojis = []
  29. for inputFileKey in sys.argv[2:]:
  30. inputFile = open(inputFileKey, 'r')
  31. keyLine = True
  32. emojiKey = ""
  33. for line in inputFile:
  34. thisLine = line.rstrip('\n')
  35. if keyLine == True:
  36. keyLine = False
  37. emojiKey = thisLine
  38. else:
  39. keyLine = True
  40. if (emojiKey in emojis) and emojis[emojiKey] != thisLine:
  41. overwrittenEmojis.append((emojiKey, emojis[emojiKey]))
  42. emojis[emojiKey] = thisLine
  43. # emojis is now a list of unique keys with values.
  44. # A value can exist several times, with different keys.
  45. updatedEmojis = [] # Emojis values that might have lost old keys
  46. deletedEmojis = []
  47. for (key, value) in overwrittenEmojis:
  48. if value in emojis.values():
  49. updatedEmojis.append(value)
  50. else:
  51. deletedEmojis.append((key, value))
  52. busyKeys = emojis.keys()
  53. renamedEmojis = []
  54. renameLog = []
  55. for (key, value) in deletedEmojis:
  56. newKey = key[:-1] + 'X' + key[-1:]
  57. while(newKey in busyKeys):
  58. newKey = newKey[:-1] + 'X' + newKey[-1:]
  59. renamedEmojis.append((newKey, value))
  60. renameLog.append((value, key, newKey))
  61. busyKeys.append(newKey)
  62. emojisByValue = sorted(emojis.items(), key=lambda emo: (emo[1], emo[0]))
  63. for (key, value) in emojisByValue:
  64. output.write(key + '\n')
  65. output.write(value + '\n')
  66. for (key, value) in renamedEmojis:
  67. output.write(key + '\n')
  68. output.write(value + '\n')
  69. if updatedEmojis:
  70. print 'The following emojis have had their keys updated:'
  71. for value in updatedEmojis:
  72. print value, ' is now known as:'
  73. for (key, ivalue) in emojisByValue + renamedEmojis:
  74. if value == ivalue:
  75. print '\t', key
  76. if renamedEmojis:
  77. print 'The following emojis have lost their keys\nbut have new keys at the end of the file:'
  78. for (value, oldKey, newKey) in renameLog:
  79. print value, 'renamed from', oldKey, 'to', newKey