setup.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env python
  2. import sys
  3. from setuptools import setup
  4. from setuptools.command.sdist import sdist
  5. _name_of_lib = 'libgumbo.so'
  6. if sys.platform.startswith('darwin'):
  7. _name_of_lib = 'libgumbo.dylib'
  8. elif sys.platform.startswith('win'):
  9. _name_of_lib = 'gumbo.dll'
  10. class CustomSdistCommand(sdist):
  11. """Customized Sdist command, to copy libgumbo.so into the Python directory
  12. so that it can be installed with `pip install`."""
  13. def run(self):
  14. try:
  15. import shutil
  16. shutil.copyfile('.libs/' + _name_of_lib,
  17. 'python/gumbo/' + _name_of_lib)
  18. sdist.run(self)
  19. except IOError as e:
  20. print(e)
  21. README = '''Gumbo - A pure-C HTML5 parser.
  22. ==============================
  23. Gumbo is an implementation of the `HTML5 parsing algorithm <http://www.whatwg.org/specs/web-apps/current-work/multipage/#auto-toc-12>`_ implemented
  24. as a pure C99 library with no outside dependencies. It's designed to serve
  25. as a building block for other tools and libraries such as linters,
  26. validators, templating languages, and refactoring and analysis tools. This
  27. package contains the library itself, Python ctypes bindings for the library, and
  28. adapters for html5lib and BeautifulSoup (3.2) that give it the same API as those
  29. libaries.
  30. Goals & features:
  31. -----------------
  32. - Robust and resilient to bad input.
  33. - Simple API that can be easily wrapped by other languages.
  34. - Support for source locations and pointers back to the original text.
  35. - Relatively lightweight, with no outside dependencies.
  36. - Passes all `html5lib-0.95 tests <https://github.com/html5lib/html5lib-tests>`_.
  37. - Tested on over 2.5 billion pages from Google's index.
  38. Non-goals:
  39. ----------
  40. - Execution speed. Gumbo gains some of this by virtue of being written in
  41. C, but it is not an important consideration for the intended use-case, and
  42. was not a major design factor.
  43. - Support for encodings other than UTF-8. For the most part, client code
  44. can convert the input stream to UTF-8 text using another library before
  45. processing.
  46. - Security. Gumbo was initially designed for a product that worked with
  47. trusted input files only. We're working to harden this and make sure that it
  48. behaves as expected even on malicious input, but for now, Gumbo should only be
  49. run on trusted input or within a sandbox.
  50. - C89 support. Most major compilers support C99 by now; the major exception
  51. (Microsoft Visual Studio) should be able to compile this in C++ mode with
  52. relatively few changes. (Bug reports welcome.)
  53. Wishlist (aka "We couldn't get these into the original release, but are
  54. hoping to add them soon"):
  55. - Support for recent HTML5 spec changes to support the template tag.
  56. - Support for fragment parsing.
  57. - Full-featured error reporting.
  58. - Bindings in other languages.
  59. Installation
  60. ------------
  61. ```pip install gumbo``` should do it. If you have a local copy, ```python
  62. setup.py install``` from the root directory.
  63. The `html5lib <https://pypi.python.org/pypi/html5lib/0.999>`_ and
  64. `BeautifulSoup <https://pypi.python.org/pypi/BeautifulSoup/3.2.1>`_ adapters
  65. require that their respective libraries be installed separately to work.
  66. Basic Usage
  67. -----------
  68. For the ctypes bindings:
  69. .. code-block:: python
  70. import gumbo
  71. with gumbo.parse(text) as output:
  72. root = output.contents.root.contents
  73. # root is a Node object representing the root of the parse tree
  74. # tree-walk over it as necessary.
  75. For the BeautifulSoup bindings:
  76. .. code-block:: python
  77. import gumbo
  78. soup = gumbo.soup_parse(text)
  79. # soup is a BeautifulSoup object representing the parse tree.
  80. For the html5lib bindings:
  81. .. code-block:: python
  82. from gumbo import html5lib
  83. doc = html5lib.parse(text[, treebuilder='lxml'])
  84. Recommended best-practice for Python usage is to use one of the adapters to
  85. an existing API (personally, I prefer BeautifulSoup) and write your program
  86. in terms of those. The raw CTypes bindings should be considered building
  87. blocks for higher-level libraries and rarely referenced directly.
  88. See the source code, Pydoc, and implementation of soup_adapter and
  89. html5lib_adapter for more information.
  90. A note on API/ABI compatibility
  91. -------------------------------
  92. We'll make a best effort to preserve API compatibility between releases.
  93. The initial release is a 0.9 (beta) release to solicit comments from early
  94. adopters, but if no major problems are found with the API, a 1.0 release
  95. will follow shortly, and the API of that should be considered stable. If
  96. changes are necessary, we follow [semantic versioning][].
  97. We make no such guarantees about the ABI, and it's very likely that
  98. subsequent versions may require a recompile of client code. For this
  99. reason, we recommend NOT using Gumbo data structures throughout a program,
  100. and instead limiting them to a translation layer that picks out whatever
  101. data is needed from the parse tree and then converts that to persistent
  102. data structures more appropriate for the application. The API is
  103. structured to encourage this use, with a single delete function for the
  104. whole parse tree, and is not designed with mutation in mind.
  105. Most of this is transparent to Python usage, as the Python adapters are all
  106. built with this in mind. However, since ctypes requires ABI compatibility, it
  107. does mean you'll have to re-deploy the gumboc library and C extension when
  108. upgrading to a new version.
  109. '''
  110. CLASSIFIERS = [
  111. 'Development Status :: 4 - Beta',
  112. 'Intended Audience :: Developers',
  113. 'License :: OSI Approved :: Apache Software License',
  114. 'Operating System :: Unix',
  115. 'Operating System :: POSIX :: Linux',
  116. 'Programming Language :: C',
  117. 'Programming Language :: Python',
  118. 'Programming Language :: Python :: 2',
  119. 'Programming Language :: Python :: 2.7',
  120. 'Programming Language :: Python :: 3',
  121. 'Programming Language :: Python :: 3.4',
  122. 'Topic :: Software Development :: Libraries :: Python Modules',
  123. 'Topic :: Text Processing :: Markup :: HTML'
  124. ]
  125. setup(name='gumbo',
  126. version='0.10.1',
  127. description='Python bindings for Gumbo HTML parser',
  128. long_description=README,
  129. url='http://github.com/google/gumbo-parser',
  130. keywords='gumbo html html5 parser google html5lib beautifulsoup',
  131. author='Jonathan Tang',
  132. author_email='jonathan.d.tang@gmail.com',
  133. license='Apache 2.0',
  134. classifiers=CLASSIFIERS,
  135. packages=['gumbo'],
  136. package_dir={'': 'python'},
  137. package_data={'gumbo': [_name_of_lib]},
  138. cmdclass={ 'sdist': CustomSdistCommand },
  139. zip_safe=False)