Subversion Repositories basico

Rev

Rev 230 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: annotations.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Annotations service

import os
import json
import uuid
import glob
from os.path import sep as SEP

from basico.core.env import FILE, LPATH
from basico.core.service import Service


class Annotation(Service):
    def initialize(self):
        '''
        Setup Annotation Service
        '''

        self.get_services()


    def get_services(self):
        self.utils = self.app.get_service('Utils')


    def gen_aid(self, sid):
        '''
        Generate new annotation id
        '''

        return "%s@%s" % (sid, str(uuid.uuid4()))


    def create(self, annotation):
        ANNOTATION_FILE_METADATA = LPATH['ANNOTATIONS'] + annotation['aid'] + '.json'
        ANNOTATION_FILE_CONTENT = LPATH['ANNOTATIONS'] + annotation['aid'] + '.adoc'
        annotation['timestamp'] = self.utils.timestamp()
        # Write annotation content first and delete it
        with open(ANNOTATION_FILE_CONTENT, 'w') as fc:
            fc.write(annotation['content'])
        del annotation['content']

        # Write annotation metadata
        with open(ANNOTATION_FILE_METADATA, 'w') as fa:
            json.dump(annotation, fa)


    def update(self, annotation):
        ANNOTATION_FILE_METADATA = LPATH['ANNOTATIONS'] + annotation['aid'] + '.json'
        ANNOTATION_FILE_CONTENT = LPATH['ANNOTATIONS'] + annotation['aid'] + '.adoc'

        annotation['timestamp'] = self.utils.timestamp()
        # Write updated annotation first and delete the old one after
        with open(ANNOTATION_FILE_CONTENT, 'w') as fc:
            fc.write(annotation['content'])
        del annotation['content']

        # Write annotation metadata
        with open(ANNOTATION_FILE_METADATA, 'w') as fa:
            json.dump(annotation, fa)


    def delete(self, aid):
        sid = self.get_sid(aid)
        ANNOTATION_FILE_METADATA = LPATH['ANNOTATIONS'] + aid + '.json'
        ANNOTATION_FILE_CONTENT = LPATH['ANNOTATIONS'] + aid + '.adoc'

        if os.path.exists(ANNOTATION_FILE_METADATA):
            os.unlink(ANNOTATION_FILE_METADATA)

        if os.path.exists(ANNOTATION_FILE_CONTENT):
            os.unlink(ANNOTATION_FILE_CONTENT)


    def get_by_sid(self, sid):
        ANNOTATION_FILES = LPATH['ANNOTATIONS'] + '%s*.json' % sid
        annotations = glob.glob(ANNOTATION_FILES)
        annotations.sort(reverse=True)

        return annotations


    def get_all(self):
        return glob.glob(LPATH['ANNOTATIONS'] + '*.json')
        return annotations


    def get_sid(self, aid):
        if '@' in aid:
            return aid[:aid.find('@')]
        else:
            return aid # aid = sid


    def get_metadata_from_file(self, aid=None):
        if aid is not None:
            ANNOTATION_FILE_METADATA = LPATH['ANNOTATIONS'] + aid + '.json'
            with open(ANNOTATION_FILE_METADATA, 'r') as fa:
                annotation = json.load(fa)
            return annotation
        else:
            return None


    def is_valid(self, aid):
        ANNOTATION_FILE = LPATH['ANNOTATIONS'] + aid + '.json'
        valid = os.path.exists(ANNOTATION_FILE)
        if valid is False:
            self.debug("Annotation %s is not valid? Please, check files in %s." % (aid, LPATH['ANNOTATIONS']))

        return valid