util.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.util
  4. ~~~~~~~~~~~~~
  5. Utility functions.
  6. :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. import sys
  11. import codecs
  12. split_path_re = re.compile(r'[/\\ ]')
  13. doctype_lookup_re = re.compile(r'''(?smx)
  14. (<\?.*?\?>)?\s*
  15. <!DOCTYPE\s+(
  16. [a-zA-Z_][a-zA-Z0-9]*\s+
  17. [a-zA-Z_][a-zA-Z0-9]*\s+
  18. "[^"]*")
  19. [^>]*>
  20. ''')
  21. tag_re = re.compile(r'<(.+?)(\s.*?)?>.*?</.+?>(?uism)')
  22. class ClassNotFound(ValueError):
  23. """
  24. If one of the get_*_by_* functions didn't find a matching class.
  25. """
  26. class OptionError(Exception):
  27. pass
  28. def get_choice_opt(options, optname, allowed, default=None, normcase=False):
  29. string = options.get(optname, default)
  30. if normcase:
  31. string = string.lower()
  32. if string not in allowed:
  33. raise OptionError('Value for option %s must be one of %s' %
  34. (optname, ', '.join(map(str, allowed))))
  35. return string
  36. def get_bool_opt(options, optname, default=None):
  37. string = options.get(optname, default)
  38. if isinstance(string, bool):
  39. return string
  40. elif isinstance(string, int):
  41. return bool(string)
  42. elif not isinstance(string, basestring):
  43. raise OptionError('Invalid type %r for option %s; use '
  44. '1/0, yes/no, true/false, on/off' % (
  45. string, optname))
  46. elif string.lower() in ('1', 'yes', 'true', 'on'):
  47. return True
  48. elif string.lower() in ('0', 'no', 'false', 'off'):
  49. return False
  50. else:
  51. raise OptionError('Invalid value %r for option %s; use '
  52. '1/0, yes/no, true/false, on/off' % (
  53. string, optname))
  54. def get_int_opt(options, optname, default=None):
  55. string = options.get(optname, default)
  56. try:
  57. return int(string)
  58. except TypeError:
  59. raise OptionError('Invalid type %r for option %s; you '
  60. 'must give an integer value' % (
  61. string, optname))
  62. except ValueError:
  63. raise OptionError('Invalid value %r for option %s; you '
  64. 'must give an integer value' % (
  65. string, optname))
  66. def get_list_opt(options, optname, default=None):
  67. val = options.get(optname, default)
  68. if isinstance(val, basestring):
  69. return val.split()
  70. elif isinstance(val, (list, tuple)):
  71. return list(val)
  72. else:
  73. raise OptionError('Invalid type %r for option %s; you '
  74. 'must give a list value' % (
  75. val, optname))
  76. def docstring_headline(obj):
  77. if not obj.__doc__:
  78. return ''
  79. res = []
  80. for line in obj.__doc__.strip().splitlines():
  81. if line.strip():
  82. res.append(" " + line.strip())
  83. else:
  84. break
  85. return ''.join(res).lstrip()
  86. def make_analysator(f):
  87. """
  88. Return a static text analysation function that
  89. returns float values.
  90. """
  91. def text_analyse(text):
  92. rv = f(text)
  93. if not rv:
  94. return 0.0
  95. return min(1.0, max(0.0, float(rv)))
  96. text_analyse.__doc__ = f.__doc__
  97. return staticmethod(text_analyse)
  98. def shebang_matches(text, regex):
  99. """
  100. Check if the given regular expression matches the last part of the
  101. shebang if one exists.
  102. >>> from pygments.util import shebang_matches
  103. >>> shebang_matches('#!/usr/bin/env python', r'python(2\.\d)?')
  104. True
  105. >>> shebang_matches('#!/usr/bin/python2.4', r'python(2\.\d)?')
  106. True
  107. >>> shebang_matches('#!/usr/bin/python-ruby', r'python(2\.\d)?')
  108. False
  109. >>> shebang_matches('#!/usr/bin/python/ruby', r'python(2\.\d)?')
  110. False
  111. >>> shebang_matches('#!/usr/bin/startsomethingwith python',
  112. ... r'python(2\.\d)?')
  113. True
  114. It also checks for common windows executable file extensions::
  115. >>> shebang_matches('#!C:\\Python2.4\\Python.exe', r'python(2\.\d)?')
  116. True
  117. Parameters (``'-f'`` or ``'--foo'`` are ignored so ``'perl'`` does
  118. the same as ``'perl -e'``)
  119. Note that this method automatically searches the whole string (eg:
  120. the regular expression is wrapped in ``'^$'``)
  121. """
  122. index = text.find('\n')
  123. if index >= 0:
  124. first_line = text[:index].lower()
  125. else:
  126. first_line = text.lower()
  127. if first_line.startswith('#!'):
  128. try:
  129. found = [x for x in split_path_re.split(first_line[2:].strip())
  130. if x and not x.startswith('-')][-1]
  131. except IndexError:
  132. return False
  133. regex = re.compile('^%s(\.(exe|cmd|bat|bin))?$' % regex, re.IGNORECASE)
  134. if regex.search(found) is not None:
  135. return True
  136. return False
  137. def doctype_matches(text, regex):
  138. """
  139. Check if the doctype matches a regular expression (if present).
  140. Note that this method only checks the first part of a DOCTYPE.
  141. eg: 'html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
  142. """
  143. m = doctype_lookup_re.match(text)
  144. if m is None:
  145. return False
  146. doctype = m.group(2)
  147. return re.compile(regex).match(doctype.strip()) is not None
  148. def html_doctype_matches(text):
  149. """
  150. Check if the file looks like it has a html doctype.
  151. """
  152. return doctype_matches(text, r'html\s+PUBLIC\s+"-//W3C//DTD X?HTML.*')
  153. _looks_like_xml_cache = {}
  154. def looks_like_xml(text):
  155. """
  156. Check if a doctype exists or if we have some tags.
  157. """
  158. key = hash(text)
  159. try:
  160. return _looks_like_xml_cache[key]
  161. except KeyError:
  162. m = doctype_lookup_re.match(text)
  163. if m is not None:
  164. return True
  165. rv = tag_re.search(text[:1000]) is not None
  166. _looks_like_xml_cache[key] = rv
  167. return rv
  168. # Python 2/3 compatibility
  169. if sys.version_info < (3,0):
  170. b = bytes = str
  171. u_prefix = 'u'
  172. import StringIO, cStringIO
  173. BytesIO = cStringIO.StringIO
  174. StringIO = StringIO.StringIO
  175. uni_open = codecs.open
  176. else:
  177. import builtins
  178. bytes = builtins.bytes
  179. u_prefix = ''
  180. def b(s):
  181. if isinstance(s, str):
  182. return bytes(map(ord, s))
  183. elif isinstance(s, bytes):
  184. return s
  185. else:
  186. raise TypeError("Invalid argument %r for b()" % (s,))
  187. import io
  188. BytesIO = io.BytesIO
  189. StringIO = io.StringIO
  190. uni_open = builtins.open