Giter VIP home page Giter VIP logo

mimeparse's Introduction

jcgregorio

Miscellaneous bits of code too small to keep in their own repository.

mimeparse's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mimeparse's Issues

Python mimeparse isn't on cheeseshop

I have a project (http://grassyknoll.googlecode.com) that uses mimeparse. 
It'd simplify installation for users if mimeparse was installable from the
Cheeseshop: http://pypi.python.org/pypi  This could be done by writing a
trivial setup.py: http://docs.python.org/dist/ and linking on the Cheeseshop.

Doing so would allow me to specify mimeparse as a setuptools dependency,
enabling it to be installed via easy_install.  To be clear, mimeparse
itself does *not* need to use setuptools - only distutils.

If this ticket isn't resolved, I'll probably import mimeparse.py into my
source tree, which is lame.

Original issue reported on code.google.com by peter.fein on 6 Mar 2008 at 3:21

Insufficient handling of possible valid Accept headers

What steps will reproduce the problem?
1. Parsing of the Mime header misses some legitimate accept headers, namely 
quoted tokens which can contain commas, spaces, escaped quotes, and semi-colons.

I've written an implementation in ruby that handles all cases with some speed 
optimizations for the common case where quoted tokens do not occur. I'm just 
going to paste it here in-line instead of attempting to patch every version in 
this library.

ACCEPT_HEADER_REGEXP = /(\S+="(?:\\"|.)*?"[^\s,]*|\S+=[^\s,]+|[^\s;,]+[^\s,]*)/
ACCEPT_PARAMETER_REGEXP = /([^\s;=,]+)=("(?:\\"|.)*?"|[^;]+)/

def parse(header)
  # Only use the complex regexp if the header contains quoted tokens
  formats = if header.index('"')
    header.scan(ACCEPT_HEADER_REGEXP).map(&:first)
  else
    header.split(/,\s*/)
  end

  formats.map { |format|
    # Set default quality
    params = {'q' => 1.0}

    # Split into type and following parameters
    type, accept_params = format.split(";", 2)

    # Correct a standard wildcard error
    type = "*/*" if type == "*"

    if accept_params
      # Only use a complex regexp if the parameters contain quoted tokens
      accept_params = if accept_params.index('"')
        accept_params.scan(ACCEPT_PARAMETER_REGEXP)
      else
        accept_params.split(";").map { |a| a.split("=") }
      end

      accept_params.each { |(key, val)|
        val = if key == 'q'
          val.to_f
        elsif val[0] == '"' and val[-1] == '"'
          val[1..-2].gsub(/\\(.)/, "\\1")
        else
          val
        end
        params[key] = val
      }
    end
    [*type.split("/"), params]
  }
end

Original issue reported on code.google.com by [email protected] on 28 Mar 2014 at 3:34

poor error message on malformed media range

I'm seeing this Accept header in the wild: 

`text/html,application/json;q=0.9application/xhtml+xml,application/xml;q=0.9,*/*
;q=0.8`

Note the `0.9application`, which I believe is an error and should be 
`0.9,application`. mimeparse.parse_media_range fails with `ValueError: invalid 
literal for float(): 0.9application/xhtml+xml`. I would expect instead an error 
message saying the header is malformed. Does mimeparse have a concept of strict 
and lenient parsing? Does it want one?

Original issue reported on code.google.com by [email protected] on 20 May 2013 at 3:44

License problem

Hi,

Your code is clearly licensed under the MIT license on the website.
However, there is neither a clear copyright statement in the files/source
which details the license, nor does the code include a copy of the MIT
license, which is actually in violation of the MIT license itself, which reads:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

It would be trivial to just add a Copyright notice at the top of your files
in the comments, and paste in the MIT license as well, which would resolve
this.


Original issue reported on code.google.com by [email protected] on 17 Mar 2010 at 5:38

[PATCH] Port mimeparse to python 3

Another issue for porting mimeparse.py to python 3 :)

Patch applies cleanly to r22 and was tested with 2.5, 2.6, 2.7, 3.1, 3.2, and 
3.3.

Also updates setup.py with the python3 trove classifiers.

$ patch -p0 -i ../py3k-patch.diff

$ for ver in 3.1.4 3.2.3 3.3; do echo $ver && 
$(~/.pythonbrew/pythons/Python-$ver/bin/python3 mimeparse_test.py); done;
3.1.4
.............................
----------------------------------------------------------------------
Ran 29 tests in 0.002s

