Index: trunk/tools/editor_trends/utils/utils.py |
— | — | @@ -13,7 +13,7 @@ |
14 | 14 | ''' |
15 | 15 | |
16 | 16 | __author__ = '''\n'''.join(['Diederik van Liere (dvanliere@gmail.com)', ]) |
17 | | -__author__email = 'dvanliere at gmail dot com' |
| 17 | +__email__ = 'dvanliere at gmail dot com' |
18 | 18 | __date__ = '2010-10-21' |
19 | 19 | __version__ = '0.1' |
20 | 20 | |
— | — | @@ -31,13 +31,14 @@ |
32 | 32 | import os |
33 | 33 | import ctypes |
34 | 34 | import time |
35 | | -#import subprocess |
36 | 35 | import sys |
37 | 36 | import shutil |
38 | 37 | sys.path.append('..') |
39 | 38 | |
| 39 | + |
40 | 40 | import configuration |
41 | 41 | settings = configuration.Settings() |
| 42 | + |
42 | 43 | import exceptions |
43 | 44 | import messages |
44 | 45 | |
— | — | @@ -113,12 +114,12 @@ |
114 | 115 | return messages |
115 | 116 | |
116 | 117 | |
117 | | -def readline(file): |
| 118 | +def readline(fh): |
118 | 119 | ''' |
119 | | - @file should be a file object |
| 120 | + @fh should be a file object |
120 | 121 | ''' |
121 | | - for line in file: |
122 | | - line = line.replace('\n', '') |
| 122 | + for line in fh: |
| 123 | + line = line.strip() |
123 | 124 | if line == '': |
124 | 125 | continue |
125 | 126 | else: |
— | — | @@ -240,9 +241,11 @@ |
241 | 242 | if type(data[key]) == type([]): |
242 | 243 | for d in data[key]: |
243 | 244 | 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])) |
247 | 250 | else: |
248 | 251 | fh.write('%s\n' % (data[key])) |
249 | 252 | elif format == 'wide': |
— | — | @@ -274,26 +277,30 @@ |
275 | 278 | |
276 | 279 | def construct_filename(name, extension): |
277 | 280 | if hasattr(name, '__call__'): |
278 | | - return name.func_name + extension |
| 281 | + return '%s%s' % (name.func_name, extension) |
279 | 282 | else: |
280 | 283 | return name |
281 | 284 | |
282 | 285 | |
283 | 286 | 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): |
286 | 289 | try: |
287 | 290 | path = os.path.join(location, filename) |
288 | 291 | os.remove(path) |
289 | 292 | except WindowsError, error: |
290 | 293 | 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 |
296 | 299 | |
| 300 | +def determine_filesize(location, filename): |
| 301 | + path = os.path.join(location, filename) |
| 302 | + return os.path.getsize(path) |
297 | 303 | |
| 304 | + |
298 | 305 | def check_file_exists(location, filename): |
299 | 306 | if hasattr(filename, '__call__'): |
300 | 307 | filename = construct_filename(filename, '.bin') |
— | — | @@ -341,11 +348,6 @@ |
342 | 349 | return obj |
343 | 350 | |
344 | 351 | |
345 | | -def clean_string(string): |
346 | | - string = string.replace('\n', '') |
347 | | - return string |
348 | | - |
349 | | - |
350 | 352 | def invert_dict(dictionary): |
351 | 353 | ''' |
352 | 354 | @dictionary is a simple dictionary containing simple values, ie. no lists, |
— | — | @@ -364,7 +366,7 @@ |
365 | 367 | ''' |
366 | 368 | d = {} |
367 | 369 | for line in read_data_from_csv(location, filename, encoding): |
368 | | - line = clean_string(line) |
| 370 | + line = line.strip() |
369 | 371 | line = line.split('\t') |
370 | 372 | key = line[0] |
371 | 373 | values = line[1:] |
— | — | @@ -411,9 +413,6 @@ |
412 | 414 | return files |
413 | 415 | |
414 | 416 | |
415 | | - |
416 | | - |
417 | | - |
418 | 417 | def merge_list(datalist): |
419 | 418 | merged = [] |
420 | 419 | for d in datalist: |
— | — | @@ -435,25 +434,12 @@ |
436 | 435 | return chunks |
437 | 436 | |
438 | 437 | |
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(): |
456 | 439 | tool = settings.determine_ziptool() |
457 | 440 | path = settings.detect_installed_program(tool) |
458 | 441 | location = os.path.join(settings.input_location, 'en', 'wiki') |
459 | 442 | source = 'enwiki-20100916-stub-meta-history.xml' |
460 | 443 | zip_archive(path, location, source) |
| 444 | + |
| 445 | +if __name__ == '__main__': |
| 446 | + debug() |