Browse Source

----------------------------------------------------------------------

Modified Files:
 	XMLSchema.py
        -- added a few convience methods, and 'disabled' a few methods
        to force a sane usage.

        __init__.py
        -- removed use of python logging module, and replaced it with
        the below.

 Added Files:
 	logging.py
        -- simple interface to log message to, can write your own
        logger class.  By default do no logging.

 ----------------------------------------------------------------------
main
Joshua Boverhof 20 years ago
parent
commit
91099e18c1
3 changed files with 118 additions and 30 deletions
  1. +49
    -4
      XMLSchema.py
  2. +2
    -26
      __init__.py
  3. +67
    -0
      logging.py

+ 49
- 4
XMLSchema.py View File

@@ -622,7 +622,6 @@ class WSDLToolsAdapter(XMLSchemaComponent):
tag = 'definitions'

def __init__(self, wsdl):
#XMLSchemaComponent.__init__(self, None)
XMLSchemaComponent.__init__(self, parent=wsdl)
self.setAttributes(DOMAdapter(wsdl.document))

@@ -1652,13 +1651,34 @@ class ElementDeclaration(XMLSchemaComponent,\
XMLSchemaComponent.__init__(self, parent)
self.annotation = None
self.content = None
self.constraints = None
self.constraints = ()

def getElementDeclaration(self, attribute):
raise Warning, 'invalid operation for <%s>' %self.tag

def getTypeDefinition(self, attribute=None):
'''If attribute is None, "type" is assumed, return the corresponding
representation of the global type definition (TypeDefinition),
or the local definition if don't find "type". To maintain backwards
compat, if attribute is provided call base class method.
'''
if attribute:
return XMLSchemaComponent.getTypeDefinition(self, attribute)
gt = XMLSchemaComponent.getTypeDefinition(self, 'type')
if gt:
return gt
return self.content

def getConstraints(self):
return self._constraints
def setConstraints(self, constraints):
self._constraints = tuple(constraints)
constraints = property(getConstraints, setConstraints, None, "tuple of key, keyref, unique constraints")

def fromDom(self, node):
self.setAttributes(node)
contents = self.getContents(node)
constraints = []

for i in contents:
component = SplitQName(i.getTagName())[1]
if component in self.__class__.contents['xsd']:
@@ -1684,7 +1704,8 @@ class ElementDeclaration(XMLSchemaComponent,\
raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
else:
raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
self.constraints = tuple(constraints)

self.constraints = constraints


class LocalElementDeclaration(ElementDeclaration,\
@@ -1747,6 +1768,15 @@ class ElementReference(XMLSchemaComponent,\
def __init__(self, parent):
XMLSchemaComponent.__init__(self, parent)
self.annotation = None

def getElementDeclaration(self, attribute=None):
'''If attribute is None, "ref" is assumed, return the corresponding
representation of the global element declaration (ElementDeclaration),
To maintain backwards compat, if attribute is provided call base class method.
'''
if attribute:
return XMLSchemaComponent.getElementDeclaration(self, attribute)
return XMLSchemaComponent.getElementDeclaration(self, 'ref')
def fromDom(self, node):
self.annotation = None
@@ -1789,6 +1819,9 @@ class ElementWildCard(LocalElementDeclaration,\
XMLSchemaComponent.__init__(self, parent)
self.annotation = None

def getTypeDefinition(self, attribute):
raise Warning, 'invalid operation for <%s>' %self.tag

def fromDom(self, node):
self.annotation = None
self.setAttributes(node)
@@ -2101,6 +2134,12 @@ class ComplexType(XMLSchemaComponent,\
def getAttributeContent(self):
return self.attr_content

def getElementDeclaration(self, attribute):
raise Warning, 'invalid operation for <%s>' %self.tag

def getTypeDefinition(self, attribute):
raise Warning, 'invalid operation for <%s>' %self.tag

def fromDom(self, node):
self.setAttributes(node)
contents = self.getContents(node)
@@ -2502,6 +2541,12 @@ class SimpleType(XMLSchemaComponent,\
self.annotation = None
self.content = None

def getElementDeclaration(self, attribute):
raise Warning, 'invalid operation for <%s>' %self.tag

def getTypeDefinition(self, attribute):
raise Warning, 'invalid operation for <%s>' %self.tag

def fromDom(self, node):
self.setAttributes(node)
contents = self.getContents(node)


+ 2
- 26
__init__.py View File

@@ -5,32 +5,8 @@ ident = "$Id$"

import WSDLTools
import XMLname
from logging import getLogger as _getLogger
import logging.config as _config

LOGGING = 'logging.txt'
DEBUG = True

#
# If LOGGING configuration file is not found, turn off logging
# and use _noLogger class because logging module's performance
# is terrible.
#

try:
_config.fileConfig(LOGGING)
except:
DEBUG = False

import logging

class Base:
def __init__(self, module=__name__):
self.logger = _noLogger()
if DEBUG is True:
self.logger = _getLogger('%s-%s(%x)' %(module, self.__class__, id(self)))

class _noLogger:
def __init__(self, *args): pass
def warning(self, *args): pass
def debug(self, *args): pass
def error(self, *args): pass
self.logger = logging.getLogger('%s-%s(%x)' %(module, self.__class__, id(self)))

+ 67
- 0
logging.py View File

@@ -0,0 +1,67 @@
#! /usr/bin/env python
"""Logging"""
import sys


class ILogger:
'''Logger interface, by default this class
will be used and logging calls are no-ops.
'''
level = 0
def __init__(self, msg):
return
def warning(self, *args):
return
def debug(self, *args):
return
def error(self, *args):
return
_LoggerClass = ILogger


class BasicLogger(ILogger):
def __init__(self, msg, out=sys.stdout):
self.msg, self.out = msg, out

def warning(self, msg, *args):
print >>self, self.WARN, self.msg,
print >>self, msg %args
WARN = 'WARN'
def debug(self, msg, *args):
print >>self, self.DEBUG, self.msg,
print >>self, msg %args
DEBUG = 'DEBUG'
def error(self, msg, *args):
print >>self, self.ERROR, self.msg,
print >>self, msg %args
ERROR = 'ERROR'

def write(self, *args):
'''Write convenience function; writes strings.
'''
for s in args: self.out.write(s)


def setBasicLogger():
'''Use Basic Logger.
'''
setLoggerClass(BasicLogger)

def setLoggerClass(loggingClass):
'''Set Logging Class.
'''
assert issubclass(loggingClass, ILogger), 'loggingClass must subclass ILogger'
global _LoggerClass
_LoggerClass = loggingClass

def setLevel(level=0):
'''Set Global Logging Level.
'''
ILogger.level = level

def getLogger(msg):
'''Return instance of Logging class.
'''
return _LoggerClass(msg)



||||||
x
 
000:0
Loading…
Cancel
Save