1#!/usr/bin/env kross 2# -*- coding: utf-8 -*- 3 4import os, datetime, sys, traceback, pickle 5import Kross, Plan 6 7T = Kross.module("kdetranslation") 8 9class BusyinfoImporter: 10 11 def __init__(self, scriptaction): 12 self.scriptaction = scriptaction 13 self.currentpath = self.scriptaction.currentPath() 14 15 self.proj = Plan.project() 16 17 self.forms = Kross.module("forms") 18 self.dialog = self.forms.createDialog(T.i18n("Busy Information Import")) 19 self.dialog.setButtons("Ok|Cancel") 20 self.dialog.setFaceType("List") #Auto Plain List Tree Tabbed 21 22 openpage = self.dialog.addPage(T.i18n("Open"), T.i18n("Import Busy Info File"),"document-open") 23 self.openwidget = self.forms.createFileWidget(openpage, "kfiledialog:///kplatobusyinfoimportopen") 24 self.openwidget.setMode("Opening") 25 self.openwidget.setFilter("*.rbi|%(1)s\n*|%(2)s" % { '1' : T.i18n("Resource Busy Information"), '2' : T.i18n("All Files") } ) 26 27 if self.dialog.exec_loop(): 28 try: 29 Plan.beginCommand( T.i18nc("(qtundo_format)", "Import resource busy information") ) 30 self.doImport( self.proj ) 31 Plan.endCommand() 32 except: 33 Plan.revertCommand() 34 self.forms.showMessageBox("Error", T.i18n("Error"), "%s" % "".join( traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]) )) 35 36 def doImport( self, project ): 37 filename = self.openwidget.selectedFile() 38 if not os.path.isfile(filename): 39 self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("No file selected") ) 40 return 41 42 file = open(filename,'r') 43 try: 44 # load project id and -name 45 data = pickle.load( file ) 46 #print data 47 pid = data[0] 48 if project.id() == pid: 49 self.forms.showMessageBox("Error", T.i18n("Error"), T.i18n("Cannot load data from project with the same identity") ) 50 raise Exception 51 pname = data[1].decode( "UTF-8" ) 52 # clear existing, so we don't get double up 53 project.clearExternalAppointments( pid ) 54 # load the intervals 55 while True: 56 data = pickle.load( file ) 57 self.loadAppointment( project, pid, pname, data ) 58 59 except: 60 file.close() 61 62 def loadAppointment( self, project, pid, pname, data ): 63 r = project.findResource( data[0] ) 64 if r is None: 65 print "Resource is not used in this project: %s, %s" % ( data[0], data[1] ) 66 return 67 if project.data( r, 'ResourceName' ) != data[1]: 68 #TODO Warning ? 69 print "Resources has same id but different names %s - %s" % ( project.data( r, 'ResourceName' ), data[1] ) 70 r.addExternalAppointment( pid, pname, data[2:5] ) 71 72BusyinfoImporter( self ) 73