1 import sys
, string
, SambaParm
2 from smbparm
import parm_table
4 ######################################################################
6 ## smb.conf parser class
8 ## Copyright (C) Gerald Carter 2004.
10 ## This program is free software; you can redistribute it and/or modify
11 ## it under the terms of the GNU General Public License as published by
12 ## the Free Software Foundation; either version 3 of the License, or
13 ## (at your option) any later version.
15 ## This program is distributed in the hope that it will be useful,
16 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ## GNU General Public License for more details.
20 ## You should have received a copy of the GNU General Public License
21 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
23 ######################################################################
26 #####################################################################
27 ## multi line Samba comment
30 def __init__( self
, comment
):
31 self
.comment
= comment
33 def Dump( self
, stream
, whitespace
=None ):
36 for line
in self
.comment
:
38 stream
.write( whitespace
)
43 #####################################################################
44 ## string smb.conf parms
45 class SambaParameter
:
47 ## indexs into the parm table tuples
53 ## Stores a key into the parm_table and creates an
54 ## SambaParmXXX object to store the value
55 def __init__( self
, name
, value
, comment
=None ):
56 self
.key
= string
.upper(string
.strip(name
))
58 assert parm_table
.has_key( self
.key
), "Bad parameter name! [%s]" % name
59 self
.parm
= parm_table
[self
.key
][self
.ObjectType
]( value
)
61 self
.comment
= SambaComment( comment
)
63 #if not self.parm.valid:
64 # self.parm.SetValue( parm_table[self.key][self.DefaultValue] )
66 ## simple test for global or service parameter scope
67 def isGlobalParm( self
) :
68 return parm_table
[self
.key
][Scope
]
70 ## dump <the parameter to stdout
71 def Dump( self
, stream
):
73 self
.comment
.Dump( stream
, "\t" )
74 stream
.write( "\t%s = %s\n" % ( parm_table
[self
.key
][self
.DisplayName
], self
.parm
.StringValue() ))
77 #####################################################################
78 ## Class for parsing and modifying Smb.conf
84 self
.services
["GLOBAL"] = {}
85 self
.services_order
= []
88 ## always return a non-empty line of input or None
90 def ReadLine( self
, stream
):
95 input_str
= stream
.readline()
97 ## Are we done with the file ?
99 if len(input_str
) == 0:
102 ## we need one line of valid input at least
103 ## continue around the loop again if the result
106 input_str
= string
.strip( input_str
)
107 if len(input_str
) == 0:
113 ## we have > 1` character so setup the result
117 ## Check for comments -- terminated by \n -- no continuation
119 if input_str
[0] == '#' or input_str
[0] == ';' :
123 ## check for line continuation
125 if input_str
[-1] == "\\" :
126 result
+= input_str
[0:-1]
129 ## otherwise we have a complete line
135 ## convert the parameter name to a form suitable as a dictionary key
136 def NormalizeParamName( self
, param
):
137 return string
.upper( string
.join(string
.split(param
), "") )
139 ## Open the file and parse it into a services dictionary
141 def ReadConfig( self
, filename
):
142 self
.filename
= filename
145 fconfig
= open( filename
, "r" )
152 ## the most recent seen comment is stored as an array
157 str = self
.ReadLine( fconfig
)
161 ## Check for comments
162 if str[0] == '#' or str[0] == ';' :
163 current_comment
.append( str )
166 ## look for a next section name
167 if str[0]=='[' and str[-1]==']' :
168 section_name
= str[1:-1]
169 self
.AddService( section_name
, current_comment
)
173 str_list
= string
.split( str, "=" )
175 if len(str_list
) != 2 :
178 if not section_name
:
179 print "parameter given without section name!"
182 param
= self
.NormalizeParamName( str_list
[0] )
183 value
= string
.strip(str_list
[1])
185 self
.SetServiceOption( section_name
, param
, value
, current_comment
)
188 ## reset the comment strinf if we have one
193 ## Add a parameter to the global section
194 def SetGlobalOption( self
, param
, value
, comment
=None ) :
195 self
.SetServiceOption( "GLOBAL", param
, value
, comment
)
197 ## Add a parameter to a specific service
198 def SetServiceOption( self
, servicename
, param
, value
, comment
=None ) :
199 service
= string
.upper(servicename
)
200 parm
= self
.NormalizeParamName(param
)
201 self
.services
[service
]['_order_'].append( parm
)
202 self
.services
[service
][parm
] = SambaParameter( parm
, value
, comment
)
205 ## remove a service from the config file
206 def DelService( self
, servicename
) :
207 service
= string
.upper(servicename
)
208 self
.services
[service
] = None
211 ## remove a service from the config file
212 def AddService( self
, servicename
, comment
=None ) :
213 service
= string
.upper(servicename
)
215 self
.services
[service
] = {}
216 self
.services
[service
]['_order_'] = []
219 self
.services
[service
]['_comment_'] = SambaComment( comment
)
221 self
.services_order
.append( service
)
225 def isService( self
, servicename
):
226 service
= string
.upper(servicename
)
227 return self
.services
.has_key( service
)
229 ## dump a single service to stream
230 def DumpService( self
, stream
, servicename
):
233 if self
.services
[servicename
].has_key( '_comment_' ):
234 self
.services
[servicename
]['_comment_'].Dump( stream
)
237 stream
.write( "[%s]\n" % (servicename
) )
240 for parm
in self
.services
[servicename
]['_order_']:
241 self
.services
[servicename
][parm
].Dump(stream
)
243 ## dump the config to stream
244 def Dump( self
, stream
):
245 self
.DumpService( stream
, "GLOBAL" )
248 for section
in self
.services_order
:
249 ## already handled the global section
250 if section
== "GLOBAL":
253 ## check for deleted sections ##
254 if not self
.services
[section
]:
257 self
.DumpService( stream
, section
)
260 ## write out any changes to disk
266 fconfig
= open( self
.filename
, "w" )
268 sys
.stderr
.write( "ERROR!\n" )
275 def Services( self
):
277 for section
in self
.services
.keys():
278 service_list
.append( section
)
282 def NumServices( self
):
283 return len(self
.Services())
285 def Write( self
, filename
):
286 self
.filename
= filename
296 ######################################################################
298 ######################################################################
300 if __name__
== "__main__" :
303 x
.ReadConfig( sys
.argv
[1] )
312 ## end of SambaConfig.py ######################################################
313 ###############################################################################