~ubuntu-branches/debian/sid/gtg/sid

« back to all changes in this revision

Viewing changes to GTG/backends/genericbackend.py

  • Committer: Package Import Robot
  • Author(s): Luca Falavigna
  • Date: 2013-05-05 14:23:30 UTC
  • mfrom: (17.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130505142330-a1lmq91yf5ld4zoq
Tags: 0.3-2
* Upload to unstable.
* debian/control:
  - Use canonical URIs for VCS fields
* debian/install:
  - Install bash-completion script.
* debian/links:
  - gtcli link must point to gtcli, not gtg.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# this program.  If not, see <http://www.gnu.org/licenses/>.
18
18
# -----------------------------------------------------------------------------
19
19
 
20
 
'''
21
 
This file contains the most generic representation of a backend, the
22
 
GenericBackend class
23
 
'''
 
20
"""
 
21
This file contains the most generic representation of a backend,
 
22
the GenericBackend class
 
23
"""
24
24
 
25
25
import os
26
26
import sys
35
35
from GTG.tools.logger            import Log
36
36
from GTG.tools.interruptible     import _cancellation_point
37
37
 
 
38
PICKLE_BACKUP_NBR = 2
38
39
 
39
40
 
40
41
class GenericBackend(object):
91
92
        Optional. 
92
93
        NOTE: make sure to call super().initialize()
93
94
        '''
94
 
        #NOTE: I'm disabling this since support for runtime checking of the
95
 
        #        presence of the necessary modules is disabled. (invernizzi)
96
 
#        for module_name in self.get_required_modules():
97
 
#            sys.modules[module_name]= __import__(module_name)
98
95
        self._parameters[self.KEY_ENABLED] = True
99
96
        self._is_initialized = True
100
97
        #we signal that the backend has been enabled
141
138
        '''
142
139
        pass
143
140
 
144
 
#NOTE: task counting is disabled in the UI, so I've disabled it here
145
 
#      (invernizzi)
146
 
#    def get_number_of_tasks(self):
147
 
#        '''
148
 
#        Returns the number of tasks stored in the backend. Doesn't need
149
 
#        to be a fast function, is called just for the UI
150
 
#        '''
151
 
#        raise NotImplemented()
152
 
 
153
 
#NOTE: I'm disabling this since support for runtime checking of the
154
 
#        presence of the necessary modules is disabled. (invernizzi)
155
 
#    @staticmethod
156
 
#    def get_required_modules():
157
 
#        return []
158
 
 
159
141
    def quit(self, disable = False):
160
142
        '''
161
143
        Called when GTG quits or the user wants to disable the backend.
163
145
        @param disable: If disable is True, the backend won't
164
146
                        be automatically loaded when GTG starts
165
147
        '''
166
 
        self._is_initialized = False
167
 
        if disable:
168
 
            self._parameters[self.KEY_ENABLED] = False
169
 
            #we signal that we have been disabled
170
 
            self._signal_manager.backend_state_changed(self.get_id())
171
 
            self._signal_manager.backend_sync_ended(self.get_id())
172
 
        syncing_thread = threading.Thread(target = self.sync).run()
 
148
        if self._parameters[self.KEY_ENABLED]:
 
149
            self._is_initialized = False
 
150
            if disable:
 
151
                self._parameters[self.KEY_ENABLED] = False
 
152
                #we signal that we have been disabled
 
153
                self._signal_manager.backend_state_changed(self.get_id())
 
154
                self._signal_manager.backend_sync_ended(self.get_id())
 
155
            syncing_thread = threading.Thread(target = self.sync).run()
173
156
 
174
157
    def save_state(self):
175
158
        '''
499
482
        '''
500
483
        Returns if the backend is enabled
501
484
 
502
 
        @returns bool
 
485
        @returns: bool
503
486
        '''
504
487
        return self.get_parameters()[GenericBackend.KEY_ENABLED] or \
505
488
                self.is_default()
508
491
        '''
509
492
        Returns if the backend is enabled
510
493
 
511
 
        @returns bool
 
494
        @returns: bool
512
495
        '''
513
496
        return self.get_parameters()[GenericBackend.KEY_DEFAULT_BACKEND]
514
497
 
516
499
        '''
517
500
        Returns if the backend is up and running
518
501
 
519
 
        @returns is_initialized
 
502
        @returns: is_initialized
520
503
        '''
521
504
        return self._is_initialized
522
505
 
562
545
        except OSError, exception:
563
546
            if exception.errno != errno.EEXIST: 
564
547
                raise
 
548
 
 
549
        # Shift backups
 
550
        for i in range(PICKLE_BACKUP_NBR, 1, -1):
 
551
            destination = "%s.bak.%d" % (path, i)
 
552
            source = "%s.bak.%d" % (path, i-1)
 
553
 
 
554
            if os.path.exists(destination):
 
555
                os.unlink(destination)
 
556
 
 
557
            if os.path.exists(source):
 
558
                os.rename(source, destination)
 
559
 
 
560
        # Backup main file
 
561
        if PICKLE_BACKUP_NBR > 0:
 
562
            destination = "%s.bak.1" % path
 
563
            if os.path.exists(path):
 
564
                os.rename(path, destination)
 
565
 
565
566
        #saving
566
567
        with open(path, 'wb') as file:
567
568
                pickle.dump(data, file)
578
579
        path = os.path.join(CoreConfig().get_data_dir(), path)
579
580
        if not os.path.exists(path):
580
581
            return default_value
581
 
        else:
582
 
            with open(path, 'r') as file:
583
 
                try:
584
 
                    return pickle.load(file)
585
 
                except pickle.PickleError:
586
 
                    Log.error("PICKLE ERROR")
587
 
                    return default_value
 
582
 
 
583
        with open(path, 'r') as file:
 
584
            try:
 
585
                return pickle.load(file)
 
586
            except Exception:
 
587
                Log.error("Pickle file for backend '%s' is damaged" % \
 
588
                    self.get_name())
 
589
 
 
590
        # Loading file failed, trying backups
 
591
        for i in range(1, PICKLE_BACKUP_NBR+1):
 
592
            backup_file = "%s.bak.%d" % (path, i)
 
593
            if os.path.exists(backup_file):
 
594
                with open(backup_file, 'r') as file:
 
595
                    try:
 
596
                        data = pickle.load(file)
 
597
                        Log.info("Succesfully restored backup #%d for '%s'" % \
 
598
                            (i, self.get_name()))
 
599
                        return data
 
600
                    except Exception:
 
601
                        Log.error("Backup #%d for '%s' is damaged as well" % \
 
602
                            (i, self.get_name()))
 
603
 
 
604
        # Data could not be loaded, degrade to default data
 
605
        Log.error("There is no suitable backup for '%s', " \
 
606
            "loading default data" % self.get_name())
 
607
        return default_value
 
608
 
588
609
 
589
610
    def _gtg_task_is_syncable_per_attached_tags(self, task):
590
611
        '''
689
710
                pass
690
711
        self.launch_setting_thread(bypass_quit_request = True)
691
712
        self.save_state()
692
 
 
693