File: config.py

package info (click to toggle)
synce-sync-engine 0.11.1-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 996 kB
  • ctags: 923
  • sloc: python: 8,586; xml: 949; makefile: 51; sh: 7
file content (369 lines) | stat: -rw-r--r-- 11,755 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# -*- coding: utf-8 -*-
############################################################################
#    New file: Dr J A Gow 18/2/2007                                        #
#              Bundle all the config stuff in one place.                   #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

import cPickle as pickle
import os
import os.path
import getopt
import string
import logging
import libxml2
import xml2util
import shutil
from formatapi import SupportedFormats,DefaultFormat

#
# Debug log levels

DBLOGLEVELS = { "INFO"    : logging.INFO,
		"DEBUG"   : logging.DEBUG,
		"WARNING" : logging.WARNING,
		"ERROR"   : logging.ERROR
	      }

#
#
# ValidateCommandOptions
#
# Validate command line options.

def ValidateCommandOptions(olist):
	
	try:
		opts,args = getopt.getopt(olist,
					  "hc:ov:l:d",
					  ["help","config=","once","verbosity=","logfile=","detached"])
		if len(args) > 1:
			raise Exception("cmderr")
		for opt,arg in opts:
			if opt in ("-h","--help"):
				raise Exception("usage")
	except:
		print "Usage: sync-engine [opts]"
		print
		print "Options"
		print "-------"
		print " -h, --help          Display usage info"
		print
		print " -c<confdir>,"
		print " --config=<confdir>  Use <confdir> as the sync-engine config location"
		print
		print " -o, --once          Run for one connection only"
		print 
		print " -v<arg>,"
		print " --verbosity=<arg>   Set the log level to one of:"
		print "                     INFO,DEBUG,ERROR"
		print " -l<file>,"
		print " --logfile=<file>    Set the log file to <file>. If not specified"
		print "                     will log to stdout"
		print
		print " -d, --detached      Daemonize the sync-engine"
		raise
	
	return opts,args


############################################################################
# ConfigObject
#
# Base configuration object. Each configuration block is handled by one
# instance of a derived class of this object
#
############################################################################

class ConfigObject:
	def __init__(self):
		self.handlers = {}
		self.cfg = {}

############################################################################
# FileSyncConfig
#
# Configuration object for the file synchronization subsystem
#
############################################################################

class FileSyncConfig(ConfigObject):
	
	def __init__(self):
		ConfigObject.__init__(self)
		self.logger = logging.getLogger("FileSyncConfig")
		self.LocalFilePath=""
		self.Disabled = 1
		self.handlers = { "BaseFilePath" : self.validate_BaseFilePath,
				  "Disable" : self.validate_Disable,
				  "LocalUpdateFreq" : self.validate_UpdateFreq, 
				  "ExtraDeleteDelay" : self.validate_DeleteDelay,
				  "ObjectReportTimeout" : self.validate_ObjectReportTimeout
				}
				
		self.cfg = { "BaseFilePath" : "~/PDAFiles",
		             "Disable" : 1,
			     "LocalUpdateFreq" : 10,
			     "ExtraDeleteDelay" : 0,
			     "ObjectReportTimeout" : 8
			   }
	
	def validate_BaseFilePath(self,arg):
		self.cfg["BaseFilePath"] = os.path.expanduser(arg)
		
	def validate_Disable(self,arg):
		try:
			self.cfg["Disable"] = int(arg)
		except:
			self.logger.debug("'Disable': invalid argument %s in config file" % arg)
			
	def validate_UpdateFreq(self,arg):
		try:
			self.cfg["LocalUpdateFreq"] = int(arg)
		except:
			self.logger.debug("'LocalUpdateFreq': invalid argument %s in config file" % arg)

	def validate_DeleteDelay(self,arg):
		try:
			self.cfg["ExtraDeleteDelay"] = int(arg)
		except:
			self.logger.debug("'ExtraDeleteDelay': invalid argument %s in config file" % arg)
	
	def validate_ObjectReportTimeout(self,arg):
		try:
			self.cfg["ObjectReportTimeout"] = int(arg)
		except:
			self.logger.debug("'ObjectReportTimeout': invalid argument %s in config file" % arg)
	
	def dump(self):
		print "File-sync config: "
		print "   local path           %s " % self.cfg["BaseFilePath"]
		print "   disable?             %d " % self.cfg["Disable"]
		print "   LocalUpdateFreq      %d " % self.cfg["LocalUpdateFreq"]
		print "   ExtraDeleteDelay     %d " % self.cfg["ExtraDeleteDelay"]

############################################################################
# AutoSyncConfig
#
# Configuration object for autosync
#
############################################################################

class AutoSyncConfig(ConfigObject):
	def __init__(self):
		ConfigObject.__init__(self)
		self.logger = logging.getLogger("AutoSyncConfig")
		
		self.handlers = { "AutoSyncCommand" : self.validate_AutoSyncCommand,
				  "Disable"         : self.validate_Disable
				}
				
		self.cfg = { "AutoSyncCommand" : [],
		             "Disable" : 1
			   }
	
	def validate_AutoSyncCommand(self,arg):
		self.cfg["AutoSyncCommand"] = string.split(arg)
		
	def validate_Disable(self,arg):
		try:
			self.cfg["Disable"] = int(arg)
		except:
			self.logger.debug("'Disable': invalid argument %s in config file" % arg)
			
	def dump(self):
		self.logger.info("Autosync config: ")
		if len(self.cfg["AutoSyncCommand"]) > 0: 
			self.logger.info("   Autosync command    L%s " % self.cfg["AutoSyncCommand"])
		self.logger.info("   disable?             %d " % self.cfg["Disable"])

############################################################################
# GlobalConfig
#
# Global configuration object for autosync
#
############################################################################

