def validate(self):
     try:
         import xml.parsers.xmlproc.xmlval
     except:
         raise exceptions.ImportError(
             'PyXml is required for validating SVG')
     svg = self.toXml()
     xv = xml.parsers.xmlproc.xmlval.XMLValidator()
     try:
         xv.feed(svg)
     except:
         raise Exception("SVG is not well formed, see messages above")
     else:
         print("SVG well formed")
Example #2
0
def init_db_conn(connect_string, username, passwd, show_connection_info, show_version_info=True):
	"""initializes db connections, can work with PyGres or psycopg2"""
	global _CONN
	try:
		dbinfo = connect_string
		if show_connection_info:
			print(dbinfo)
		if USE_JYTHON:
			_CONN = zxJDBC.connect(connect_string, username, passwd, 'org.postgresql.Driver')
		elif '/' in connect_string:
			import odbc
			_CONN = odbc.odbc(connect_string)
			if show_connection_info:
				print(_CONN)
		elif connect_string.startswith('Driver='):
			import pyodbc
			# Driver={PostgreSQL};Server=IP address;Port=5432;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
			# Driver={PostgreSQL};Server=isof-test64;Port=5435;Database=isof_stable;Uid=postgres;Pwd=postgres;
			_CONN = pyodbc.connect(connect_string)
			if show_connection_info:
				print(_CONN)
		else:
			# 'host:[port]:database:user:password'
			arr = connect_string.split(':')
			if len(arr) > 4:
				host = '%s:%s' % (arr[0], arr[1])
				port = int(arr[1])
				dbname = arr[2]
				user = arr[3]
				passwd = arr[4]
			elif len(arr) == 4:
				host = arr[0]
				port = -1
				dbname = arr[1]
				user = arr[2]
				passwd = arr[3]
			else:
				raise exceptions.ImportError('Incorrect connect_string!\n\n%s' % (USAGE))
			if port > 0:
				host = host.split(':')[0]
				sport = 'port=%d' % (port)
			else:
				sport = ''
			dsn = "host=%s %s dbname=%s user=%s password=%s" % (host, sport, dbname, user, passwd)
			if show_connection_info:
				print(dsn)
			dbinfo = 'db: %s:%s' % (host, dbname)
			use_pgdb = 0
			try:
				import psycopg2
			except:
				try:
					import pgdb
					use_pgdb = 1
				except:
					raise exceptions.ImportError('No PostgreSQL library, install psycopg2 or PyGres!')
			if not _CONN:
				if show_connection_info:
					print(dbinfo)
				if use_pgdb:
					_CONN = pgdb.connect(database=dbname, host=host, user=user, password=passwd)
					if show_connection_info:
						print(_CONN)
				else:
					_CONN = psycopg2.connect(dsn)
					if show_connection_info:
						print(_CONN)
		if show_version_info:
			add_ver_info(connect_string, username)
	except:
		ex = sys.exc_info()
		s = 'Exception: %s: %s\n%s' % (ex[0], ex[1], dbinfo)
		print(s)
		return None
	return _CONN
# via a dom implementation and directly using <element>text</element> strings
# the latter is way faster (and shorter in coding)
# the former is only used in debugging svg programs
# maybe it will be removed alltogether after a while
# with the following variable you indicate whether to use the dom implementation
# Note that PyXML is required for using the dom implementation.
# It is also possible to use the standard minidom. But I didn't try that one.
# Anyway the text based approach is about 60 times faster than using the full dom implementation.
use_dom_implementation = 0

if use_dom_implementation != 0:
    try:
        from xml.dom import implementation
        from xml.dom.ext import PrettyPrint
    except:
        raise exceptions.ImportError(
            "PyXML is required for using the dom implementation")
# The implementation is used for the creating the XML document.
# The prettyprint module is used for converting the xml document object to a xml file

assert sys.version_info[0] >= 2
if sys.version_info[1] < 2:
    True = 1
    False = 0
    file = open

sys.setrecursionlimit = 50
# The recursion limit is set conservative so mistakes like s=svg() s.addElement(s)
# won't eat up too much processor time.

# the following code is pasted form xml.sax.saxutils
# it makes it possible to run the code without the xml sax package installed