OK
3.2.3
.............................
----------------------------------------------------------------------
Ran 29 tests in 0.004s

OK
3.3
.............................
----------------------------------------------------------------------
Ran 29 tests in 0.004s

OK

$ ~/.pythonbrew/pythons/Python-2.5.6/bin/python mimeparse_test.py
.............................
----------------------------------------------------------------------
Ran 29 tests in 0.002s

OK

$ python2.6 mimeparse_test.py
.............................
----------------------------------------------------------------------
Ran 29 tests in 0.002s

OK

$ ~/.pythonbrew/pythons/Python-2.7.3/bin/python mimeparse_test.py
.............................
----------------------------------------------------------------------
Ran 29 tests in 0.003s

OK

Original issue reported on code.google.com by skryskalla on 4 Oct 2012 at 7:24

Attachments:

Poor error message for invalid mime types

What steps will reproduce the problem?

mhaase@ubuntu:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mimeparse
>>> mimeparse.best_match(['text/plain', 'application/json'], 'foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/mimeparse.py", line 153, in best_match
    parsed_header = [parse_media_range(r) for r in split_header]
  File "/usr/local/lib/python2.7/dist-packages/mimeparse.py", line 67, in parse_media_range
    (type, subtype, params) = parse_mime_type(range)
  File "/usr/local/lib/python2.7/dist-packages/mimeparse.py", line 48, in parse_mime_type
    (type, subtype) = full_type.split('/')
ValueError: need more than 1 value to unpack


What is the expected output? What do you see instead?

There should be a specific type of exception indicating a malformed content 
type.

What version of the product are you using? On what operating system?

>>> mimeparse.__version__
'0.1.4'

Original issue reported on code.google.com by [email protected] on 11 Jun 2013 at 9:14

Erlang version of mimeparse

Attached find an Erlang version of mimeparse, which I'd like you to add to your 
repository.

Unlike the other language implementations, it doesn't export the 
quality_parsed() function from the 
module because that function doesn't seem all that useful at the application 
level. Otherwise the 
code largely mirrors that of the other languages, except best_match() is more 
efficient by avoiding 
the sort step via a fold. All in all it's functionally identical to the patched 
versions of the other 
languages I submitted in my previous issue, and it includes (and passes) the 
same unit tests as the 
others.

Original issue reported on code.google.com by [email protected] on 13 Sep 2008 at 10:45

Attachments:

Unable to install on Python 3.3

Downloading/unpacking mimeparse (from -r 
/home/christian/documents/projects/rh/requirements.txt (line 44))
  Downloading mimeparse-0.1.3.tar.gz
  Running setup.py egg_info for package mimeparse
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
      File "/tmp/pip_build_christian/mimeparse/setup.py", line 1
        # -*- coding: utf-8 -*-
          ^
    SyntaxError: invalid character in identifier
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "/tmp/pip_build_christian/mimeparse/setup.py", line 1

    # -*- coding: utf-8 -*-

      ^

SyntaxError: invalid character in identifier

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in 
/tmp/pip_build_christian/mimeparse
Storing complete log in /home/christian/.pip/pip.log

Original issue reported on code.google.com by [email protected] on 9 Nov 2013 at 8:51

Python mimeparse.

What steps will reproduce the problem?
1.
2.
3.

What is the expected output? What do you see instead?


What version of the product are you using? On what operating system?


Please provide any additional information below.


Original issue reported on code.google.com by [email protected] on 19 Oct 2009 at 11:01

Porting mimeparse to python3

Since Ubuntu will switch to python 3 by default on 12.10, it will be nice to 
see that mimeparse can seamlessly work under python 3; therefore, I spend some 
time on porting it and write the automate testing system using tox. Now, it 
works on py27, py32, py33, and pypy. See  
http://travis-ci.org/#!/dbtsai/python-mimeparse

My ported project is hosted in https://github.com/dbtsai/python-mimeparse

Any comments are welcome.

Thanks.

Original issue reported on code.google.com by [email protected] on 23 Aug 2012 at 1:06

split() is deprecated in PHP >= 5.3

What steps will reproduce the problem?
1. Install PHP 5.3

What is the expected output? What do you see instead?

No warnings. Since parse_mime_type() method is not using split() for regex, 
explode() is a suitable replacement.

What version of the product are you using? On what operating system?

trunk, revision 22

Please provide any additional information below.

http://us3.php.net/manual/en/function.split.php
http://us3.php.net/manual/en/function.explode.php

"Works for me" patch attached. Test result below.

php mimeparse.php
application/xml;q=1 - OK
application/xml - OK
application/xml;q= - OK
application/xml ; q=1;b=other - OK
application/xml ; q=2;b=other - OK
 *; q=.2 - OK
text/html;level=1 - OK
text/html - OK
text/plain - OK
image/jpeg - OK
text/html;level=2 - OK
text/html;level=3 - OK
application/xbel+xml - OK
application/xbel+xml; q=1 - OK
application/xml; q=1 - OK
application/*; q=1 - OK
* / * - OK
text/ *;q=0.5,* / *;q=0.1 - OK
text/html,application/atom+xml; q=0.9 - OK
application/json, text/javascript, */* - OK
application/json, text/html;q=0.9 - OK
image/png - OK
image/* - OK

Original issue reported on code.google.com by [email protected] on 9 Nov 2011 at 8:16

Attachments:

Java version

Attached is a Java version of mimeparse. It uses two commons-lang utility
classes (for ease), but could be updated to exclude them.

Tested w/Java 1.6.

Not sure if this is helpful, or if you want to add this to the suite, but I
figured someone else might be able to get some use out of it as well.

Thanks,
Tom

Original issue reported on code.google.com by [email protected] on 5 Feb 2009 at 8:48

Attachments:

best_match() chooses */* match over exact match

Because best_match() considers only q values, it can erroneously choose a */* 
match as best when in fact 
there's an exact match that would be better.

To reproduce:

>>> mimeparse.best_match(['application/json', 'text/html'], "application/json, 
text/javascript, */*")
'text/html'

I expect to get the exact match "application/json" instead. Same thing happens 
in Ruby and PHP as well.

The problem is that best_match() does not use the fitness score as part of its 
decision. For this case, 
application/json scores 110 while text/html scores 0, but since q for both is 
1.0 and best_match() looks 
only at q, it sees them both as equally viable candidates. The one that's 
chosen is therefore whichever 
one happens to appear last in the supported list passed as the first argument 
to best_match().

I think the cleanest way to fix this would be to change quality_parsed() to 
return both the fitness score 
and q. However, that would change what appears to be a public interface, so the 
attached patch fixes it 
by adding a new fitness_and_quality_parsed() function and having best_match() 
call that instead. Making 
quality_parsed() public, however, seems like an odd choice, as I can't imagine 
why applications would use 
it directly. If you'd rather have a patch that doesn't introduce a new function 
and modifies 
quality_parsed() instead, just let me know and I'll rework it.

The patch also fixes some typos in the comments, and moves the param_matches 
determination (which 
used to be in quality_parsed() but is now in fitness_and_quality_parsed()) 
inside the "if" since calculating 
it before the "if" is a waste if the "if" is false.

I added two new unit tests for this to all implementations, and of course they 
along with all the original 
tests pass.

Patch attached.

Original issue reported on code.google.com by [email protected] on 13 Sep 2008 at 9:55

Attachments:

[Python] parse_media_range overrides quality values of 0

What steps will reproduce the problem?

mimeparse.parse_media_range('*/*;q=0')

Expected Output: ('*', '*', {'q': '0'})
Actual Output: ('*', '*', {'q': '1'})

I encountered this while parsing an request Accept header with
'application/xml,*/*;q=0' and the only acceptable type on the
server is application/json. It freely let the server return a
JSON representation, but should have failed since the user agent
explicitly defined a quality of 0 for all other mimetypes. The
value of 0 for */* was being overwritten with 1.

56c56,57
<             float(params['q']) > 1 or float(params['q']) < 0:

---
>             not float(params['q']) or float(params['q']) > 1\
>             or float(params['q']) < 0:


What version of the product are you using? On what operating system?

Python version of mimeparse 0.1.3
Using Python 2.7.1

Original issue reported on code.google.com by [email protected] on 12 Feb 2012 at 2:57

setup.py fails on python 3.2

What steps will reproduce the problem?
1. run "pip install mimeparse" on python 3.2

What is the expected output? What do you see instead?

mimeparse should install, but it fails instead:

$ python --version
Python 3.2.2
$ pip --version
pip 1.1 from 
/home/vagrant/virtualenv/python3.2/lib/python3.2/site-packages/pip-1.1-py3.2.egg
 (python 3.2)
$ pip install mimeparse --use-mirrors
Downloading/unpacking mimeparse
  Real name of requirement mimeparse is mimeparse
  Downloading mimeparse-0.1.3.tar.gz
  Running setup.py egg_info for package mimeparse
    Traceback (most recent call last):
      File "<string>", line 14, in <module>
      File "/home/vagrant/virtualenv/python3.2/build/mimeparse/setup.py", line 1
        \ufeff# -*- coding: utf-8 -*-
          ^
    SyntaxError: invalid character in identifier
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 14, in <module>

  File "/home/vagrant/virtualenv/python3.2/build/mimeparse/setup.py", line 1

    \ufeff# -*- coding: utf-8 -*-

      ^

SyntaxError: invalid character in identifier

What version of the product are you using? On what operating system?

mimeparse 0.1.3 on a travis CI build machine. full output is here: 
http://travis-ci.org/#!/martinblech/mimerender/jobs/1773477

Please provide any additional information below.

Original issue reported on code.google.com by [email protected] on 4 Jul 2012 at 4:36

ValueError in 1.3 python version

What steps will reproduce the problem?
In [3]: mimeparse.parse_mime_type("text/html, image/gif, image/jpeg, *; q=.2, 
*/*; q=.2")

What is the expected output? What do you see instead?

In [3]: mimeparse.parse_mime_type("text/html, image/gif, image/jpeg, *; q=.2, 
*/*; q=.2")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/<ipython console> in <module>()
/tmp/mimeparse.py in parse_mime_type(mime_type)
     36     # Turn it into a legal wildcard.
     37     if full_type == '*': full_type = '*/*'
---> 38     (type, subtype) = full_type.split("/")
     39     return (type.strip(), subtype.strip(), params)
     40 
ValueError: too many values to unpack

What version of the product are you using? On what operating system?
1.3

Please provide any additional information below.
https://bitbucket.org/benoitc/django-authopenid/issue/54/type-subtype-parts-0-sp
lit-valueerror-too#comment-483268

Original issue reported on code.google.com by [email protected] on 20 May 2011 at 10:08

Some improvements you may want to backport

The mimeparse module had been included in Python Paste a long time ago.

Not being aware that it was taken from here, I made some improvements to the 
module and added unit tests in the Python code base:

http://bitbucket.org/ianb/paste/src/tip/paste/util/mimeparse.py
http://bitbucket.org/ianb/paste/src/tip/tests/test_util/test_mimeparse.py

I have now added a link to the mimeparse project page to the source to make 
this clearer, and made my test module also include the test cases provided here.

Maybe you want to backport my version.

Note that I made a small incompatible change to best_match: In case of 
ambiguity, my method returns the *first* of the best mime types from the list, 
not the *last* one. I think this makes more sense. I have also mentioned this 
in the docstring.

Original issue reported on code.google.com by [email protected] on 19 Aug 2010 at 4:32

Wrong ordering of candidates

What steps will reproduce the problem?

    mimeparse.best_match(["image/jpeg", "text/plain"], "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5")

What is the expected output? What do you see instead?

Expected: 'image/jpeg'
Instead: 'text/plain'

What version of the product are you using? On what operating system?

Version: 0.1.3
OS: Mac OS X 10.6.4, Python 2.6.1
(Using Python version of mimeparse library)

Please provide any additional information below.

The best_match function is using fitness_and_quality_parsed() to order the 
candidate media ranges, but the first value from that is the fitness, which 
corresponds to what rfc2616 calls the "precedence", or the specificity of the 
range to the particular candidate type. The specificity matters, but the major 
ordering factor should be the quality (the client's measure of which media 
ranges are most preferred).

In the example above, image/jpeg should have a quality of 0.5 (from the */*) 
and text/plain should have a quality of 0.3 (from the text/*). The type with 
the higher quality factor is image/jpeg, and it should win.

I'm attaching a patch that fixes this by swapping the elements returned by 
fitness_and_quality_parsed(), along with an extra test for your suite. Hope 
this helps.

Original issue reported on code.google.com by [email protected] on 15 Sep 2010 at 11:28

Attachments:

python-mimeparse does not include LICENSE file

Hi,

it would be great from packagers view if python-mimeparse would include LICENSE 
file which could be put into /usr/share/doc directory. Please consider 
including it.

Best regards,
Jan Kaluza

Original issue reported on code.google.com by [email protected] on 22 Nov 2011 at 9:59

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.