~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/searchengines.py

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Miro - an RSS based video player application
2
 
# Copyright (C) 2005-2009 Participatory Culture Foundation
 
2
# Copyright (C) 2005-2010 Participatory Culture Foundation
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
30
30
:class:`SearchEngineInfo` and related helper functions.
31
31
"""
32
32
 
33
 
from miro.util import checkU, returnsUnicode
 
33
from miro.util import check_u, returns_unicode
34
34
from miro.xhtmltools import urlencode
35
35
from xml.dom.minidom import parse
36
36
from miro.plat import resources
41
41
import logging
42
42
from miro.gtcache import gettext as _
43
43
 
 
44
class IntentionalCrash(Exception):
 
45
    pass
 
46
 
44
47
class SearchEngineInfo:
45
48
    """Defines a search engine in Miro.
46
49
 
47
50
    .. note::
48
51
 
49
 
       Don't instantiate this yourself--search engines are defined by
 
52
       Don't instantiate this yourself---search engines are defined by
50
53
       ``.xml`` files in the ``resources/searchengines/`` directory.
51
54
    """
52
55
    def __init__(self, name, title, url, sort_order=0, filename=None):
53
 
        checkU(name)
54
 
        checkU(title)
55
 
        checkU(url)
 
56
        check_u(name)
 
57
        check_u(title)
 
58
        check_u(url)
56
59
        self.name = name
57
60
        self.title = title
58
61
        self.url = url
67
70
        """Returns the request url expanding the query, filter adult content,
68
71
        and results limit place holders.
69
72
        """
70
 
        requestURL = self.url.replace(u"%s", urlencode(query))
71
 
        requestURL = requestURL.replace(u"%a", unicode(int(not filterAdultContents)))
72
 
        requestURL = requestURL.replace(u"%l", unicode(int(limit)))
73
 
        return requestURL
 
73
        request_url = self.url.replace(u"%s", urlencode(query))
 
74
        request_url = request_url.replace(u"%a", 
 
75
                                          unicode(int(not filterAdultContents)))
 
76
        request_url = request_url.replace(u"%l", unicode(int(limit)))
 
77
 
 
78
        return request_url
74
79
 
75
80
    def __repr__(self):
76
81
        return "<SearchEngineInfo %s %s>" % (self.name, self.title)
89
94
    try:
90
95
        for f in os.listdir(dir_):
91
96
            if f.endswith(".xml"):
92
 
                engines[os.path.normcase(f)] = os.path.normcase(os.path.join(dir_, f))
 
97
                engines[os.path.normcase(f)] = os.path.normcase(
 
98
                    os.path.join(dir_, f))
93
99
    except OSError:
94
100
        pass
95
101
    return engines
160
166
    global _engines
161
167
    _delete_engines()
162
168
    engines = _search_for_search_engines(resources.path("searchengines"))
163
 
    engines_dir = os.path.join(config.get(prefs.SUPPORT_DIRECTORY), "searchengines")
 
169
    engines_dir = os.path.join(
 
170
        config.get(prefs.SUPPORT_DIRECTORY), "searchengines")
164
171
    engines.update(_search_for_search_engines(engines_dir))
165
172
    if config.get(prefs.THEME_NAME):
166
173
        theme_engines_dir = resources.theme_path(config.get(prefs.THEME_NAME),
192
199
                new_engines.extend(_engines)
193
200
        _engines = new_engines
194
201
 
195
 
@returnsUnicode
 
202
@returns_unicode
196
203
def get_request_url(engine_name, query, filter_adult_contents=True, limit=50):
197
204
    """Returns the url for the given search engine, query,
198
205
    filter_adult_contents, and limit.
199
206
 
200
207
    There are two "magic" queries:
201
208
 
202
 
    * ``LET'S TEST DTV'S CRASH REPORTER TODAY`` which rases a NameError thus
203
 
      allowing us to test the crash reporter
 
209
    * ``LET'S TEST DTV'S CRASH REPORTER TODAY`` which rases a
 
210
      NameError thus allowing us to test the crash reporter
204
211
 
205
212
    * ``LET'S DEBUT DTV: DUMP DATABASE`` which causes Miro to dump the
206
 
       database to xml and place it in the Miro configuration directory
 
213
       database to xml and place it in the Miro configuration
 
214
       directory
207
215
    """
208
216
    if query == "LET'S TEST DTV'S CRASH REPORTER TODAY":
209
 
        # FIXME - should change this to a real exception rather than a NameError
210
 
        someVariable = intentionallyUndefinedVariableToTestCrashReporter
211
 
        return u""
 
217
        raise IntentionalCrash("intentional error here")
212
218
 
213
219
    if query == "LET'S DEBUG DTV: DUMP DATABASE":
214
220
        app.db.dumpDatabase()
239
245
    return None
240
246
 
241
247
def get_last_engine():
242
 
    """Checks the preferences and returns the SearchEngine object of that
243
 
    name or ``None``.
 
248
    """Checks the preferences and returns the SearchEngine object of
 
249
    that name or ``None``.
244
250
    """
245
251
    e = config.get(prefs.LAST_SEARCH_ENGINE)
246
252
    engine = get_engine_for_name(e)