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, write to the Free Software
22 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 ######################################################################
27 #####################################################################
28 ## multi line Samba comment
31 def __init__( self, comment ):
32 self.comment = comment
34 def Dump( self, stream, whitespace=None ):
37 for line in self.comment:
39 stream.write( whitespace )
44 #####################################################################
45 ## string smb.conf parms
46 class SambaParameter :
48 ## indexs into the parm table tuples
54 ## Stores a key into the parm_table and creates an
55 ## SambaParmXXX object to store the value
56 def __init__( self, name, value, comment=None ):
57 self.key = string.upper(string.strip(name))
59 assert parm_table.has_key( self.key ), "Bad parameter name! [%s]" % name
60 self.parm = parm_table[self.key][self.ObjectType]( value )
62 self.comment = SambaComment( comment )
64 #if not self.parm.valid:
65 # self.parm.SetValue( parm_table[self.key][self.DefaultValue] )
67 ## simple test for global or service parameter scope
68 def isGlobalParm( self ) :
69 return parm_table[self.key][Scope]
71 ## dump <the parameter to stdout
72 def Dump( self, stream ):
74 self.comment.Dump( stream, "\t" )
75 stream.write( "\t%s = %s\n" % ( parm_table[self.key][self.DisplayName], self.parm.StringValue() ))
78 #####################################################################
79 ## Class for parsing and modifying Smb.conf
85 self.services["GLOBAL"] = {}
86 self.services_order = []
89 ## always return a non-empty line of input or None
91 def ReadLine( self, stream ):
96 input_str = stream.readline()
98 ## Are we done with the file ?
100 if len(input_str) == 0:
103 ## we need one line of valid input at least
104 ## continue around the loop again if the result
107 input_str = string.strip( input_str )
108 if len(input_str) == 0:
114 ## we have > 1` character so setup the result
118 ## Check for comments -- terminated by \n -- no continuation
120 if input_str[0] == '#' or input_str[0] == ';' :
124 ## check for line continuation
126 if input_str[-1] == "\\" :
127 result += input_str[0:-1]
130 ## otherwise we have a complete line
136 ## convert the parameter name to a form suitable as a dictionary key
137 def NormalizeParamName( self, param ):
138 return string.upper( string.join(string.split(param), "") )
140 ## Open the file and parse it into a services dictionary
142 def ReadConfig( self, filename ):
143 self.filename = filename
146 fconfig = open( filename, "r" )
153 ## the most recent seen comment is stored as an array
158 str = self.ReadLine( fconfig )
162 ## Check for comments
163 if str[0] == '#' or str[0] == ';' :
164 current_comment.append( str )
167 ## look for a next section name
168 if str[0]=='[' and str[-1]==']' :
169 section_name = str[1:-1]
170 self.AddService( section_name, current_comment )
174 str_list = string.split( str, "=" )
176 if len(str_list) != 2 :
179 if not section_name :
180 print "parameter given without section name!"
183 param = self.NormalizeParamName( str_list[0] )
184 value = string.strip(str_list[1])
186 self.SetServiceOption( section_name, param, value, current_comment )
189 ## reset the comment strinf if we have one
194 ## Add a parameter to the global section
195 def SetGlobalOption( self, param, value, comment=None ) :
196 self.SetServiceOption( "GLOBAL", param, value, comment )
198 ## Add a parameter to a specific service
199 def SetServiceOption( self, servicename, param, value, comment=None ) :
200 service = string.upper(servicename)
201 parm = self.NormalizeParamName(param)
202 self.services[service]['_order_'].append( parm )
203 self.services[service][parm] = SambaParameter( parm, value, comment )
206 ## remove a service from the config file
207 def DelService( self, servicename ) :
208 service = string.upper(servicename)
209 self.services[service] = None
212 ## remove a service from the config file
213 def AddService( self, servicename, comment=None ) :
214 service = string.upper(servicename)
216 self.services[service] = {}
217 self.services[service]['_order_'] = []
220 self.services[service]['_comment_'] = SambaComment( comment )
222 self.services_order.append( service )
226 def isService( self, servicename ):
227 service = string.upper(servicename)
228 return self.services.has_key( service )
230 ## dump a single service to stream
231 def DumpService( self, stream, servicename ):
234 if self.services[servicename].has_key( '_comment_' ):
235 self.services[servicename]['_comment_'].Dump( stream )
238 stream.write( "[%s]\n" % (servicename) )
241 for parm in self.services[servicename]['_order_']:
242 self.services[servicename][parm].Dump(stream)
244 ## dump the config to stream
245 def Dump( self, stream ):
246 self.DumpService( stream, "GLOBAL" )
249 for section in self.services_order:
250 ## already handled the global section
251 if section == "GLOBAL":
254 ## check for deleted sections ##
255 if not self.services[section]:
258 self.DumpService( stream, section )
261 ## write out any changes to disk
267 fconfig = open( self.filename, "w" )
269 sys.stderr.write( "ERROR!\n" )
276 def Services( self ):
278 for section in self.services.keys():
279 service_list.append( section )
283 def NumServices( self ):
284 return len(self.Services())
286 def Write( self, filename ):
287 self.filename = filename
297 ######################################################################
299 ######################################################################
301 if __name__ == "__main__" :
304 x.ReadConfig( sys.argv[1] )
313 ## end of SambaConfig.py ######################################################
314 ###############################################################################