r80710 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80709‎ | r80710 | r80711 >
Date:20:46, 21 January 2011
Author:diederik
Status:deferred
Tags:
Comment:
Commit before refactoring code
Modified paths:
  • /trunk/tools/editor_trends/utils/utils.py (modified) (history)

Diff [purge]

Index: trunk/tools/editor_trends/utils/utils.py
@@ -13,7 +13,7 @@
1414 '''
1515
1616 __author__ = '''\n'''.join(['Diederik van Liere (dvanliere@gmail.com)', ])
17 -__author__email = 'dvanliere at gmail dot com'
 17+__email__ = 'dvanliere at gmail dot com'
1818 __date__ = '2010-10-21'
1919 __version__ = '0.1'
2020
@@ -31,13 +31,14 @@
3232 import os
3333 import ctypes
3434 import time
35 -#import subprocess
3635 import sys
3736 import shutil
3837 sys.path.append('..')
3938
 39+
4040 import configuration
4141 settings = configuration.Settings()
 42+
4243 import exceptions
4344 import messages
4445
@@ -113,12 +114,12 @@
114115 return messages
115116
116117
117 -def readline(file):
 118+def readline(fh):
118119 '''
119 - @file should be a file object
 120+ @fh should be a file object
120121 '''
121 - for line in file:
122 - line = line.replace('\n', '')
 122+ for line in fh:
 123+ line = line.strip()
123124 if line == '':
124125 continue
125126 else:
@@ -240,9 +241,11 @@
241242 if type(data[key]) == type([]):
242243 for d in data[key]:
243244 fh.write('%s\t%s\n' % (key, d))
244 - elif getattr(data[key], '__iter__', False):
245 - for d in data[key]:
246 - fh.write('%s\t%s\t%s\n' % (key, d, data[key][d]))
 245+ elif type(data[key]) == type({}):
 246+ write_dict_to_csv(data[key], fh, data[key].keys(), write_key=False, format=format)
 247+# elif getattr(data[key], '__iter__', False):
 248+# for d in data[key]:
 249+# fh.write('%s\t%s\t%s\n' % (key, d, data[key][d]))
247250 else:
248251 fh.write('%s\n' % (data[key]))
249252 elif format == 'wide':
@@ -274,26 +277,30 @@
275278
276279 def construct_filename(name, extension):
277280 if hasattr(name, '__call__'):
278 - return name.func_name + extension
 281+ return '%s%s' % (name.func_name, extension)
279282 else:
280283 return name
281284
282285
283286 def delete_file(location, filename, directory=False):
284 - if check_file_exists(location, filename):
285 - if not directory:
 287+ if not directory:
 288+ if check_file_exists(location, filename):
286289 try:
287290 path = os.path.join(location, filename)
288291 os.remove(path)
289292 except WindowsError, error:
290293 print error
291 - else:
292 - try:
293 - shutil.rmtree(location)
294 - except Exception, error:
295 - print error
 294+ else:
 295+ try:
 296+ shutil.rmtree(location)
 297+ except Exception, error:
 298+ print error
296299
 300+def determine_filesize(location, filename):
 301+ path = os.path.join(location, filename)
 302+ return os.path.getsize(path)
297303
 304+
298305 def check_file_exists(location, filename):
299306 if hasattr(filename, '__call__'):
300307 filename = construct_filename(filename, '.bin')
@@ -341,11 +348,6 @@
342349 return obj
343350
344351
345 -def clean_string(string):
346 - string = string.replace('\n', '')
347 - return string
348 -
349 -
350352 def invert_dict(dictionary):
351353 '''
352354 @dictionary is a simple dictionary containing simple values, ie. no lists,
@@ -364,7 +366,7 @@
365367 '''
366368 d = {}
367369 for line in read_data_from_csv(location, filename, encoding):
368 - line = clean_string(line)
 370+ line = line.strip()
369371 line = line.split('\t')
370372 key = line[0]
371373 values = line[1:]
@@ -411,9 +413,6 @@
412414 return files
413415
414416
415 -
416 -
417 -
418417 def merge_list(datalist):
419418 merged = []
420419 for d in datalist:
@@ -435,25 +434,12 @@
436435 return chunks
437436
438437
439 -# Progress bar related functions
440 -def update_progressbar(pbar, queue):
441 - '''
442 - Updates the progressbar by determining how much work is left in a queue
443 - '''
444 - x = pbar.maxval - messages.show(queue.qsize)
445 - '''
446 - Currently, calling the pbar.update function gives the following error:
447 - File "build\bdist.win32\egg\progressbar.py", line 352, in update
448 - self.fd.write(self._format_line() + '\r')
449 - ValueError: I/O operation on closed file
450 - Not sure how to fix this, that's why the line is commented.
451 - '''
452 - pbar.update(pbar.currval + x)
453 -
454 -
455 -if __name__ == '__main__':
 438+def debug():
456439 tool = settings.determine_ziptool()
457440 path = settings.detect_installed_program(tool)
458441 location = os.path.join(settings.input_location, 'en', 'wiki')
459442 source = 'enwiki-20100916-stub-meta-history.xml'
460443 zip_archive(path, location, source)
 444+
 445+if __name__ == '__main__':
 446+ debug()

Status & tagging log