#-*- mode: python; mode: fold -*- # $Id: exportimport.py 31640 2006-01-15 19:22:29Z ogrisel $ """WAeUP Tool XML Adapter. An XML adapter tells the GenericSetup machinery howto im- / export persistent configuration that is relative to a specific CMF component such as our WAeUP Tool. GenericSetup uses the Zope3 interfaces and components machinery to find the right XML adapter for the right object. This is why we flagged our waeup tool with the `implements(IWAeUPTool)` class attribute and made an adapter association in the `configure.zcml` file. """ import csv,re # Zope3 component architecture from zope.component import adapts from zope.interface import implements # Standard GenericSetup base classes and functions from Products.GenericSetup.utils import exportObjects from Products.GenericSetup.utils import importObjects from Products.GenericSetup.utils import XMLAdapterBase from Products.GenericSetup.utils import PropertyManagerHelpers from Products.CMFCore.utils import getToolByName # GenericSetup multi adapts a class that implement IWAeUP and a particular # ISetupEnvironment to and IBody (piece of XML configuration). from Products.GenericSetup.interfaces import IBody from Products.GenericSetup.interfaces import ISetupEnviron from Products.WAeUP.interfaces import IWAeUPTool import Globals p_home = Globals.package_home(globals()) i_home = Globals.INSTANCE_HOME TOOL = 'portal_waeup' NAME = 'waeup' # The exportWAeUP and importWAeUP methods are called by the specific # im- / export steps defined in the corresponding XML files in the # WAeUP/profiles/default/ directory. def installFaculties( academics): ###( """install Universityspecific Faculies with Departments""" faculties = [ ###( {'id': 'agriculture', ###( 'Title': 'Agriculture', 'departments': [ ## { 'id': 'dep1', ###( ## 'Title': 'One', ## }, ], },###) ###) {'id': 'arts', ###( 'Title': 'Arts', 'departments': [ ## { 'id': 'dep1', ###( ## 'Title': 'One', ## }, ], },###) ###) {'id': 'BasicMedicalSciences', ###( 'Title': 'Basic Medical Sciences', 'departments': [ ## { 'id': 'dep1', ###( ## 'Title': 'One', ## }, ], },###) ###) {'id': 'science', ###( 'Title': 'Science', 'departments': [ { 'id': 'bio', ###( 'Title': 'Biochemistry', }, { 'id': 'bot', 'Title': 'Botany', }, { 'id': 'che', 'Title': 'Chemistry', }, { 'id': 'com', 'Title': 'Computer Science', }, { 'id': 'geo', 'Title': 'Geologie', }, { 'id': 'mat', 'Title': 'Mathematics', }, { 'id': 'mic', 'Title': 'Microbiology', }, { 'id': 'opt', 'Title': 'Optometry', }, { 'id': 'phy', 'Title': 'Physics', }, { 'id': 'zoo', 'Title': 'Zoology', }, ], },###) ]###) ###) for faculty in faculties: fid = faculty['id'] f = getattr(academics,fid,None) if f is None: #self.log('Creating Faculty %(id)s = %(Title)s' % faculty) academics.invokeFactory('Faculty', fid) f = getattr(academics,fid) f.getContent().edit(mapping=faculty) for department in faculty['departments']: #self.log('Checking Department %(id)s = %(Title)s' % department) did = department['id'] d = getattr(f,did,None) if d is None: #self.log('Creating Department %(id)s = %(Title)s' % department) f.invokeFactory('Department', did) d = getattr(f,did) d.getContent().edit(mapping=department) ###) def loadFacultiesFromCSV(academics,site,context): ###( """install Universityspecific Faculies from CSV values""" logger = context.getLogger('loadfaculties') faculties = csv.DictReader(open("%s/import/faculty.csv" % i_home,"rb")) for faculty in faculties: fid = faculty['FacultyKey'] f = getattr(academics,fid,None) if f is None: #self.log('Creating Faculty %(id)s = %(Title)s' % faculty) logger.info('Creating Faculty %(FacultyKey)s = %(Description)s' % faculty) academics.invokeFactory('Faculty', fid) f = getattr(academics,fid) d = {'Title': faculty['Description']} f.getContent().edit(mapping=d) ###) def loadDepartmentsFromCSV(academics,site,context): ###( """install Universityspecific Faculies from CSV values""" logger = context.getLogger('loaddepartments') deps = csv.DictReader(open("%s/import/departments.csv" % i_home,"rb")) for dep in deps: fid = dep['FacultyCode'] f = getattr(academics,fid,None) if f is None: logger.info( "No Faculty with ID: %s" % fid) else: did = dep.get('DeptCode') d = getattr(f,did,None) if d is None: #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep) logger.info('Creating Department %(DeptCode)s = %(Description)s' % dep) f.invokeFactory('Department', did) d = getattr(f,did) dict = {'Title': dep['Description']} d.getContent().edit(mapping=dict) ###) def loadCoursesFromCSV(academics,site,context): ###( """install Universityspecific Faculies from CSV values""" logger = context.getLogger('loadcourses') courses = csv.DictReader(open("%s/import/courses.csv" % i_home,"rb")) for course in courses: if course.get("FORMERCODE") != '': continue depid = course['Dept'] res = site.portal_catalog({'meta_type': "Department", 'id': depid}) if not res: continue dept=res[0].getObject() course_id = ''.join(re.split('\W+',course.get('CourseCode'))) if len(course_id) != 6: logger.info("invalid course_code %(CourseCode)s" % course) #print course_id,course.get('CourseCode'),course.get('Description') continue c = getattr(dept,course_id,None) if c is None: #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep) logger.info('Creating Course %(CourseCode)s %(Description)s in Department %(Dept)s' % course) dept.invokeFactory('Course', course_id) c = getattr(dept,course_id) dict = {'Title': course['Description']} dict['code'] = course_id dict['credit'] = course.get('Credits') #dict['level'] = course.get('CourseCode').split()[1][0] dict['level'] = "%c00" % course_id[3] dict['semester'] = "%(Semester)s" % course dict['passmark'] = course.get('PassMark') c.getContent().edit(mapping=dict) ###) def setupStructure(site,context): ###( sections = getattr(site,'sections') portal = getattr(sections,'uniportal',None) if portal is None: sections.invokeFactory('University','uniportal') portal = getattr(site,'uniportal',None) portal.getContent().edit(mapping={'Title':SRPP_TITLE}) students = getattr(portal,'students',None) if students is None: portal.invokeFactory('StudentsFolder','students') students = getattr(portal,'students').getContent() students.edit(mapping={'Title':'Students'}) academics = getattr(portal,'academics',None) if academics is None: portal.invokeFactory('AcademicsFolder','academics') academics = getattr(portal,'academics') academics.getContent().edit(mapping={'Title':'Academics'}) loadFacultiesFromCSV(academics,site,context) loadDepartmentsFromCSV(academics,site,context) loadCoursesFromCSV(academics,site,context) studycourses = getattr(academics,'studycourses',None) if studycourses is None: academics.invokeFactory('SCFolder','studycourses') studycourses = getattr(academics,'studycourses') studycourses.getContent().edit(mapping={'Title':'Studycourses'}) installFaculties(academics) if not hasattr(portal,'accommodation'): portal.invokeFactory('AccoFolder','accommodation') accommodation = getattr(portal,'accommodation').getContent() accommodation.edit(mapping={'Title':'Accommodation'}) ###) def exportWAeUP(context): """Export our WAeUP tool configuration """ site = context.getSite() tool = getToolByName(site, TOOL, None) if tool is None: logger = context.getLogger(NAME) logger.info("Nothing to export.") return exportObjects(tool, '', context) def importWAeUP(context): """Import WAeUP tool configuration """ site = context.getSite() setupStructure(site,context) #import pdb; pdb.set_trace() pass #site = context.getSite() #tool = getToolByName(site, TOOL) #importObjects(tool, '', context) # This the XMLAdapter itself. It encodes the im- / export logic that is specific # to our tool. `im- / exportObjects` functions will find it thanks to the zope # components machinery and the associations made in the configure.zcml file. class WAeUPXMLAdapter(XMLAdapterBase, PropertyManagerHelpers): """XML importer and exporter for the WAeUP tool. Hence this XMLAdapter is really simple. To get more complete examples of what XMLAdapters are meant to do, please have a look at the CPSSkins.exportimport.py or CPSDirectory.exportimport.py files, for instance. """ adapts(IWAeUPTool, ISetupEnviron) implements(IBody) _LOGGER_ID = NAME name = NAME def _exportNode(self): """Export the object as a DOM node. """ node = self._getObjectNode('object') node.appendChild(self._extractProperties()) self._logger.info("WAeUP tool exported.") return node def _importNode(self, node): """Import the object from the DOM node. """ if self.environ.shouldPurge(): self._purgeProperties() self._initProperties(node) self._logger.info("WAeUP tool imported.")