source: NDG1_archive/BADC_identifiers/identifiers.py @ 310

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/NDG1_archive/BADC_identifiers/identifiers.py@2128
Revision 310, 2.4 KB checked in by selatham, 16 years ago (diff)

changed by selatham at 01/11/04

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/local/python/bin/python
2###########################################################################
3#"""
4#Module:  identifiers
5#Author:  Sue Latham
6#Date:    2004/07/26
7#Version: 0.1
8#Description: This module contains classes for dealing with local identifiers in NDG MetaDataModel
9#instances. For BADC, local identifiers will have standard roots with a numeric suffix which is #incremented  starting from 1.
10# The latest suffix will be held in a python shelve persistent object with the name root as the key.
11#"""
12###########################################################################
13#import the system modules required
14import sys, string, shelve
15
16#import local modules
17import errorHandler
18
19#class for local ids
20class localIDs:
21    def __init__(self, ids_db="BADC_ids", debug=False):
22
23        #open the core ids database for reading and writing. It should already exist.
24        self.base_ids = shelve.open(filename=ids_db, flag='w')
25        self.ids_db=ids_db
26        self.err = errorHandler.handleError()
27
28    def getLastID(self,idtype):
29        """returns the last existing numeric value for the specified id name root """
30        if self.base_ids.has_key(idtype):
31                return self.base_ids[idtype]
32        else:
33                self.err.handleMiscError("ID type %s does not exist in identifiers database %s"  %(idtype,self.ids_db))
34                return
35
36    def getNextID(self,idtype):
37        """returns the next numeric value for the specified id name root. (i.e. doesn't exist yet)"""
38        if self.base_ids.has_key(idtype):
39                return self.base_ids[idtype] +1
40        else:
41                self.err.handleMiscError("ID type %s does not exist in identifiers database %s"  %(idtype,self.ids_db))
42                return
43
44    def createNextID(self,idtype):
45        """returns a local identifier string which can be used in a NDG MetadataModel instance."""
46        if self.base_ids.has_key(idtype):
47                self.base_ids[idtype] = self.base_ids[idtype] + 1
48                return idtype + str(self.base_ids[idtype])
49        else:
50                self.err.handleMiscError("ID type %s does not exist in identifiers database %s"  %(idtype,self.ids_db))
51                return
52
53    def createIDtype(self,idtype):
54        """create a new id type. Default to value of zero initially.."""
55        if self.base_ids.has_key(idtype):
56                self.err.handleMiscError("ID type %s already exists in identifiers database %s"  %(idtype,self.ids_db))
57                return
58        else:
59                self.base_ids[idtype]=0
60
61    def displayIDs(self):
62        for a in self.base_ids.keys():
63                print ("object type = %s, last used value = %s") %(a, self.base_ids[a])
64
Note: See TracBrowser for help on using the repository browser.