annotate src/tools/misc.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 1fe00f0c9a91
children 65bf1bc70f6b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
4 # SAT: a jabber client
811
1fe00f0c9a91 dates update
Goffi <goffi@goffi.org>
parents: 771
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
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: 590
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: 590
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: 590
diff changeset
10 # (at your option) any later version.
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
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: 590
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: 590
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: 590
diff changeset
15 # GNU Affero General Public License for more details.
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
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: 590
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
20 """Misc usefull classes"""
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
21
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 742
diff changeset
22 from sat.core.i18n import _
741
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
23 import sys
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
24 from logging import debug, warning
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
25
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents: 741
diff changeset
26
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
27 class TriggerException(Exception):
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
28 pass
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
30
525
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
31 class SkipOtherTriggers(Exception):
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
32 """ Exception to raise if normal behaviour must be followed instead of
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents: 741
diff changeset
33 following triggers list """
525
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
34 pass
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
35
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
36
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
37 class TriggerManager(object):
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents: 741
diff changeset
38 """This class manage triggers: code which interact to change the behaviour
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
39 of SàT"""
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
40
741
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
41 MIN_PRIORITY = float('-inf')
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
42 MAX_PRIORITY = float('+inf')
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
43
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def __init__(self):
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
45 self.__triggers = {}
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
46
516
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
47 def add(self, point_name, callback, priority=0):
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
48 """Add a trigger to a point
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49 @param point_name: name of the point when the trigger should be run
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 @param callback: method to call at the trigger point
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
51 @param priority: callback will be called in priority order, biggest
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
52 first
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
53 """
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
54 if point_name not in self.__triggers:
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
55 self.__triggers[point_name] = []
741
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
56 if priority != 0 and priority in [trigger_tuple[0] for trigger_tuple in self.__triggers[point_name]]:
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
57 if priority in (MIN_PRIORITY, MAX_PRIORITY):
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
58 warning(_("There is already a bound priority [%s]") % point_name)
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
59 else:
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
60 debug(_("There is already a trigger with the same priority [%s]") % point_name)
516
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
61 self.__triggers[point_name].append((priority, callback))
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
62 self.__triggers[point_name].sort(key=lambda trigger_tuple:
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
63 trigger_tuple[0], reverse=True)
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def remove(self, point_name, callback):
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
66 """Remove a trigger from a point
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
67 @param point_name: name of the point when the trigger should be run
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 @param callback: method to remove, must exists in the trigger point"""
516
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
69 for trigger_tuple in self.__triggers[point_name]:
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
70 if trigger_tuple[1] == callback:
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
71 self.__triggers[point_name].remove(trigger_tuple)
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
72 return
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
73 raise TriggerException("Trying to remove an unexisting trigger")
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
74
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75 def point(self, point_name, *args, **kwargs):
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76 """This put a trigger point
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 All the trigger for that point will be run
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78 @param point_name: name of the trigger point
501
e9634d2e7b38 core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents: 480
diff changeset
79 @return: True if the action must be continued, False else"""
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
80 if point_name not in self.__triggers:
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 return True
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
82
516
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
83 for priority,trigger in self.__triggers[point_name]:
525
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
84 try:
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
85 if not trigger(*args, **kwargs):
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
86 return False
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
87 except SkipOtherTriggers:
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
88 break
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 return True