class GlobalConfig(ConfigObject):
	def __init__(self):
		ConfigObject.__init__(self)
		self.logger = logging.getLogger("GlobalConfig")
		
		self.handlers = { "SlowSyncDisable" : self.validate_SlowSyncEnable,
		                  "AuthMethod"      : self.validate_AuthMethod,
				  "AppendDefaultTimezone" : self.validate_AppendDefaultTimezone,
				  "OpensyncXMLFormat" : self.validate_OpensyncXMLFormat,
				  "FlushIDB" : self.validate_FlushIDB,
				  "EnableDTPT" : self.validate_EnableDTPT }
				  
		self.cfg = { "SlowSyncDisable" : 0,
		             "AuthMethod"      : "INTERNAL_CLI",
			     "AppendDefaultTimezone" : 0,
			     "OpensyncXMLFormat" : DefaultFormat,
			     "FlushIDB" : 1,
			     "EnableDTPT" : 1
			   }
			
	def validate_SlowSyncEnable(self,arg):
		try:
			self.cfg["SlowSyncDisable"] = int(arg)
		except:
			self.logger.debug("'SlowSyncDisable': invalid argument %s in config file" % arg)
			
	def validate_AuthMethod(self,arg):
		self.cfg["AuthMethod"] = arg
			
	def validate_AppendDefaultTimezone(self,arg):
		try:
			self.cfg["AppendDefaultTimezone"] = int(arg)
		except:
			self.logger.debug("'Disable': invalid argument %s in config file" % arg)
		
	def validate_OpensyncXMLFormat(self,arg):
		if arg in SupportedFormats:
			self.cfg["OpensyncXMLFormat"] = arg
		else:
			self.logger.debug("'OpensyncXMLFormat': no such format type '%s', substituting default" % arg)

	def validate_FlushIDB(self,arg):
		try:
			self.cfg["FlushIDB"] = int(arg)
		except:
			self.logger.debug("'Disable': invalid argument %s in config file" % arg)

	def validate_EnableDTPT(self,arg):
		try:
			self.cfg["EnableDTPT"] = int(arg)
		except:
			self.logger.debug("'Disable': invalid argument %s in config file" % arg)

	def dump(self):
		self.logger.info("Global config: ")
		self.logger.info("   SlowSyncDisable: %d " % self.cfg["SlowSyncEnable"])

############################################################################
# Config
#
# Configuration subsystem object. This object houses the configuration
# system, and it is from here that all configuration objects are referenced
# by other components of the sync-engine
#
############################################################################

class Config:
	
	def __init__(self,progopts):
		
		self.logger = logging.getLogger("engine.config.Config")
		self.config_FileSync = FileSyncConfig()
		self.config_AutoSync = AutoSyncConfig()
		self.config_Global   = GlobalConfig()

		# Now, get our current path (this may be useful)
		# Avoids messing about with having to pull a PREFIX from
		# an installer
		
		self.sepath = str(xml2util).split()[3][1:-2]
		self.sepath = os.path.dirname(self.sepath)	# lose module name
		self.sepath = os.path.dirname(self.sepath)	# lose SyncEngine
		
		# check if we have a config

		self.config_dir   = os.path.join(os.path.expanduser("~"), ".synce")
		self.config_path  = os.path.join(self.config_dir, "config.xml")
		
		if not os.path.exists(self.config_path):
			if not os.path.isdir(self.config_dir):
				os.mkdir(self.config_dir)
			oldconf = os.path.join(self.sepath,"config/config.xml")
			shutil.copy(oldconf, self.config_path)

		
		self.logfile = None
	
		self.runonce = False
		self.fork = False
		self.loglevel = DBLOGLEVELS["DEBUG"]
		self._ProcessCommandOptions(progopts)
		
	#
	# _ProcessCommandOptions
	#
	# Process the command line options
	
	def _ProcessCommandOptions(self,olist):
		opts,args = olist
		for opt,arg in opts:
			if opt in ("-o","--once"):
				self.runonce = True
			if opt in ("-c","--config"):
				self.config_dir = os.path.join(arg,".synce")
			if opt in ("-d","--detached"):
				self.fork = True
			if opt in ("-l","--logfile"):
				self.logfile = os.path.expanduser(arg)
			if opt in ("-v","--verbosity"):
				if DBLOGLEVELS.has_key(arg):
					self.loglevel = DBLOGLEVELS[arg]
				else:
					print "error: unknown log level %s, defaulting to DEBUG" % arg
					
	
	#
	# UpdateConfig updates information that may be changed between connections. It is re-read
	# at the start of each connectiomn

	def UpdateConfig(self):
	
		try:
			f = open(self.config_path, "rb")
		except:
			self.logger.info("UpdateConfig - unable to open config file - using defaults")
			return False
		try:
			cf = f.read()
			f.close()
		except:
			self.logger.info("UpdateConfig - could not read config file - using defaults")
			return False
		
		confdoc = libxml2.parseDoc(cf)
		
		config = xml2util.FindChildNode(confdoc,"syncengine-config")
		if config is not None:
			self._ReadConfElements(config,"FileSync",self.config_FileSync)
			self._ReadConfElements(config,"Autosync",self.config_AutoSync)	
			self._ReadConfElements(config,"Global",self.config_Global)
		
		return True
		
	#
	# Read configuration elements from the XML config file (private)
	#
		
	def _ReadConfElements(self,doc, element, conf_obj):
		
		conf_element = xml2util.FindChildNode(doc,element)
		if conf_element is not None:
			if conf_element.children != None:
				for item in conf_element.children:
					if item.type == "element" and conf_obj.cfg.has_key(item.name):
						f=xml2util.GetNodeValue(item)
						if f is not None:
							conf_obj.handlers[item.name](f)