PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/nltk/sourcedstring.py

https://github.com/haewoon/nltk
Python | 1382 lines | 1374 code | 1 blank | 7 comment | 9 complexity | ceaa12c227f60d86bf3e044de099cfc9 MD5 | raw file
Possible License(s): Apache-2.0
  1. # Natural Language Toolkit: Sourced Strings
  2. #
  3. # Copyright (C) 2001-2009 NLTK Project
  4. # Author: Edward Loper <edloper@gmail.com>
  5. # URL: <http://www.nltk.org/>
  6. # For license information, see LICENSE.TXT
  7. """
  8. "Sourced strings" are strings that are annotated with information
  9. about the location in a document where they were originally found.
  10. Sourced strings are subclassed from Python strings. As a result, they
  11. can usually be used anywhere a normal Python string can be used.
  12. >>> from nltk.sourcedstring import SourcedString
  13. >>> newt_contents = '''
  14. ... She turned me into a newt!
  15. ... I got better.'''.strip()
  16. >>> newt_doc = SourcedString(newt_contents, 'newt.txt')
  17. >>> newt_doc
  18. 'She turned me into a newt!\\nI got better.'@[0:40]
  19. >>> newt = newt_doc.split()[5] # Find the sixth word.
  20. >>> newt
  21. 'newt!'@[21:26]
  22. """
  23. import re, sys
  24. from nltk.internals import slice_bounds, abstract
  25. __all__ = [
  26. 'StringSource',
  27. 'ConsecutiveCharStringSource', 'ContiguousCharStringSource',
  28. 'SourcedString', 'SourcedStringStream', 'SourcedStringRegexp',
  29. 'SimpleSourcedString', 'CompoundSourcedString',
  30. 'SimpleSourcedByteString', 'SimpleSourcedUnicodeString',
  31. 'CompoundSourcedByteString', 'CompoundSourcedUnicodeString',
  32. ]
  33. #//////////////////////////////////////////////////////////////////////
  34. # String Sources
  35. #//////////////////////////////////////////////////////////////////////
  36. class StringSource(object):
  37. """
  38. A description of the location of a string in a document. Each
  39. ``StringSource`` consists of a document identifier, along with
  40. information about the begin and end offsets of each character in
  41. the string. These offsets are typically either byte offsets or
  42. character offsets. (Note that for unicode strings, byte offsets
  43. and character offsets are not the same thing.)
  44. ``StringSource`` is an abstract base class. Two concrete
  45. subclasses are used depending on the properties of the string
  46. whose source is being described:
  47. - ``ConsecutiveCharStringSource`` describes the source of strings
  48. whose characters have consecutive offsets (in particular, byte
  49. strings w/ byte offsets; and unicode strings with character
  50. offsets).
  51. - ``ContiguousCharStringSource`` describes the source of strings
  52. whose characters are contiguous, but do not necessarily have
  53. consecutive offsets (in particular, unicode strings with byte
  54. offsets).
  55. :ivar docid: An identifier (such as a filename) that specifies
  56. which document contains the string.
  57. :ivar offsets: A list of offsets specifying the location of each
  58. character in the document. The *i* th character of the string
  59. begins at offset ``offsets[i]`` and ends at offset
  60. ``offsets[i+1]``. The length of the ``offsets`` list is one
  61. greater than the list of the string described by this
  62. ``StringSource``.
  63. :ivar begin: The document offset where the string begins. (I.e.,
  64. the offset of the first character in the string.)
  65. ``source.begin`` is always equal to ``source.offsets[0]``.
  66. :ivar end: The document offset where the string ends. (For
  67. character offsets, one plus the offset of the last character;
  68. for byte offsets, one plus the offset of the last byte that
  69. encodes the last character). ``source.end`` is always equal
  70. to ``source.offsets[-1]``.
  71. """
  72. def __new__(cls, docid, *args, **kwargs):
  73. # If the StringSource constructor is called directly, then
  74. # choose one of its subclasses to delegate to.
  75. if cls is StringSource:
  76. if args:
  77. raise TypeError("Specifcy either begin and end, or "
  78. "offsets, using keyword arguments")
  79. if 'begin' in kwargs and 'end' in kwargs and 'offsets' not in kwargs:
  80. cls = ConsecutiveCharStringSource
  81. elif ('begin' not in kwargs and 'end' not in kwargs and
  82. 'offsets' in kwargs):
  83. cls = ContiguousCharStringSource
  84. else:
  85. raise TypeError("Specify either begin and end, or offsets "
  86. "(but not both)")
  87. # Construct the object.
  88. return object.__new__(cls)
  89. def __init__(self, docid, **kwargs):
  90. """
  91. Create a new ``StringSource``. When the ``StringSource``
  92. constructor is called directly, it automatically delegates to
  93. one of its two subclasses:
  94. - If ``begin`` and ``end`` are specified, then a
  95. ``ConsecutiveCharStringSource`` is returned.
  96. - If ``offsets`` is specified, then a
  97. ``ContiguousCharStringSource`` is returned.
  98. In both cases, the arguments must be specified as keyword
  99. arguments (not positional arguments).
  100. """
  101. def __getitem__(self, index):
  102. """
  103. Return a ``StringSource`` describing the location where the
  104. specified character was found. In particular, if ``s`` is the
  105. string that this source describes, then return a
  106. ``StringSource`` describing the location of ``s[index]``.
  107. :raise IndexError: If index is out of range.
  108. """
  109. if isinstance(index, slice):
  110. start, stop = slice_bounds(self, index)
  111. return self.__getslice__(start, stop)
  112. else:
  113. if index < 0: index += len(self)
  114. if index < 0 or index >= len(self):
  115. raise IndexError('StringSource index out of range')
  116. return self.__getslice__(index, index+1)
  117. @abstract
  118. def __getslice__(self, start, stop):
  119. """
  120. Return a ``StringSource`` describing the location where the
  121. specified substring was found. In particular, if ``s`` is the
  122. string that this source describes, then return a
  123. ``StringSource`` describing the location of ``s[start:stop]``.
  124. """
  125. @abstract
  126. def __len__(self):
  127. """
  128. Return the length of the string described by this
  129. ``StringSource``. Note that this may not be equal to
  130. ``self.end-self.begin`` for unicode strings described using
  131. byte offsets.
  132. """
  133. def __str__(self):
  134. if self.end == self.begin+1:
  135. return '@%s[%s]' % (self.docid, self.begin,)
  136. else:
  137. return '@%s[%s:%s]' % (self.docid, self.begin, self.end)
  138. def __cmp__(self, other):
  139. return (cmp(self.docid, self.docid) or
  140. cmp([(charloc.begin, charloc.end) for charloc in self],
  141. [(charloc.begin, charloc.end) for charloc in other]))
  142. def __hash__(self):
  143. # Cache hash values.
  144. if not hasattr(self, '_hash'):
  145. self._hash = hash( (self.docid,
  146. tuple((charloc.begin, charloc.end)
  147. for charloc in self)) )
  148. return self._hash
  149. class ConsecutiveCharStringSource(StringSource):
  150. """
  151. A ``StringSource`` that specifies the source of strings whose
  152. characters have consecutive offsets. In particular, the following
  153. two properties must hold for all valid indices:
  154. - source[i].end == source[i].begin + 1
  155. - source[i].end == source[i+1].begin
  156. These properties allow the source to be stored using just a start
  157. offset and an end offset (along with a docid).
  158. This ``StringSource`` can be used to describe byte strings that are
  159. indexed using byte offsets or character offsets; or unicode
  160. strings that are indexed using character offsets.
  161. """
  162. def __init__(self, docid, begin, end):
  163. if not isinstance(begin, (int, long)):
  164. raise TypeError("begin attribute expected an integer")
  165. if not isinstance(end, (int, long)):
  166. raise TypeError("end attribute expected an integer")
  167. if not end >= begin:
  168. raise ValueError("begin must be less than or equal to end")
  169. self.docid = docid
  170. self.begin = begin
  171. self.end = end
  172. @property
  173. def offsets(self):
  174. return tuple(range(self.begin, self.end+1))
  175. def __len__(self):
  176. return self.end-self.begin
  177. def __getslice__(self, start, stop):
  178. start = max(0, min(len(self), start))
  179. stop = max(start, min(len(self), stop))
  180. return ConsecutiveCharStringSource(
  181. self.docid, self.begin+start, self.begin+stop)
  182. def __cmp__(self, other):
  183. if isinstance(other, ConsecutiveCharStringSource):
  184. return (cmp(self.docid, other.docid) or
  185. cmp(self.begin, other.begin) or
  186. cmp(self.end, other.end))
  187. else:
  188. return StringSource.__cmp__(self, other)
  189. def __repr__(self):
  190. return 'StringSource(%r, begin=%r, end=%r)' % (
  191. self.docid, self.begin, self.end)
  192. class ContiguousCharStringSource(StringSource):
  193. """
  194. A ``StringSource`` that specifies the source of strings whose
  195. character are contiguous, but do not necessarily have consecutive
  196. offsets. In particular, each character's end offset must be equal
  197. to the next character's start offset:
  198. - source[i].end == source[i+1].begin
  199. This property allow the source to be stored using a list of
  200. ``len(source)+1`` offsets (along with a docid).
  201. This ``StringSource`` can be used to describe unicode strings that
  202. are indexed using byte offsets.
  203. """
  204. CONSTRUCTOR_CHECKS_OFFSETS = False
  205. def __init__(self, docid, offsets):
  206. offsets = tuple(offsets)
  207. if len(offsets) == 0:
  208. raise ValueError("at least one offset must be specified")
  209. if self.CONSTRUCTOR_CHECKS_OFFSETS:
  210. for i in range(len(offsets)):
  211. if not isinstance(offsets[i], (int,long)):
  212. raise TypeError("offsets must be integers")
  213. if i>0 and offsets[i-1]>offsets[i]:
  214. raise TypeError("offsets must be monotonic increasing")
  215. self.docid = docid
  216. self.offsets = offsets
  217. @property
  218. def begin(self): return self.offsets[0]
  219. @property
  220. def end(self): return self.offsets[-1]
  221. def __len__(self):
  222. return len(self.offsets)-1
  223. def __getslice__(self, start, stop):
  224. start = max(0, min(len(self), start))
  225. stop = max(start, min(len(self), stop))
  226. return ContiguousCharStringSource(
  227. self.docid, self.offsets[start:stop+1])
  228. def __cmp__(self, other):
  229. if isinstance(other, ConsecutiveCharStringSource):
  230. return (cmp(self.docid, other.docid) or
  231. cmp(self.offsets, other._offsets))
  232. else:
  233. return StringSource.__cmp__(self, other)
  234. def __repr__(self):
  235. return 'StringSource(%r, offsets=%r)' % (self.docid, self.offsets)
  236. #//////////////////////////////////////////////////////////////////////
  237. # Base Class for Sourced Strings.
  238. #//////////////////////////////////////////////////////////////////////
  239. class SourcedString(object):
  240. """
  241. A string that is annotated with information about the location in
  242. a document where it was originally found. Sourced strings are
  243. subclassed from Python strings. As a result, they can usually be
  244. used anywhere a normal Python string can be used.
  245. There are two types of sourced strings: ``SimpleSourcedString``s,
  246. which correspond to a single substring of a document; and
  247. ``CompoundSourcedString``s, which are constructed by concatenating
  248. strings from multiple sources. Each of these types has two
  249. concrete subclasses: one for unicode strings (subclassed from
  250. ``unicode``), and one for byte strings (subclassed from ``str``).
  251. Two sourced strings are considered equal if their contents are
  252. equal, even if their sources differ. This fact is important in
  253. ensuring that sourced strings act like normal strings. In
  254. particular, it allows sourced strings to be used with code that
  255. was originally intended to process plain Python strings.
  256. If you wish to determine whether two sourced strings came from the
  257. same location in the same document, simply compare their
  258. ``sources`` attributes. If you know that both sourced strings are
  259. ``SimpleSourcedStrings``, then you can compare their ``source``
  260. attribute instead.
  261. String operations that act on sourced strings will preserve
  262. location information whenever possible. However, there are a few
  263. types of string manipulation that can cause source information to
  264. be discarded. The most common examples of operations that will
  265. lose source information are:
  266. - ``str.join()``, where the joining string is not sourced.
  267. - ``str.replace()``, where the original string is not sourced.
  268. - String formatting (the ``%`` operator).
  269. - Regular expression substitution.
  270. :ivar sources: A sorted tuple of ``(index, source)`` pairs. Each
  271. such pair specifies that the source of
  272. ``self[index:index+len(source)]`` is ``source``. Any characters
  273. for which no source is specified are sourceless (e.g., plain
  274. Python characters that were concatenated to a sourced string).
  275. When working with simple sourced strings, it's usually easier
  276. to use the ``source`` attribute instead; however, the
  277. ``sources`` attribute is defined for both simple and compound
  278. sourced strings.
  279. """
  280. def __new__(cls, contents, source):
  281. # If the SourcedString constructor is called directly, then
  282. # choose one of its subclasses to delegate to.
  283. if cls is SourcedString:
  284. if isinstance(contents, str):
  285. cls = SimpleSourcedByteString
  286. elif isinstance(contents, unicode):
  287. cls = SimpleSourcedUnicodeString
  288. else:
  289. raise TypeError("Expected 'contents' to be a unicode "
  290. "string or a byte string")
  291. # Create the new object using the appropriate string class's
  292. # __new__, which takes just the contents argument.
  293. return cls._stringtype.__new__(cls, contents)
  294. _stringtype = None
  295. """A class variable, defined by subclasses of ``SourcedString``,
  296. determining what type of string this class contains. Its
  297. value must be either str or ``unicode``."""
  298. #//////////////////////////////////////////////////////////////////////
  299. #{ Splitting & Stripping Methods
  300. #//////////////////////////////////////////////////////////////////////
  301. def lstrip(self, chars=None):
  302. s = self._stringtype.lstrip(self, chars)
  303. return self[len(self)-len(s):]
  304. def rstrip(self, chars=None):
  305. s = self._stringtype.rstrip(self, chars)
  306. return self[:len(s)]
  307. def strip(self, chars=None):
  308. return self.lstrip(chars).rstrip(chars)
  309. _WHITESPACE_RE = re.compile(r'\s+')
  310. def split(self, sep=None, maxsplit=None):
  311. # Check for unicode/bytestring mismatches:
  312. if self._mixed_string_types(sep, maxsplit):
  313. return self._decode_and_call('split', sep, maxsplit)
  314. # Use a regexp to split self.
  315. if sep is None: sep_re = self._WHITESPACE_RE
  316. else: sep_re = re.compile(re.escape(sep))
  317. if maxsplit is None: return sep_re.split(self)
  318. else: return sep_re.split(self, maxsplit)
  319. def rsplit(self, sep=None, maxsplit=None):
  320. # Check for unicode/bytestring mismatches:
  321. if self._mixed_string_types(sep, maxsplit):
  322. return self._decode_and_call('rsplit', sep, maxsplit)
  323. # Split on whitespace use a regexp.
  324. if sep is None:
  325. seps = list(self._WHITESPACE_RE.finditer(self))
  326. if maxsplit: seps = seps[-maxsplit:]
  327. if not seps: return [self]
  328. result = [self[:seps[0].start()]]
  329. for i in range(1, len(seps)):
  330. result.append(self[seps[i-1].end():seps[i].start()])
  331. result.append(self[seps[-1].end():])
  332. return result
  333. # Split on a given string: use rfind.
  334. else:
  335. result = []
  336. piece_end = len(self)
  337. while maxsplit != 0:
  338. sep_pos = self.rfind(sep, 0, piece_end)
  339. if sep_pos < 0: break
  340. result.append(self[sep_pos+len(sep):piece_end])
  341. piece_end = sep_pos
  342. if maxsplit is not None: maxsplit -= 1
  343. if piece_end > 0:
  344. result.append(self[:piece_end])
  345. return result[::-1]
  346. def partition(self, sep):
  347. head, sep, tail = self._stringtype.partition(self, sep)
  348. i, j = len(head), len(head)+len(sep)
  349. return (self[:i], self[i:j], self[j:])
  350. def rpartition(self, sep):
  351. head, sep, tail = self._stringtype.rpartition(self, sep)
  352. i, j = len(head), len(head)+len(sep)
  353. return (self[:i], self[i:j], self[j:])
  354. _NEWLINE_RE = re.compile(r'\n')
  355. _LINE_RE = re.compile(r'.*\n?')
  356. def splitlines(self, keepends=False):
  357. if keepends:
  358. return self._LINE_RE.findall(self)
  359. else:
  360. return self._NEWLINE_RE.split(self)
  361. #//////////////////////////////////////////////////////////////////////
  362. #{ String Concatenation Methods
  363. #//////////////////////////////////////////////////////////////////////
  364. @staticmethod
  365. def concat(substrings):
  366. """
  367. Return a sourced string formed by concatenating the given list
  368. of substrings. Adjacent substrings will be merged when
  369. possible.
  370. Depending on the types and values of the supplied substrings,
  371. the concatenated string's value may be a Python string (str
  372. or ``unicode``), a ``SimpleSourcedString``, or a
  373. ``CompoundSourcedString``.
  374. """
  375. # Flatten nested compound sourced strings, and merge adjacent
  376. # strings where possible:
  377. merged = []
  378. for substring in substrings:
  379. SourcedString.__add_substring_to_list(substring, merged)
  380. # Return the concatenated string.
  381. if len(merged) == 0:
  382. return ''
  383. elif len(merged) == 1:
  384. return merged[0]
  385. else:
  386. return CompoundSourcedString(merged)
  387. def __add__(self, other):
  388. return SourcedString.concat([self, other])
  389. def __radd__(self, other):
  390. return SourcedString.concat([other, self])
  391. def __mul__(self, other):
  392. if other <= 0:
  393. return self._stringtype('')
  394. else:
  395. result = self
  396. for i in range(1, other):
  397. result += self
  398. return result
  399. def __rmul__(self, other):
  400. return self.__mul__(other)
  401. def join(self, sequence):
  402. seq_iter = iter(sequence)
  403. # Add the first element; but if sequence is empty, return an
  404. # empty string.
  405. try:
  406. s = seq_iter.next()
  407. except StopIteration:
  408. return self._stringtype('')
  409. # Add the remaining elements, separated by self.
  410. for elt in seq_iter:
  411. s += self
  412. s += elt
  413. return s
  414. @staticmethod
  415. def __add_substring_to_list(substring, result):
  416. """
  417. Helper for ``concat()``: add ``substring`` to the end of the
  418. list of substrings in ``result``. If ``substring`` is compound,
  419. then add its own substrings instead. Merge adjacent
  420. substrings whenever possible. Discard empty un-sourced
  421. substrings.
  422. """
  423. # Flatten nested compound sourced strings.
  424. if isinstance(substring, CompoundSourcedString):
  425. for s in substring.substrings:
  426. SourcedString.__add_substring_to_list(s, result)
  427. # Discard empty Python substrings.
  428. elif len(substring) == 0 and not isinstance(substring, SourcedString):
  429. pass # discard.
  430. # Merge adjacent simple sourced strings (when possible).
  431. elif (result and isinstance(result[-1], SimpleSourcedString) and
  432. isinstance(substring, SimpleSourcedString) and
  433. result[-1].end == substring.begin and
  434. result[-1].docid == substring.docid):
  435. result[-1] = SourcedString.__merge_simple_substrings(
  436. result[-1], substring)
  437. # Merge adjacent Python strings.
  438. elif (result and not isinstance(result[-1], SourcedString) and
  439. not isinstance(substring, SourcedString)):
  440. result[-1] += substring
  441. # All other strings just get appended to the result list.
  442. else:
  443. result.append(substring)
  444. @staticmethod
  445. def __merge_simple_substrings(lhs, rhs):
  446. """
  447. Helper for ``__add_substring_to_list()``: Merge ``lhs`` and
  448. ``rhs`` into a single simple sourced string, and return it.
  449. """
  450. contents = lhs._stringtype.__add__(lhs, rhs)
  451. if (isinstance(lhs.source, ConsecutiveCharStringSource) and
  452. isinstance(rhs.source, ConsecutiveCharStringSource)):
  453. source = ConsecutiveCharStringSource(
  454. lhs.source.docid, lhs.source.begin, rhs.source.end)
  455. else:
  456. source = ContiguousCharStringSource(
  457. lhs.source.docid, lhs.source.offsets+rhs.source.offsets[1:])
  458. return SourcedString(contents, source)
  459. #//////////////////////////////////////////////////////////////////////
  460. #{ Justification Methods
  461. #//////////////////////////////////////////////////////////////////////
  462. def center(self, width, fillchar=' '):
  463. return (fillchar * ((width-len(self))/2) + self +
  464. fillchar * ((width-len(self)+1)/2))
  465. def ljust(self, width, fillchar=' '):
  466. return self + fillchar * (width-len(self))
  467. def rjust(self, width, fillchar=' '):
  468. return fillchar * (width-len(self)) + self
  469. def zfill(self, width):
  470. return self.rjust(width, '0')
  471. #//////////////////////////////////////////////////////////////////////
  472. #{ Replacement Methods
  473. #//////////////////////////////////////////////////////////////////////
  474. # [xx] There's no reason in principle why this can't preserve
  475. # location information. But for now, it doesn't.
  476. def __mod__(self, other):
  477. return self._stringtype.__mod__(self, other)
  478. def replace(self, old, new, count=0):
  479. # Check for unicode/bytestring mismatches:
  480. if self._mixed_string_types(old, new, count):
  481. return self._decode_and_call('replace', old, new, count)
  482. # Use a regexp to find all occurrences of old, and replace them w/ new.
  483. result = ''
  484. pos = 0
  485. for match in re.finditer(re.escape(old), self):
  486. result += self[pos:match.start()]
  487. result += new
  488. pos = match.end()
  489. result += self[pos:]
  490. return result
  491. def expandtabs(self, tabsize=8):
  492. if len(self) == 0: return self
  493. pieces = re.split(r'([\t\n])', self)
  494. result = ''
  495. offset = 0
  496. for piece in pieces:
  497. if piece == '\t':
  498. spaces = 8 - (offset % tabsize)
  499. # Each inserted space's source is the same as the
  500. # source of the tab character that generated it.
  501. result += spaces * SourcedString(' ', piece.source)
  502. offset = 0
  503. else:
  504. result += piece
  505. if piece == '\n': offset = 0
  506. else: offset += len(piece)
  507. return result
  508. def translate(self, table, deletechars=''):
  509. # Note: str.translate() and unicode.translate() have
  510. # different interfaces.
  511. if isinstance(self, unicode):
  512. if deletechars:
  513. raise TypeError('The unicode version of translate() does not '
  514. 'accept the deletechars parameter')
  515. return SourcedString.concat(
  516. [SourcedString(table.get(c,c), c.source)
  517. for c in self if table.get(c,c) is not None])
  518. else:
  519. if len(table) != 256:
  520. raise ValueError('translation table must be 256 characters long')
  521. return SourcedString.concat(
  522. [SourcedString(table[ord(c)], c.source)
  523. for c in self if c not in deletechars])
  524. #//////////////////////////////////////////////////////////////////////
  525. #{ Unicode
  526. #//////////////////////////////////////////////////////////////////////
  527. # Unicode string -> byte string
  528. def encode(self, encoding=None, errors='strict'):
  529. if encoding is None: encoding = sys.getdefaultencoding()
  530. if isinstance(self, str):
  531. return self.decode().encode(encoding, errors)
  532. # Encode characters one at a time.
  533. result = []
  534. for i, char in enumerate(self):
  535. char_bytes = self._stringtype.encode(char, encoding, errors)
  536. for char_byte in char_bytes:
  537. if isinstance(char, SimpleSourcedString):
  538. result.append(SourcedString(char_byte, char.source))
  539. else:
  540. assert not isinstance(char, CompoundSourcedString)
  541. result.append(char_byte)
  542. return SourcedString.concat(result)
  543. # Byte string -> unicode string.
  544. def decode(self, encoding=None, errors='strict'):
  545. if encoding is None: encoding = sys.getdefaultencoding()
  546. if isinstance(self, unicode):
  547. return self.encode().decode(encoding, errors)
  548. # Decode self into a plain unicode string.
  549. unicode_chars = self._stringtype.decode(self, encoding, errors)
  550. # Special case: if the resulting string has the same length
  551. # that the source string does, then we can safely assume that
  552. # each character is encoded with one byte; so we can just
  553. # reuse our source.
  554. if len(unicode_chars) == len(self):
  555. return self._decode_one_to_one(unicode_chars)
  556. # Otherwise: re-encode the characters, one at a time, to
  557. # determine how long their encodings are.
  558. result = []
  559. first_byte = 0
  560. for unicode_char in unicode_chars:
  561. char_width = len(unicode_char.encode(encoding, errors))
  562. last_byte = first_byte + char_width - 1
  563. if (isinstance(self[first_byte], SourcedString) and
  564. isinstance(self[last_byte], SourcedString)):
  565. begin = self[first_byte].begin
  566. end = self[last_byte].end
  567. if end-begin == 1:
  568. source = StringSource(docid=self[first_byte].docid,
  569. begin=begin, end=end)
  570. else:
  571. source = StringSource(docid=self[first_byte].docid,
  572. offsets=[begin, end])
  573. result.append(SourcedString(unicode_char, source))
  574. else:
  575. result.append(unicode_char)
  576. # First byte of the next char is 1+last byte of this char.
  577. first_byte = last_byte+1
  578. if last_byte+1 != len(self):
  579. raise AssertionError("SourcedString.decode() does not support "
  580. "encodings that are not symmetric.")
  581. return SourcedString.concat(result)
  582. @abstract
  583. def _decode_one_to_one(unicode_chars):
  584. """
  585. Helper for ``self.decode()``. Returns a unicode-decoded
  586. version of this ``SourcedString``. ``unicode_chars`` is the
  587. unicode-decoded contents of this ``SourcedString``.
  588. This is used in the special case where the decoded string has
  589. the same length that the source string does. As a result, we
  590. can safely assume that each character is encoded with one
  591. byte; so we can just reuse our source. E.g., this will happen
  592. when decoding an ASCII string with utf-8.
  593. """
  594. def _mixed_string_types(self, *args):
  595. """
  596. Return true if the list (self,)+args contains at least one
  597. unicode string and at least one byte string. (If this is the
  598. case, then all byte strings should be converted to unicode by
  599. calling decode() before the operation is performed. You can
  600. do this automatically using ``_decode_and_call()``.
  601. """
  602. any_unicode = isinstance(self, unicode)
  603. any_bytestring = isinstance(self, str)
  604. for arg in args:
  605. any_unicode = any_unicode or isinstance(arg, unicode)
  606. any_bytestring = any_bytestring or isinstance(arg, str)
  607. return any_unicode and any_bytestring
  608. def _decode_and_call(self, op, *args):
  609. """
  610. If self or any of the values in args is a byte string, then
  611. convert it to unicode by calling its decode() method. Then
  612. return the result of calling self.op(*args). ``op`` is
  613. specified using a string, because if ``self`` is a byte string,
  614. then it will change type when it is decoded.
  615. """
  616. # Make sure all args are decoded to unicode.
  617. args = list(args)
  618. for i in range(len(args)):
  619. if isinstance(args[i], str):
  620. args[i] = args[i].decode()
  621. # Make sure self is decoded to unicode.
  622. if isinstance(self, str):
  623. self = self.decode()
  624. # Retry the operation.
  625. method = getattr(self, op)
  626. return method(*args)
  627. #//////////////////////////////////////////////////////////////////////
  628. #{ Display
  629. #//////////////////////////////////////////////////////////////////////
  630. def pprint(self, vertical=False, wrap=70):
  631. """
  632. Return a string containing a pretty-printed display of this
  633. sourced string.
  634. :param vertical: If true, then the returned display string will
  635. have vertical orientation, rather than the default horizontal
  636. orientation.
  637. :param wrap: Controls when the pretty-printed output is wrapped
  638. to the next line. If ``wrap`` is an integer, then lines are
  639. wrapped when they become longer than ``wrap``. If ``wrap`` is
  640. a string, then lines are wrapped immediately following that
  641. string. If ``wrap`` is None, then lines are never wrapped.
  642. """
  643. if len(self) == 0: return '[Empty String]'
  644. if vertical == 1: return self._pprint_vertical() # special-cased
  645. max_digits = len(str(max(max(getattr(c, 'begin', 0),
  646. getattr(c, 'end', 0)) for c in self)))
  647. if not isinstance(wrap, (basestring, int, long, type(None))):
  648. raise TypeError("Expected wrap to be a sring, int, or None.")
  649. result = []
  650. prev_offset = None # most recently displayed offset.
  651. prev_docid = None
  652. docid_line = ''
  653. output_lines = [''] * (max_digits+2)
  654. for pos, char in enumerate(self):
  655. char_begin = getattr(char, 'begin', None)
  656. char_end = getattr(char, 'end', None)
  657. char_docid = getattr(char, 'docid', None)
  658. # If the docid changed, then display the docid for the
  659. # previous segment.
  660. if char_docid != prev_docid:
  661. width = len(output_lines[0]) - len(docid_line)
  662. docid_line += self._pprint_docid(width, prev_docid)
  663. prev_docid = char_docid
  664. # Put a cap on the beginning of sourceless strings
  665. elif not output_lines[0] and char_begin is None:
  666. self._pprint_offset(' ', output_lines)
  667. # Display the character.
  668. if char_begin != prev_offset:
  669. self._pprint_offset(char_begin, output_lines)
  670. self._pprint_char(char, output_lines)
  671. self._pprint_offset(char_end, output_lines)
  672. prev_offset = char_end
  673. # Decide whether we're at the end of the line or not.
  674. line_len = len(output_lines[0])
  675. if ( (isinstance(wrap, basestring) and
  676. self[max(0,pos-len(wrap)+1):pos+1] == wrap) or
  677. (isinstance(wrap, (int,long)) and line_len>=wrap) or
  678. pos == len(self)-1):
  679. # Put a cap on the end of sourceless strings
  680. if char_end is None:
  681. self._pprint_offset(' ', output_lines)
  682. # Filter out any empty output lines.
  683. output_lines = [l for l in output_lines if l.strip()]
  684. # Draw the docid line
  685. width = len(output_lines[0]) - len(docid_line)
  686. docid_line += self._pprint_docid(width, prev_docid)
  687. result.append(docid_line)
  688. # Draw the output lines
  689. for output_line in reversed(output_lines):
  690. result.append(output_line)
  691. result.append(output_lines[1])
  692. # Reset variables for the next line.
  693. prev_offset = None
  694. prev_docid = None
  695. docid_line = ''
  696. output_lines = [''] * (max_digits+2)
  697. return '\n'.join(result)
  698. def _pprint_vertical(self):
  699. result = []
  700. prev_offset = None
  701. max_digits = len(str(max(max(getattr(c, 'begin', 0),
  702. getattr(c, 'end', 0)) for c in self)))
  703. for pos, char in enumerate(self):
  704. char_begin = getattr(char, 'begin', None)
  705. char_end = getattr(char, 'end', None)
  706. char_docid = getattr(char, 'docid', None)
  707. if char_begin is None:
  708. assert char_end is None
  709. if pos == 0: result.append('+-----+')
  710. result.append(':%s:' %
  711. self._pprint_char_repr(char).center(5))
  712. if pos == len(self)-1: result.append('+-----+')
  713. prev_offset = None
  714. else:
  715. if char_begin != prev_offset:
  716. result.append('+-----+ %s [%s]' % (
  717. str(char_begin).rjust(max_digits), char_docid))
  718. result.append('|%s| %s [%s]' % (
  719. self._pprint_char_repr(char).center(5),
  720. ' '*max_digits, char_docid))
  721. result.append('+-----+ %s [%s]' % (
  722. str(char_end).rjust(max_digits), char_docid))
  723. prev_offset = char_end
  724. return '\n'.join(result)
  725. _PPRINT_CHAR_REPRS = {'\n': r'\n', '\r': r'\r',
  726. '\a': r'\a', '\t': r'\t'}
  727. def _pprint_docid(self, width, docid):
  728. if docid is None: return ' '*width
  729. else: return '[%s]' % (docid[:width-2].center(width-2, '='))
  730. def _pprint_char_repr(self, char):
  731. # Decide how to represent this character.
  732. if 32 <= ord(char) <= 127:
  733. return str(char)
  734. elif char in self._PPRINT_CHAR_REPRS:
  735. return self._PPRINT_CHAR_REPRS[char]
  736. elif isinstance(char, str):
  737. return r'\x%02x' % ord(char)
  738. else:
  739. return r'\u%04x' % ord(char)
  740. def _pprint_char(self, char, output_lines):
  741. """Helper for ``pprint()``: add a character to the
  742. pretty-printed output."""
  743. char_repr = self._pprint_char_repr(char)
  744. output_lines[0] += char_repr
  745. # Add fillers to the offset lines.
  746. output_lines[1] += '-'*len(char_repr)
  747. for i in range(2, len(output_lines)):
  748. output_lines[i] += ' '*len(char_repr)
  749. def _pprint_offset(self, offset, output_lines):
  750. """Helper for ``pprint()``: add an offset marker to the
  751. pretty-printed output."""
  752. if offset is None: return
  753. output_lines[0] += '|'
  754. output_lines[1] += '+'
  755. offset_rep = str(offset).rjust(len(output_lines)-2)
  756. for digit in range(len(offset_rep)):
  757. output_lines[-digit-1] += offset_rep[digit]
  758. #//////////////////////////////////////////////////////////////////////
  759. # Simple Sourced String
  760. #//////////////////////////////////////////////////////////////////////
  761. class SimpleSourcedString(SourcedString):
  762. """
  763. A single substring of a document, annotated with information about
  764. the location in the document where it was originally found. See
  765. ``SourcedString`` for more information.
  766. """
  767. def __new__(cls, contents, source):
  768. # If the SimpleSourcedString constructor is called directly,
  769. # then choose one of its subclasses to delegate to.
  770. if cls is SimpleSourcedString:
  771. if isinstance(contents, str):
  772. cls = SimpleSourcedByteString
  773. elif isinstance(contents, unicode):
  774. cls = SimpleSourcedUnicodeString
  775. else:
  776. raise TypeError("Expected 'contents' to be a unicode "
  777. "string or a byte string")
  778. # Create the new object using the appropriate string class's
  779. # __new__, which takes just the contents argument.
  780. return cls._stringtype.__new__(cls, contents)
  781. def __init__(self, contents, source):
  782. """
  783. Construct a new sourced string.
  784. :param contents: The string contents of the new sourced string.
  785. :type contents: str or unicode
  786. :param source: The source for the new string. If ``source`` is
  787. a string, then it is used to automatically construct a new
  788. ``ConsecutiveCharStringSource`` with a begin offset of
  789. ``0`` and an end offset of ``len(contents)``. Otherwise,
  790. ``source`` shoulde be a ``StringSource`` whose length matches
  791. the length of ``contents``.
  792. """
  793. if not isinstance(source, StringSource):
  794. source = ConsecutiveCharStringSource(source, 0, len(contents))
  795. elif len(source) != len(contents):
  796. raise ValueError("Length of source (%d) must match length of "
  797. "contents (%d)" % (len(source), len(contents)))
  798. self.source = source
  799. """A ``StringLocation`` specifying the location where this string
  800. occurred in the source document."""
  801. @property
  802. def begin(self):
  803. """
  804. The document offset where the string begins. (I.e.,
  805. the offset of the first character in the string.)"""
  806. return self.source.begin
  807. @property
  808. def end(self):
  809. """The document offset where the string ends. (For character
  810. offsets, one plus the offset of the last character; for byte
  811. offsets, one plus the offset of the last byte that encodes the
  812. last character)."""
  813. return self.source.end
  814. @property
  815. def docid(self):
  816. """
  817. An identifier (such as a filename) that specifies the document
  818. where the string was found.
  819. """
  820. return self.source.docid
  821. @property
  822. def sources(self):
  823. return ((0, self.source),)
  824. def __repr__(self):
  825. if self.end == self.begin+1:
  826. source_repr = '@[%s]' % (self.begin,)
  827. else:
  828. source_repr = '@[%s:%s]' % (self.begin, self.end)
  829. return self._stringtype.__repr__(self) + source_repr
  830. def __getitem__(self, index):
  831. result = self._stringtype.__getitem__(self, index)
  832. if isinstance(index, slice):
  833. if index.step not in (None, 1):
  834. return result
  835. else:
  836. start, stop = slice_bounds(self, index)
  837. return self.__getslice__(start, stop)
  838. else:
  839. return SourcedString(result, self.source[index])
  840. def __getslice__(self, start, stop):
  841. # Negative indices get handled *before* __getslice__ is
  842. # called. Restrict start/stop to be within the range of the
  843. # string, to prevent negative indices from being adjusted
  844. # twice.
  845. start = max(0, min(len(self), start))
  846. stop = max(start, min(len(self), stop))
  847. return SourcedString(
  848. self._stringtype.__getslice__(self, start, stop),
  849. self.source[start:stop])
  850. def capitalize(self):
  851. result = self._stringtype.capitalize(self)
  852. return SourcedString(result, self.source)
  853. def lower(self):
  854. result = self._stringtype.lower(self)
  855. return SourcedString(result, self.source)
  856. def upper(self):
  857. result = self._stringtype.upper(self)
  858. return SourcedString(result, self.source)
  859. def swapcase(self):
  860. result = self._stringtype.swapcase(self)
  861. return SourcedString(result, self.source)
  862. def title(self):
  863. result = self._stringtype.title(self)
  864. return SourcedString(result, self.source)
  865. def _decode_one_to_one(self, unicode_chars):
  866. return SourcedString(unicode_chars, self.source)
  867. #//////////////////////////////////////////////////////////////////////
  868. # Compound Sourced String
  869. #//////////////////////////////////////////////////////////////////////
  870. class CompoundSourcedString(SourcedString):
  871. """
  872. A string constructed by concatenating substrings from multiple
  873. sources, and annotated with information about the locations where
  874. those substrings were originally found. See ``SourcedString`` for
  875. more information.
  876. :ivar substrings: The tuple of substrings that compose this
  877. compound sourced string. Every compound sourced string is
  878. required to have at least two substrings; and the substrings
  879. themselves may never be CompoundSourcedStrings.
  880. """
  881. def __new__(cls, substrings):
  882. # If the CompoundSourcedString constructor is called directly,
  883. # then choose one of its subclasses to delegate to.
  884. if cls is CompoundSourcedString:
  885. # Decide whether to use a unicode string or a byte string.
  886. use_unicode = sum(1 for substring in substrings
  887. if isinstance(substring, unicode))
  888. if use_unicode:
  889. cls = CompoundSourcedUnicodeString
  890. else:
  891. cls = CompoundSourcedByteString
  892. # Build the concatenated string using str.join(), which will
  893. # return a str or unicode object; never a sourced string.
  894. contents = ''.join(substrings)
  895. # Create the new object using the appropriate string class's
  896. # __new__, which takes just the contents argument.
  897. return cls._stringtype.__new__(cls, contents)
  898. def __init__(self, substrings):
  899. """
  900. Construct a new compound sourced string that combines the
  901. given list of substrings.
  902. Typically, compound sourced strings should not be constructed
  903. directly; instead, use ``SourcedString.concat()``, which
  904. flattens nested compound sourced strings, and merges adjacent
  905. substrings when possible.
  906. :raise ValueError: If ``len(substrings) < 2``
  907. :raise ValueError: If ``substrings`` contains any
  908. ``CompoundSourcedString``s.
  909. """
  910. if len(substrings) < 2:
  911. raise ValueError("CompoundSourcedString requires at least "
  912. "two substrings")
  913. # Don't nest compound sourced strings.
  914. for substring in substrings:
  915. if isinstance(substring, CompoundSourcedString):
  916. raise ValueError("substrings may not contain "
  917. "CompoundSourcedStrings.")
  918. self.substrings = tuple(substrings)
  919. @property
  920. def sources(self):
  921. index = 0
  922. source_list = []
  923. for substring in self.substrings:
  924. if isinstance(substring, SourcedString):
  925. source_list.append( (index, substring.source) )
  926. index += len(substring)
  927. return tuple(source_list)
  928. def __repr__(self):
  929. sources = [self._source_repr(s) for s in self.substrings]
  930. source_str = '@[%s]' % ','.join(sources)
  931. return self._stringtype.__repr__(self) + source_str
  932. def _source_repr(self, substring):
  933. if isinstance(substring, SimpleSourcedString):
  934. return '%s:%s' % (substring.begin, substring.end)
  935. else:
  936. return '...'
  937. def __getitem__(self, index):
  938. if isinstance(index, slice):
  939. if index.step not in (None, 1):
  940. return self._stringtype.__getitem__(self, index)
  941. else:
  942. start, stop = slice_bounds(self, index)
  943. return self.__getslice__(start, stop)
  944. else:
  945. if index < 0: index += len(self)
  946. if index < 0 or index >= len(self):
  947. raise IndexError('StringSource index out of range')
  948. return self.__getslice__(index, index+1)
  949. def __getslice__(self, start, stop):
  950. # Bounds checking.
  951. start = max(0, min(len(self), start))
  952. stop = max(start, min(len(self), stop))
  953. # Construct a source list for the resulting string.
  954. result_substrings = []
  955. offset = 0
  956. for substring in self.substrings:
  957. if offset+len(substring) > start:
  958. s, e = max(0, start-offset), stop-offset
  959. result_substrings.append(substring[s:e])
  960. offset += len(substring)
  961. if offset >= stop: break
  962. # Concatentate the resulting substrings.
  963. if len(result_substrings) == 0:
  964. return ''
  965. elif len(result_substrings) == 1:
  966. return result_substrings[0]
  967. else:
  968. return SourcedString.concat(result_substrings)
  969. def capitalize(self):
  970. return SourcedString.concat([s.capitalize() for s in self.substrings])
  971. def lower(self):
  972. return SourcedString.concat([s.lower() for s in self.substrings])
  973. def upper(self):
  974. return SourcedString.concat([s.upper() for s in self.substrings])
  975. def swapcase(self):
  976. return SourcedString.concat([s.swapcase() for s in self.substrings])
  977. def title(self):
  978. return SourcedString.concat([s.title() for s in self.substrings])
  979. def encode(self, encoding=None, errors='strict'):
  980. return SourcedString.concat([s.encode(encoding, errors)
  981. for s in self.substrings])
  982. def _decode_one_to_one(self, unicode_chars):
  983. index = 0
  984. result = []
  985. for substring in self.substrings:
  986. decoded_substring = unicode_chars[index:index+len(substring)]
  987. if isinstance(substring, SourcedString):
  988. result.append(SourcedString(decoded_substring, substring.source))
  989. else:
  990. result.append(decoded_substring)
  991. index += len(substring)
  992. return SourcedString.concat(result)
  993. #//////////////////////////////////////////////////////////////////////
  994. # Concrete Sourced String Classes
  995. #//////////////////////////////////////////////////////////////////////
  996. class SimpleSourcedByteString(SimpleSourcedString, str):
  997. _stringtype = str
  998. class SimpleSourcedUnicodeString(SimpleSourcedString, unicode):
  999. _stringtype = unicode
  1000. class CompoundSourcedByteString(CompoundSourcedString, str):
  1001. _stringtype = str
  1002. class CompoundSourcedUnicodeString(CompoundSourcedString, unicode):
  1003. _stringtype = unicode
  1004. def __init__(self, substrings):
  1005. # If any substrings have type 'str', then decode them to unicode.
  1006. for i in range(len(substrings)):
  1007. if not isinstance(substrings[i], unicode):
  1008. substrings[i] = substrings[i].decode()
  1009. CompoundSourcedString.__init__(self, substrings)
  1010. #//////////////////////////////////////////////////////////////////////
  1011. # Sourced String Regexp
  1012. #//////////////////////////////////////////////////////////////////////
  1013. _original_re_compile = re.compile
  1014. _original_re_sub = re.sub
  1015. _original_re_subn = re.subn
  1016. class SourcedStringRegexp(object):
  1017. """
  1018. Wrapper for regexp pattern objects that cause the ``sub`` and
  1019. ``subn`` methods to return sourced strings.
  1020. """
  1021. def __init__(self, pattern, flags=0):
  1022. if isinstance(pattern, basestring):
  1023. pattern = _original_re_compile(pattern, flags)
  1024. self.pattern = pattern
  1025. def __getattr__(self, attr):
  1026. return getattr(self.pattern, attr)
  1027. def subn(self, repl, string, count=0):
  1028. if (isinstance(repl, SourcedString) or
  1029. isinstance(string, SourcedString)):
  1030. result = ''
  1031. pos = 0
  1032. n = 0
  1033. for match in self.pattern.finditer(string):
  1034. result += string[pos:match.start()]
  1035. result += repl
  1036. pos = match.end()
  1037. n += 1
  1038. if count and n==count: break
  1039. result += string[pos:]
  1040. return result, n
  1041. else:
  1042. return self.pattern.subn(repl, string, count)
  1043. def sub(self, repl, string, count=0):
  1044. return self.subn(repl, string, count)[0]
  1045. @staticmethod
  1046. def patch_re_module():
  1047. """
  1048. Modify the standard ``re`` module by installing new versions of
  1049. the functions ``re.compile``, ``re.sub``, and ``re.subn``,
  1050. causing regular expression substitutions to return
  1051. ``SourcedStrings`` when called with ``SourcedStrings``
  1052. arguments.
  1053. Use this function only if necessary: it potentially affects
  1054. all Python modules that use regular expressions!
  1055. """
  1056. def new_re_sub(pattern, repl, string, count=0):
  1057. return re.compile(pattern).sub(repl, string, count)
  1058. def new_re_subn(pattern, repl, string, count=0):
  1059. return re.compile(pattern).subn(repl, string, count)
  1060. re.compile = SourcedStringRegexp
  1061. re.sub = new_re_sub
  1062. re.subn = new_re_subn
  1063. @staticmethod
  1064. def unpatch_re_module():
  1065. """
  1066. Restore the standard ``re`` module to its original state
  1067. (undoing the work that was done by ``patch_re_module()``).
  1068. """
  1069. re.compile = _original_re_compile
  1070. re.sub = _original_re_sub
  1071. re.subn = _original_re_subn
  1072. #//////////////////////////////////////////////////////////////////////
  1073. # Sourced String Stream
  1074. #//////////////////////////////////////////////////////////////////////
  1075. class SourcedStringStream(object):
  1076. """
  1077. Wrapper for a read-only stream that causes ``read()`` (and related
  1078. methods) to return ``SourcedStringBase``.
  1079. ``seek()`` and ``tell()`` are supported, but (currently) there are
  1080. some restrictions on the values that may be passed to ``seek()``.
  1081. """
  1082. def __init__(self, stream, docid=None, byte_offsets=False):
  1083. self.stream = stream
  1084. """The underlying stream."""
  1085. self.docid = docid
  1086. """The docid attribute for sourced strings"""
  1087. self.charpos = 0
  1088. """The current character (not byte) position"""
  1089. assert not byte_offsets, 'not supported yet!'
  1090. #/////////////////////////////////////////////////////////////////
  1091. # Read methods
  1092. #/////////////////////////////////////////////////////////////////
  1093. def read(self, size=None):
  1094. if size is None: return self._sourced_string(self.stream.read())
  1095. else: return self._sourced_string(self.stream.read(size))
  1096. def readline(self, size=None):
  1097. if size is None: return self._sourced_string(self.stream.readline())
  1098. else: return self._sourced_string(self.stream.readline(size))
  1099. def readlines(self, sizehint=None, keepends=True):
  1100. """
  1101. Read this file's contents, decode them using this reader's
  1102. encoding, and return it as a list of unicode lines.
  1103. :rtype: list(unicode)
  1104. :param sizehint: Ignored.
  1105. :param keepends: If false, then strip newlines.
  1106. """
  1107. return self.read().splitlines(keepends)
  1108. def next(self):
  1109. """Return the next decoded line from the underlying stream."""
  1110. line = self.readline()
  1111. if line: return line
  1112. else: raise StopIteration
  1113. def __iter__(self):
  1114. """Return self"""
  1115. return self
  1116. def xreadlines(self):
  1117. """Return self"""
  1118. return self
  1119. def _sourced_string(self, contents):
  1120. """Turn the given string into an sourced string, and update
  1121. charpos."""
  1122. # [xx] currently we only support character offsets, not byte
  1123. # offsets!
  1124. source = ConsecutiveCharStringSource(self.docid, self.charpos,
  1125. self.charpos+len(contents))
  1126. self.charpos += len(contents)
  1127. return SourcedString(contents, source)
  1128. #/////////////////////////////////////////////////////////////////
  1129. # Pass-through methods & properties
  1130. #/////////////////////////////////////////////////////////////////
  1131. @property
  1132. def closed(self):
  1133. """True if the underlying stream is closed."""
  1134. return self.stream.closed
  1135. @property
  1136. def name(self):
  1137. """The name of the underlying stream."""
  1138. return self.stream.name
  1139. @property
  1140. def mode(self):
  1141. """The mode of the underlying stream."""
  1142. return self.stream.mode
  1143. def close(self):
  1144. """Close the underlying stream."""
  1145. self.stream.close()
  1146. #/////////////////////////////////////////////////////////////////
  1147. # Seek and tell
  1148. #/////////////////////////////////////////////////////////////////
  1149. class SourcedStringStreamPos(int):
  1150. def __new__(cls, bytepos, charpos):
  1151. self = int.__new__(cls, bytepos)
  1152. self.charpos = charpos
  1153. return self
  1154. def seek(self, offset, whence=0):
  1155. if whence == 0:
  1156. if isinstance(offset, self.SourcedStringStreamPos):
  1157. self.stream.seek(offset)
  1158. self.charpos = offset.charpos
  1159. elif offset == 0:
  1160. self.stream.seek(0)
  1161. self.charpos = 0
  1162. else:
  1163. raise TypeError('seek() must be called with a value that '
  1164. 'was returned by tell().')
  1165. elif whence == 1:
  1166. raise TypeError('Relative seek not supported for '
  1167. 'SourcedStringStream.')
  1168. elif whence == 2:
  1169. raise TypeError('Seek-from-end not supported for '
  1170. 'SourcedStringStream.')
  1171. else:
  1172. raise ValueError('Bad whence value %r' % whence)
  1173. def tell(self):
  1174. bytepos = self.stream.tell()
  1175. return self.SourcedStringStreamPos(bytepos, self.charpos)