Changeset 1771 for pykota/trunk/bin
- Timestamp:
- 10/04/04 23:25:29 (20 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/dumpykota
r1724 r1771 24 24 # 25 25 # $Log$ 26 # Revision 1.7 2004/10/04 21:25:29 jalet 27 # dumpykota can now output datas in the XML format 28 # 26 29 # Revision 1.6 2004/09/15 18:28:41 jalet 27 30 # Updated help for dumpykota … … 51 54 import os 52 55 import pwd 56 57 try : 58 import jaxml 59 except ImportError : 60 sys.stderr.write("The jaxml Python module is not installed. XML output is disabled.\n") 61 sys.stderr.write("Download jaxml from http://www.librelogiciel.com/software/ or from your Debian archive of choice\n") 62 hasJAXML = 0 63 else : 64 hasJAXML = 1 53 65 54 66 from pykota import version … … 93 105 - ssv : separate datas with semicolons 94 106 - tsv : separate datas with tabs 107 - xml : dump data as XML 108 109 -o | --output fname All datas will be dumped to the file instead of 110 to the standard output. The special '-' filename 111 is the default value and means stdout. 112 WARNING : existing files are truncated ! 113 114 Examples : 115 116 $ dumpykota --data history --format csv >myfile.csv 117 118 This dumps the history in a comma separated values file, for possible 119 use in a spreadsheet. 120 121 $ dumpykota --data users --format xml -o users.xml 122 123 Dumps all users datas to the users.xml file. 95 124 96 125 This program is free software; you can redistribute it and/or modify … … 131 160 "ssv", 132 161 "tsv", 162 "xml", 133 163 ] : 134 164 raise PyKotaToolError, _("Invalid modifier [%s] for --format command line option, see help.") % datatype 135 165 166 if (format == "xml") and not hasJAXML : 167 raise PyKotaToolError, _("XML output is disabled because the jaxml module is not available.") 168 136 169 entries = getattr(self.storage, "extract%s" % datatype.title())() 137 if entries is not None : 138 return getattr(self, "dump%s" % format.title())(entries) 170 if entries : 171 mustclose = 0 172 if options["output"].strip() == "-" : 173 self.outfile = sys.stdout 174 else : 175 self.outfile = open(options["output"], "w") 176 mustclose = 1 177 178 retcode = getattr(self, "dump%s" % format.title())(entries, datatype) 179 180 if mustclose : 181 self.outfile.close() 182 183 return retcode 139 184 return 0 140 185 … … 144 189 line = separator.join([ '"%s"' % field for field in entry ]) 145 190 try : 146 print line191 self.outfile.write("%s\n" % line) 147 192 except IOError, msg : 148 193 sys.stderr.write("%s : %s\n" % (_("PyKota data dumper failed : I/O error"), msg)) … … 150 195 return 0 151 196 152 def dumpCsv(self, entries ) :197 def dumpCsv(self, entries, dummy) : 153 198 """Dumps datas with a comma as the separator.""" 154 199 return self.dumpWithSeparator(",", entries) 155 200 156 def dumpSsv(self, entries ) :201 def dumpSsv(self, entries, dummy) : 157 202 """Dumps datas with a comma as the separator.""" 158 203 return self.dumpWithSeparator(";", entries) 159 204 160 def dumpTsv(self, entries ) :205 def dumpTsv(self, entries, dummy) : 161 206 """Dumps datas with a comma as the separator.""" 162 207 return self.dumpWithSeparator("\t", entries) 208 209 def dumpXml(self, entries, datatype) : 210 """Dumps datas as XML.""" 211 x = jaxml.XML_document(encoding="UTF-8") 212 x.pykota(version=version.__version__) 213 x.dump(storage=self.config.getStorageBackend()["storagebackend"], type=datatype) 214 headers = entries[0] 215 for entry in entries[1:] : 216 x._push() 217 x.entry() 218 for i in range(len(entry)) : 219 value = entry[i] 220 strvalue = str(value).decode(self.getCharset()).encode("UTF-8") 221 x.attribute(strvalue, type=type(value).__name__, name=headers[i]) 222 x._pop() 223 x._output(self.outfile) 163 224 164 225 if __name__ == "__main__" : … … 167 228 defaults = { \ 168 229 "format" : "csv", \ 230 "output" : "-", \ 169 231 } 170 short_options = "vhd:f: "171 long_options = ["help", "version", "data=", "format=" ]232 short_options = "vhd:f:o:" 233 long_options = ["help", "version", "data=", "format=", "output="] 172 234 173 235 # Initializes the command line tool … … 182 244 options["data"] = options["d"] or options["data"] 183 245 options["format"] = options["f"] or options["format"] or defaults["format"] 246 options["output"] = options["o"] or options["output"] or defaults["output"] 184 247 185 248 if options["help"] :