| 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 | |
|---|
| 11 | import re |
|---|
| 12 | |
|---|
| 13 | from trac.test import Mock, MockPerm |
|---|
| 14 | |
|---|
| 15 | from tractags.compat import partial |
|---|
| 16 | |
|---|
| 17 | _TAG_SPLIT = re.compile('[,\s]+') |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | # DEVEL: This needs monitoring for possibly varying endpoint requirements. |
|---|
| 21 | MockReq = partial(Mock, args=dict(), authname='anonymous', |
|---|
| 22 | perm=MockPerm(), session=dict()) |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | def 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 | |
|---|
| 39 | def 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 | |
|---|
| 46 | def split_into_tags(text): |
|---|
| 47 | """Split plain text into tags.""" |
|---|
| 48 | return set(filter(None, [tag.strip() for tag in _TAG_SPLIT.split(text)])) |
|---|