| 1 |
#!/usr/bin/env kross |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
""" |
| 5 |
Python script to import content from a Plan Project stored |
| 6 |
within a Plan file into Tables. |
| 7 |
|
| 8 |
(C)2008 Dag Andersen <[email protected]> |
| 9 |
(C)2011 Dag Andersen <[email protected]> |
| 10 |
Licensed under LGPL v2+higher. |
| 11 |
""" |
| 12 |
|
| 13 |
import sys, os, traceback |
| 14 |
import Kross, KSpread |
| 15 |
|
| 16 |
T = Kross.module("kdetranslation") |
| 17 |
|
| 18 |
class PlanImport: |
| 19 |
|
| 20 |
def __init__(self, scriptaction): |
| 21 |
self.scriptaction = scriptaction |
| 22 |
self.currentpath = self.scriptaction.currentPath() |
| 23 |
self.forms = Kross.module("forms") |
| 24 |
try: |
| 25 |
self.start() |
| 26 |
except Exception, inst: |
| 27 |
self.forms.showMessageBox("Sorry", T.i18n("Error"), "%s" % inst) |
| 28 |
except: |
| 29 |
self.forms.showMessageBox("Error", T.i18n("Error"), "%s" % "".join( traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]) )) |
| 30 |
|
| 31 |
def start(self): |
| 32 |
writer = KSpread.writer() |
| 33 |
filename = self.showImportDialog(writer) |
| 34 |
if not filename: |
| 35 |
return # no exception, user prob pressed cancel |
| 36 |
|
| 37 |
Plan = Kross.module("Plan") |
| 38 |
if Plan is None: |
| 39 |
raise Exception, T.i18n("Failed to start Plan. Is Plan installed?") |
| 40 |
|
| 41 |
Plan.openUrl( filename ) |
| 42 |
proj = Plan.project() |
| 43 |
data = self.showDataSelectionDialog( writer, Plan ) |
| 44 |
if len(data) == 0: |
| 45 |
raise Exception, T.i18n("No data to import") |
| 46 |
|
| 47 |
objectType = data[0] |
| 48 |
schedule = data[1] |
| 49 |
props = data[2] |
| 50 |
if len(props) == 0: |
| 51 |
raise Exception, T.i18n("No properties to import") |
| 52 |
|
| 53 |
record = [] |
| 54 |
if data[3] == True: |
| 55 |
for prop in props: |
| 56 |
record.append( proj.headerData( objectType, prop ) ) |
| 57 |
if not writer.setValues(record): |
| 58 |
if self.forms.showMessageBox("WarningContinueCancel", T.i18n("Warning"), T.i18n("Failed to set all properties of '%1' to cell '%2'", [", ".join(record), writer.cell()])) == "Cancel": |
| 59 |
return |
| 60 |
writer.next() |
| 61 |
|
| 62 |
if objectType == 0: # Nodes |
| 63 |
self.importValues( writer, proj, proj, props, schedule ) |
| 64 |
if objectType == 1: # Resources |
| 65 |
for i in range( proj.resourceGroupCount() ): |
| 66 |
self.importValues( writer, proj, proj.resourceGroupAt( i ), props, schedule ) |
| 67 |
if objectType == 2: # Accounts |
| 68 |
for i in range( proj.accountCount() ): |
| 69 |
self.importValues( writer, proj, proj.accountAt( i ), props, schedule ) |
| 70 |
|
| 71 |
def importValues(self, writer, project, dataobject, props, schedule ): |
| 72 |
record = [] |
| 73 |
for prop in props: |
| 74 |
record.append( project.data( dataobject, prop, "DisplayRole", schedule ) ) |
| 75 |
if not writer.setValues(record): |
| 76 |
if self.forms.showMessageBox("WarningContinueCancel", T.i18n("Warning"), T.i18n("Failed to set all properties of '%1' to cell '%2'", [", ".join(record), writer.cell()])) == "Cancel": |
| 77 |
return |
| 78 |
writer.next() |
| 79 |
for i in range( dataobject.childCount() ): |
| 80 |
self.importValues( writer, project, dataobject.childAt( i ), props, schedule ) |
| 81 |
|
| 82 |
def showImportDialog(self, writer): |
| 83 |
dialog = self.forms.createDialog(T.i18n("Plan Import")) |
| 84 |
dialog.setButtons("Ok|Cancel") |
| 85 |
dialog.setFaceType("List") #Auto Plain List Tree Tabbed |
| 86 |
|
| 87 |
openpage = dialog.addPage(T.i18n("Open"),T.i18n("Import from Plan Project File"),"document-import") |
| 88 |
openwidget = self.forms.createFileWidget(openpage, "kfiledialog:///kspreadplanimportopen") |
| 89 |
openwidget.setMode("Opening") |
| 90 |
openwidget.setFilter("*.plan|%(1)s\n*|%(2)s" % { '1' : T.i18n("Plan Project Files"), '2' : T.i18n("All Files") }) |
| 91 |
if dialog.exec_loop(): |
| 92 |
filename = openwidget.selectedFile() |
| 93 |
if not os.path.isfile(filename): |
| 94 |
raise Exception, T.i18n("No file selected.") |
| 95 |
return filename |
| 96 |
return None |
| 97 |
|
| 98 |
def showDataSelectionDialog(self, writer, Plan ): |
| 99 |
tabledialog = self.forms.createDialog("Property List") |
| 100 |
tabledialog.setButtons("Ok|Cancel") |
| 101 |
tabledialog.setFaceType("List") #Auto Plain List Tree Tabbed |
| 102 |
|
| 103 |
datapage = tabledialog.addPage(T.i18n("Destination"),T.i18n("Import to sheet beginning at cell")) |
| 104 |
sheetslistview = KSpread.createSheetsListView(datapage) |
| 105 |
sheetslistview.setEditorType("Cell") |
| 106 |
|
| 107 |
schedulepage = tabledialog.addPage(T.i18n("Schedules"),T.i18n("Select schedule")) |
| 108 |
schedulewidget = Plan.createScheduleListView(schedulepage) |
| 109 |
|
| 110 |
sourcepage = tabledialog.addPage(T.i18n("Data"),T.i18n("Select data")) |
| 111 |
sourcewidget = Plan.createDataQueryView(sourcepage) |
| 112 |
if tabledialog.exec_loop(): |
| 113 |
currentSheet = sheetslistview.sheet() |
| 114 |
if not currentSheet: |
| 115 |
raise Exception, T.i18n("No current sheet.") |
| 116 |
if not writer.setSheet(currentSheet): |
| 117 |
raise Exception, T.i18n("Invalid sheet '%1' defined.", [currentSheet]) |
| 118 |
|
| 119 |
cell = sheetslistview.editor() |
| 120 |
if not writer.setCell(cell): |
| 121 |
raise Exception, T.i18n("Invalid cell '%1' defined.", [cell]) |
| 122 |
|
| 123 |
schedule = schedulewidget.currentSchedule() |
| 124 |
#print "schedule: ", schedule |
| 125 |
props = sourcewidget.selectedProperties() |
| 126 |
#print "props: ", props |
| 127 |
ot = sourcewidget.objectType() |
| 128 |
#print "objectType: ", ot |
| 129 |
return [ot, schedule, props, sourcewidget.includeHeaders() ] |
| 130 |
return None |
| 131 |
|
| 132 |
|
| 133 |
PlanImport( self ) |