Changeset 5011 for main/waeup.sirp/trunk/src/waeup/sirp/utils
- Timestamp:
- 4 Feb 2010, 17:11:39 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/importexport.txt
r4965 r5011 18 18 >>> grok.testing.grok('waeup') 19 19 20 Furthermore we need something to import/export. As only pickable 21 classes are importable/exportable and these have to be defined in some 22 globally reachable module, we first define some silly object in 23 filesystem and adjust the path to lookup modules: 24 25 >>> import os 26 >>> import sys 27 >>> os.mkdir('stoneage') 28 >>> old_sys_path = sys.path 29 >>> open(os.path.join('stoneage', '__init__.py'), 'wb').write( 30 ... """ 31 ... class Cave(object): 32 ... name = 'Unnamed cave' 33 ... dinoports = 2 34 ... """) 35 >>> sys.path.append(os.getcwd()) 36 37 Now we can import stoneage.Cave objects. 38 20 39 21 40 Exporting XML … … 28 47 i.e. an in-memory file. 29 48 30 To show this basic functionality provided for all XML exportable 31 objects, we choose faculties. 49 We can export caves. First we create a cave: 32 50 33 We can export faculties. First we create a faculty:: 34 35 >>> from waeup.sirp.university.faculty import Faculty 36 >>> myfaculty = Faculty() 37 >>> myfaculty.name = 'My very faculty.' 51 >>> from stoneage import Cave 52 >>> mycave = Cave() 53 >>> mycave.name = 'Freds Cave' 54 >>> mycave.dinoports = 1 38 55 39 56 Exporting plain XML … … 44 61 45 62 >>> from waeup.sirp.interfaces import IWAeUPXMLExporter 46 >>> exporter = IWAeUPXMLExporter(my faculty)63 >>> exporter = IWAeUPXMLExporter(mycave) 47 64 >>> exporter 48 65 <waeup.sirp.utils.importexport.XMLExporter object at 0x...> … … 68 85 <?xml version="1.0" encoding="utf-8" ?> 69 86 <pickle> 70 < initialized_object>87 <object> 71 88 ... 72 89 </pickle> 73 90 74 Clean up::75 76 >>> import os77 >>> os.unlink('myexport.xml')78 91 79 92 Importing XML … … 82 95 We can generate objects from XML. 83 96 84 Let's create a faculty instance, that we want to be restored 85 afterwards:: 97 We change the name of the cave: 86 98 87 >>> from waeup.sirp.university.faculty import Faculty 88 >>> myfaculty = Faculty() 89 >>> myfaculty.name = 'My very faculty.' 99 >>> mycave.name = 'Another name' 90 100 91 We create an XML dump of this object:: 92 93 >>> from waeup.sirp.interfaces import IWAeUPXMLExporter 94 >>> exporter = IWAeUPXMLExporter(myfaculty) 95 >>> result = exporter.export('myexport.xml') 96 97 We change the name of the faculty:: 98 99 >>> myfaculty.name = 'Another name' 100 101 Now we create an importer for that file:: 101 Now we create an importer for that object: 102 102 103 103 >>> from waeup.sirp.interfaces import IWAeUPXMLImporter 104 >>> importer = IWAeUPXMLImporter(my faculty)104 >>> importer = IWAeUPXMLImporter(mycave) 105 105 106 106 Importing from filenames … … 111 111 >>> new_obj = importer.doImport('myexport.xml') 112 112 113 The object created is indeed a faculty::113 The object created is indeed a cave:: 114 114 115 115 >>> new_obj 116 < waeup.sirp.university.faculty.Facultyobject at 0x...>116 <stoneage.Cave object at 0x...> 117 117 118 118 >>> new_obj.name 119 ' My very faculty.'119 'Freds Cave' 120 120 121 121 The new object is really a new object:: 122 122 123 >>> new_obj is my faculty123 >>> new_obj is mycave 124 124 False 125 125 … … 135 135 136 136 >>> new_obj 137 < waeup.sirp.university.faculty.Facultyobject at 0x...>137 <stoneage.Cave object at 0x...> 138 138 139 139 Clean up:: 140 140 141 141 >>> import os 142 >>> import shutil 143 >>> sys.path = old_sys_path 142 144 >>> os.unlink('myexport.xml') 145 >>> shutil.rmtree('stoneage')
Note: See TracChangeset for help on using the changeset viewer.