Package gavo :: Package base :: Module common
[frames] | no frames]

Source Code for Module gavo.base.common

 1  """ 
 2  Common code for DaCHS's base package. 
 3  """ 
 4   
 5  #c Copyright 2008-2019, the GAVO project 
 6  #c 
 7  #c This program is free software, covered by the GNU GPL.  See the 
 8  #c COPYING file in the source distribution. 
 9   
10   
11  from __future__ import print_function 
12   
13  from gavo.utils.excs import *  #noflake: really want those names 
14  from gavo.utils import excs # make life a bit easier for pyflakes. 
15   
16   
17 -class NotGivenType(type):
18 - def __str__(self):
19 raise excs.StructureError( 20 "%s cannot be stringified"%self.__class__.__name__)
21 22 __unicode__ = __str__ 23
24 - def __repr__(self):
25 return "<Not given/empty>"
26
27 - def __nonzero__(self):
28 return False
29 30
31 -class NotGiven(object):
32 """A sentinel class for defaultless values that can remain undefined. 33 """ 34 __metaclass__ = NotGivenType
35 36
37 -class Ignore(excs.ExecutiveAction):
38 """An executive action causing an element to be not adopted by its 39 parent. 40 41 Raise this in -- typically -- onElementComplete if the element just 42 built goes somewhere else but into its parent structure (or is 43 somehow benignly unusable). Classic use case: Active Tags. 44 """
45 46
47 -class Replace(excs.ExecutiveAction):
48 """An executive action replacing the current child with the Exception's 49 argument. 50 51 Use this sparingly. I'd like to get rid of it. 52 """
53 - def __init__(self, newOb, newName=None):
54 self.newOb, self.newName = newOb, newName
55 56
57 -class Parser(object):
58 """is an object that routes events. 59 60 It is constructed with up to three functions for handling start, 61 value, and end events; these would override methods start_, end_, 62 or value_. Thus, you can simply implement when inheriting from 63 Parser. In that case, no call the the constructor is necessary 64 (i.e., Parser works as a mixin as well). 65 """
66 - def __init__(self, start=None, value=None, end=None):
67 self.start, self.value, self.end = start, value, end
68
69 - def feedEvent(self, ctx, type, name, value):
70 if type=="start": 71 return self.start_(ctx, name, value) 72 elif type=="value": 73 return self.value_(ctx, name, value) 74 elif type=="end": 75 return self.end_(ctx, name, value) 76 else: 77 raise excs.StructureError( 78 "Illegal event type while building: '%s'"%type)
79 80
81 -class StructParseDebugMixin(object):
82 """put this before Parser in the parent class list of a struct, 83 and you'll see the events coming in to your parser. 84 """
85 - def feedEvent(self, ctx, type, name, value):
86 print(type, name, value, self) 87 return Parser.feedEvent(self, ctx, type, name, value)
88