source: ndgCommon/trunk/ndg/common/src/clients/abstractcrudclient.py @ 4919

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/clients/abstractcrudclient.py@4985
Revision 4919, 2.0 KB checked in by cbyrom, 11 years ago (diff)

Create abstract implementation of some of the new interfaces - to
provide generic, universal methods.

Line 
1'''
2 Abstract class for clients extending search functionality to include standard CRUD
3 (create, replace, update, delete) functionality for metadata documents
4 - provides some methods that should be universal to all client implementations
5 
6 @author: C Byrom - Tessella, Feb 09
7'''
8import os, sys, logging, datetime, re
9from interfacecrudclient import InterfaceCRUDClient
10from threading import Thread
11
12class backingUpThread(Thread):
13   
14   def __init__ (self, xmldbClient, doc, collection, fileName):
15       logging.info("Setting up thread to run backup for file, '%s'" %fileName)
16       Thread.__init__(self)
17       self.xdc = xmldbClient
18       self.doc = doc
19       self.collection = collection
20       self.fileName = fileName
21       logging.info("- finished setting up thread")
22     
23     
24   def run(self):
25       logging.info("Running thread to perform backup of file, '%s'" %self.fileName)
26       self.xdc.createDoc(self.doc, self.collection, self.fileName)
27       logging.info("- finished backing up file")
28
29
30class AbstractCRUDClient(InterfaceCRUDClient):
31
32    backupName = None
33   
34    # keep track of files and their collections - to reduce db lookups - use
35    # key = filename, val = collectionPath
36    collections = {}
37
38    def _addTimeStamp(self, fileName):
39        '''
40        Add timestamp to input filename
41        NB, this assumes there is a file type identifier at the end of the filename; if so, the datestamp
42        is included before this; if not it is just added at the end
43        '''
44        bits = fileName.rsplit(".", 1)
45        fileName = bits[0] + "_" + datetime.datetime.today().strftime("%Y-%m-%dT%H_%M_%S")
46       
47        if len(bits) > 1:
48            fileName += "." + bits[1]
49        return fileName
50
51
52    def _removeTimeStamp(self, fileName):
53        '''
54        Remove a timestamp from a file name
55        '''
56        match = re.search('(.*)(_\d{4}-\d{2}-\d{2}T\d{2}_\d{2}_\d{2})(.*)', fileName)
57        if match:
58            return match.group(1) + match.group(3)
59
60        return fileName
Note: See TracBrowser for help on using the repository browser.