Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/parsers/__init__.py
blob: 4c626cb388166b7a4d7bf70bcb86616ec0b430d6 (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

# __init__.py - general content-type parser interface
#
# Copyright (C) 2005 Arthur de Jong
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

"""This package groups all the supported content-types.

A content-type module can be requested by the get_parsemodule() function.
Each module should export the following function:

    parse(content,link)
        Based on the content fill in the common fields of the link object."""

# the modules that should be imported
_modules = ('html', 'css')

# a map of mimetypes to modules
_parsermodules = {}

def _init_modules():
    """Initialize the modules."""
    # go throught all known modules to probe the content-types (do this only once)
    for m in _modules:
        p = __import__('parsers.'+m,globals(),locals(),[m])
        for t in p.mimetypes:
            _parsermodules[t] = p

def get_parsermodule(mimetype):
    """Look up the correct module for the specified mimetype."""
    if _parsermodules == {}:
        _init_modules()
    # check if we have a supported content-type
    if _parsermodules.has_key(mimetype):
        return _parsermodules[mimetype]
    return None

def get_mimetypes():
    if _parsermodules == {}:
        _init_modules()
    return _parsermodules.keys()