Guest User

Untitled

a guest
Oct 14th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. """
  4. Titlecase: https://github.com/ppannuto/python-titlecase
  5. Original Perl version by: John Gruber http://daringfireball.net/ 10 May 2008
  6. Python version by Stuart Colville http://muffinresearch.co.uk
  7. License: http://www.opensource.org/licenses/mit-license.php
  8. """
  9.  
  10. import os
  11. import argparse
  12. import re
  13. import sys
  14.  
  15. SMALL = 'a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\.?|via|vs\.?'
  16. PUNCT = r"""!"#$%&'‘()*+,\-–‒—―./:;?@[\\\]_`{|}~"""
  17.  
  18. SMALL_WORDS = re.compile(r'^(%s)$' % SMALL, re.I)
  19. INLINE_PERIOD = re.compile(r'[a-z][.][a-z]', re.I)
  20. UC_ELSEWHERE = re.compile(r'[%s]*?[a-zA-Z]+[A-Z]+?' % PUNCT)
  21. CAPFIRST = re.compile(r"^[%s]*?([A-Za-z])" % PUNCT)
  22. SMALL_FIRST = re.compile(r'^([%s]*)(%s)\b' % (PUNCT, SMALL), re.I)
  23. SMALL_LAST = re.compile(r'\b(%s)[%s]?$' % (SMALL, PUNCT), re.I)
  24. SUBPHRASE = re.compile(r'([:.;?!\-–‒—―][ ])(%s)' % SMALL)
  25. APOS_SECOND = re.compile(r"^[dol]{1}['‘]{1}[a-z]+(?:['s]{2})?$", re.I)
  26. UC_INITIALS = re.compile(r"^(?:[A-Z]{1}\.{1}|[A-Z]{1}\.{1}[A-Z]{1})+$")
  27. MAC_MC = re.compile(r"^([Mm]c|MC)(\w.+)")
  28.  
  29.  
  30. class Immutable(object):
  31. pass
  32.  
  33.  
  34. text_type = unicode if sys.version_info < (3,) else str
  35.  
  36.  
  37. class ImmutableString(text_type, Immutable):
  38. pass
  39.  
  40.  
  41. class ImmutableBytes(bytes, Immutable):
  42. pass
  43.  
  44.  
  45. def _mark_immutable(text):
  46. if isinstance(text, bytes):
  47. return ImmutableBytes(text)
  48. return ImmutableString(text)
  49.  
  50.  
  51. def set_small_word_list(small=SMALL):
  52. global SMALL_WORDS
  53. global SMALL_FIRST
  54. global SMALL_LAST
  55. global SUBPHRASE
  56. SMALL_WORDS = re.compile(r'^(%s)$' % small, re.I)
  57. SMALL_FIRST = re.compile(r'^([%s]*)(%s)\b' % (PUNCT, small), re.I)
  58. SMALL_LAST = re.compile(r'\b(%s)[%s]?$' % (small, PUNCT), re.I)
  59. SUBPHRASE = re.compile(r'([:.;?!][ ])(%s)' % small)
  60.  
  61.  
  62. def titlecase(text, callback=None, small_first_last=True):
  63. """
  64. Titlecases input text
  65. This filter changes all words to Title Caps, and attempts to be clever
  66. about *un*capitalizing SMALL words like a/an/the in the input.
  67. The list of "SMALL words" which are not capped comes from
  68. the New York Times Manual of Style, plus 'vs' and 'v'.
  69. """
  70.  
  71. lines = re.split('[\r\n]+', text)
  72. processed = []
  73. for line in lines:
  74. all_caps = line.upper() == line
  75. words = re.split('[\t ]', line)
  76. tc_line = []
  77. for word in words:
  78. if callback:
  79. new_word = callback(word, all_caps=all_caps)
  80. if new_word:
  81. # Address #22: If a callback has done something
  82. # specific, leave this string alone from now on
  83. tc_line.append(_mark_immutable(new_word))
  84. continue
  85.  
  86. if all_caps:
  87. if UC_INITIALS.match(word):
  88. tc_line.append(word)
  89. continue
  90.  
  91. if APOS_SECOND.match(word):
  92. if len(word[0]) == 1 and word[0] not in 'aeiouAEIOU':
  93. word = word[0].lower() + word[1] + word[2].upper() + word[3:]
  94. else:
  95. word = word[0].upper() + word[1] + word[2].upper() + word[3:]
  96. tc_line.append(word)
  97. continue
  98.  
  99. match = MAC_MC.match(word)
  100. if match:
  101. tc_line.append("%s%s" % (match.group(1).capitalize(),
  102. titlecase(match.group(2),callback,small_first_last)))
  103. continue
  104.  
  105. if INLINE_PERIOD.search(word) or (not all_caps and UC_ELSEWHERE.match(word)):
  106. tc_line.append(word)
  107. continue
  108. if SMALL_WORDS.match(word):
  109. tc_line.append(word.lower())
  110. continue
  111.  
  112. if "/" in word and "//" not in word:
  113. slashed = map(
  114. lambda t: titlecase(t,callback,False),
  115. word.split('/')
  116. )
  117. tc_line.append("/".join(slashed))
  118. continue
  119.  
  120. if '-' in word:
  121. hyphenated = map(
  122. lambda t: titlecase(t,callback,small_first_last),
  123. word.split('-')
  124. )
  125. tc_line.append("-".join(hyphenated))
  126. continue
  127.  
  128. if all_caps:
  129. word = word.lower()
  130.  
  131. # Just a normal word that needs to be capitalized
  132. tc_line.append(CAPFIRST.sub(lambda m: m.group(0).upper(), word))
  133.  
  134. if small_first_last and tc_line:
  135. if not isinstance(tc_line[0], Immutable):
  136. tc_line[0] = SMALL_FIRST.sub(lambda m: '%s%s' % (
  137. m.group(1),
  138. m.group(2).capitalize()
  139. ), tc_line[0])
  140.  
  141. if not isinstance(tc_line[-1], Immutable):
  142. tc_line[-1] = SMALL_LAST.sub(
  143. lambda m: m.group(0).capitalize(), tc_line[-1]
  144. )
  145.  
  146. result = " ".join(tc_line)
  147.  
  148. result = SUBPHRASE.sub(lambda m: '%s%s' % (
  149. m.group(1),
  150. m.group(2).capitalize()
  151. ), result)
  152.  
  153. processed.append(result)
  154.  
  155. return "\n".join(processed)
  156.  
  157.  
  158. def cmd():
  159. '''Handler for command line invocation'''
  160.  
  161. # Try to handle any reasonable thing thrown at this.
  162. # Consume '-f' and '-o' as input/output, allow '-' for stdin/stdout
  163. # and treat any subsequent arguments as a space separated string to
  164. # be titlecased (so it still works if people forget quotes)
  165. parser = argparse.ArgumentParser()
  166. in_group = parser.add_mutually_exclusive_group()
  167. in_group.add_argument('string', nargs='*', default=[],
  168. help='String to titlecase')
  169. in_group.add_argument('-f', '--input-file',
  170. help='File to read from to titlecase')
  171. parser.add_argument('-o', '--output-file',
  172. help='File to write titlecased output to)')
  173.  
  174. args = parser.parse_args()
  175.  
  176. if args.input_file is not None:
  177. if args.input_file == '-':
  178. ifile = sys.stdin
  179. else:
  180. ifile = open(args.input_file)
  181. else:
  182. ifile = sys.stdin
  183.  
  184. if args.output_file is not None:
  185. if args.output_file == '-':
  186. ofile = sys.stdout
  187. else:
  188. ofile = open(args.output_file, 'w')
  189. else:
  190. ofile = sys.stdout
  191.  
  192. if len(args.string) > 0:
  193. in_string = ' '.join(args.string)
  194. else:
  195. with ifile:
  196. in_string = ifile.read()
  197.  
  198. with ofile:
  199. ofile.write(titlecase(in_string))
  200.  
  201. path = raw_input("Enter path to folder: ")
  202.  
  203. for root, dirs, files in os.walk(path):
  204. for name in files:
  205. old = os.path.join(root, name)
  206. new = os.path.join(root, titlecase(name))
  207. os.rename(old, new)
  208.  
  209. print "Alldone"
Advertisement
Add Comment
Please, Sign In to add comment