data.py 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. # Natural Language Toolkit: Utility functions
  2. #
  3. # Copyright (C) 2001-2020 NLTK Project
  4. # Author: Edward Loper <edloper@gmail.com>
  5. # URL: <http://nltk.org/>
  6. # For license information, see LICENSE.TXT
  7. """
  8. Functions to find and load NLTK resource files, such as corpora,
  9. grammars, and saved processing objects. Resource files are identified
  10. using URLs, such as ``nltk:corpora/abc/rural.txt`` or
  11. ``http://nltk.org/sample/toy.cfg``. The following URL protocols are
  12. supported:
  13. - ``file:path``: Specifies the file whose path is *path*.
  14. Both relative and absolute paths may be used.
  15. - ``http://host/path``: Specifies the file stored on the web
  16. server *host* at path *path*.
  17. - ``nltk:path``: Specifies the file stored in the NLTK data
  18. package at *path*. NLTK will search for these files in the
  19. directories specified by ``nltk.data.path``.
  20. If no protocol is specified, then the default protocol ``nltk:`` will
  21. be used.
  22. This module provides to functions that can be used to access a
  23. resource file, given its URL: ``load()`` loads a given resource, and
  24. adds it to a resource cache; and ``retrieve()`` copies a given resource
  25. to a local file.
  26. """
  27. import functools
  28. import textwrap
  29. import io
  30. from io import BytesIO
  31. import os
  32. import re
  33. import sys
  34. import zipfile
  35. import codecs
  36. import pickle
  37. from abc import ABCMeta, abstractmethod
  38. from gzip import GzipFile, WRITE as GZ_WRITE
  39. from urllib.request import urlopen, url2pathname
  40. try:
  41. from zlib import Z_SYNC_FLUSH as FLUSH
  42. except ImportError:
  43. from zlib import Z_FINISH as FLUSH
  44. # this import should be more specific:
  45. import nltk
  46. from nltk.compat import py3_data, add_py3_data
  47. from nltk.internals import deprecated
  48. textwrap_indent = functools.partial(textwrap.indent, prefix=" ")
  49. ######################################################################
  50. # Search Path
  51. ######################################################################
  52. path = []
  53. """A list of directories where the NLTK data package might reside.
  54. These directories will be checked in order when looking for a
  55. resource in the data package. Note that this allows users to
  56. substitute in their own versions of resources, if they have them
  57. (e.g., in their home directory under ~/nltk_data)."""
  58. # User-specified locations:
  59. _paths_from_env = os.environ.get("NLTK_DATA", str("")).split(os.pathsep)
  60. path += [d for d in _paths_from_env if d]
  61. if "APPENGINE_RUNTIME" not in os.environ and os.path.expanduser("~/") != "~/":
  62. path.append(os.path.expanduser(str("~/nltk_data")))
  63. if sys.platform.startswith("win"):
  64. # Common locations on Windows:
  65. path += [
  66. os.path.join(sys.prefix, str("nltk_data")),
  67. os.path.join(sys.prefix, str("share"), str("nltk_data")),
  68. os.path.join(sys.prefix, str("lib"), str("nltk_data")),
  69. os.path.join(os.environ.get(str("APPDATA"), str("C:\\")), str("nltk_data")),
  70. str(r"C:\nltk_data"),
  71. str(r"D:\nltk_data"),
  72. str(r"E:\nltk_data"),
  73. ]
  74. else:
  75. # Common locations on UNIX & OS X:
  76. path += [
  77. os.path.join(sys.prefix, str("nltk_data")),
  78. os.path.join(sys.prefix, str("share"), str("nltk_data")),
  79. os.path.join(sys.prefix, str("lib"), str("nltk_data")),
  80. str("/usr/share/nltk_data"),
  81. str("/usr/local/share/nltk_data"),
  82. str("/usr/lib/nltk_data"),
  83. str("/usr/local/lib/nltk_data"),
  84. ]
  85. ######################################################################
  86. # Util Functions
  87. ######################################################################
  88. def gzip_open_unicode(
  89. filename,
  90. mode="rb",
  91. compresslevel=9,
  92. encoding="utf-8",
  93. fileobj=None,
  94. errors=None,
  95. newline=None,
  96. ):
  97. if fileobj is None:
  98. fileobj = GzipFile(filename, mode, compresslevel, fileobj)
  99. return io.TextIOWrapper(fileobj, encoding, errors, newline)
  100. def split_resource_url(resource_url):
  101. """
  102. Splits a resource url into "<protocol>:<path>".
  103. >>> windows = sys.platform.startswith('win')
  104. >>> split_resource_url('nltk:home/nltk')
  105. ('nltk', 'home/nltk')
  106. >>> split_resource_url('nltk:/home/nltk')
  107. ('nltk', '/home/nltk')
  108. >>> split_resource_url('file:/home/nltk')
  109. ('file', '/home/nltk')
  110. >>> split_resource_url('file:///home/nltk')
  111. ('file', '/home/nltk')
  112. >>> split_resource_url('file:///C:/home/nltk')
  113. ('file', '/C:/home/nltk')
  114. """
  115. protocol, path_ = resource_url.split(":", 1)
  116. if protocol == "nltk":
  117. pass
  118. elif protocol == "file":
  119. if path_.startswith("/"):
  120. path_ = "/" + path_.lstrip("/")
  121. else:
  122. path_ = re.sub(r"^/{0,2}", "", path_)
  123. return protocol, path_
  124. def normalize_resource_url(resource_url):
  125. r"""
  126. Normalizes a resource url
  127. >>> windows = sys.platform.startswith('win')
  128. >>> os.path.normpath(split_resource_url(normalize_resource_url('file:grammar.fcfg'))[1]) == \
  129. ... ('\\' if windows else '') + os.path.abspath(os.path.join(os.curdir, 'grammar.fcfg'))
  130. True
  131. >>> not windows or normalize_resource_url('file:C:/dir/file') == 'file:///C:/dir/file'
  132. True
  133. >>> not windows or normalize_resource_url('file:C:\\dir\\file') == 'file:///C:/dir/file'
  134. True
  135. >>> not windows or normalize_resource_url('file:C:\\dir/file') == 'file:///C:/dir/file'
  136. True
  137. >>> not windows or normalize_resource_url('file://C:/dir/file') == 'file:///C:/dir/file'
  138. True
  139. >>> not windows or normalize_resource_url('file:////C:/dir/file') == 'file:///C:/dir/file'
  140. True
  141. >>> not windows or normalize_resource_url('nltk:C:/dir/file') == 'file:///C:/dir/file'
  142. True
  143. >>> not windows or normalize_resource_url('nltk:C:\\dir\\file') == 'file:///C:/dir/file'
  144. True
  145. >>> windows or normalize_resource_url('file:/dir/file/toy.cfg') == 'file:///dir/file/toy.cfg'
  146. True
  147. >>> normalize_resource_url('nltk:home/nltk')
  148. 'nltk:home/nltk'
  149. >>> windows or normalize_resource_url('nltk:/home/nltk') == 'file:///home/nltk'
  150. True
  151. >>> normalize_resource_url('http://example.com/dir/file')
  152. 'http://example.com/dir/file'
  153. >>> normalize_resource_url('dir/file')
  154. 'nltk:dir/file'
  155. """
  156. try:
  157. protocol, name = split_resource_url(resource_url)
  158. except ValueError:
  159. # the resource url has no protocol, use the nltk protocol by default
  160. protocol = "nltk"
  161. name = resource_url
  162. # use file protocol if the path is an absolute path
  163. if protocol == "nltk" and os.path.isabs(name):
  164. protocol = "file://"
  165. name = normalize_resource_name(name, False, None)
  166. elif protocol == "file":
  167. protocol = "file://"
  168. # name is absolute
  169. name = normalize_resource_name(name, False, None)
  170. elif protocol == "nltk":
  171. protocol = "nltk:"
  172. name = normalize_resource_name(name, True)
  173. else:
  174. # handled by urllib
  175. protocol += "://"
  176. return "".join([protocol, name])
  177. def normalize_resource_name(resource_name, allow_relative=True, relative_path=None):
  178. """
  179. :type resource_name: str or unicode
  180. :param resource_name: The name of the resource to search for.
  181. Resource names are posix-style relative path names, such as
  182. ``corpora/brown``. Directory names will automatically
  183. be converted to a platform-appropriate path separator.
  184. Directory trailing slashes are preserved
  185. >>> windows = sys.platform.startswith('win')
  186. >>> normalize_resource_name('.', True)
  187. './'
  188. >>> normalize_resource_name('./', True)
  189. './'
  190. >>> windows or normalize_resource_name('dir/file', False, '/') == '/dir/file'
  191. True
  192. >>> not windows or normalize_resource_name('C:/file', False, '/') == '/C:/file'
  193. True
  194. >>> windows or normalize_resource_name('/dir/file', False, '/') == '/dir/file'
  195. True
  196. >>> windows or normalize_resource_name('../dir/file', False, '/') == '/dir/file'
  197. True
  198. >>> not windows or normalize_resource_name('/dir/file', True, '/') == 'dir/file'
  199. True
  200. >>> windows or normalize_resource_name('/dir/file', True, '/') == '/dir/file'
  201. True
  202. """
  203. is_dir = bool(re.search(r"[\\/.]$", resource_name)) or resource_name.endswith(
  204. os.path.sep
  205. )
  206. if sys.platform.startswith("win"):
  207. resource_name = resource_name.lstrip("/")
  208. else:
  209. resource_name = re.sub(r"^/+", "/", resource_name)
  210. if allow_relative:
  211. resource_name = os.path.normpath(resource_name)
  212. else:
  213. if relative_path is None:
  214. relative_path = os.curdir
  215. resource_name = os.path.abspath(os.path.join(relative_path, resource_name))
  216. resource_name = resource_name.replace("\\", "/").replace(os.path.sep, "/")
  217. if sys.platform.startswith("win") and os.path.isabs(resource_name):
  218. resource_name = "/" + resource_name
  219. if is_dir and not resource_name.endswith("/"):
  220. resource_name += "/"
  221. return resource_name
  222. ######################################################################
  223. # Path Pointers
  224. ######################################################################
  225. class PathPointer(metaclass=ABCMeta):
  226. """
  227. An abstract base class for 'path pointers,' used by NLTK's data
  228. package to identify specific paths. Two subclasses exist:
  229. ``FileSystemPathPointer`` identifies a file that can be accessed
  230. directly via a given absolute path. ``ZipFilePathPointer``
  231. identifies a file contained within a zipfile, that can be accessed
  232. by reading that zipfile.
  233. """
  234. @abstractmethod
  235. def open(self, encoding=None):
  236. """
  237. Return a seekable read-only stream that can be used to read
  238. the contents of the file identified by this path pointer.
  239. :raise IOError: If the path specified by this pointer does
  240. not contain a readable file.
  241. """
  242. @abstractmethod
  243. def file_size(self):
  244. """
  245. Return the size of the file pointed to by this path pointer,
  246. in bytes.
  247. :raise IOError: If the path specified by this pointer does
  248. not contain a readable file.
  249. """
  250. @abstractmethod
  251. def join(self, fileid):
  252. """
  253. Return a new path pointer formed by starting at the path
  254. identified by this pointer, and then following the relative
  255. path given by ``fileid``. The path components of ``fileid``
  256. should be separated by forward slashes, regardless of
  257. the underlying file system's path seperator character.
  258. """
  259. class FileSystemPathPointer(PathPointer, str):
  260. """
  261. A path pointer that identifies a file which can be accessed
  262. directly via a given absolute path.
  263. """
  264. @py3_data
  265. def __init__(self, _path):
  266. """
  267. Create a new path pointer for the given absolute path.
  268. :raise IOError: If the given path does not exist.
  269. """
  270. _path = os.path.abspath(_path)
  271. if not os.path.exists(_path):
  272. raise IOError("No such file or directory: %r" % _path)
  273. self._path = _path
  274. # There's no need to call str.__init__(), since it's a no-op;
  275. # str does all of its setup work in __new__.
  276. @property
  277. def path(self):
  278. """The absolute path identified by this path pointer."""
  279. return self._path
  280. def open(self, encoding=None):
  281. stream = open(self._path, "rb")
  282. if encoding is not None:
  283. stream = SeekableUnicodeStreamReader(stream, encoding)
  284. return stream
  285. def file_size(self):
  286. return os.stat(self._path).st_size
  287. def join(self, fileid):
  288. _path = os.path.join(self._path, fileid)
  289. return FileSystemPathPointer(_path)
  290. def __repr__(self):
  291. return "FileSystemPathPointer(%r)" % self._path
  292. def __str__(self):
  293. return self._path
  294. @deprecated("Use gzip.GzipFile instead as it also uses a buffer.")
  295. class BufferedGzipFile(GzipFile):
  296. """A ``GzipFile`` subclass for compatibility with older nltk releases.
  297. Use ``GzipFile`` directly as it also buffers in all supported
  298. Python versions.
  299. """
  300. @py3_data
  301. def __init__(
  302. self, filename=None, mode=None, compresslevel=9, fileobj=None, **kwargs
  303. ):
  304. """Return a buffered gzip file object."""
  305. GzipFile.__init__(self, filename, mode, compresslevel, fileobj)
  306. def write(self, data):
  307. # This is identical to GzipFile.write but does not return
  308. # the bytes written to retain compatibility.
  309. super().write(data)
  310. class GzipFileSystemPathPointer(FileSystemPathPointer):
  311. """
  312. A subclass of ``FileSystemPathPointer`` that identifies a gzip-compressed
  313. file located at a given absolute path. ``GzipFileSystemPathPointer`` is
  314. appropriate for loading large gzip-compressed pickle objects efficiently.
  315. """
  316. def open(self, encoding=None):
  317. stream = GzipFile(self._path, "rb")
  318. if encoding:
  319. stream = SeekableUnicodeStreamReader(stream, encoding)
  320. return stream
  321. class ZipFilePathPointer(PathPointer):
  322. """
  323. A path pointer that identifies a file contained within a zipfile,
  324. which can be accessed by reading that zipfile.
  325. """
  326. @py3_data
  327. def __init__(self, zipfile, entry=""):
  328. """
  329. Create a new path pointer pointing at the specified entry
  330. in the given zipfile.
  331. :raise IOError: If the given zipfile does not exist, or if it
  332. does not contain the specified entry.
  333. """
  334. if isinstance(zipfile, str):
  335. zipfile = OpenOnDemandZipFile(os.path.abspath(zipfile))
  336. # Check that the entry exists:
  337. if entry:
  338. # Normalize the entry string, it should be relative:
  339. entry = normalize_resource_name(entry, True, "/").lstrip("/")
  340. try:
  341. zipfile.getinfo(entry)
  342. except Exception:
  343. # Sometimes directories aren't explicitly listed in
  344. # the zip file. So if `entry` is a directory name,
  345. # then check if the zipfile contains any files that
  346. # are under the given directory.
  347. if entry.endswith("/") and [
  348. n for n in zipfile.namelist() if n.startswith(entry)
  349. ]:
  350. pass # zipfile contains a file in that directory.
  351. else:
  352. # Otherwise, complain.
  353. raise IOError(
  354. "Zipfile %r does not contain %r" % (zipfile.filename, entry)
  355. )
  356. self._zipfile = zipfile
  357. self._entry = entry
  358. @property
  359. def zipfile(self):
  360. """
  361. The zipfile.ZipFile object used to access the zip file
  362. containing the entry identified by this path pointer.
  363. """
  364. return self._zipfile
  365. @property
  366. def entry(self):
  367. """
  368. The name of the file within zipfile that this path
  369. pointer points to.
  370. """
  371. return self._entry
  372. def open(self, encoding=None):
  373. data = self._zipfile.read(self._entry)
  374. stream = BytesIO(data)
  375. if self._entry.endswith(".gz"):
  376. stream = GzipFile(self._entry, fileobj=stream)
  377. elif encoding is not None:
  378. stream = SeekableUnicodeStreamReader(stream, encoding)
  379. return stream
  380. def file_size(self):
  381. return self._zipfile.getinfo(self._entry).file_size
  382. def join(self, fileid):
  383. entry = "%s/%s" % (self._entry, fileid)
  384. return ZipFilePathPointer(self._zipfile, entry)
  385. def __repr__(self):
  386. return str("ZipFilePathPointer(%r, %r)") % (self._zipfile.filename, self._entry)
  387. def __str__(self):
  388. return os.path.normpath(os.path.join(self._zipfile.filename, self._entry))
  389. ######################################################################
  390. # Access Functions
  391. ######################################################################
  392. # Don't use a weak dictionary, because in the common case this
  393. # causes a lot more reloading that necessary.
  394. _resource_cache = {}
  395. """A dictionary used to cache resources so that they won't
  396. need to be loaded more than once."""
  397. def find(resource_name, paths=None):
  398. """
  399. Find the given resource by searching through the directories and
  400. zip files in paths, where a None or empty string specifies an absolute path.
  401. Returns a corresponding path name. If the given resource is not
  402. found, raise a ``LookupError``, whose message gives a pointer to
  403. the installation instructions for the NLTK downloader.
  404. Zip File Handling:
  405. - If ``resource_name`` contains a component with a ``.zip``
  406. extension, then it is assumed to be a zipfile; and the
  407. remaining path components are used to look inside the zipfile.
  408. - If any element of ``nltk.data.path`` has a ``.zip`` extension,
  409. then it is assumed to be a zipfile.
  410. - If a given resource name that does not contain any zipfile
  411. component is not found initially, then ``find()`` will make a
  412. second attempt to find that resource, by replacing each
  413. component *p* in the path with *p.zip/p*. For example, this
  414. allows ``find()`` to map the resource name
  415. ``corpora/chat80/cities.pl`` to a zip file path pointer to
  416. ``corpora/chat80.zip/chat80/cities.pl``.
  417. - When using ``find()`` to locate a directory contained in a
  418. zipfile, the resource name must end with the forward slash
  419. character. Otherwise, ``find()`` will not locate the
  420. directory.
  421. :type resource_name: str or unicode
  422. :param resource_name: The name of the resource to search for.
  423. Resource names are posix-style relative path names, such as
  424. ``corpora/brown``. Directory names will be
  425. automatically converted to a platform-appropriate path separator.
  426. :rtype: str
  427. """
  428. resource_name = normalize_resource_name(resource_name, True)
  429. # Resolve default paths at runtime in-case the user overrides
  430. # nltk.data.path
  431. if paths is None:
  432. paths = path
  433. # Check if the resource name includes a zipfile name
  434. m = re.match(r"(.*\.zip)/?(.*)$|", resource_name)
  435. zipfile, zipentry = m.groups()
  436. # Check each item in our path
  437. for path_ in paths:
  438. # Is the path item a zipfile?
  439. if path_ and (os.path.isfile(path_) and path_.endswith(".zip")):
  440. try:
  441. return ZipFilePathPointer(path_, resource_name)
  442. except IOError:
  443. # resource not in zipfile
  444. continue
  445. # Is the path item a directory or is resource_name an absolute path?
  446. elif not path_ or os.path.isdir(path_):
  447. if zipfile is None:
  448. p = os.path.join(path_, url2pathname(resource_name))
  449. if os.path.exists(p):
  450. if p.endswith(".gz"):
  451. return GzipFileSystemPathPointer(p)
  452. else:
  453. return FileSystemPathPointer(p)
  454. else:
  455. p = os.path.join(path_, url2pathname(zipfile))
  456. if os.path.exists(p):
  457. try:
  458. return ZipFilePathPointer(p, zipentry)
  459. except IOError:
  460. # resource not in zipfile
  461. continue
  462. # Fallback: if the path doesn't include a zip file, then try
  463. # again, assuming that one of the path components is inside a
  464. # zipfile of the same name.
  465. if zipfile is None:
  466. pieces = resource_name.split("/")
  467. for i in range(len(pieces)):
  468. modified_name = "/".join(pieces[:i] + [pieces[i] + ".zip"] + pieces[i:])
  469. try:
  470. return find(modified_name, paths)
  471. except LookupError:
  472. pass
  473. # Identify the package (i.e. the .zip file) to download.
  474. resource_zipname = resource_name.split("/")[1]
  475. if resource_zipname.endswith(".zip"):
  476. resource_zipname = resource_zipname.rpartition(".")[0]
  477. # Display a friendly error message if the resource wasn't found:
  478. msg = str(
  479. "Resource \33[93m{resource}\033[0m not found.\n"
  480. "Please use the NLTK Downloader to obtain the resource:\n\n"
  481. "\33[31m" # To display red text in terminal.
  482. ">>> import nltk\n"
  483. ">>> nltk.download('{resource}')\n"
  484. "\033[0m"
  485. ).format(resource=resource_zipname)
  486. msg = textwrap_indent(msg)
  487. msg += "\n For more information see: https://www.nltk.org/data.html\n"
  488. msg += "\n Attempted to load \33[93m{resource_name}\033[0m\n".format(
  489. resource_name=resource_name
  490. )
  491. msg += "\n Searched in:" + "".join("\n - %r" % d for d in paths)
  492. sep = "*" * 70
  493. resource_not_found = "\n%s\n%s\n%s\n" % (sep, msg, sep)
  494. raise LookupError(resource_not_found)
  495. def retrieve(resource_url, filename=None, verbose=True):
  496. """
  497. Copy the given resource to a local file. If no filename is
  498. specified, then use the URL's filename. If there is already a
  499. file named ``filename``, then raise a ``ValueError``.
  500. :type resource_url: str
  501. :param resource_url: A URL specifying where the resource should be
  502. loaded from. The default protocol is "nltk:", which searches
  503. for the file in the the NLTK data package.
  504. """
  505. resource_url = normalize_resource_url(resource_url)
  506. if filename is None:
  507. if resource_url.startswith("file:"):
  508. filename = os.path.split(resource_url)[-1]
  509. else:
  510. filename = re.sub(r"(^\w+:)?.*/", "", resource_url)
  511. if os.path.exists(filename):
  512. filename = os.path.abspath(filename)
  513. raise ValueError("File %r already exists!" % filename)
  514. if verbose:
  515. print("Retrieving %r, saving to %r" % (resource_url, filename))
  516. # Open the input & output streams.
  517. infile = _open(resource_url)
  518. # Copy infile -> outfile, using 64k blocks.
  519. with open(filename, "wb") as outfile:
  520. while True:
  521. s = infile.read(1024 * 64) # 64k blocks.
  522. outfile.write(s)
  523. if not s:
  524. break
  525. infile.close()
  526. #: A dictionary describing the formats that are supported by NLTK's
  527. #: load() method. Keys are format names, and values are format
  528. #: descriptions.
  529. FORMATS = {
  530. "pickle": "A serialized python object, stored using the pickle module.",
  531. "json": "A serialized python object, stored using the json module.",
  532. "yaml": "A serialized python object, stored using the yaml module.",
  533. "cfg": "A context free grammar.",
  534. "pcfg": "A probabilistic CFG.",
  535. "fcfg": "A feature CFG.",
  536. "fol": "A list of first order logic expressions, parsed with "
  537. "nltk.sem.logic.Expression.fromstring.",
  538. "logic": "A list of first order logic expressions, parsed with "
  539. "nltk.sem.logic.LogicParser. Requires an additional logic_parser "
  540. "parameter",
  541. "val": "A semantic valuation, parsed by nltk.sem.Valuation.fromstring.",
  542. "raw": "The raw (byte string) contents of a file.",
  543. "text": "The raw (unicode string) contents of a file. ",
  544. }
  545. #: A dictionary mapping from file extensions to format names, used
  546. #: by load() when format="auto" to decide the format for a
  547. #: given resource url.
  548. AUTO_FORMATS = {
  549. "pickle": "pickle",
  550. "json": "json",
  551. "yaml": "yaml",
  552. "cfg": "cfg",
  553. "pcfg": "pcfg",
  554. "fcfg": "fcfg",
  555. "fol": "fol",
  556. "logic": "logic",
  557. "val": "val",
  558. "txt": "text",
  559. "text": "text",
  560. }
  561. def load(
  562. resource_url,
  563. format="auto",
  564. cache=True,
  565. verbose=False,
  566. logic_parser=None,
  567. fstruct_reader=None,
  568. encoding=None,
  569. ):
  570. """
  571. Load a given resource from the NLTK data package. The following
  572. resource formats are currently supported:
  573. - ``pickle``
  574. - ``json``
  575. - ``yaml``
  576. - ``cfg`` (context free grammars)
  577. - ``pcfg`` (probabilistic CFGs)
  578. - ``fcfg`` (feature-based CFGs)
  579. - ``fol`` (formulas of First Order Logic)
  580. - ``logic`` (Logical formulas to be parsed by the given logic_parser)
  581. - ``val`` (valuation of First Order Logic model)
  582. - ``text`` (the file contents as a unicode string)
  583. - ``raw`` (the raw file contents as a byte string)
  584. If no format is specified, ``load()`` will attempt to determine a
  585. format based on the resource name's file extension. If that
  586. fails, ``load()`` will raise a ``ValueError`` exception.
  587. For all text formats (everything except ``pickle``, ``json``, ``yaml`` and ``raw``),
  588. it tries to decode the raw contents using UTF-8, and if that doesn't
  589. work, it tries with ISO-8859-1 (Latin-1), unless the ``encoding``
  590. is specified.
  591. :type resource_url: str
  592. :param resource_url: A URL specifying where the resource should be
  593. loaded from. The default protocol is "nltk:", which searches
  594. for the file in the the NLTK data package.
  595. :type cache: bool
  596. :param cache: If true, add this resource to a cache. If load()
  597. finds a resource in its cache, then it will return it from the
  598. cache rather than loading it.
  599. :type verbose: bool
  600. :param verbose: If true, print a message when loading a resource.
  601. Messages are not displayed when a resource is retrieved from
  602. the cache.
  603. :type logic_parser: LogicParser
  604. :param logic_parser: The parser that will be used to parse logical
  605. expressions.
  606. :type fstruct_reader: FeatStructReader
  607. :param fstruct_reader: The parser that will be used to parse the
  608. feature structure of an fcfg.
  609. :type encoding: str
  610. :param encoding: the encoding of the input; only used for text formats.
  611. """
  612. resource_url = normalize_resource_url(resource_url)
  613. resource_url = add_py3_data(resource_url)
  614. # Determine the format of the resource.
  615. if format == "auto":
  616. resource_url_parts = resource_url.split(".")
  617. ext = resource_url_parts[-1]
  618. if ext == "gz":
  619. ext = resource_url_parts[-2]
  620. format = AUTO_FORMATS.get(ext)
  621. if format is None:
  622. raise ValueError(
  623. "Could not determine format for %s based "
  624. 'on its file\nextension; use the "format" '
  625. "argument to specify the format explicitly." % resource_url
  626. )
  627. if format not in FORMATS:
  628. raise ValueError("Unknown format type: %s!" % (format,))
  629. # If we've cached the resource, then just return it.
  630. if cache:
  631. resource_val = _resource_cache.get((resource_url, format))
  632. if resource_val is not None:
  633. if verbose:
  634. print("<<Using cached copy of %s>>" % (resource_url,))
  635. return resource_val
  636. # Let the user know what's going on.
  637. if verbose:
  638. print("<<Loading %s>>" % (resource_url,))
  639. # Load the resource.
  640. opened_resource = _open(resource_url)
  641. if format == "raw":
  642. resource_val = opened_resource.read()
  643. elif format == "pickle":
  644. resource_val = pickle.load(opened_resource)
  645. elif format == "json":
  646. import json
  647. from nltk.jsontags import json_tags
  648. resource_val = json.load(opened_resource)
  649. tag = None
  650. if len(resource_val) != 1:
  651. tag = next(resource_val.keys())
  652. if tag not in json_tags:
  653. raise ValueError("Unknown json tag.")
  654. elif format == "yaml":
  655. import yaml
  656. resource_val = yaml.safe_load(opened_resource)
  657. else:
  658. # The resource is a text format.
  659. binary_data = opened_resource.read()
  660. if encoding is not None:
  661. string_data = binary_data.decode(encoding)
  662. else:
  663. try:
  664. string_data = binary_data.decode("utf-8")
  665. except UnicodeDecodeError:
  666. string_data = binary_data.decode("latin-1")
  667. if format == "text":
  668. resource_val = string_data
  669. elif format == "cfg":
  670. resource_val = nltk.grammar.CFG.fromstring(string_data, encoding=encoding)
  671. elif format == "pcfg":
  672. resource_val = nltk.grammar.PCFG.fromstring(string_data, encoding=encoding)
  673. elif format == "fcfg":
  674. resource_val = nltk.grammar.FeatureGrammar.fromstring(
  675. string_data,
  676. logic_parser=logic_parser,
  677. fstruct_reader=fstruct_reader,
  678. encoding=encoding,
  679. )
  680. elif format == "fol":
  681. resource_val = nltk.sem.read_logic(
  682. string_data,
  683. logic_parser=nltk.sem.logic.LogicParser(),
  684. encoding=encoding,
  685. )
  686. elif format == "logic":
  687. resource_val = nltk.sem.read_logic(
  688. string_data, logic_parser=logic_parser, encoding=encoding
  689. )
  690. elif format == "val":
  691. resource_val = nltk.sem.read_valuation(string_data, encoding=encoding)
  692. else:
  693. raise AssertionError(
  694. "Internal NLTK error: Format %s isn't "
  695. "handled by nltk.data.load()" % (format,)
  696. )
  697. opened_resource.close()
  698. # If requested, add it to the cache.
  699. if cache:
  700. try:
  701. _resource_cache[(resource_url, format)] = resource_val
  702. # TODO: add this line
  703. # print('<<Caching a copy of %s>>' % (resource_url,))
  704. except TypeError:
  705. # We can't create weak references to some object types, like
  706. # strings and tuples. For now, just don't cache them.
  707. pass
  708. return resource_val
  709. def show_cfg(resource_url, escape="##"):
  710. """
  711. Write out a grammar file, ignoring escaped and empty lines.
  712. :type resource_url: str
  713. :param resource_url: A URL specifying where the resource should be
  714. loaded from. The default protocol is "nltk:", which searches
  715. for the file in the the NLTK data package.
  716. :type escape: str
  717. :param escape: Prepended string that signals lines to be ignored
  718. """
  719. resource_url = normalize_resource_url(resource_url)
  720. resource_val = load(resource_url, format="text", cache=False)
  721. lines = resource_val.splitlines()
  722. for l in lines:
  723. if l.startswith(escape):
  724. continue
  725. if re.match("^$", l):
  726. continue
  727. print(l)
  728. def clear_cache():
  729. """
  730. Remove all objects from the resource cache.
  731. :see: load()
  732. """
  733. _resource_cache.clear()
  734. def _open(resource_url):
  735. """
  736. Helper function that returns an open file object for a resource,
  737. given its resource URL. If the given resource URL uses the "nltk:"
  738. protocol, or uses no protocol, then use ``nltk.data.find`` to find
  739. its path, and open it with the given mode; if the resource URL
  740. uses the 'file' protocol, then open the file with the given mode;
  741. otherwise, delegate to ``urllib2.urlopen``.
  742. :type resource_url: str
  743. :param resource_url: A URL specifying where the resource should be
  744. loaded from. The default protocol is "nltk:", which searches
  745. for the file in the the NLTK data package.
  746. """
  747. resource_url = normalize_resource_url(resource_url)
  748. protocol, path_ = split_resource_url(resource_url)
  749. if protocol is None or protocol.lower() == "nltk":
  750. return find(path_, path + [""]).open()
  751. elif protocol.lower() == "file":
  752. # urllib might not use mode='rb', so handle this one ourselves:
  753. return find(path_, [""]).open()
  754. else:
  755. return urlopen(resource_url)
  756. ######################################################################
  757. # Lazy Resource Loader
  758. ######################################################################
  759. class LazyLoader(object):
  760. @py3_data
  761. def __init__(self, _path):
  762. self._path = _path
  763. def __load(self):
  764. resource = load(self._path)
  765. # This is where the magic happens! Transform ourselves into
  766. # the object by modifying our own __dict__ and __class__ to
  767. # match that of `resource`.
  768. self.__dict__ = resource.__dict__
  769. self.__class__ = resource.__class__
  770. def __getattr__(self, attr):
  771. self.__load()
  772. # This looks circular, but its not, since __load() changes our
  773. # __class__ to something new:
  774. return getattr(self, attr)
  775. def __repr__(self):
  776. self.__load()
  777. # This looks circular, but its not, since __load() changes our
  778. # __class__ to something new:
  779. return repr(self)
  780. ######################################################################
  781. # Open-On-Demand ZipFile
  782. ######################################################################
  783. class OpenOnDemandZipFile(zipfile.ZipFile):
  784. """
  785. A subclass of ``zipfile.ZipFile`` that closes its file pointer
  786. whenever it is not using it; and re-opens it when it needs to read
  787. data from the zipfile. This is useful for reducing the number of
  788. open file handles when many zip files are being accessed at once.
  789. ``OpenOnDemandZipFile`` must be constructed from a filename, not a
  790. file-like object (to allow re-opening). ``OpenOnDemandZipFile`` is
  791. read-only (i.e. ``write()`` and ``writestr()`` are disabled.
  792. """
  793. @py3_data
  794. def __init__(self, filename):
  795. if not isinstance(filename, str):
  796. raise TypeError("ReopenableZipFile filename must be a string")
  797. zipfile.ZipFile.__init__(self, filename)
  798. assert self.filename == filename
  799. self.close()
  800. # After closing a ZipFile object, the _fileRefCnt needs to be cleared
  801. # for Python2and3 compatible code.
  802. self._fileRefCnt = 0
  803. def read(self, name):
  804. assert self.fp is None
  805. self.fp = open(self.filename, "rb")
  806. value = zipfile.ZipFile.read(self, name)
  807. # Ensure that _fileRefCnt needs to be set for Python2and3 compatible code.
  808. # Since we only opened one file here, we add 1.
  809. self._fileRefCnt += 1
  810. self.close()
  811. return value
  812. def write(self, *args, **kwargs):
  813. """:raise NotImplementedError: OpenOnDemandZipfile is read-only"""
  814. raise NotImplementedError("OpenOnDemandZipfile is read-only")
  815. def writestr(self, *args, **kwargs):
  816. """:raise NotImplementedError: OpenOnDemandZipfile is read-only"""
  817. raise NotImplementedError("OpenOnDemandZipfile is read-only")
  818. def __repr__(self):
  819. return repr(str("OpenOnDemandZipFile(%r)") % self.filename)
  820. ######################################################################
  821. # { Seekable Unicode Stream Reader
  822. ######################################################################
  823. class SeekableUnicodeStreamReader(object):
  824. """
  825. A stream reader that automatically encodes the source byte stream
  826. into unicode (like ``codecs.StreamReader``); but still supports the
  827. ``seek()`` and ``tell()`` operations correctly. This is in contrast
  828. to ``codecs.StreamReader``, which provide *broken* ``seek()`` and
  829. ``tell()`` methods.
  830. This class was motivated by ``StreamBackedCorpusView``, which
  831. makes extensive use of ``seek()`` and ``tell()``, and needs to be
  832. able to handle unicode-encoded files.
  833. Note: this class requires stateless decoders. To my knowledge,
  834. this shouldn't cause a problem with any of python's builtin
  835. unicode encodings.
  836. """
  837. DEBUG = True # : If true, then perform extra sanity checks.
  838. @py3_data
  839. def __init__(self, stream, encoding, errors="strict"):
  840. # Rewind the stream to its beginning.
  841. stream.seek(0)
  842. self.stream = stream
  843. """The underlying stream."""
  844. self.encoding = encoding
  845. """The name of the encoding that should be used to encode the
  846. underlying stream."""
  847. self.errors = errors
  848. """The error mode that should be used when decoding data from
  849. the underlying stream. Can be 'strict', 'ignore', or
  850. 'replace'."""
  851. self.decode = codecs.getdecoder(encoding)
  852. """The function that is used to decode byte strings into
  853. unicode strings."""
  854. self.bytebuffer = b""
  855. """A buffer to use bytes that have been read but have not yet
  856. been decoded. This is only used when the final bytes from
  857. a read do not form a complete encoding for a character."""
  858. self.linebuffer = None
  859. """A buffer used by ``readline()`` to hold characters that have
  860. been read, but have not yet been returned by ``read()`` or
  861. ``readline()``. This buffer consists of a list of unicode
  862. strings, where each string corresponds to a single line.
  863. The final element of the list may or may not be a complete
  864. line. Note that the existence of a linebuffer makes the
  865. ``tell()`` operation more complex, because it must backtrack
  866. to the beginning of the buffer to determine the correct
  867. file position in the underlying byte stream."""
  868. self._rewind_checkpoint = 0
  869. """The file position at which the most recent read on the
  870. underlying stream began. This is used, together with
  871. ``_rewind_numchars``, to backtrack to the beginning of
  872. ``linebuffer`` (which is required by ``tell()``)."""
  873. self._rewind_numchars = None
  874. """The number of characters that have been returned since the
  875. read that started at ``_rewind_checkpoint``. This is used,
  876. together with ``_rewind_checkpoint``, to backtrack to the
  877. beginning of ``linebuffer`` (which is required by ``tell()``)."""
  878. self._bom = self._check_bom()
  879. """The length of the byte order marker at the beginning of
  880. the stream (or None for no byte order marker)."""
  881. # /////////////////////////////////////////////////////////////////
  882. # Read methods
  883. # /////////////////////////////////////////////////////////////////
  884. def read(self, size=None):
  885. """
  886. Read up to ``size`` bytes, decode them using this reader's
  887. encoding, and return the resulting unicode string.
  888. :param size: The maximum number of bytes to read. If not
  889. specified, then read as many bytes as possible.
  890. :type size: int
  891. :rtype: unicode
  892. """
  893. chars = self._read(size)
  894. # If linebuffer is not empty, then include it in the result
  895. if self.linebuffer:
  896. chars = "".join(self.linebuffer) + chars
  897. self.linebuffer = None
  898. self._rewind_numchars = None
  899. return chars
  900. def discard_line(self):
  901. if self.linebuffer and len(self.linebuffer) > 1:
  902. line = self.linebuffer.pop(0)
  903. self._rewind_numchars += len(line)
  904. else:
  905. self.stream.readline()
  906. def readline(self, size=None):
  907. """
  908. Read a line of text, decode it using this reader's encoding,
  909. and return the resulting unicode string.
  910. :param size: The maximum number of bytes to read. If no
  911. newline is encountered before ``size`` bytes have been read,
  912. then the returned value may not be a complete line of text.
  913. :type size: int
  914. """
  915. # If we have a non-empty linebuffer, then return the first
  916. # line from it. (Note that the last element of linebuffer may
  917. # not be a complete line; so let _read() deal with it.)
  918. if self.linebuffer and len(self.linebuffer) > 1:
  919. line = self.linebuffer.pop(0)
  920. self._rewind_numchars += len(line)
  921. return line
  922. readsize = size or 72
  923. chars = ""
  924. # If there's a remaining incomplete line in the buffer, add it.
  925. if self.linebuffer:
  926. chars += self.linebuffer.pop()
  927. self.linebuffer = None
  928. while True:
  929. startpos = self.stream.tell() - len(self.bytebuffer)
  930. new_chars = self._read(readsize)
  931. # If we're at a '\r', then read one extra character, since
  932. # it might be a '\n', to get the proper line ending.
  933. if new_chars and new_chars.endswith("\r"):
  934. new_chars += self._read(1)
  935. chars += new_chars
  936. lines = chars.splitlines(True)
  937. if len(lines) > 1:
  938. line = lines[0]
  939. self.linebuffer = lines[1:]
  940. self._rewind_numchars = len(new_chars) - (len(chars) - len(line))
  941. self._rewind_checkpoint = startpos
  942. break
  943. elif len(lines) == 1:
  944. line0withend = lines[0]
  945. line0withoutend = lines[0].splitlines(False)[0]
  946. if line0withend != line0withoutend: # complete line
  947. line = line0withend
  948. break
  949. if not new_chars or size is not None:
  950. line = chars
  951. break
  952. # Read successively larger blocks of text.
  953. if readsize < 8000:
  954. readsize *= 2
  955. return line
  956. def readlines(self, sizehint=None, keepends=True):
  957. """
  958. Read this file's contents, decode them using this reader's
  959. encoding, and return it as a list of unicode lines.
  960. :rtype: list(unicode)
  961. :param sizehint: Ignored.
  962. :param keepends: If false, then strip newlines.
  963. """
  964. return self.read().splitlines(keepends)
  965. def next(self):
  966. """Return the next decoded line from the underlying stream."""
  967. line = self.readline()
  968. if line:
  969. return line
  970. else:
  971. raise StopIteration
  972. def __next__(self):
  973. return self.next()
  974. def __iter__(self):
  975. """Return self"""
  976. return self
  977. def __del__(self):
  978. # let garbage collector deal with still opened streams
  979. if not self.closed:
  980. self.close()
  981. def xreadlines(self):
  982. """Return self"""
  983. return self
  984. # /////////////////////////////////////////////////////////////////
  985. # Pass-through methods & properties
  986. # /////////////////////////////////////////////////////////////////
  987. @property
  988. def closed(self):
  989. """True if the underlying stream is closed."""
  990. return self.stream.closed
  991. @property
  992. def name(self):
  993. """The name of the underlying stream."""
  994. return self.stream.name
  995. @property
  996. def mode(self):
  997. """The mode of the underlying stream."""
  998. return self.stream.mode
  999. def close(self):
  1000. """
  1001. Close the underlying stream.
  1002. """
  1003. self.stream.close()
  1004. # /////////////////////////////////////////////////////////////////
  1005. # Seek and tell
  1006. # /////////////////////////////////////////////////////////////////
  1007. def seek(self, offset, whence=0):
  1008. """
  1009. Move the stream to a new file position. If the reader is
  1010. maintaining any buffers, then they will be cleared.
  1011. :param offset: A byte count offset.
  1012. :param whence: If 0, then the offset is from the start of the file
  1013. (offset should be positive), if 1, then the offset is from the
  1014. current position (offset may be positive or negative); and if 2,
  1015. then the offset is from the end of the file (offset should
  1016. typically be negative).
  1017. """
  1018. if whence == 1:
  1019. raise ValueError(
  1020. "Relative seek is not supported for "
  1021. "SeekableUnicodeStreamReader -- consider "
  1022. "using char_seek_forward() instead."
  1023. )
  1024. self.stream.seek(offset, whence)
  1025. self.linebuffer = None
  1026. self.bytebuffer = b""
  1027. self._rewind_numchars = None
  1028. self._rewind_checkpoint = self.stream.tell()
  1029. def char_seek_forward(self, offset):
  1030. """
  1031. Move the read pointer forward by ``offset`` characters.
  1032. """
  1033. if offset < 0:
  1034. raise ValueError("Negative offsets are not supported")
  1035. # Clear all buffers.
  1036. self.seek(self.tell())
  1037. # Perform the seek operation.
  1038. self._char_seek_forward(offset)
  1039. def _char_seek_forward(self, offset, est_bytes=None):
  1040. """
  1041. Move the file position forward by ``offset`` characters,
  1042. ignoring all buffers.
  1043. :param est_bytes: A hint, giving an estimate of the number of
  1044. bytes that will be needed to move forward by ``offset`` chars.
  1045. Defaults to ``offset``.
  1046. """
  1047. if est_bytes is None:
  1048. est_bytes = offset
  1049. bytes = b""
  1050. while True:
  1051. # Read in a block of bytes.
  1052. newbytes = self.stream.read(est_bytes - len(bytes))
  1053. bytes += newbytes
  1054. # Decode the bytes to characters.
  1055. chars, bytes_decoded = self._incr_decode(bytes)
  1056. # If we got the right number of characters, then seek
  1057. # backwards over any truncated characters, and return.
  1058. if len(chars) == offset:
  1059. self.stream.seek(-len(bytes) + bytes_decoded, 1)
  1060. return
  1061. # If we went too far, then we can back-up until we get it
  1062. # right, using the bytes we've already read.
  1063. if len(chars) > offset:
  1064. while len(chars) > offset:
  1065. # Assume at least one byte/char.
  1066. est_bytes += offset - len(chars)
  1067. chars, bytes_decoded = self._incr_decode(bytes[:est_bytes])
  1068. self.stream.seek(-len(bytes) + bytes_decoded, 1)
  1069. return
  1070. # Otherwise, we haven't read enough bytes yet; loop again.
  1071. est_bytes += offset - len(chars)
  1072. def tell(self):
  1073. """
  1074. Return the current file position on the underlying byte
  1075. stream. If this reader is maintaining any buffers, then the
  1076. returned file position will be the position of the beginning
  1077. of those buffers.
  1078. """
  1079. # If nothing's buffered, then just return our current filepos:
  1080. if self.linebuffer is None:
  1081. return self.stream.tell() - len(self.bytebuffer)
  1082. # Otherwise, we'll need to backtrack the filepos until we
  1083. # reach the beginning of the buffer.
  1084. # Store our original file position, so we can return here.
  1085. orig_filepos = self.stream.tell()
  1086. # Calculate an estimate of where we think the newline is.
  1087. bytes_read = (orig_filepos - len(self.bytebuffer)) - self._rewind_checkpoint
  1088. buf_size = sum(len(line) for line in self.linebuffer)
  1089. est_bytes = int(
  1090. (bytes_read * self._rewind_numchars / (self._rewind_numchars + buf_size))
  1091. )
  1092. self.stream.seek(self._rewind_checkpoint)
  1093. self._char_seek_forward(self._rewind_numchars, est_bytes)
  1094. filepos = self.stream.tell()
  1095. # Sanity check
  1096. if self.DEBUG:
  1097. self.stream.seek(filepos)
  1098. check1 = self._incr_decode(self.stream.read(50))[0]
  1099. check2 = "".join(self.linebuffer)
  1100. assert check1.startswith(check2) or check2.startswith(check1)
  1101. # Return to our original filepos (so we don't have to throw
  1102. # out our buffer.)
  1103. self.stream.seek(orig_filepos)
  1104. # Return the calculated filepos
  1105. return filepos
  1106. # /////////////////////////////////////////////////////////////////
  1107. # Helper methods
  1108. # /////////////////////////////////////////////////////////////////
  1109. def _read(self, size=None):
  1110. """
  1111. Read up to ``size`` bytes from the underlying stream, decode
  1112. them using this reader's encoding, and return the resulting
  1113. unicode string. ``linebuffer`` is not included in the result.
  1114. """
  1115. if size == 0:
  1116. return ""
  1117. # Skip past the byte order marker, if present.
  1118. if self._bom and self.stream.tell() == 0:
  1119. self.stream.read(self._bom)
  1120. # Read the requested number of bytes.
  1121. if size is None:
  1122. new_bytes = self.stream.read()
  1123. else:
  1124. new_bytes = self.stream.read(size)
  1125. bytes = self.bytebuffer + new_bytes
  1126. # Decode the bytes into unicode characters
  1127. chars, bytes_decoded = self._incr_decode(bytes)
  1128. # If we got bytes but couldn't decode any, then read further.
  1129. if (size is not None) and (not chars) and (len(new_bytes) > 0):
  1130. while not chars:
  1131. new_bytes = self.stream.read(1)
  1132. if not new_bytes:
  1133. break # end of file.
  1134. bytes += new_bytes
  1135. chars, bytes_decoded = self._incr_decode(bytes)
  1136. # Record any bytes we didn't consume.
  1137. self.bytebuffer = bytes[bytes_decoded:]
  1138. # Return the result
  1139. return chars
  1140. def _incr_decode(self, bytes):
  1141. """
  1142. Decode the given byte string into a unicode string, using this
  1143. reader's encoding. If an exception is encountered that
  1144. appears to be caused by a truncation error, then just decode
  1145. the byte string without the bytes that cause the trunctaion
  1146. error.
  1147. Return a tuple ``(chars, num_consumed)``, where ``chars`` is
  1148. the decoded unicode string, and ``num_consumed`` is the
  1149. number of bytes that were consumed.
  1150. """
  1151. while True:
  1152. try:
  1153. return self.decode(bytes, "strict")
  1154. except UnicodeDecodeError as exc:
  1155. # If the exception occurs at the end of the string,
  1156. # then assume that it's a truncation error.
  1157. if exc.end == len(bytes):
  1158. return self.decode(bytes[: exc.start], self.errors)
  1159. # Otherwise, if we're being strict, then raise it.
  1160. elif self.errors == "strict":
  1161. raise
  1162. # If we're not strict, then re-process it with our
  1163. # errors setting. This *may* raise an exception.
  1164. else:
  1165. return self.decode(bytes, self.errors)
  1166. _BOM_TABLE = {
  1167. "utf8": [(codecs.BOM_UTF8, None)],
  1168. "utf16": [(codecs.BOM_UTF16_LE, "utf16-le"), (codecs.BOM_UTF16_BE, "utf16-be")],
  1169. "utf16le": [(codecs.BOM_UTF16_LE, None)],
  1170. "utf16be": [(codecs.BOM_UTF16_BE, None)],
  1171. "utf32": [(codecs.BOM_UTF32_LE, "utf32-le"), (codecs.BOM_UTF32_BE, "utf32-be")],
  1172. "utf32le": [(codecs.BOM_UTF32_LE, None)],
  1173. "utf32be": [(codecs.BOM_UTF32_BE, None)],
  1174. }
  1175. def _check_bom(self):
  1176. # Normalize our encoding name
  1177. enc = re.sub("[ -]", "", self.encoding.lower())
  1178. # Look up our encoding in the BOM table.
  1179. bom_info = self._BOM_TABLE.get(enc)
  1180. if bom_info:
  1181. # Read a prefix, to check against the BOM(s)
  1182. bytes = self.stream.read(16)
  1183. self.stream.seek(0)
  1184. # Check for each possible BOM.
  1185. for (bom, new_encoding) in bom_info:
  1186. if bytes.startswith(bom):
  1187. if new_encoding:
  1188. self.encoding = new_encoding
  1189. return len(bom)
  1190. return None
  1191. __all__ = [
  1192. "path",
  1193. "PathPointer",
  1194. "FileSystemPathPointer",
  1195. "BufferedGzipFile",
  1196. "GzipFileSystemPathPointer",
  1197. "GzipFileSystemPathPointer",
  1198. "find",
  1199. "retrieve",
  1200. "FORMATS",
  1201. "AUTO_FORMATS",
  1202. "load",
  1203. "show_cfg",
  1204. "clear_cache",
  1205. "LazyLoader",
  1206. "OpenOnDemandZipFile",
  1207. "GzipFileSystemPathPointer",
  1208. "SeekableUnicodeStreamReader",
  1209. ]