| 1 |
#!/usr/bin/env kross |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
""" |
| 5 |
Python script to import task data from a spreadsheet |
| 6 |
|
| 7 |
(C)2011 Dag Andersen <[email protected]> |
| 8 |
Licensed under LGPL v2.1+higher. |
| 9 |
|
| 10 |
Requires Calligra Sheets |
| 11 |
|
| 12 |
The data in the spreadsheet must be organized as follows: |
| 13 |
|
| 14 |
1) The data must be found in sheet 1 |
| 15 |
2) Row 1 must define the header text for each column |
| 16 |
3) The header text must match the task data property name |
| 17 |
4) Column A must contain the WBS Code |
| 18 |
5) Column A *MUST* be set to text format |
| 19 |
6) The wbs code must be in the format defined by Tools -> Define WBS Pattern |
| 20 |
(Default is arabic numbers with '.' separator, e.g. 1.2.3) |
| 21 |
7) The separator must be a '.' for all levels |
| 22 |
8) The tasks must be ordered in increasing wbs code order |
| 23 |
9) The WBS codes must be consecutive: |
| 24 |
If you have task 1.2 and 1.4 you must also have tasks 1, 1.1 and 1.3 |
| 25 |
|
| 26 |
Example spreadsheet: |
| 27 |
|
| 28 |
WBSCode Name Responsible Allocation EstimateType Estimate Description |
| 29 |
1 A Elvis Summary task |
| 30 |
1.2 B Elvis Jane Effort 3d Task |
| 31 |
1.1 C Elvis John Duration 4d Task |
| 32 |
|
| 33 |
2 D Gardner Summary task |
| 34 |
2.1 E Gardner Summary task |
| 35 |
2.1.1 F Gardner Jane,John Effort 3h Next level |
| 36 |
2.1.2 G Gardner Effort 0h Milestone |
| 37 |
|
| 38 |
(Note that the resources listed in Allocation will be created if they do not already exists) |
| 39 |
""" |
| 40 |
|
| 41 |
import os, sys, traceback |
| 42 |
import Kross, Plan |
| 43 |
|
| 44 |
T = Kross.module("kdetranslation") |
| 45 |
|
| 46 |
class TaskImporter: |
| 47 |
|
| 48 |
def __init__(self, scriptaction): |
| 49 |
self.scriptaction = scriptaction |
| 50 |
|
| 51 |
self.proj = Plan.project() |
| 52 |
|
| 53 |
self.forms = Kross.module("forms") |
| 54 |
self.dialog = self.forms.createDialog(T.i18n("Task Import")) |
| 55 |
self.dialog.setButtons("Ok|Cancel") |
| 56 |
self.dialog.setFaceType("List") #Auto Plain List Tree Tabbed |
| 57 |
|
| 58 |
openpage = self.dialog.addPage(T.i18n("Open"),T.i18n("Tables Tasks"),"document-open") |
| 59 |
self.openwidget = self.forms.createFileWidget(openpage, "kfiledialog:///tablestaskimportopen") |
| 60 |
self.openwidget.setMode("Opening") |
| 61 |
self.openwidget.setFilter("*.ods|%(1)s\n*|%(2)s" % { '1' : T.i18n("Tables"), '2' : T.i18n("All Files") } ) |
| 62 |
|
| 63 |
if self.dialog.exec_loop(): |
| 64 |
try: |
| 65 |
Plan.beginCommand( T.i18nc( "(qtundoformat)", "Import tasks" ) ) |
| 66 |
self.doImport( self.proj ) |
| 67 |
Plan.endCommand() |
| 68 |
except: |
| 69 |
Plan.revertCommand() # in case partly loaded |
| 70 |
self.forms.showMessageBox("Error", T.i18n("Error"), "%s" % "".join( traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]) )) |
| 71 |
|
| 72 |
def doImport( self, project ): |
| 73 |
filename = self.openwidget.selectedFile() |
| 74 |
if not os.path.isfile(filename): |
| 75 |
raise Exception, T.i18n("No file selected") |
| 76 |
|
| 77 |
Tables = Kross.module("kspread") |
| 78 |
if not Tables: |
| 79 |
raise Exception, T.i18n("Could not start Calligra Sheets") |
| 80 |
|
| 81 |
if not Tables.openUrl(filename): |
| 82 |
self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("Could not open document: %1", [filename])) |
| 83 |
return |
| 84 |
|
| 85 |
def findParentTask(parent, wbs): |
| 86 |
if wbs == project.data(parent, 'WBSCode'): |
| 87 |
return parent |
| 88 |
for i in range(parent.childCount()): |
| 89 |
task = findParentTask(parent.childAt(i), wbs) |
| 90 |
if task is not None: |
| 91 |
return task |
| 92 |
return None |
| 93 |
|
| 94 |
def parentTask(wbs): |
| 95 |
codes = wbs.split('.') |
| 96 |
if len(codes) > 1: |
| 97 |
codes.pop() |
| 98 |
pwbs = '.'.join(codes) |
| 99 |
for i in range(project.taskCount()): |
| 100 |
parent = findParentTask(project.taskAt(i), pwbs) |
| 101 |
if parent is not None: |
| 102 |
return parent |
| 103 |
return project |
| 104 |
|
| 105 |
def createTask(properties, data): |
| 106 |
if len(data) == 0: |
| 107 |
return |
| 108 |
task = project.createTask(parentTask(data[0])) |
| 109 |
if task is None: |
| 110 |
raise Exception, T.i18n("Failed to create task") |
| 111 |
|
| 112 |
for i in range(1, len(properties)): |
| 113 |
project.setData(task, properties[i], data[i]) |
| 114 |
|
| 115 |
# Get data into list that can be sorted after wbs code |
| 116 |
reader = Tables.reader() |
| 117 |
props = [] |
| 118 |
def changedRow(row): |
| 119 |
if row == 1: |
| 120 |
props.append(reader.currentValues()) |
| 121 |
else: |
| 122 |
data = reader.currentValues() |
| 123 |
# skip lines without wbs code |
| 124 |
if data[0] is not None: |
| 125 |
createTask(props[0], data) |
| 126 |
|
| 127 |
reader.setSheet("Sheet1") |
| 128 |
reader.connect("changedRow(int)", changedRow) |
| 129 |
reader.start() |
| 130 |
|
| 131 |
|
| 132 |
TaskImporter( self ) |