From: <sy...@us...> - 2003-07-31 11:36:11
|
Update of /cvsroot/archetypes/ArchXMLTool In directory sc8-pr-cvs1:/tmp/cvs-serv27177 Modified Files: ArchXMLTool.py arch_xml.py fields_handlers.py Log Message: bug fixes and minor refactoring to permit futur marshaller Index: ArchXMLTool.py =================================================================== RCS file: /cvsroot/archetypes/ArchXMLTool/ArchXMLTool.py,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ArchXMLTool.py 29 Jul 2003 10:57:52 -0000 1.3 --- ArchXMLTool.py 31 Jul 2003 11:36:06 -0000 1.4 *************** *** 108,112 **** dest_dir = join(fsdir, object.getId()) mkdir(dest_dir) ! arch_xml.export_content(object, join(dest_dir, '_directory_info.xml'), handlers) for child in object.objectValues(): self._export_to_directory(dest_dir, child, out, handlers) --- 108,113 ---- dest_dir = join(fsdir, object.getId()) mkdir(dest_dir) ! dest_file = join(dest_dir, '_directory_info.xml') ! arch_xml.export_content(object, open(dest_file, 'w'), handlers, dest_dir) for child in object.objectValues(): self._export_to_directory(dest_dir, child, out, handlers) *************** *** 114,118 **** # non folderish content print >>out, 'exporting %s content' % object.absolute_url() ! arch_xml.export_content(object, join(fsdir, '%s.xml' % object.getId()), handlers) --- 115,120 ---- # non folderish content print >>out, 'exporting %s content' % object.absolute_url() ! dest_file = join(fsdir, '%s.xml' % object.getId()) ! arch_xml.export_content(object, open(dest_file, 'w') , handlers, fsdir) Index: arch_xml.py =================================================================== RCS file: /cvsroot/archetypes/ArchXMLTool/arch_xml.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** arch_xml.py 29 Jul 2003 10:57:52 -0000 1.2 --- arch_xml.py 31 Jul 2003 11:36:06 -0000 1.3 *************** *** 32,36 **** def startElement(self, tag, attrs) : if self.level == 0: ! self._schema = getType(tag)['schema'] self.result = getattr(self.container, attrs['id']) elif self.level == 1: --- 32,37 ---- def startElement(self, tag, attrs) : if self.level == 0: ! type_name = attrs.get('meta_type', tag) ! self._schema = getType(type_name)['schema'] self.result = getattr(self.container, attrs['id']) elif self.level == 1: *************** *** 49,54 **** tag, attrs = self._current_attr field = self._schema[tag] ! field_type = field.__class__.__name__ ! handler = getattr(self._handlers, field_type.lower() + '_import', None) if handler is None: raise Exception('Unable to handle %s fields' % field_type) --- 50,55 ---- tag, attrs = self._current_attr field = self._schema[tag] ! field_type = normalize_field(field) ! handler = getattr(self._handlers, field_type + '_import', None) if handler is None: raise Exception('Unable to handle %s fields' % field_type) *************** *** 83,88 **** def startElement(self, tag, attrs) : ! tag = tag.encode() ! self.result = create_object(self.container, attrs['id'], type_name=tag) if attrs.has_key('uid'): self._map[attrs['uid']] = self.result.UID() --- 84,90 ---- def startElement(self, tag, attrs) : ! type_name = attrs.get('portal_type', tag) ! self.result = create_object(self.container, attrs['id'], ! type_name=type_name.encode()) if attrs.has_key('uid'): self._map[attrs['uid']] = self.result.UID() *************** *** 94,115 **** for file in os.listdir(fsdir): absfile = join(fsdir, file) if isdir(absfile) and exists(join(absfile, '_directory_info.xml')): print >>out, 'importing %s folder' % absfile ! plone_dir = _create_content(join(absfile, '_directory_info.xml'), ! self, map) _import_create(plone_dir, absfile, map, out) ! elif file[-4:] == '.xml' and file[0] != '_': print >>out, 'importing %s content' % absfile ! _create_content(absfile, self, map) else: print >>out, 'skipping %s' % absfile ! def _create_content(fsfile, container, map): """create parser, set hdlr as SAX handler and return hdlr.result""" - __traceback_info__ = (fsfile, container) hdlr = ArchXMLRefHandler(container, map) p = create_parser(hdlr) try: ! p.parse(open(fsfile)) except ParseStopException: pass --- 96,117 ---- for file in os.listdir(fsdir): absfile = join(fsdir, file) + __traceback_info__ = (absfile, self) if isdir(absfile) and exists(join(absfile, '_directory_info.xml')): print >>out, 'importing %s folder' % absfile ! plone_dir = create_content(open(join(absfile, '_directory_info.xml')), ! self, map) _import_create(plone_dir, absfile, map, out) ! elif file[-4:].lower() == '.xml' and file[0] != '_': print >>out, 'importing %s content' % absfile ! create_content(open(absfile), self, map) else: print >>out, 'skipping %s' % absfile ! def create_content(stream, container, map): """create parser, set hdlr as SAX handler and return hdlr.result""" hdlr = ArchXMLRefHandler(container, map) p = create_parser(hdlr) try: ! p.parse(stream) except ParseStopException: pass *************** *** 121,159 **** for file in os.listdir(fsdir): absfile = join(fsdir, file) if isdir(absfile) and exists(join(absfile, '_directory_info.xml')): print >>out, 'importing %s folder' % absfile ! plone_dir = _import_content(join(absfile, '_directory_info.xml'), ! self, map, handlers) _import_fill(plone_dir, absfile, map, out, handlers) elif file[-4:] == '.xml' and file[0] != '_': print >>out, 'importing %s content' % absfile ! _import_content(absfile, self, map, handlers) elif file != '_directory_info.xml': print >>out, 'skipping %s' % absfile ! def _import_content(fsfile, container, map, handlers): """create parser, set hdlr as SAX handler and return hdlr.result""" ! __traceback_info__ = (fsfile, container) ! hdlr = ArchXMLImportHandler(container, map, dirname(fsfile), handlers) p = create_parser(hdlr) ! p.parse(open(fsfile)) return hdlr.result ! def export_content(self, dest_file, handlers): """export content of an archetypes object to a xml file""" ! f = open(dest_file, 'w') ! f.write('<?xml version="1.0" encoding="UTF-8" ?>\n') ! f.write('<%s id="%s" uid="%s">\n' % (self.archetype_name, self.getId(), self.UID())) for field in self.Schema().fields(): ! # skip special 'id' field if field.getName() == 'id': continue ! handler = getattr(handlers, field.__class__.__name__.lower() + '_export') ! handler(self, field, f, dirname(dest_file)) ! f.write('</%s>\n' % (self.archetype_name)) # common utilities ############################################################ ! def format_tag(tagname, attributes): """ --- 123,166 ---- for file in os.listdir(fsdir): absfile = join(fsdir, file) + __traceback_info__ = (absfile, self) if isdir(absfile) and exists(join(absfile, '_directory_info.xml')): print >>out, 'importing %s folder' % absfile ! plone_dir = fill_content(open(join(absfile, '_directory_info.xml')), ! self, map, handlers, absfile) _import_fill(plone_dir, absfile, map, out, handlers) elif file[-4:] == '.xml' and file[0] != '_': print >>out, 'importing %s content' % absfile ! fill_content(open(absfile), self, map, handlers, fsdir) elif file != '_directory_info.xml': print >>out, 'skipping %s' % absfile ! def fill_content(stream, container, map, handlers, curdir=None): """create parser, set hdlr as SAX handler and return hdlr.result""" ! hdlr = ArchXMLImportHandler(container, map, curdir, handlers) p = create_parser(hdlr) ! p.parse(stream) return hdlr.result ! def export_content(self, stream, handlers, curdir=None): """export content of an archetypes object to a xml file""" ! portal_type = self.getPortalTypeName() ! tag = portal_type.replace(' ', '') ! stream.write('<?xml version="1.0" encoding="UTF-8" ?>\n') ! stream.write('<%s id="%s" uid="%s" portal_type="%s" meta_type="%s">\n' % ( ! tag, self.getId(), self.UID(), portal_type, self.meta_type)) for field in self.Schema().fields(): ! # skip special 'id' field exported in root attributes if field.getName() == 'id': continue ! handler = getattr(handlers, normalize_field(field) + '_export') ! handler(self, field, stream, curdir) ! stream.write('</%s>\n' % (self.archetype_name)) # common utilities ############################################################ ! ! def normalize_field(field): ! return field.__class__.__name__.lower() ! def format_tag(tagname, attributes): """ *************** *** 181,186 **** def create_object(self, id, type_name): ! self.invokeFactory(id=id, type_name=type_name) ! return getattr(self, id) --- 188,206 ---- def create_object(self, id, type_name): ! """return an object with the given id and type ! create it if it doesn't exist, else check it's of the correct type ! """ ! try: ! self.invokeFactory(id=id, type_name=type_name) ! except: ! import traceback ! traceback.print_exc() ! obj = getattr(self, id) ! if not obj.meta_type == type_name: ! raise Exception('An object with id %s yet exists and is not of type %s but %s' % ! (id, type_name, obj.meta_type)) ! else: ! obj = getattr(self, id) ! return obj Index: fields_handlers.py =================================================================== RCS file: /cvsroot/archetypes/ArchXMLTool/fields_handlers.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** fields_handlers.py 29 Jul 2003 10:57:52 -0000 1.2 --- fields_handlers.py 31 Jul 2003 11:36:06 -0000 1.3 *************** *** 70,73 **** --- 70,76 ---- raise XMLImportError('Missing mime type for %s' % field.getName()) if kwargs.has_key('data_file'): + if curdir is None: + raise XMLImportError('Can not import external data file for %s.%s' % ( + instance, field)) assert value is None or not value.strip() value = open(join(curdir, kwargs['data_file']), 'rb').read() *************** *** 81,84 **** --- 84,90 ---- raise XMLImportError('Missing mime type for %s' % field.getName()) if kwargs.has_key('data_file'): + if curdir is None: + raise XMLImportError('Can not import external data file for %s.%s' % ( + instance, field)) assert value is None or not value.strip() value = open(join(curdir, kwargs['data_file']), 'rb').read() *************** *** 254,262 **** if not uid: continue ! new_uid = map.get(uid) ! if new_uid is None: ! print 'Unable to create reference to %s (known are: %s)' % (uid, map.keys()) ! continue ! new_uids.append(new_uid) if field.multiValued: mutator(new_uids, **kwargs) --- 260,264 ---- if not uid: continue ! new_uids.append(map.get(uid, uid)) if field.multiValued: mutator(new_uids, **kwargs) |