Subversion Repositories basico

Rev

Rev 355 | Rev 358 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
355 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
"""
4
# File: srv_attach.py
5
# Author: Tomás Vírseda
6
# License: GPL v3
7
# Description: Attachment service
8
"""
9
 
10
import os
11
import json
12
import uuid
13
import glob
356 t00mlabs 14
import shutil
355 t00mlabs 15
from os.path import sep as SEP
16
 
17
from basico.core.mod_env import FILE, LPATH
18
from basico.core.mod_srv import Service
19
 
20
 
21
class Attachment(Service):
22
    def initialize(self):
23
        '''
24
        Setup Attachment Service
25
        '''
26
        self.get_services()
27
 
28
 
29
    def get_services(self):
30
        self.srvutl = self.get_service('Utils')
31
 
32
 
356 t00mlabs 33
    def gen_tid(self, sid='000000000'):
355 t00mlabs 34
        '''
35
        Generate new attachment id (same as for annotations)
36
        '''
356 t00mlabs 37
        return "%s@T%s" % (sid, str(uuid.uuid4()))
355 t00mlabs 38
 
39
 
356 t00mlabs 40
    def create(self, path, aid):
41
        sid = self.get_sid(aid)
42
        tid = self.gen_tid(sid)
43
        metadata = self.srvutl.get_file_metadata(path)
44
        metadata['AID'] = aid
45
        metadata['TID'] = tid
46
        metadata['Source'] = path
47
        metadata['Created'] = self.srvutl.timestamp()
48
        ATTACHMENT_FILE_METADATA = LPATH['ATTACHMENTS'] + tid + '.json'
49
        ATTACHMENT_FILE_CONTENT = LPATH['ATTACHMENTS'] + tid # No extension needed
50
 
355 t00mlabs 51
        # copy attachment file to attachemnts database
52
        shutil.copy(path, ATTACHMENT_FILE_CONTENT)
53
 
54
        # Write attachment metadata
55
        with open(ATTACHMENT_FILE_METADATA, 'w') as fa:
56
            json.dump(metadata, fa)
57
 
356 t00mlabs 58
        self.log.info("Attachment '%s' (%s) created" % (metadata['Title'], metadata['TID']))
355 t00mlabs 59
 
60
 
61
    def delete(self, aid):
62
        sid = self.get_sid(aid)
63
        ATTACHMENT_FILE_METADATA = LPATH['ATTACHMENTS'] + aid + '.json'
64
        ATTACHMENT_FILE_CONTENT = LPATH['ATTACHMENTS'] + aid
65
        title = self.get_title(aid)
66
 
67
        if os.path.exists(ATTACHMENT_FILE_METADATA):
68
            os.unlink(ATTACHMENT_FILE_METADATA)
69
 
70
        if os.path.exists(ATTACHMENT_FILE_CONTENT):
71
            os.unlink(ATTACHMENT_FILE_CONTENT)
72
 
73
        self.log.info("Attachment '%s' (%s) deleted" % (title, aid))
74
 
75
 
76
    def get_by_sid(self, sid):
77
        ATTACHMENT_FILES = LPATH['ATTACHMENTS'] + '%s*.json' % sid
78
        attachments = glob.glob(ATTACHMENT_FILES)
79
        attachments.sort(reverse=True)
80
 
81
        return attachments
82
 
83
 
84
    def get_all(self):
85
        return glob.glob(LPATH['ATTACHMENTS'] + '*.json')
86
 
87
 
88
    def get_total(self):
89
        return len(self.get_all())
90
 
91
 
92
    def get_sid(self, aid):
93
        if '@' in aid:
94
            return aid[:aid.find('@')]
95
        else:
356 t00mlabs 96
            return '0000000000'
355 t00mlabs 97
 
98
 
99
    def get_metadata_from_aid(self, aid=None):
100
        if aid is not None:
101
            ATTACHMENT_FILE_METADATA = LPATH['ATTACHMENTS'] + aid + '.json'
102
            with open(ATTACHMENT_FILE_METADATA, 'r') as fa:
103
                attachment = json.load(fa)
104
            return attachment
105
        else:
106
            return None
107
 
108
 
109
    def get_metadata_from_file(self, filename=None):
110
        if filename is not None:
111
            if os.path.exists(filename):
112
                with open(filename, 'r') as fa:
113
                    metadata = json.load(fa)
114
                return metadata
115
            else:
116
                return None
117
 
118
 
119
    def is_valid(self, aid):
120
        ATTACHMENT_FILE_METADATA = LPATH['ATTACHMENTS'] + aid + '.json'
121
        ATTACHMENT_FILE_CONTENT = LPATH['ATTACHMENTS'] + aid
122
        ATTACHMENT_FILE = LPATH['ATTACHMENTS'] + aid + '.json'
123
        valid = os.path.exists(ATTACHMENT_FILE_METADATA) and os.path.exist(ATTACHMENT_FILE_CONTENT)
124
        if valid is False:
125
            self.log.debug("Attachment %s is not valid or it doesn't exist yet." % aid)
126
 
127
        return valid
128
 
129
 
130
    def get_title(self, aid):
131
        ATTACHMENT_FILE = LPATH['ATTACHMENTS'] + aid + '.json'
132
        with open(ATTACHMENT_FILE, 'r') as fa:
133
            metadata = json.load(fa)
134
            return metadata['Title']
135
 
136
 
137
    def search_term(self, term):
138
        matches = set()
139
        attachments = self.get_all()
140
 
141
        for fname in attachments:
142
            # search only in title
143
            with open(fname, 'r') as fa:
144
                try:
145
                    attachment = json.load(fa)
146
                    text = attachment['Title']
147
                    if term.upper() in text.upper():
148
                        # ~ self.log.debug("Found '%s' in '%s'", term, text)
149
                        matches.add(fname)
150
                except Exception as error:
151
                    self.log.error("%s: %s", fname, error)
152
 
153
            # SEARCH IN ALL PROPERTIES (DISABLED)
154
            # ~ with open(fname, 'r') as fa:
155
                # ~ try:
156
                    # ~ attachment = json.load(fa)
157
                    # ~ text = ''
158
                    # ~ for node in attachment:
159
                        # ~ text += attachment[node]
160
                    # ~ if term.upper() in text.upper():
161
                        # ~ self.log.debug("Found '%s' in '%s'", term, text)
162
                        # ~ matches.add(fname)
163
                # ~ except Exception as error:
164
                    # ~ self.log.error("%s: %s", fname, error)
165
 
166
            # SEARCH IN CONTENT (DISABLED)
167
            # ~ fcontent = fname.replace('.json', '.adoc')
168
            # ~ text = open(fcontent, 'r').read()
169
            # ~ if term.upper() in text.upper():
170
                # ~ matches.add(fname)
171
 
172
        return matches
173
 
174
 
175
    def finalize(self):
176
        pass
177