ids.py :  » Blog » Frog » FrogComplete-1.8 » webapps » frog » frog » xmlstorage » 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 » Blog » Frog 
Frog » FrogComplete 1.8 » webapps » frog » frog » xmlstorage » ids.py
#
#   XML-FILES-BASED STORAGE OBJECT IMPLEMENTATION: ID GENERATOR
#
#   This implements an unique-ID-generator that writes its 'state' in
#   an XML file in the filesystem.
#

from xml.dom import minidom
import xml.dom
import threading
import os
from transactionalfile import TransactionalFile
from frog import FILEPROT

import logging
log=logging.getLogger("Snakelets.logger")


FILENAME = "ids.xml"
IDNAMES = ["blogentry", "comment", "category"] 


class IDGenerator:
    def __init__(self, path):
        self.idfilename = os.path.join(path, FILENAME)
        self.lock=threading.Lock()
        self.ids={}
        self.load(IDNAMES)
        
    def create(self, idnames):
        self.ids={}
        for name in idnames:
            self.ids[name]=0
        self.store()

    def load(self, idnames=None):
        self.ids=dict.fromkeys(IDNAMES,0)
        try:
            dom=minidom.parse(self.idfilename)
            for child in dom.documentElement.childNodes:
                if child.nodeType==xml.dom.Node.ELEMENT_NODE:
                    self.ids[str(child.nodeName)] = int(child.firstChild.nodeValue)
        except EnvironmentError,x:
            if idnames:
                self.create( idnames )
            else:
                raise

    def store(self):
        xml='<?xml version="1.0"?>\n<ids>\n'
        for (name,value) in self.ids.items():
            xml+=' <%s>%d</%s>\n' % (name,value,name)
        xml+='</ids>\n'
        newfile=TransactionalFile(self.idfilename,"wb")
        newfile.write(xml)
        newfile.commit()
        os.chmod(newfile.name, FILEPROT)

    def getId(self, idname):
        self.lock.acquire()   # could also have used file locking on the id storage file.
        try:
            self.ids[idname]+=1
            self.store()
            Id=self.ids[idname]
            log.debug("new id for %s: %d" % (idname, Id))
            return Id
        finally:
            self.lock.release()

if __name__=="__main__":
    import tempfile
    gen=IDGenerator(tempfile.gettempdir())
    print "IDFILE=",gen.idfilename
    
    print gen.getId("comment")
    print gen.getId("blogentry")
    print gen.getId("blogentry")
    print gen.getId("category")
    print gen.getId("foobar")

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.