def create_source(self, objects=[]): """Create report from objects (list of DNs).""" tmpfile = self.__create_tempfile() admin.set_format(self._type == Document.TYPE_LATEX) parser = Parser(filename=self._template) parser.tokenize() tokens = parser._tokens fd = codecs.open(tmpfile, 'wb+', encoding='utf8') if parser._header: fd.write(parser._header.data) elif self._header: self.__append_file(fd, self._header) for dn in objects: if isinstance(dn, basestring): obj = admin.get_object(None, dn) else: obj = admin.cache_object(dn) if obj is None: print >> sys.stderr, "warning: dn '%s' not found, skipped." % dn continue tks = copy.deepcopy(tokens) interpret = Interpreter(obj, tks) interpret.run() output = Output(tks, fd=fd) output.write() if parser._footer: fd.write(parser._footer.data) elif self._footer: self.__append_file(fd, self._footer) fd.close() return tmpfile
def resolve( self, token, base ): if token.attrs.has_key( 'module' ): attr = token.attrs.get( 'dn-attribute', None ) if attr and base.has_key( attr ) and base[ attr ]: values = base[ attr ] if not isinstance( values, ( list, tuple ) ): values = [ values, ] for value in values: new_base = admin.get_object( token.attrs[ 'module' ], value ) token.objects.append( new_base )
def resolve(self, token, base): if token.attrs.has_key('module'): attr = token.attrs.get('dn-attribute', None) if attr and base.has_key(attr) and base[attr]: values = base[attr] if not isinstance(values, (list, tuple)): values = [ values, ] for value in values: new_base = admin.get_object(token.attrs['module'], value) token.objects.append(new_base)
def query( self, token, base ): if token.attrs.has_key( 'module' ): attr = token.attrs.get( 'start', None ) if attr and base.has_key( attr ) and base[ attr ]: new_base = admin.get_object( token.attrs[ 'module' ], base[ attr ][ 0 ] ) if not isinstance( base[ attr ], ( list, tuple ) ): base[ attr ] = [ base[ attr ], ] filter = token.attrs.get( 'pattern', None ) if filter: filter = filter.split( '=', 1 ) regex = token.attrs.get( 'regex', None ) if regex: regex = regex.split( '=', 1 ) regex[ 1 ] = re.compile( regex[ 1 ] ) objects = self._query_recursive( base[ attr ], token.attrs[ 'next' ], token.attrs[ 'module' ], filter, regex ) token.objects.extend( objects )
def _query_recursive( self, objects, attr, module, filter = None, regex = None ): _objs = [] for dn in objects: obj = admin.get_object( module, dn ) if not filter and not regex: _objs.append( obj ) elif filter and obj.has_key( filter[ 0 ] ) and obj[ filter[ 0 ] ] and \ fnmatch.fnmatch( obj[ filter[ 0 ] ], filter[ 1 ] ): _objs.append( obj ) elif regex and obj.has_key( regex[ 0 ] ) and obj[ regex[ 0 ] ] and \ regex[ 1 ].match( obj[ regex[ 0 ] ] ): _objs.append( obj ) if not obj.has_key( attr ): continue _objs.extend( self._query_recursive( obj[ attr ], attr, module, filter, regex ) ) return _objs
def _query_recursive(self, objects, attr, module, filter=None, regex=None): _objs = [] for dn in objects: obj = admin.get_object(module, dn) if not filter and not regex: _objs.append(obj) elif filter and obj.has_key( filter[ 0 ] ) and obj[ filter[ 0 ] ] and \ fnmatch.fnmatch( obj[ filter[ 0 ] ], filter[ 1 ] ): _objs.append(obj) elif regex and obj.has_key( regex[ 0 ] ) and obj[ regex[ 0 ] ] and \ regex[ 1 ].match( obj[ regex[ 0 ] ] ): _objs.append(obj) if not obj.has_key(attr): continue _objs.extend( self._query_recursive(obj[attr], attr, module, filter, regex)) return _objs
def query(self, token, base): if token.attrs.has_key('module'): attr = token.attrs.get('start', None) if attr and base.has_key(attr) and base[attr]: new_base = admin.get_object(token.attrs['module'], base[attr][0]) if not isinstance(base[attr], (list, tuple)): base[attr] = [ base[attr], ] filter = token.attrs.get('pattern', None) if filter: filter = filter.split('=', 1) regex = token.attrs.get('regex', None) if regex: regex = regex.split('=', 1) regex[1] = re.compile(regex[1]) objects = self._query_recursive(base[attr], token.attrs['next'], token.attrs['module'], filter, regex) token.objects.extend(objects)