source: tagsplugin/tags/0.8/tractags/util.py

Last change on this file was 14157, checked in by Steffen Hoffmann, 9 years ago

TagsPlugin: Add a versatile Trac request mockup, refs #11945.

This is partially a rework of [14146] using partial built-in for better
maintainability.

Thanks to Ryan J Ollos for designing and proposing the new utility class.

File size: 1.4 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2006 Alec Thomas <alec@swapoff.org>
4# Copyright (C) 2013,2014 Steffen Hoffmann <hoff.st@web.de>
5# Copyright (C) 2014 Ryan J Ollos <ryan.j.ollos@gmail.com>
6#
7# This software is licensed as described in the file COPYING, which
8# you should have received as part of this distribution.
9#
10
11import re
12
13from trac.test import Mock, MockPerm
14
15from tractags.compat import partial
16
17_TAG_SPLIT = re.compile('[,\s]+')
18
19
20# DEVEL: This needs monitoring for possibly varying endpoint requirements.
21MockReq = partial(Mock, args=dict(), authname='anonymous',
22                  perm=MockPerm(), session=dict())
23
24
25def get_db_exc(env):
26    if hasattr(env, 'db_exc'):
27        return env.db_exc
28    database = env.config.get('trac', 'database')
29    if database.startswith('sqlite:'):
30        from trac.db.sqlite_backend import sqlite
31        return sqlite
32    if database.startswith('postgres:'):
33        from trac.db.postgres_backend import psycopg
34        return psycopg
35    if database.startswith('mysql:'):
36        from trac.db.mysql_backend import MySQLdb
37        return MySQLdb
38
39def query_realms(query, all_realms):
40    realms = []
41    for realm in all_realms:
42        if re.search('(^|\W)realm:%s(\W|$)' % realm, query):
43            realms.append(realm)
44    return realms
45
46def split_into_tags(text):
47    """Split plain text into tags."""
48    return set(filter(None, [tag.strip() for tag in _TAG_SPLIT.split(text)]))
Note: See TracBrowser for help on using the repository browser.