service.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » examples » pastebin » pastebin » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » examples » pastebin » pastebin » service.py
import cPickle as pickle
import os.path
import time
from zope.interface import implements

from twisted.application import service
from twisted.python import log

from pastebin import interfaces
from pastebin import pasting


class Record(object):

    def __init__(self, oid, author, time):
        self.oid = oid
        self.author = author
        self.time = time
        self.version = 0


class FSPasteBinService(service.Service):

    implements(interfaces.IPasteBin)

    def __init__(self, storageDir):
        self._dir = storageDir

    def getListOfPastings(self, limit=None):
        if limit is None:
            limited = self._index
        else:
            limited = self._index[0:limit]
        return [(r.oid, r.author, r.time) for r in limited]

    def _makeFilename(self, name):
        return os.path.join(self._dir, name)

    def _loadPastingData(self, oid):
        f = file(self._makeFilename(str(oid)), 'rb')
        return pickle.load(f)

    def _savePastingData(self, oid, data):
        f = file(self._makeFilename(str(oid)), 'wb')
        pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)

    def getPasting(self, oid):
        data = self._loadPastingData(oid)
        return Pasting(data)

    def addPasting(self, author, text):
        oid = self._nextOid
        now = time.gmtime()
        data = [{'author':author, 'time':now, 'text':text}]
        self._savePastingData(oid, data)
        self._index.insert(0, Record(oid, author, now))
        self._nextOid += 1
        return oid

    def updatePasting(self, oid, author, text):
        now = time.gmtime()
        data = self._loadPastingData(oid)
        data.append({'author':author, 'time':now, 'text':text})
        self._savePastingData(oid, data)
        for i, r in enumerate(self._index):
            if r.oid == oid:
                r.time = now
                self._index.insert(0,self._index.pop(i))
        
    def startService(self):
        log.msg('Loading index')
        try:
            f = file(self._makeFilename('index'), 'rb')
            d = pickle.load(f)
            self._index = d['index']
            self._nextOid = d['nextOid']
        except IOError:
            self._index = []
            self._nextOid = 1

    def stopService(self):
        log.msg('Storing index')
        d = {'index':self._index, 'nextOid':self._nextOid}
        f = file(self._makeFilename('index'), 'wb')
        pickle.dump(d, f, pickle.HIGHEST_PROTOCOL)

class Pasting(object):
    
    implements(pasting.IPasting)

    def __init__(self, data):
        self._data = data
    
    def getLatestVersion(self):
        return self.getVersion(-1)
    
    def getVersion(self, version):
        return Version(self._data[version])

    def getHistory(self):
        history = [(i,d['author'],d['time']) for i,d in enumerate(self._data)]
        history.reverse()
        return history
                              

class Version:

    implements(pasting.IVersion)

    def __init__(self, data):
        self._data = data

    def getAuthor(self):
        return self._data['author']

    def getText(self):
        return self._data['text']

    def getTime(self):
        return self._data['time']
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.