Mercurial > libervia-backend
annotate src/tools/utils.py @ 606:21ddafccf32d
installation: added forgotten imaging dependency
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 23 Feb 2013 17:50:58 +0100 |
parents | a4f6f78f0620 |
children | 84a6e83157c2 |
rev | line source |
---|---|
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 SAT: a jabber client |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU Affero General Public License for more details. |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 """ various useful methods """ |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 import unicodedata |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 def clean_ustr(ustr): |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 """Clean unicode string |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 remove special characters from unicode string""" |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 def valid_chars(unicode_source): |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 for char in unicode_source: |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 if unicodedata.category(char) == 'Cc' and char!='\n': |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 continue |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 yield char |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 return ''.join(valid_chars(ustr)) |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |