Subversion Repositories basico

Rev

Rev 229 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
195 t00m 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
229 t00m 3
# File: collections.py
195 t00m 4
# Author: Tomás Vírseda
5
# License: GPL v3
229 t00m 6
# Description: Collections service
195 t00m 7
 
8
import os
197 t00m 9
import json
195 t00m 10
import uuid
11
 
233 t00m 12
from basico.core.env import FILE
13
from basico.core.service import Service
195 t00m 14
 
197 t00m 15
HEADER = ['id', 'title']
229 t00m 16
COLLECTIONS_FILE = FILE['COLLECTIONS']
195 t00m 17
 
197 t00m 18
 
229 t00m 19
class Collections(Service):
195 t00m 20
    def initialize(self):
21
        '''
229 t00m 22
        Setup Collections Service
195 t00m 23
        '''
24
        self.get_services()
229 t00m 25
        self.clts = {}
197 t00m 26
        self.load()
195 t00m 27
 
28
 
29
    def get_services(self):
30
        self.gui = self.app.get_service("GUI")
31
        self.uif = self.app.get_service("UIF")
32
        self.cb = self.app.get_service('Callbacks')
33
        self.im = self.app.get_service('IM')
34
 
35
 
197 t00m 36
    def load(self):
37
        try:
229 t00m 38
            with open(COLLECTIONS_FILE, 'r') as ft:
39
                self.clts = json.load(ft)
40
                self.debug ("Loaded %d collections" % len(self.clts))
197 t00m 41
        except Exception as error:
229 t00m 42
            self.debug("Collections file not found. Creating a new one.")
197 t00m 43
            self.save()
44
 
45
 
229 t00m 46
    def save(self, collections={}):
47
        if len(collections) == 0:
48
            collections = self.clts
49
        with open(COLLECTIONS_FILE, 'w') as ft:
50
            json.dump(collections, ft)
51
            self.debug ("Saved %d collections" % (len(collections)))
197 t00m 52
 
53
 
195 t00m 54
    def get_all(self):
229 t00m 55
        return self.clts
195 t00m 56
 
57
 
229 t00m 58
    def get_collections_name(self):
197 t00m 59
        names = []
229 t00m 60
        for tid in self.clts:
61
            names.append(self.clts[tid])
197 t00m 62
        return names
63
 
64
 
229 t00m 65
    def get_collections_id(self):
195 t00m 66
        tids = []
229 t00m 67
        for tid in self.clts:
197 t00m 68
            tids.append(tid)
195 t00m 69
        return tids
70
 
71
 
196 t00mlabs 72
    def create(self, entry):
197 t00m 73
        tid = str(uuid.uuid4())
195 t00m 74
        name = entry.get_text()
75
 
76
        if len(name) == 0:
77
            return
78
 
229 t00m 79
        tid_exists = tid in self.get_collections_id()
80
        name_exists = name in self.get_collections_name()
197 t00m 81
 
82
        if tid_exists:
195 t00m 83
            self.debug('Collision? :)')
84
            self.create(entry)
197 t00m 85
        else:
86
            if name_exists:
87
                return
88
            else:
229 t00m 89
                self.clts[tid] = name
90
                self.debug("Created collection: %s" % name)
197 t00m 91
                self.save()
195 t00m 92
 
196 t00mlabs 93
 
197 t00m 94
    def delete(self, tid):
95
        try:
96
            name = self.get_name(tid)
229 t00m 97
            del(self.clts[tid])
98
            self.debug("Deleted Collection: %s" % name)
197 t00m 99
            self.save()
100
            return True
101
        except KeyError:
229 t00m 102
            self.debug("You can't delete a non existent collection...")
197 t00m 103
        except Exception as error:
229 t00m 104
            self.debug("Error deleting collection: %s" % error)
105
            self.debug(self.clts)
197 t00m 106
            raise
196 t00mlabs 107
 
108
 
197 t00m 109
    def get_name(self, tid):
229 t00m 110
        return self.clts[tid]
196 t00mlabs 111