Package flumotion :: Package common :: Module python
[hide private]

Source Code for Module flumotion.common.python

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_config -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3   
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  """ 
 19  forward compatibility with future python versions 
 20  """ 
 21   
 22  import sys 
 23   
 24  __version__ = "$Rev$" 
 25   
 26  # we're possibly redefining some builtins, so don't warn 
 27  __pychecker__ = 'no-shadowbuiltin' 
 28   
 29  # any() was introduced in 2.5 
 30  if sys.version_info[:2] < (2, 5): 
 31   
32 - def any(seq):
33 for item in seq: 34 if item: 35 return True 36 return False
37 else: 38 any = any 39 40 # all() was introduced in 2.5 41 if sys.version_info[:2] < (2, 5): 42
43 - def all(seq):
44 for item in seq: 45 if not item: 46 return False 47 return True
48 else: 49 all = all 50 51 52 # python2.4's os.makedirs() lacks EEXIST checks, so here's almost a 53 # literal copy from the python2.5's version of os module 54 if sys.version_info[:2] < (2, 5): 55 import os.path as path 56 from os import mkdir, curdir 57 from errno import EEXIST 58
59 - def makedirs(name, mode=0777):
60 head, tail = path.split(name) 61 if not tail: 62 head, tail = path.split(head) 63 if head and tail and not path.exists(head): 64 try: 65 makedirs(head, mode) 66 except OSError, e: 67 # be happy if someone already created the path 68 if e.errno != EEXIST: 69 raise 70 if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists 71 return 72 mkdir(name, mode)
73 else: 74 from os import makedirs 75 76 # python 2.6 deprecates sha and md5 modules in favor of hashlib 77 try: 78 _hashlib = __import__("hashlib") 79 except ImportError: 80 from md5 import md5 81 from sha import sha as sha1 82 else: 83 from hashlib import md5 as md5 84 from hashlib import sha1 as sha1 85 86 # python 2.6 deprecated the sets module in favor of a builtin set class 87 try: 88 set = set 89 except NameError: 90 from sets import Set as set 91 92 # itertools.chain.from_iterable appeared in python 2.6 93 if sys.version_info[:2] < (2, 6): 94
95 - def from_iterable(iterables):
96 for it in iterables: 97 for element in it: 98 yield element
99 else: 100 from itertools import chain 101 from_iterable = chain.from_iterable 102