File: mimetypes.py

package info (click to toggle)
python-pweave 0.30.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,064 kB
  • sloc: python: 30,281; makefile: 167
file content (46 lines) | stat: -rw-r--r-- 1,194 bytes parent folder | download
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
import os
import sys

class MimeType(object):
    """Default mimetypes for input extensions"""

    def __init__(self, mimetype, file_ext):
        self.type = mimetype
        self.file_ext = file_ext



class MimeTypes(object):
    """Methods for handling mimetypes and file extensions"""

    # Supported input types
    known_types = [
            ("text/markdown", "md"),
            ("text/latex", "tex"),
            ("text/html", "html"),
            ("text/restructuredtext", "rst"),
    ]

    @classmethod
    def guess_mimetype(cls, filename):
        """Guess mimetype based on input filename"""
        _, ext = os.path.splitext(filename)
        ext = ext.lower()

        for type in cls.known_types:
            if type[1] in ext:
                return MimeType(*type)

        #Default to markdown
        return MimeType("text/markdown", "md")

    @classmethod
    def get_mimetype(cls, mimetype):
        """Return mimetype object based on type"""

        for type in cls.known_types:
            if type[0] == mimetype:
                return MimeType(*type)

        sys.stderr.write("Unsupport mimetype, using markdown")
        return MimeType("text/markdown", "md")