#!/usr/bin/env python # ****************************************************** # Copyright 2004: Commonwealth of Australia. # # Developed by the Computer Network Vulnerability Team, # Information Security Group. # Department of Defence. # # Michael Cohen # # ****************************************************** # Version: FLAG $Version: 0.87-pre1 Date: Thu Jun 12 00:48:38 EST 2008$ # ****************************************************** # # * This program is free software; you can redistribute it and/or # * modify it under the terms of the GNU General Public License # * as published by the Free Software Foundation; either version 2 # * of the License, or (at your option) any later version. # * # * This program is distributed in the hope that it will be useful, # * but WITHOUT ANY WARRANTY; without even the implied warranty of # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # * GNU General Public License for more details. # * # * You should have received a copy of the GNU General Public License # * along with this program; if not, write to the Free Software # * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # ****************************************************** """ This is an implementation of an ASCII Text UI suitable for producing simple automated reports. """ import re, types, textwrap, csv, sys import pyflag.FlagFramework as FlagFramework import pyflag.DB as DB import pyflag.conf import pyflag.UI as UI config=pyflag.conf.ConfObject() import pyflag.Registry as Registry import cStringIO class TextObject: generator = None class TEXTUI(UI.GenericUI): """ A simple text UI """ def __init__(self, default= None, query=None): self.result = "" self.text_var = '' self.current_table=None self.generator = TextObject() if query: self.defaults=query if default: self.defaults = default.defaults def display(self): if self.current_table: self.end_table() return self.result def __str__(self): return self.result def heading(self,string): self.result+=string+"\r\n"+ "-" * len(string) + "\r\n\r\n" def pre(self,string): self.result+=string def start_table(self,**options): if self.current_table==None: self.current_table_size=[0,0] self.current_table=[] def table(self,sql="select ",columns=[],names=[],links=[],table='',where='',groupby = None,case=None,callbacks={},**opts): names=list(names) ## Establish the sorting order try: self.sort=[list(names).index(self.defaults['order']),'order'] except KeyError: try: self.sort=[self.defaults['dorder'],'dorder'] except KeyError: self.sort=[0,'order'] self.filter_conditions=[] self.filter_text=[] try: if not groupby: groupby=self.defaults['group_by'] except KeyError: groupby=None # Get a new SQL generator for building the table with. generator,new_query,names,columns,links = self._make_sql(sql=sql,columns=columns,names=names,links=links,table=table,where=where,groupby = groupby,case=case,callbacks=callbacks, query=self.defaults) output = cStringIO.StringIO() writer=None for row in generator: if not writer: ## Print the headers in a comment field: output.write("#%s\r\n" % ','.join(row.keys())) writer=csv.DictWriter(output, row.keys()) writer.writerow(row) output.seek(0) self.result+=output.read() def text(self,*cuts,**options): self.text_var += "".join(cuts) try: if options['wrap']=='full': for line in self.text_var.splitlines(True): new_lines = textwrap.wrap(line, config.WRAP) for i in range(len(new_lines)): new_line = new_lines[i] self.result+=new_line if len(new_line)max_height: max_height=len(lines) for line in lines: if width