comparison roundup/admin.py @ 3670:64f0b85ea603

fix importing into anydbm backend [SF#1512939] also, refactored last patch
author Richard Jones <richard@users.sourceforge.net>
date Fri, 11 Aug 2006 05:10:15 +0000
parents 07d1d8e22271
children cecdd102733c
comparison
equal deleted inserted replaced
3669:07d1d8e22271 3670:64f0b85ea603
14 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 14 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" 15 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
16 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, 16 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
17 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 17 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
18 # 18 #
19 # $Id: admin.py,v 1.103 2006-08-11 05:00:19 richard Exp $ 19 # $Id: admin.py,v 1.104 2006-08-11 05:10:15 richard Exp $
20 20
21 '''Administration commands for maintaining Roundup trackers. 21 '''Administration commands for maintaining Roundup trackers.
22 ''' 22 '''
23 __docformat__ = 'restructuredtext' 23 __docformat__ = 'restructuredtext'
24 24
1052 raise UsageError, _('no such class "%(classname)s"')%locals() 1052 raise UsageError, _('no such class "%(classname)s"')%locals()
1053 except IndexError: 1053 except IndexError:
1054 raise UsageError, _('no such %(classname)s node "%(nodeid)s"')%locals() 1054 raise UsageError, _('no such %(classname)s node "%(nodeid)s"')%locals()
1055 return 0 1055 return 0
1056 1056
1057 def do_export(self, args): 1057 def do_export(self, args, export_files=True):
1058 ""'''Usage: export [[-]class[,class]] export_dir 1058 ""'''Usage: export [[-]class[,class]] export_dir
1059 Export the database to colon-separated-value files. 1059 Export the database to colon-separated-value files.
1060 To exclude the files (e.g. for the msg or file class), 1060 To exclude the files (e.g. for the msg or file class),
1061 use the exporttables command. 1061 use the exporttables command.
1062 1062
1092 1092
1093 # do all the classes specified 1093 # do all the classes specified
1094 for classname in classes: 1094 for classname in classes:
1095 cl = self.get_class(classname) 1095 cl = self.get_class(classname)
1096 1096
1097 if not export_files and hasattr(cl, 'export_files'):
1098 sys.stdout.write('Exporting %s WITHOUT the files\r\n'%
1099 classname)
1100
1097 f = open(os.path.join(dir, classname+'.csv'), 'wb') 1101 f = open(os.path.join(dir, classname+'.csv'), 'wb')
1098 writer = csv.writer(f, colon_separated) 1102 writer = csv.writer(f, colon_separated)
1099 1103
1100 properties = cl.getprops() 1104 properties = cl.getprops()
1101 propnames = cl.export_propnames() 1105 propnames = cl.export_propnames()
1110 sys.stdout.flush() 1114 sys.stdout.flush()
1111 if self.verbose: 1115 if self.verbose:
1112 sys.stdout.write('Exporting %s - %s\r'%(classname, nodeid)) 1116 sys.stdout.write('Exporting %s - %s\r'%(classname, nodeid))
1113 sys.stdout.flush() 1117 sys.stdout.flush()
1114 writer.writerow(cl.export_list(propnames, nodeid)) 1118 writer.writerow(cl.export_list(propnames, nodeid))
1115 if hasattr(cl, 'export_files'): 1119 if export_files and hasattr(cl, 'export_files'):
1116 cl.export_files(dir, nodeid) 1120 cl.export_files(dir, nodeid)
1117 1121
1118 # close this file 1122 # close this file
1119 f.close() 1123 f.close()
1120 if self.verbose:
1121 sys.stdout.write("\nExporting Journal for %s\n" % classname)
1122 sys.stdout.flush()
1123 journals = csv.writer(jf, colon_separated)
1124 map(journals.writerow, cl.export_journals())
1125 jf.close()
1126 return 0
1127
1128 def do_exporttables(self, args):
1129 ""'''Usage: exporttables [[-]class[,class]] export_dir
1130 Export the database to colon-separated-value files, excluding the
1131 files below $TRACKER_HOME/db/files/ (which can be archived separately).
1132 To include the files, use the export command.
1133
1134 Optionally limit the export to just the named classes
1135 or exclude the named classes, if the 1st argument starts with '-'.
1136
1137 This action exports the current data from the database into
1138 colon-separated-value files that are placed in the nominated
1139 destination directory.
1140 '''
1141 # grab the directory to export to
1142 if len(args) < 1:
1143 raise UsageError, _('Not enough arguments supplied')
1144
1145 dir = args[-1]
1146
1147 # get the list of classes to export
1148 if len(args) == 2:
1149 if args[0].startswith('-'):
1150 classes = [ c for c in self.db.classes.keys()
1151 if not c in args[0][1:].split(',') ]
1152 else:
1153 classes = args[0].split(',')
1154 else:
1155 classes = self.db.classes.keys()
1156
1157 class colon_separated(csv.excel):
1158 delimiter = ':'
1159
1160 # make sure target dir exists
1161 if not os.path.exists(dir):
1162 os.makedirs(dir)
1163
1164 # do all the classes specified
1165 for classname in classes:
1166 cl = self.get_class(classname)
1167 if hasattr(cl, 'export_files'):
1168 sys.stdout.write('Exporting %s WITHOUT the files\r\n' % classname)
1169
1170 f = open(os.path.join(dir, classname+'.csv'), 'wb')
1171 writer = csv.writer(f, colon_separated)
1172
1173 properties = cl.getprops()
1174 propnames = cl.export_propnames()
1175 fields = propnames[:]
1176 fields.append('is retired')
1177 writer.writerow(fields)
1178
1179 # all nodes for this class
1180 for nodeid in cl.getnodeids():
1181 if self.verbose:
1182 sys.stdout.write('Exporting %s - %s\r'%(classname, nodeid))
1183 sys.stdout.flush()
1184 writer.writerow(cl.export_list(propnames, nodeid))
1185
1186 # close this file
1187 f.close()
1188
1189 # export the journals
1190 jf = open(os.path.join(dir, classname+'-journals.csv'), 'wb')
1191 if self.verbose:
1192 sys.stdout.write("\nExporting Journal for %s\n" % classname)
1193 sys.stdout.flush()
1194 1124
1195 # export the journals 1125 # export the journals
1196 jf = open(os.path.join(dir, classname+'-journals.csv'), 'wb') 1126 jf = open(os.path.join(dir, classname+'-journals.csv'), 'wb')
1197 if self.verbose: 1127 if self.verbose:
1198 sys.stdout.write("\nExporting Journal for %s\n" % classname) 1128 sys.stdout.write("\nExporting Journal for %s\n" % classname)
1199 sys.stdout.flush() 1129 sys.stdout.flush()
1200 journals = csv.writer(jf, colon_separated) 1130 journals = csv.writer(jf, colon_separated)
1201 map(journals.writerow, cl.export_journals()) 1131 map(journals.writerow, cl.export_journals())
1202 jf.close() 1132 jf.close()
1203 return 0 1133 return 0
1134
1135 def do_exporttables(self, args):
1136 ""'''Usage: exporttables [[-]class[,class]] export_dir
1137 Export the database to colon-separated-value files, excluding the
1138 files below $TRACKER_HOME/db/files/ (which can be archived separately).
1139 To include the files, use the export command.
1140
1141 Optionally limit the export to just the named classes
1142 or exclude the named classes, if the 1st argument starts with '-'.
1143
1144 This action exports the current data from the database into
1145 colon-separated-value files that are placed in the nominated
1146 destination directory.
1147 '''
1148 return self.do_export(args, export_files=False)
1204 1149
1205 def do_import(self, args): 1150 def do_import(self, args):
1206 ""'''Usage: import import_dir 1151 ""'''Usage: import import_dir
1207 Import a database from the directory containing CSV files, 1152 Import a database from the directory containing CSV files,
1208 two per class to import. 1153 two per class to import.
1255 if self.verbose: 1200 if self.verbose:
1256 sys.stdout.write('Importing %s - %s\r'%(classname, n)) 1201 sys.stdout.write('Importing %s - %s\r'%(classname, n))
1257 sys.stdout.flush() 1202 sys.stdout.flush()
1258 1203
1259 # do the import and figure the current highest nodeid 1204 # do the import and figure the current highest nodeid
1260 nodeid = int(cl.import_list(file_props, r)) 1205 nodeid = cl.import_list(file_props, r)
1261 if hasattr(cl, 'import_files'): 1206 if hasattr(cl, 'import_files'):
1262 cl.import_files(dir, nodeid) 1207 cl.import_files(dir, nodeid)
1263 maxid = max(maxid, nodeid) 1208 maxid = max(maxid, int(nodeid))
1264 print 1209 print
1265 f.close() 1210 f.close()
1266 1211
1267 # import the journals 1212 # import the journals
1268 f = open(os.path.join(args[0], classname + '-journals.csv'), 'r') 1213 f = open(os.path.join(args[0], classname + '-journals.csv'), 'r')

Roundup Issue Tracker: http://roundup-tracker.org/