- Timestamp:
- 31 Oct 2013, 17:58:03 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py
r10676 r10677 18 18 """General helper functions for Kofa. 19 19 """ 20 import unicodecsv as csv # XXX: csv ops should move to dedicated module.20 import unicodecsv as csv # XXX: csv ops should move to dedicated module. 21 21 import datetime 22 22 import imghdr … … 43 43 BUFSIZE = 8 * 1024 44 44 45 45 46 def remove_file_or_directory(filepath): 46 47 """Remove a file or directory. … … 58 59 os.unlink(filepath) 59 60 return 61 60 62 61 63 def copy_filesystem_tree(src, dst, overwrite=False, del_old=False): … … 86 88 for item in os.listdir(src): 87 89 if item.startswith('.'): 88 continue # We do not copy hidden stuff...90 continue # We do not copy hidden stuff... 89 91 itemsrc = os.path.join(src, item) 90 92 itemdst = os.path.join(dst, item) … … 151 153 return html_code 152 154 155 153 156 class FactoryBase(grok.GlobalUtility): 154 157 """A factory for things. … … 226 229 227 230 """ 228 grok.baseclass() # Do not grok this class, do not register us.231 grok.baseclass() # Do not grok this class, do not register us. 229 232 grok.implements(IFactory) 230 233 # You can override any of the following attributes in derived … … 246 249 # Required by IFactory 247 250 return implementedBy(self.factory) 251 248 252 249 253 def ReST2HTML_w_warnings(source_string): … … 324 328 if warning_msgs: 325 329 # Render again, this time with no warnings inline... 326 fulldoc = 330 fulldoc = publish_string( 327 331 source_string, writer_name='html4css1', 328 332 settings_overrides={ … … 337 341 result = result.decode('utf-8') 338 342 return result, warning_msgs 343 339 344 340 345 def ReST2HTML(source_string): … … 384 389 return html 385 390 391 386 392 def attrs_to_fields(cls, omit=[]): 387 393 """Turn the attributes of a class into FieldProperty instances. … … 399 405 return cls 400 406 407 401 408 def get_current_principal(): 402 409 """Get the 'current' principal. … … 416 423 except NoInteraction: 417 424 return None 418 except IndexError: # No participations present425 except IndexError: # No participations present 419 426 return None 420 427 return principal 428 421 429 422 430 def cmp_files(file_descr1, file_descr2): … … 435 443 return True 436 444 445 437 446 def string_from_bytes(number): 438 447 """Turn a number into some textual representation. … … 455 464 if number < 1024: 456 465 return u'%s byte(s)' % (str(number),) 457 elif number < 1024 **2:466 elif number < 1024 ** 2: 458 467 return u'%s KB' % (number / 1024,) 459 elif number < 1024**3: 460 return u'%.2f MB' % (number / 1024**2,) 461 return u'%.2f GB' % (number / 1024**3,) 468 elif number < 1024 ** 3: 469 return u'%.2f MB' % (number / 1024 ** 2,) 470 return u'%.2f GB' % (number / 1024 ** 3,) 471 462 472 463 473 def file_size(file_like_obj): … … 480 490 if hasattr(file_like_obj, 'fileno'): 481 491 return os.fstat(file_like_obj.fileno())[6] 482 file_like_obj.seek(0, 2) # seek to last position in file492 file_like_obj.seek(0, 2) # seek to last position in file 483 493 return file_like_obj.tell() 494 484 495 485 496 def get_user_account(request): … … 490 501 account = authenticator.getAccount(principal_id) 491 502 return account 503 492 504 493 505 def iface_names(iface, omit=[], exclude_attribs=True, exclude_methods=True): … … 531 543 return result 532 544 545 533 546 def get_sorted_preferred(tuples_iterable, preferred_list): 534 547 """Get a list of tuples (<TITLE>,<TOKEN>) with values in … … 573 586 return tuple(result) 574 587 588 575 589 def now(tz=None): 576 590 """Get current datetime in timezone of `tz`. … … 581 595 """ 582 596 return to_timezone(datetime.datetime.utcnow(), tz=tz) 597 583 598 584 599 def to_timezone(dt, tz=None): … … 600 615 dt = pytz.utc.localize(dt) 601 616 return tz.normalize(dt.tzinfo.normalize(dt).astimezone(tz)) 617 602 618 603 619 def get_fileformat(path, bytestream=None): … … 633 649 return img_type 634 650 651 635 652 def check_pdf(bytestream, file): 636 653 """Tell whether a file or bytestream is a PDF file. … … 650 667 if check_pdf not in imghdr.tests: 651 668 imghdr.tests.append(check_pdf) 669 652 670 653 671 def merge_csv_files(path1, path2): … … 685 703 wp, tmp_path = tempfile.mkstemp() 686 704 writer = csv.DictWriter(os.fdopen(wp, 'wb'), fieldnames) 687 writer.writerow(dict((x, x) for x in fieldnames))# header705 writer.writerow(dict((x, x) for x in fieldnames)) # header 688 706 for row in reader1: 689 707 writer.writerow(row) … … 691 709 writer.writerow(row) 692 710 return tmp_path 711 693 712 694 713 def product(sequence, start=1): … … 703 722 result *= item 704 723 return result 724 705 725 706 726 class NullHandler(logging.Handler):
Note: See TracChangeset for help on using the changeset viewer.