Mercurial > libervia-backend
annotate src/tools/utils.py @ 1482:80cd55dd5b04
core, bridge: added getFeatures method:
this async method indicate which plugins are activated, and add an additional data dictionary with plugin specific data.
The result can be general (if profile==C.PROF_KEY_NONE) or specific to profile.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 21 Aug 2015 17:08:26 +0200 |
parents | 934e402c90bf |
children | 566908d483f6 |
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 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
4 # SAT: a jabber client |
1396 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org) |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
10 # (at your option) any later version. |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
15 # GNU Affero General Public License for more details. |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ various useful methods """ |
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 import unicodedata |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
23 import os.path |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
24 from sat.core.log import getLogger |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
25 log = getLogger(__name__) |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
26 |
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
27 |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 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
|
29 """Clean unicode string |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
30 |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 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
|
32 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
|
33 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
|
34 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
|
35 continue |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 yield char |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 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
|
38 |
1376
28fd9e838f8f
core: getRepositoryData now get the module in argument
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
39 def getRepositoryData(module, as_string=True): |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
40 """Retrieve info on current mecurial repository |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
41 |
1376
28fd9e838f8f
core: getRepositoryData now get the module in argument
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
42 @param module: module to look for (e.g. sat, libervia) |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
43 @param as_string(bool): if True return a string, else return a dictionary |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
44 @return (unicode, dictionary): retrieved info in a nice string, |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
45 or a dictionary with retrieved data (key is not present if data is not found), |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
46 key can be: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
47 - node: full revision number (40 bits) |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
48 - branch: branch name |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
49 - date: ISO 8601 format date |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
50 - tag: latest tag used in hierarchie |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
51 """ |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
52 from distutils.spawn import find_executable |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
53 import subprocess |
1471
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
54 KEYS=("node", "node_short", "branch", "date", "tag") |
1376
28fd9e838f8f
core: getRepositoryData now get the module in argument
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
55 repos_root = os.path.dirname(module.__file__) |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
56 hg_path = find_executable('hg') |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
57 |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
58 if hg_path is not None: |
1376
28fd9e838f8f
core: getRepositoryData now get the module in argument
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
59 os.chdir(repos_root) |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
60 try: |
1471
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
61 hg_data_raw = subprocess.check_output(["hg","log", "-r", "-1", "--template","{node}\n{node|short}\n{branch}\n{date|isodate}\n{latesttag}"]) |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
62 except subprocess.CalledProcessError: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
63 hg_data = {} |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
64 else: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
65 hg_data = dict(zip(KEYS, hg_data_raw.split('\n'))) |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
66 try: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
67 hg_data['modified'] = '+' in subprocess.check_output(["hg","id","-i"]) |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
68 except subprocess.CalledProcessError: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
69 pass |
1471
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
70 else: |
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
71 hg_data = {} |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
72 |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
73 if not hg_data: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
74 log.debug(u"Mercurial not available or working, trying to get data from dirstate") |
1376
28fd9e838f8f
core: getRepositoryData now get the module in argument
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
75 os.chdir(os.path.relpath('..', repos_root)) |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
76 try: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
77 with open('.hg/dirstate') as hg_dirstate: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
78 hg_data['node'] = hg_dirstate.read(20).encode('hex') |
1471
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
79 hg_data['node_short'] = hg_data['node'][:12] |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
80 except IOError: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
81 log.warning(u"Can't access repository data") |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
82 |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
83 if as_string: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
84 if not hg_data: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
85 return u'repository data unknown' |
1471
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
86 strings = [u'rev', hg_data['node_short']] |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
87 try: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
88 if hg_data['modified']: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
89 strings.append(u"[M]") |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
90 except KeyError: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
91 pass |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
92 try: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
93 strings.extend([u'({branch} {date})'.format(**hg_data)]) |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
94 except KeyError: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
95 pass |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
96 |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
97 return u' '.join(strings) |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
98 else: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
99 return hg_data |