literals_to_xrefs.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """
  2. Runs through a reST file looking for old-style literals, and helps replace them
  3. with new-style references.
  4. """
  5. import re
  6. import sys
  7. import shelve
  8. refre = re.compile(r'``([^`\s]+?)``')
  9. ROLES = (
  10. 'attr',
  11. 'class',
  12. "djadmin",
  13. 'data',
  14. 'exc',
  15. 'file',
  16. 'func',
  17. 'lookup',
  18. 'meth',
  19. 'mod' ,
  20. "djadminopt",
  21. "ref",
  22. "setting",
  23. "term",
  24. "tfilter",
  25. "ttag",
  26. # special
  27. "skip"
  28. )
  29. ALWAYS_SKIP = [
  30. "NULL",
  31. "True",
  32. "False",
  33. ]
  34. def fixliterals(fname):
  35. data = open(fname).read()
  36. last = 0
  37. new = []
  38. storage = shelve.open("/tmp/literals_to_xref.shelve")
  39. lastvalues = storage.get("lastvalues", {})
  40. for m in refre.finditer(data):
  41. new.append(data[last:m.start()])
  42. last = m.end()
  43. line_start = data.rfind("\n", 0, m.start())
  44. line_end = data.find("\n", m.end())
  45. prev_start = data.rfind("\n", 0, line_start)
  46. next_end = data.find("\n", line_end + 1)
  47. # Skip always-skip stuff
  48. if m.group(1) in ALWAYS_SKIP:
  49. new.append(m.group(0))
  50. continue
  51. # skip when the next line is a title
  52. next_line = data[m.end():next_end].strip()
  53. if next_line[0] in "!-/:-@[-`{-~" and all(c == next_line[0] for c in next_line):
  54. new.append(m.group(0))
  55. continue
  56. sys.stdout.write("\n"+"-"*80+"\n")
  57. sys.stdout.write(data[prev_start+1:m.start()])
  58. sys.stdout.write(colorize(m.group(0), fg="red"))
  59. sys.stdout.write(data[m.end():next_end])
  60. sys.stdout.write("\n\n")
  61. replace_type = None
  62. while replace_type is None:
  63. replace_type = raw_input(
  64. colorize("Replace role: ", fg="yellow")
  65. ).strip().lower()
  66. if replace_type and replace_type not in ROLES:
  67. replace_type = None
  68. if replace_type == "":
  69. new.append(m.group(0))
  70. continue
  71. if replace_type == "skip":
  72. new.append(m.group(0))
  73. ALWAYS_SKIP.append(m.group(1))
  74. continue
  75. default = lastvalues.get(m.group(1), m.group(1))
  76. if default.endswith("()") and replace_type in ("class", "func", "meth"):
  77. default = default[:-2]
  78. replace_value = raw_input(
  79. colorize("Text <target> [", fg="yellow") + default + colorize("]: ", fg="yellow")
  80. ).strip()
  81. if not replace_value:
  82. replace_value = default
  83. new.append(":%s:`%s`" % (replace_type, replace_value))
  84. lastvalues[m.group(1)] = replace_value
  85. new.append(data[last:])
  86. open(fname, "w").write("".join(new))
  87. storage["lastvalues"] = lastvalues
  88. storage.close()
  89. #
  90. # The following is taken from django.utils.termcolors and is copied here to
  91. # avoid the dependancy.
  92. #
  93. def colorize(text='', opts=(), **kwargs):
  94. """
  95. Returns your text, enclosed in ANSI graphics codes.
  96. Depends on the keyword arguments 'fg' and 'bg', and the contents of
  97. the opts tuple/list.
  98. Returns the RESET code if no parameters are given.
  99. Valid colors:
  100. 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
  101. Valid options:
  102. 'bold'
  103. 'underscore'
  104. 'blink'
  105. 'reverse'
  106. 'conceal'
  107. 'noreset' - string will not be auto-terminated with the RESET code
  108. Examples:
  109. colorize('hello', fg='red', bg='blue', opts=('blink',))
  110. colorize()
  111. colorize('goodbye', opts=('underscore',))
  112. print colorize('first line', fg='red', opts=('noreset',))
  113. print 'this should be red too'
  114. print colorize('and so should this')
  115. print 'this should not be red'
  116. """
  117. color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
  118. foreground = dict([(color_names[x], '3%s' % x) for x in range(8)])
  119. background = dict([(color_names[x], '4%s' % x) for x in range(8)])
  120. RESET = '0'
  121. opt_dict = {'bold': '1', 'underscore': '4', 'blink': '5', 'reverse': '7', 'conceal': '8'}
  122. text = str(text)
  123. code_list = []
  124. if text == '' and len(opts) == 1 and opts[0] == 'reset':
  125. return '\x1b[%sm' % RESET
  126. for k, v in kwargs.iteritems():
  127. if k == 'fg':
  128. code_list.append(foreground[v])
  129. elif k == 'bg':
  130. code_list.append(background[v])
  131. for o in opts:
  132. if o in opt_dict:
  133. code_list.append(opt_dict[o])
  134. if 'noreset' not in opts:
  135. text = text + '\x1b[%sm' % RESET
  136. return ('\x1b[%sm' % ';'.join(code_list)) + text
  137. if __name__ == '__main__':
  138. try:
  139. fixliterals(sys.argv[1])
  140. except (KeyboardInterrupt, SystemExit):
  141. print