| 1 |
============================= |
| 2 |
BinaryOrNot |
| 3 |
============================= |
| 4 |
|
| 5 |
.. image:: https://img.shields.io/pypi/v/binaryornot.svg?style=flat |
| 6 |
:target: https://pypi.python.org/pypi/binaryornot |
| 7 |
|
| 8 |
.. image:: https://readthedocs.org/projects/binaryornot/badge/?version=latest |
| 9 |
:target: http://binaryornot.readthedocs.io/en/latest/?badge=latest |
| 10 |
:alt: Documentation Status |
| 11 |
|
| 12 |
|
| 13 |
.. image:: https://pyup.io/repos/github/audreyr/binaryornot/shield.svg |
| 14 |
:target: https://pyup.io/repos/github/audreyr/binaryornot/ |
| 15 |
:alt: Updates |
| 16 |
|
| 17 |
Ultra-lightweight pure Python package to guess whether a file is binary or text, |
| 18 |
using a heuristic similar to Perl's `pp_fttext` and its analysis by @eliben. |
| 19 |
|
| 20 |
* Free software: BSD license |
| 21 |
* Documentation: https://binaryornot.readthedocs.io |
| 22 |
|
| 23 |
Status |
| 24 |
------ |
| 25 |
|
| 26 |
It works, and people are using this package in various places. But it doesn't cover all edge cases yet. |
| 27 |
|
| 28 |
The code could be improved. Pull requests welcome! As of now, it is based on these snippets, but that may change: |
| 29 |
|
| 30 |
* http://stackoverflow.com/questions/898669/how-can-i-detect-if-a-file-is-binary-non-text-in-python |
| 31 |
* http://stackoverflow.com/questions/1446549/how-to-identify-binary-and-text-files-using-python |
| 32 |
* http://code.activestate.com/recipes/173220/ |
| 33 |
* http://eli.thegreenplace.net/2011/10/19/perls-guess-if-file-is-text-or-binary-implemented-in-python/ |
| 34 |
|
| 35 |
Features |
| 36 |
-------- |
| 37 |
|
| 38 |
Has tests for these file types: |
| 39 |
|
| 40 |
* Text: .txt, .css, .json, .svg, .js, .lua, .pl, .rst |
| 41 |
* Binary: .png, .gif, .jpg, .tiff, .bmp, .DS_Store, .eot, .otf, .ttf, .woff, .rgb |
| 42 |
|
| 43 |
Has tests for numerous encodings. |
| 44 |
|
| 45 |
Why? |
| 46 |
---- |
| 47 |
|
| 48 |
You may be thinking, "I can write this in 2 lines of code?!" |
| 49 |
|
| 50 |
It's actually not that easy. Here's a great article about how Perl's |
| 51 |
heuristic to guess file types works: http://eli.thegreenplace.net/2011/10/19/perls-guess-if-file-is-text-or-binary-implemented-in-python/ |
| 52 |
|
| 53 |
And that's just where we started. Over time, we've found more edge cases and |
| 54 |
our heuristic has gotten more complex. |
| 55 |
|
| 56 |
Also, this package saves you from having to write and thoroughly test |
| 57 |
your code with all sorts of weird file types and encodings, cross-platform. |
| 58 |
|
| 59 |
Builds |
| 60 |
------ |
| 61 |
|
| 62 |
Linux (Ubuntu 12.04 LTS Server Edition 64 bit): |
| 63 |
|
| 64 |
.. image:: https://img.shields.io/travis/audreyr/binaryornot/master.svg |
| 65 |
:target: https://travis-ci.org/audreyr/binaryornot |
| 66 |
|
| 67 |
Windows (Windows Server 2012 R2 (x64)): |
| 68 |
|
| 69 |
.. image:: https://img.shields.io/appveyor/ci/audreyr/binaryornot/master.svg |
| 70 |
:target: https://ci.appveyor.com/project/audreyr/binaryornot |
| 71 |
|
| 72 |
Credits |
| 73 |
------- |
| 74 |
|
| 75 |
* Special thanks to Eli Bendersky (@eliben) for his writeup explaining the heuristic and his implementation, which this is largely based on. |
| 76 |
* Source code from the portion of Perl's `pp_fttext` that checks for textiness: https://github.com/Perl/perl5/blob/v5.23.1/pp_sys.c#L3527-L3587 |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
History |
| 82 |
------- |
| 83 |
|
| 84 |
0.4.4 (2017-04-13) |
| 85 |
~~~~~~~~~~~~~~~~~~ |
| 86 |
|
| 87 |
* Notify users for file i/o issues. Thanks @lukehinds! |
| 88 |
|
| 89 |
|
| 90 |
0.4.3 (2017-04-13) |
| 91 |
~~~~~~~~~~~~~~~~~~ |
| 92 |
|
| 93 |
* Restricted chardet to anything 3.0.2 or higher due to https://github.com/chardet/chardet/issues/113. Thanks @dan-blanchard for the quick fix! |
| 94 |
|
| 95 |
0.4.2 (2017-04-12) |
| 96 |
~~~~~~~~~~~~~~~~~~ |
| 97 |
|
| 98 |
* Restricted chardet to anything under 3.0 due to https://github.com/chardet/chardet/issues/113 |
| 99 |
* Added pyup badge |
| 100 |
* Added utilities for pushing new versions up |
| 101 |
|
| 102 |
0.4.0 (2015-08-21) |
| 103 |
~~~~~~~~~~~~~~~~~~ |
| 104 |
|
| 105 |
* Enhanced detection for some binary streams and UTF texts. (#10, 11) Thanks `@pombredanne`_. |
| 106 |
* Set up Appveyor for continuous testing on Windows. Thanks `@pydanny`_. |
| 107 |
* Update link to Perl source implementation. (#9) Thanks `@asmeurer`_ `@pombredanne`_ `@audreyr`_. |
| 108 |
* Handle UnicodeDecodeError in check. (#12) Thanks `@DRMacIver`_. |
| 109 |
* Add very simple Hypothesis based tests. (#13) Thanks `@DRMacIver`_. |
| 110 |
* Use setup to determine requirements and remove redundant requirements.txt. (#14) Thanks `@hackebrot`_. |
| 111 |
* Add documentation status badge to README.rst. (#15) Thanks `@hackebrot`_. |
| 112 |
* Run tox in travis.yml. Add pypy and Python 3.4 to tox environments. (#16) Thanks `@hackebrot`_ `@pydanny`_. |
| 113 |
* Handle LookupError when detecting encoding. (#17) Thanks `@DRMacIver`_. |
| 114 |
|
| 115 |
|
| 116 |
.. _`@pombredanne`: https://github.com/pombredanne |
| 117 |
.. _`@pydanny`: https://github.com/pydanny |
| 118 |
.. _`@asmeurer`: https://github.com/asmeurer |
| 119 |
.. _`@audreyr`: https://github.com/audreyr |
| 120 |
.. _`@DRMacIver`: https://github.com/DRMacIver |
| 121 |
.. _`@hackebrot`: https://github.com/hackebrot |
| 122 |
|
| 123 |
0.3.0 (2014-05-05) |
| 124 |
~~~~~~~~~~~~~~~~~~ |
| 125 |
|
| 126 |
* Include tests, docs in source package. (#6) Thanks `@vincentbernat`_. |
| 127 |
* Drop unnecessary shebangs and executable bits. (#8) Thanks `@scop`_. |
| 128 |
* Generate string of printable extended ASCII bytes only once. (#7) Thanks `@scop`_. |
| 129 |
* Make number of bytes to read parametrizable. (#7) Thanks `@scop`_. |
| 130 |
|
| 131 |
.. _`@vincentbernat`: https://github.com/vincentbernat |
| 132 |
.. _`@scop`: https://github.com/scop |
| 133 |
|
| 134 |
0.2.0 (2013-09-22) |
| 135 |
~~~~~~~~~~~~~~~~~~ |
| 136 |
|
| 137 |
* Complete rewrite of everything. Thanks `@ncoghlan`_. |
| 138 |
|
| 139 |
.. _`@ncoghlan`: https://github.com/ncoghlan |
| 140 |
|
| 141 |
0.1.1 (2013-08-17) |
| 142 |
~~~~~~~~~~~~~~~~~~ |
| 143 |
|
| 144 |
* Tests pass under Python 2.6, 2.7, 3.3, PyPy. |
| 145 |
|
| 146 |
|
| 147 |
0.1.0 (2013-08-17) |
| 148 |
~~~~~~~~~~~~~~~~~~ |
| 149 |
|
| 150 |
* First release on PyPI. |
| 151 |
|
| 152 |
|