def registerDiagramKinds(module): """Registers all diagramKinds found in the given module.""" for attrib in dir(module): try: potentialDiagramK = getattr(module, attrib) if IDiagramKind.isImplementedByInstancesOf(potentialDiagramK): Registry.register(potentialDiagramK) except TypeError: pass
def getPossibleDiagrams(self): """Get a Dictionary ot available Diagrams.""" types = Registry.getTypes() res = [] for typ in types: kinds = [] for kind in Registry.getKindNames(typ): kinds.append({'name': kind}) res.append({'name': typ, 'kinds': kinds}) res.append({'name': ' ', 'kinds': [{'name': _useDefaultDiagramKind}]}) res.sort(lambda x, y: cmp(x['name'], y['name'])) return res
def having_errors(self, whichTests='all'): """Test if we have errors. e.g. conflicts between diagramKind and converter """ if whichTests == 'all': if self.current_version < self.version: return "Diagram is older than SVGrafZ-Product. \ Please update the diagram as described in doc/update.txt" if self.current_version > self.version: return "Diagram is newer than SVGrafZ-Product. \ Please update your SVGrafZ-Product to the latest version." diagramTypesOfConverter = ICRegistry.getConverter( self.convertername()).registration().keys() try: for diagramType in Registry.getKind( self.graphname()).registration(): if diagramType in diagramTypesOfConverter: return False except RuntimeError: return "DiagramKind does not exist any more.\ Please choose another." return "DiagramKind and Converter are incompatible. \
def getPossibleConverters(self): """Get a Dictionary ot available Converters.""" try: diagramTypes = Registry.getKind(self.graphname()).registration() except RuntimeError: diagramTypes = [] ret = [{ 'name': ' ', 'sources': [{ 'name': ' ', 'converters': [{ 'name': _useDefaultConverter }] }] }] for dt in diagramTypes: sources = [] for source in ICRegistry.getSources(dt.name): converters = [{ 'name': conv } for conv in ICRegistry.getConverters(dt.name, source)] sources.append({'name': source, 'converters': converters}) ret.append({'name': dt.name, 'sources': sources}) return ret
def test_2_register_get_Type(self): """Test _registerType(type) and getType().""" n = NoDiagramType a = DiagramType1 b = DiagramType1 c = DiagramType2 Registry._clear() # remove filling of __init__.py self.assertEqual([], Registry.getTypes(), 'get from empty reg. got: %s' % Registry.getTypes()) self.assertRaises(RuntimeError, Registry._registerType, n) self.failUnless(Registry._registerType(a), 'register type1') self.failIf(Registry._registerType(a), 'register type1 second time') self.failIf(Registry._registerType(b), 'register type1 third time') self.assertEqual([a.name], Registry.getTypes()) self.failUnless(Registry._registerType(c), 'register type2') self.assertEqual([a.name, c.name], Registry.getTypes())
def test_6getDefaultKindName(self): "Test getDefaultKindName." Registry._clear() # remove previously registered things k3 = DiagramKind3 # default k4 = DiagramKind4 k5 = DiagramKind5 # second default k6 = DiagramKind6 self.failIf(Registry.getDefaultKindName(), 'get 0') self.failUnless(Registry.register(k4)) self.failIf(Registry.getDefaultKindName(), 'get 1') self.failUnless(Registry.register(k3)) self.assertEqual( k3.name, Registry.getDefaultKindName(), 'get 2 (%s!=%s)' % (k3.name, Registry.getDefaultKindName())) self.assertRaises(RuntimeError, Registry.register, k3) # double reg self.assertRaises(RuntimeError, Registry.register, k5) # double reg 2 self.assertRaises(RuntimeError, Registry.getKind, k5.name) # get 2nd default self.failUnless(Registry.register(k6)) self.assertEqual(k3.name, Registry.getDefaultKindName(), 'get 3')
def test_3_clear(self): """Test of _clear().""" a = DiagramType1 self.failIf(Registry._clear(), '1st clear') self.assertEqual([], Registry.getTypes(), '1st get') self.failUnless(Registry._registerType(a), 'register type1') self.assertEqual([a.name], Registry.getTypes(), '2nd get') self.failIf(Registry._clear(), '2nd clear') self.assertEqual([], Registry.getTypes(), '3rd get')
def test_5getKind(self): "Test getKind(name)." Registry._clear() # remove previously registered Things t1 = DiagramType1 t2 = DiagramType2 k3 = DiagramKind3 k4 = DiagramKind4 self.assertRaises(RuntimeError, Registry.getKind, k3.name) self.assertRaises(RuntimeError, Registry.getKind, k4.name) self.failUnless(Registry.register(k3), 'reg k3') self.assertEqual(k3, Registry.getKind(k3.name), 'get1 k3') self.assertRaises(RuntimeError, Registry.getKind, k4.name) self.failUnless(Registry.register(k4), 'reg k4') self.assertEqual(k3, Registry.getKind(k3.name), 'get2 k3') self.assertEqual(k4, Registry.getKind(k4.name), 'get k4')
def viewDiagramKindDesription(self): try: return self._list2dictlist( Registry.getKind(self.graphname()).description()) except RuntimeError: return self._list2dictlist(['WARNING: Not existing DiagramKind!'])
def __call__(self, client=None, REQUEST={}): """Render the diagram.""" graphClass = Registry.getKind(self.graphname()) current = self.getPropertyValues() outputConverter, dummy = self._getOutputConverter() inputConverter = ICRegistry.getConverter(current['convertername']) errortext = legend = colnames = stylesheet = data = title = None if current['data'].getExpression() is None: errortext = "You did not enter a DataSource, so can't display anything." else: try: data = self.getValue(current['data']) try: data = inputConverter.convert(data, current['fixcolumn']) except RuntimeError: errortext = str(exc_info()[1]) except (AttributeError, KeyError, CompilerError): errortext = 'DataSource "%s" is not existing.' % ( current['data']) except RuntimeError: errortext = str(exc_info()[1]) try: legend = self.getValue(current['legend']) except (AttributeError, KeyError, CompilerError): errortext = 'Legend "%s" is not existing.' % (current['legend']) if legend == 'converter': if IInputConverterWithLegend.isImplementedBy(inputConverter): legend = inputConverter.legend( ) # cause convert is already done else: errortext = "DataConverter '%s' can't be used with '%s'." % ( current['convertername'], 'string:converter') try: colnames = self.getValue(current['colnames']) except (AttributeError, KeyError, CompilerError): errortext = 'ColumnNames "%s" do not exist.' % ( current['colnames']) if current['stylesheet']: try: stylesheet = getattr(self, current['stylesheet']) stylesheet = outputConverter.getStyleSheetURL(stylesheet) except AttributeError: errortext = 'Stylesheet "%s" is not existing.' % ( current['stylesheet']) try: title = self.getValue(current['taltitle']) except (AttributeError, KeyError, CompilerError, RuntimeError): errortext = '"Title with TALES" has Errors.' if title is None: title = current['title'] or '' otherParams = { 'gridlines': current['gridlines'], 'height': current['height'], 'width': current['width'], 'intcaption': current['intcaption'], 'fillgaps': current['fillgaps'], } graph = graphClass(data=data, legend=legend, colnames=colnames, title=title, stylesheet=stylesheet, otherParams=otherParams, errortext=errortext) graph.setSpecialAttrib(current['specialattrib']) if REQUEST.RESPONSE: outputConverter.setHTTPHeaders( REQUEST.RESPONSE, title.replace(' ', '_') or 'unnamed') outputConverter.setSourceData(graph.compute().encode('UTF-8')) if not outputConverter.convert(): return outputConverter.getErrorResult() else: return outputConverter.getResultData()
def graphname(self, default=None): "get graphname." return self.getAttribute('graphname', Registry.getDefaultKindName(), default)
def getSpecialAttribName(self): "Get the name of the specialattrib of the currently selected diagram." try: return Registry.getKind(self.graphname()).specialAttribName except RuntimeError: return ''
def test_45register(self): "Test register(diagramKind)." Registry._clear() t1 = DiagramType1.name t2 = DiagramType2.name n = NoDiagramKind d1 = DiagramKind1 d2 = DiagramKind2 d3 = DiagramKind3 d32 = DiagramKind3 d4 = DiagramKind4 self.assertRaises(RuntimeError, Registry.register, n) self.assertRaises(RuntimeError, Registry.register, d2) self.failUnless(Registry.register(d1), 'reg d1') self.failUnless(Registry.register(d1), 'reg d1 2nd') self.assertEqual([], Registry.getAllKindNames(), 'getall 1') self.failUnless(Registry.register(d3), 'reg d3') self.assertRaises(RuntimeError, Registry.register, d3) self.assertRaises(RuntimeError, Registry.register, d32) self.assertEqual([d3.name], Registry.getKindNames(t1), 'get t1') self.assertNone(Registry.getKindNames(t2), 'get t2') self.failUnless(Registry.register(d4), 'reg d4') self.assertEqual([d3.name, d4.name], Registry.getKindNames(t1), 'get t1 2nd') self.assertEqual([d4.name], Registry.getKindNames(t2), 'get t2 2nd') self.assertEqual([d3.name, d4.name], Registry.getAllKindNames(), 'getall 2')
def test_4_register_get_KindNames(self): """Test _registerKind and getKindNames.""" Registry._clear() # remove previously registered Things nt = NoDiagramType t1 = DiagramType1 t2 = DiagramType2 nk = NoDiagramKind k1 = DiagramKind1 k11 = DiagramKind1 k2 = DiagramKind2 self.assertRaises(RuntimeError, Registry._registerKind, nt, nk) self.assertRaises(RuntimeError, Registry._registerKind, t1, nk) self.assertRaises(RuntimeError, Registry._registerKind, nt, k1) self.assertEqual(None, Registry.getKindNames(t1.name), 'get 0') self.assertEqual([], Registry.getAllKindNames(), 'getall 0: %s' % (Registry.getAllKindNames())) self.failUnless(Registry._registerKind(t1, k1), 'register t1,k1') self.failIf(Registry._registerKind(t1, k1), 'register t1,k1 second time') self.failIf(Registry._registerKind(t1, k11), 'register t1,k1 third time') self.assertEqual([k1.name], Registry.getKindNames(t1.name), 'get 1') self.assertEqual(None, Registry.getKindNames(t2.name), 'get 2') self.assertEqual([k1.name], Registry.getAllKindNames(), 'getall 1') self.failUnless(Registry._registerKind(t1, k2), 'register t1,k2') self.assertEqual([k1.name, k2.name], Registry.getKindNames(t1.name), 'get 3') self.assertEqual([k1.name, k2.name], Registry.getAllKindNames(), 'getall 2: %s' % Registry.getAllKindNames()) self.failUnless(Registry._registerType(t2), 'register t2') self.failUnless(Registry._registerKind(t2, k1), 'register t2,k1') self.failIf(Registry._registerKind(t2, k1), 'register t2,k1 2nd') self.assertEqual([k1.name], Registry.getKindNames(t2.name), 'get 4') self.assertEqual([k1.name, k2.name], Registry.getAllKindNames(), 'getall 3')