source: TI02-CSML/trunk/csml/csmllibs/csmlfiles.py @ 1552

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/csmllibs/csmlfiles.py@1552
Revision 1552, 4.9 KB checked in by domlowe, 13 years ago (diff)

added prefix csml. to csmllibs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#version 0.1
2#Dominic Lowe, BADC 8th December 2005
3#import csmllibs
4import sys
5import csml.csmllibs.csmldirectory
6
7
8class FileMapMaker(object):
9    ''' class which handles the lower level featurefilemap and directorytree classes
10    to build a featurefilemap object of featuretypes/files.
11    current relationships are onetomany, onetoone and mixedft,
12    but others could be created '''
13    def __init__(self,topdirectory, ftorftdictionary):
14            self.topdir=topdirectory
15            if type(ftorftdictionary) is str:
16                    self.ft = ftorftdictionary
17            elif type(ftorftdictionary) is dict:
18                    self.ftdictionary=ftorftdictionary
19            else:
20                    #wrong type
21                    print 'FileMapMaker takes a  featuretype string or dictionary'
22                    sys.exit()
23   
24    def onetomany(self):
25        ''' onetomany means one file per directory/subdirectory is the representative file '''
26        tree = csml.csmllibs.csmldirectory.DirectoryTree()
27        tree.setTopDirectory(self.topdir)
28        tree.readDirectory()
29        subdirs = tree.getSubDirectories()
30        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
31       
32        for dir in subdirs:
33                file=tree.getFirstInSubDir(dir)
34                if file == None:
35                        continue
36                #create representative file
37                repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(file)
38                repfile.setFeatureType(self.ft)
39                try:
40                        print dir
41                        otherfiles=tree.getAllCSMLFilesExceptFirst(dir)
42                        errtest=otherfiles[0]
43                except:
44                        print 'no other csml files in this directory'
45                        #add representative file with no related files.
46                        ffmap.addRepresentativeFile(repfile)
47                        continue
48               
49                #add otherfiles as related files with feature type, ft
50                #note they could have individual featuretypes.
51
52                for f in otherfiles:
53                        relfile = csml.csmllibs.csmlfeaturefilemap.relatedFile(f)
54                        relfile.setFeatureType(self.ft)
55                        repfile.addRelatedFile(relfile)
56                ffmap.addRepresentativeFile(repfile)
57               
58        return ffmap
59               
60    def onetoone(self):
61        """ onetoone means each feature is self contained within any individual file
62        """
63        tree = csml.csmllibs.csmldirectory.DirectoryTree()
64        tree.setTopDirectory(self.topdir)
65        tree.readDirectory()
66        subdirs = tree.getSubDirectories()
67        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
68        for dir in subdirs:     
69                files=tree.getCSMLSupportedFilesInSubDir(dir)
70                for f in files:
71                        repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(f)
72                        repfile.setFeatureType(self.ft)
73                        ffmap.addRepresentativeFile(repfile)
74        return ffmap   
75       
76    def oneonly(self):
77        """ oneonly means one file represents feature spanning multiple directories
78        assumes no file in toplevel directory, and then lots of subdirectories at next level containing files
79        """
80        tree = csml.csmllibs.csmldirectory.DirectoryTree()
81        tree.setTopDirectory(self.topdir)
82        tree.readDirectory()
83        subdirs = tree.getSubDirectories()
84        print subdirs
85        allfiles= tree.getAllCSMLSupportedFiles()
86        print allfiles
87        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
88       
89        #create representative file from first file
90        repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(allfiles[0])
91        repfile.setFeatureType(self.ft)
92        #create related files from all other files
93        for f in allfiles[1:]:
94            relfile = csml.csmllibs.csmlfeaturefilemap.relatedFile(f)
95            relfile.setFeatureType(self.ft)
96            repfile.addRelatedFile(relfile)
97        ffmap.addRepresentativeFile(repfile)
98        return ffmap
99
100
101           
102                       
103    def mixedft(self):
104        ''' allows the use of a dictonary object to store feature file map info not well tested '''
105        tree = csml.csmllibs.csmldirectory.DirectoryTree()
106        tree.setTopDirectory(self.topdir)
107        tree.readDirectory()
108        subdirs = tree.getSubDirectories()
109        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
110        for dir in subdirs:     
111                files=tree.getCSMLSupportedFilesInSubDir(dir)
112                for f in files:
113                        repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(f)
114                        #look up feature type in dictionary
115                        ft=self.ftdictionary[f]
116                        repfile.setFeatureType(ft)
117                        ffmap.addRepresentativeFile(repfile)   
118        return ffmap
Note: See TracBrowser for help on using the repository browser.