summaryrefslogtreecommitdiff
path: root/python/qpid/mimetype.py
blob: f512996b9fd698d9e55eaf4b4ffe787d3cca8a9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
import re, rfc822
from lexer import Lexicon, LexError
from parser import Parser, ParseError

l = Lexicon()

LPAREN = l.define("LPAREN", r"\(")
RPAREN = l.define("LPAREN", r"\)")
SLASH = l.define("SLASH", r"/")
SEMI = l.define("SEMI", r";")
EQUAL = l.define("EQUAL", r"=")
TOKEN = l.define("TOKEN", r'[^()<>@,;:\\"/\[\]?= ]+')
STRING = l.define("STRING", r'"(?:[^\\"]|\\.)*"')
WSPACE = l.define("WSPACE", r"[ \n\r\t]+")
EOF = l.eof("EOF")

LEXER = l.compile()

def lex(st):
  return LEXER.lex(st)

class MimeTypeParser(Parser):

  def __init__(self, tokens):
    Parser.__init__(self, [t for t in tokens if t.type is not WSPACE])

  def parse(self):
    result = self.mimetype()
    self.eat(EOF)
    return result

  def mimetype(self):
    self.remove_comments()
    self.reset()

    type = self.eat(TOKEN).value.lower()
    self.eat(SLASH)
    subtype = self.eat(TOKEN).value.lower()

    params = []
    while True:
      if self.matches(SEMI):
        params.append(self.parameter())
      else:
        break

    return type, subtype, params

  def remove_comments(self):
    while True:
      self.eat_until(LPAREN, EOF)
      if self.matches(LPAREN):
        self.remove(*self.comment())
      else:
        break

  def comment(self):
    start = self.eat(LPAREN)

    while True:
      self.eat_until(LPAREN, RPAREN)
      if self.matches(LPAREN):
        self.comment()
      else:
        break

    end = self.eat(RPAREN)
    return start, end

  def parameter(self):
    self.eat(SEMI)
    name = self.eat(TOKEN).value
    self.eat(EQUAL)
    value = self.value()
    return name, value

  def value(self):
    if self.matches(TOKEN):
      return self.eat().value
    elif self.matches(STRING):
      return rfc822.unquote(self.eat().value)
    else:
      raise ParseError(self.next(), TOKEN, STRING)

def parse(addr):
  return MimeTypeParser(lex(addr)).parse()

__all__ = ["parse", "ParseError"]