/tools/tldr.py

https://github.com/bighuggies/Spellathon
Python | 131 lines | 57 code | 20 blank | 54 comment | 15 complexity | 346240f683124a18b498df564f475d32 MD5 | raw file
  1. '''
  2. Module containing functions to generate and parse TLDR format files containing
  3. lists of words.
  4. '''
  5. from aid.words import Word, WordList
  6. import datetime
  7. import glob
  8. import os
  9. def parse_tldr(tldrfile, listname = 'default'):
  10. '''Gets each line from a tldr file and parses it into a WordList
  11. instance.
  12. Arguments:
  13. tldrfile -- The tldr file to read from
  14. listname -- The name of the word list to be generated from the tldr
  15. Returns:
  16. Returns the list of all words in the tldr file.
  17. '''
  18. # Get the data from the file all at once. (Should probably change this to
  19. # work line by line).
  20. tldr = open(tldrfile)
  21. data = tldr.readlines()
  22. tldr.close()
  23. wordlist = WordList(listname)
  24. for i, line in enumerate(data):
  25. # Take the first two lines of the filedata and parse them as metadata.
  26. if line[0] == '#':
  27. if i == 0:
  28. wordlist.source = line[1:].strip()
  29. if i == 1:
  30. wordlist.date_edited = line[1:].strip()
  31. elif (line[0] != '#') and (len(line) > 3):
  32. # All other lines which are not comment lines are words, so add the word
  33. # to the wordlist.
  34. word = Word.deserialise(line)
  35. wordlist._add_word(word)
  36. return wordlist
  37. def parse_tldr_files(path):
  38. '''Parse all tldr files in a given path.
  39. Arguments:
  40. path -- The path in which to look for tldr files.
  41. Returns:
  42. Dictionary containing each wordlist where the keys are the names of the
  43. lists.
  44. '''
  45. tldrs = {}
  46. # Get each tldr file in the directory and parse it into the dict of tldr
  47. # files to be returned.
  48. for t in glob.glob(path + '*.tldr'):
  49. name = t.split(os.sep)[-1].split('.')[0]
  50. tldrs[name] = parse_tldr(t, name)
  51. return tldrs
  52. def generate_tldr(wordlist, tldrfile):
  53. '''Generates a TLDR file given a WordList.
  54. Arguments:
  55. wordlist -- The wordlist to place in the file
  56. tldrfile -- The file to place the wordlist in
  57. '''
  58. try:
  59. f = open(tldrfile, 'w')
  60. except IOError:
  61. return False
  62. # Write the metadata to the first three lines of the file.
  63. f.write('#' + wordlist.source + '\n')
  64. f.write('#' + str(datetime.datetime.now()) + '\n')
  65. f.write('#' + str(len(wordlist.words)) + '\n')
  66. keys = []
  67. # Sort the keys so that we can access the wordlist in proper alphabetical
  68. # order. That is, ignoring capitalisation.
  69. for string in sorted(wordlist.words.iterkeys(), key=str.lower):
  70. keys.append(string)
  71. # Write each word to the file. If the word doesn't end in a new line char,
  72. # add it to the end so that there is guaranteed to be at most one word
  73. # per line.
  74. for k in keys:
  75. newword = wordlist.words[k].serialise()
  76. if newword[-1] != '\n':
  77. newword += '\n'
  78. f.write(newword)
  79. f.close()
  80. return True
  81. def generate_empty_tldr(path, name, author):
  82. '''Generate an empty tldr file.
  83. Arguments:
  84. path -- The directory to create the tldr file in.
  85. name -- The name of the file to create.
  86. author -- The author of the word list.
  87. '''
  88. try:
  89. f = open(path, 'w')
  90. except IOError:
  91. whole = path.split(os.sep)[0:-1]
  92. dir = ''
  93. for s in whole:
  94. dir = dir + s
  95. os.mkdir(dir)
  96. f = open(path, 'w')
  97. f.write('#' + author + '\n')
  98. f.write('#' + str(datetime.datetime.now()) + '\n')
  99. f.write('#0')
  100. f.close()