annotate frontends/src/tools/strings.py @ 853:c2f6ada7858f

core (sqlite): automatic database update: - new Updater class check database consistency (by calculating a hash on the .schema), and updates base if necessary - database now has a version (1 for current, 0 will be for 0.3's database), for each change this version will be increased - creation statements and update statements are in the form of dict of dict with tuples. There is a help text at the top of the module to explain how it works - if we are on a development version, the updater try to update the database automaticaly (without deleting table or columns). The Updater.generateUpdateData method can be used to ease the creation of update data (i.e. the dictionary at the top, see the one for the key 1 for an example). - if there is an inconsistency, an exception is raised, and a message indicate the SQL statements that should fix the situation. - well... this is rather complicated, a KISS method would maybe have been better. The future will say if we need to simplify it :-/ - new DatabaseError exception
author Goffi <goffi@goffi.org>
date Sun, 23 Feb 2014 23:30:32 +0100
parents 429c6a0ef73d
children 069ad98b360d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
690
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
1 #!/usr/bin/python
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
3
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT helpers methods for plugins
811
1fe00f0c9a91 dates update
Goffi <goffi@goffi.org>
parents: 769
diff changeset
5 # Copyright (C) 2013, 2014 Adrien Cossa (souliane@mailoo.org)
690
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
6
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # (at your option) any later version.
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
11
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
16
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
19
769
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
20 import re
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
21
690
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
22
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
23 def getURLParams(url):
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
24 """This comes from pyjamas.Location.makeUrlDict with a small change
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
25 to also parse full URLs, and parameters with no value specified
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
26 (in that case the default value "" is used).
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
27 @param url: any URL with or without parameters
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
28 @return: a dictionary of the parameters, if any was given, or {}
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
29 """
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
30 dict_ = {}
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
31 if "/" in url:
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
32 # keep the part after the last "/"
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
33 url = url[url.rindex("/") + 1:]
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
34 if url.startswith("?"):
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
35 # remove the first "?"
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
36 url = url[1:]
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
37 pairs = url.split("&")
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
38 for pair in pairs:
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
39 if len(pair) < 3:
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
40 continue
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
41 kv = pair.split("=", 1)
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
42 dict_[kv[0]] = kv[1] if len(kv) > 1 else ""
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
43 return dict_
d8e7a58eaa00 misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff changeset
44
769
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
45
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
46 def addURLToText(string):
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
47 """Check a text for what looks like an URL and make it clickable. Regexp
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
48 from http://daringfireball.net/2010/07/improved_regex_for_matching_urls"""
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
49
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
50 def repl(match):
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
51 url = match.group(0)
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
52 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url):
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
53 url = "http://" + url
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
54 return '<a href="%s" target="_blank" class="url">%s</a>' % (url, match.group(0))
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
55 pattern = r"""(?i)\b((?:[a-z]{3,}://|(www|ftp)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/|mailto:|xmpp:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))"""
8b6137f7b4e1 tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents: 719
diff changeset
56 return re.sub(pattern, repl, string)
842
429c6a0ef73d frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents: 811
diff changeset
57
429c6a0ef73d frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents: 811
diff changeset
58
429c6a0ef73d frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents: 811
diff changeset
59 def addURLToImage(string):
429c6a0ef73d frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents: 811
diff changeset
60 """Check a XHTML text for what looks like an imageURL and make it clickable"""
429c6a0ef73d frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents: 811
diff changeset
61 def repl(match):
429c6a0ef73d frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents: 811
diff changeset
62 url = match.group(1)
429c6a0ef73d frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents: 811
diff changeset
63 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0))
429c6a0ef73d frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents: 811
diff changeset
64 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>"""
429c6a0ef73d frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents: 811
diff changeset
65 return re.sub(pattern, repl, string)