1#!/usr/bin/env kross 2# -*- coding: utf-8 -*- 3 4""" 5Python script to export content from a Plan Project to an ods file. 6 7(C)2012 Dag Andersen <danders@get2net.dk> 8Licensed under LGPL v2+higher. 9""" 10 11import sys, os, traceback 12import csv 13import Kross, Plan 14 15T = Kross.module("kdetranslation") 16Sheets = Kross.module("KSpread") 17if Sheets is None: 18 raise Exception, T.i18n("Failed to start Sheets. Is Sheets installed?") 19 20class PlanExport: 21 22 def __init__(self, scriptaction): 23 self.scriptaction = scriptaction 24 self.currentpath = self.scriptaction.currentPath() 25 self.forms = Kross.module("forms") 26 try: 27 self.start() 28 except Exception, inst: 29 self.forms.showMessageBox("Sorry", T.i18n("Error"), "%s" % inst) 30 except: 31 self.forms.showMessageBox("Error", T.i18n("Error"), "%s" % "".join( traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]) )) 32 33 def start(self): 34 proj = Plan.project() 35 data = self.showDataSelectionDialog( Plan ) 36 if data is None: 37 return 38 if len(data) == 0: 39 raise Exception, T.i18n("No data to export") 40 41 filename = data[4] 42 if not filename: 43 raise Exception, T.i18n("You must select a file to write to") 44 45 file = open(filename,'w') 46 writer = csv.writer(file) 47 self.write2csv(writer, proj, data) 48 file.close() 49 50 self.forms.showMessageBox("Information", T.i18n("Information"), T.i18n("Data saved to file:\n%1", [filename])) 51 52 def write2csv(self, writer, proj, data): 53 objectType = data[0] 54 schedule = data[1] 55 props = data[2] 56 headers = data[3] 57 if len(props) == 0: 58 raise Exception, T.i18n("No properties to export") 59 60 if headers == True: 61 record = [] 62 for prop in props: 63 record.append(proj.headerData( objectType, prop )) 64 65 if len(record) > 0: 66 writer.writerow( record ) 67 68 if objectType == 0: # Nodes 69 self.exportValues( writer, proj, proj, props, schedule ) 70 elif objectType == 1: # Resources 71 for i in range( proj.resourceGroupCount() ): 72 self.exportValues( writer, proj, proj.resourceGroupAt( i ), props, schedule ) 73 elif objectType == 2: # Accounts 74 for i in range( proj.accountCount() ): 75 self.exportValues( writer, proj, proj.accountAt( i ), props, schedule ) 76 77 78 def exportValues(self, writer, project, dataobject, props, schedule ): 79 record = [] 80 for prop in props: 81 record.append(project.data( dataobject, prop, "DisplayRole", schedule )) 82 83 if len(record) > 0: 84 writer.writerow( record ) 85 86 for i in range( dataobject.childCount() ): 87 self.exportValues( writer, project, dataobject.childAt( i ), props, schedule ) 88 89 def showDataSelectionDialog(self, Plan ): 90 tabledialog = self.forms.createDialog("Property List") 91 tabledialog.setButtons("Ok|Cancel") 92 tabledialog.setFaceType("List") #Auto Plain List Tree Tabbed 93 94 schedulepage = tabledialog.addPage(T.i18n("Schedules"),T.i18n("Select schedule")) 95 schedulewidget = Plan.createScheduleListView(schedulepage) 96 97 sourcepage = tabledialog.addPage(T.i18n("Data"),T.i18n("Select data")) 98 sourcewidget = Plan.createDataQueryView(sourcepage) 99 100 savepage = tabledialog.addPage(T.i18n("Save"), T.i18n("Export ods file"),"document-save") 101 self.savewidget = self.forms.createFileWidget(savepage, "kfiledialog:///csvexportsave") 102 self.savewidget.setMode("Saving") 103 self.savewidget.setFilter("*.csv|%(1)s\n*|%(2)s" % { '1' : T.i18n("Spreadsheet"), '2' : T.i18n("All Files") } ) 104 105 if tabledialog.exec_loop(): 106 schedule = schedulewidget.currentSchedule() 107 props = sourcewidget.selectedProperties() 108 #print "props: ", props 109 ot = sourcewidget.objectType() 110 #print "objectType: ", ot 111 filename = self.savewidget.selectedFile() 112 return [ot, schedule, props, sourcewidget.includeHeaders(), filename ] 113 return None 114 115 116PlanExport( self ) 117