1 | #!/usr/bin/env python |
---|
2 | import os |
---|
3 | import nappy |
---|
4 | import sys |
---|
5 | |
---|
6 | class DirectoryTree(object): |
---|
7 | |
---|
8 | def __init__(self): |
---|
9 | self.subdirlist=[] |
---|
10 | |
---|
11 | def setTopDirectory(self,top): |
---|
12 | self.topdir=top |
---|
13 | |
---|
14 | def readSubDir(self, subdir): |
---|
15 | for item in os.listdir(subdir): |
---|
16 | #print os.path.join(subdir, item) |
---|
17 | if os.path.isfile(os.path.join(subdir,item)): |
---|
18 | #print 'file: '+ item |
---|
19 | pass |
---|
20 | else: |
---|
21 | if item[0]=='.': |
---|
22 | pass # directory is hidden, ignore |
---|
23 | else: |
---|
24 | self.subdirlist.append(os.path.join(subdir, item)) |
---|
25 | self.readSubDir(os.path.join(subdir, item)) |
---|
26 | #subdircount=len(self.subdirlist) |
---|
27 | #print subdircount |
---|
28 | |
---|
29 | |
---|
30 | def readDirectory(self): |
---|
31 | self.subdirlist.append(self.topdir) |
---|
32 | for item in os.listdir(self.topdir): |
---|
33 | if os.path.isfile(os.path.join(self.topdir,item)): |
---|
34 | pass |
---|
35 | else: |
---|
36 | if item[0]=='.': |
---|
37 | pass # directory is hidden, ignore |
---|
38 | else: |
---|
39 | self.subdirlist.append(os.path.join(self.topdir, item)) |
---|
40 | self.readSubDir(os.path.join(self.topdir, item)) |
---|
41 | |
---|
42 | print "total" |
---|
43 | counter = len(self.subdirlist) |
---|
44 | print counter |
---|
45 | |
---|
46 | |
---|
47 | def getSubDirectories(self): |
---|
48 | sublist = self.subdirlist |
---|
49 | #note the first (0) entry in the sub directory list is |
---|
50 | #actually the top level directory |
---|
51 | return sublist |
---|
52 | |
---|
53 | def getFilesInSubDir(self,sub): |
---|
54 | filelist=[] |
---|
55 | for item in os.listdir(sub): |
---|
56 | if os.path.isfile(os.path.join(sub,item)): |
---|
57 | filelist.append(os.path.join(sub,item)) |
---|
58 | return filelist |
---|
59 | |
---|
60 | def getFirstInSubDir(self,sub): |
---|
61 | for item in os.listdir(sub): |
---|
62 | filename=os.path.join(sub,item) |
---|
63 | if os.path.isfile(filename): |
---|
64 | if isCSMLSupported(filename): |
---|
65 | return filename |
---|
66 | |
---|
67 | |
---|
68 | def getCSMLSupportedFilesInSubDir(self,sub): |
---|
69 | filelist=[] |
---|
70 | for item in os.listdir(sub): |
---|
71 | filename=os.path.join(sub,item) |
---|
72 | if os.path.isfile(filename): |
---|
73 | if isCSMLSupported(filename): |
---|
74 | filelist.append(filename) |
---|
75 | return filelist |
---|
76 | |
---|
77 | def getAllCSMLFilesExceptFirst(self,sub): |
---|
78 | filelist=self.getCSMLSupportedFilesInSubDir(sub) |
---|
79 | filelist=filelist[1:] |
---|
80 | return filelist |
---|
81 | |
---|
82 | def getAllCSMLSupportedFiles(self): |
---|
83 | filelist=[] |
---|
84 | for subdir in self.subdirlist: |
---|
85 | for item in os.listdir(subdir): |
---|
86 | itempath=os.path.join(subdir,item) |
---|
87 | if os.path.isfile(itempath): |
---|
88 | if isCSMLSupported(itempath): |
---|
89 | filelist.append(itempath) |
---|
90 | else: |
---|
91 | pass |
---|
92 | return filelist |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | #function to find if file is of a supported CSML type. |
---|
98 | def isCSMLSupported(filename): |
---|
99 | fileExtension = str(filename)[-3:] |
---|
100 | supported = False |
---|
101 | try: |
---|
102 | if fileExtension == '.nc': |
---|
103 | supported = True |
---|
104 | elif fileExtension == '.pp': |
---|
105 | supported = True |
---|
106 | elif fileExtension == 'ctl': |
---|
107 | supported = True |
---|
108 | elif nappy.readFFI(filename) in [1001,1010,1020,2010,2110,2160,2310,3010,4010]: |
---|
109 | supported = True |
---|
110 | except: |
---|
111 | #the nappy.readFFI function can fail if permissions are wrong (for example) |
---|
112 | supported=False |
---|
113 | return supported |
---|
114 | |
---|
115 | |
---|