Skip to content

Commit 7799be4

Browse files
adamchainzwojcikstefan
authored andcommitted
setup.py + README tidying (#22)
* Convert `README.md` to `README.rst` since rst is all that PyPI supports * Tidy up README so all functions are explained with their own headers * Use context managers for opening files in `setup.py` * Don't import the code in `setup.py`, which doesn't work on some platforms before it's installed. Instead use parsing of the file to find the version.
1 parent 9e6ee2f commit 7799be4

File tree

5 files changed

+83
-57
lines changed

5 files changed

+83
-57
lines changed

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include README.md LICENSE mimeparse_test.py testdata.json
1+
include README.rst LICENSE mimeparse_test.py testdata.json

README.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

README.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
.. image:: https://travis-ci.org/dbtsai/python-mimeparse.svg?branch=master
2+
:target: https://travis-ci.org/dbtsai/python-mimeparse
3+
4+
This module provides basic functions for handling mime-types. It can
5+
handle matching mime-types against a list of media-ranges. See section
6+
5.3.2 of the HTTP 1.1 Semantics and Content specification [RFC 7231] for
7+
a complete explanation: https://tools.ietf.org/html/rfc7231#section-5.3.2
8+
9+
Installation
10+
============
11+
12+
Use **pip**:
13+
14+
.. code-block:: sh
15+
16+
$ pip install python-mimeparse
17+
18+
It supports Python 2.7 - 3.5 and PyPy.
19+
20+
Functions
21+
=========
22+
23+
``parse_mime_type()``
24+
----------------------
25+
26+
Parses a mime-type into its component parts.
27+
28+
``parse_media_range()``
29+
-----------------------
30+
31+
Media-ranges are mime-types with wild-cards and a "q" quality parameter.
32+
33+
``quality()``
34+
-------------
35+
36+
Determines the quality ("q") of a mime-type when compared against a list of
37+
media-ranges.
38+
39+
``quality_parsed()``
40+
--------------------
41+
42+
Just like ``quality()`` except the second parameter must be pre-parsed.
43+
44+
``best_match()``
45+
----------------
46+
47+
Choose the mime-type with the highest quality ("q") from a list of candidates.
48+
49+
Testing
50+
=======
51+
52+
Run the tests by typing: ``python mimeparse_test.py``. The tests require Python 2.6.
53+
54+
To make sure that the package works in all the supported environments, you can
55+
run **tox** tests:
56+
57+
.. code-block:: sh
58+
59+
$ pip install tox
60+
$ tox
61+
62+
The format of the JSON test data file is as follows: A top-level JSON object
63+
which has a key for each of the functions to be tested. The value corresponding
64+
to that key is a list of tests. Each test contains: the argument or arguments
65+
to the function being tested, the expected results and an optional description.

mimeparse.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
"""MIME-Type Parser
2-
3-
This module provides basic functions for handling mime-types. It can handle
4-
matching mime-types against a list of media-ranges. See section 5.3.2 of the
5-
HTTP 1.1 Semantics and Content specification [RFC 7231] for a complete
6-
explanation.
7-
8-
https://tools.ietf.org/html/rfc7231#section-5.3.2
9-
10-
Contents:
11-
- parse_mime_type(): Parses a mime-type into its component parts.
12-
- parse_media_range(): Media-ranges are mime-types with wild-cards and a 'q'
13-
quality parameter.
14-
- quality(): Determines the quality ('q') of a mime-type when
15-
compared against a list of media-ranges.
16-
- quality_parsed(): Just like quality() except the second parameter must be
17-
pre-parsed.
18-
- best_match(): Choose the mime-type with the highest quality ('q')
19-
from a list of candidates.
20-
"""
211
import cgi
222
from functools import reduce
233

setup.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,37 @@
22

33
import os
44
import codecs
5-
import mimeparse
5+
import re
66
from setuptools import setup
77

88

9+
def get_version(filename):
10+
"""
11+
Return package version as listed in `__version__` in 'filename'.
12+
"""
13+
with open(filename) as fp:
14+
contents = fp.read()
15+
return re.search("__version__ = ['\"]([^'\"]+)['\"]", contents).group(1)
16+
17+
version = get_version('mimeparse.py')
18+
19+
920
def read(fname):
1021
path = os.path.join(os.path.dirname(__file__), fname)
11-
return codecs.open(path, encoding='utf-8').read()
22+
with codecs.open(path, encoding='utf-8') as fp:
23+
return fp.read()
1224

1325
setup(
1426
name="python-mimeparse",
1527
py_modules=["mimeparse"],
16-
version=mimeparse.__version__,
28+
version=version,
1729
description=("A module provides basic functions for parsing mime-type "
1830
"names and matching them against a list of media-ranges."),
1931
author="DB Tsai",
2032
author_email="[email protected]",
2133
url="https://github.com/dbtsai/python-mimeparse",
2234
download_url=("https://github.com/dbtsai/python-mimeparse/tarball/" +
23-
mimeparse.__version__),
35+
version),
2436
keywords=["mime-type"],
2537
classifiers=[
2638
"Programming Language :: Python",
@@ -32,5 +44,5 @@ def read(fname):
3244
"Topic :: Internet :: WWW/HTTP",
3345
"Topic :: Software Development :: Libraries :: Python Modules",
3446
],
35-
long_description=read('README.md')
47+
long_description=read('README.rst')
3648
)

0 commit comments

Comments
 (0)