<s> from __future__ import unicode_literals <EOL> import logging <EOL> import os <EOL> import tempfile <EOL> import types <EOL> from django . conf import settings <EOL> from django . utils import formats <EOL> from django . utils . datastructures import MultiValueDict <EOL> from django . utils . encoding import force_text <EOL> from django . utils . http import urlquote as django_urlquote <EOL> from django . utils . http import urlencode as django_urlencode <EOL> logger = logging . getLogger ( __name__ ) <EOL> def copyfile ( source , destination , buffer_size = <NUM_LIT> * <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> source_descriptor = get_descriptor ( source ) <EOL> destination_descriptor = get_descriptor ( destination , read = False ) <EOL> while True : <EOL> copy_buffer = source_descriptor . read ( buffer_size ) <EOL> if copy_buffer : <EOL> destination_descriptor . write ( copy_buffer ) <EOL> else : <EOL> break <EOL> source_descriptor . close ( ) <EOL> destination_descriptor . close ( ) <EOL> def encapsulate ( function ) : <EOL> return lambda : function <EOL> def fs_cleanup ( filename , suppress_exceptions = True ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> os . remove ( filename ) <EOL> except OSError : <EOL> if suppress_exceptions : <EOL> pass <EOL> else : <EOL> raise <EOL> def get_descriptor ( file_input , read = True ) : <EOL> try : <EOL> file_input . seek ( <NUM_LIT:0> ) <EOL> except AttributeError : <EOL> if read : <EOL> return open ( file_input , '<STR_LIT:rb>' ) <EOL> else : <EOL> return open ( file_input , '<STR_LIT:wb>' ) <EOL> else : <EOL> return file_input <EOL> def render_date_object ( date_time_object ) : <EOL> return force_text ( formats . localize ( date_time_object , use_l10n = True ) ) <EOL> def return_attrib ( obj , attrib , arguments = None ) : <EOL> try : <EOL> if isinstance ( attrib , types . FunctionType ) : <EOL> return attrib ( obj ) <EOL> elif isinstance ( <EOL> obj , types . DictType <EOL> ) or isinstance ( obj , types . DictionaryType ) : <EOL> return obj [ attrib ] <EOL> else : <EOL> result = reduce ( getattr , attrib . split ( '<STR_LIT:.>' ) , obj ) <EOL> if isinstance ( result , types . MethodType ) : <EOL> if arguments : <EOL> return result ( ** arguments ) <EOL> else : <EOL> return result ( ) <EOL> else : <EOL> return result <EOL> except Exception as exception : <EOL> if settings . DEBUG : <EOL> return '<STR_LIT>' % ( attrib , exception ) <EOL> else : <EOL> return unicode ( exception ) <EOL> def urlquote ( link = None , get = None ) : <EOL> """<STR_LIT>""" <EOL> if get is None : <EOL> get = [ ] <EOL> assert link or get <EOL> if isinstance ( link , dict ) : <EOL> assert not get , get <EOL> get = link <EOL> link = '<STR_LIT>' <EOL> assert isinstance ( get , dict ) , '<STR_LIT>' % type ( get ) <EOL> if get : <EOL> if isinstance ( get , MultiValueDict ) : <EOL> get = get . lists ( ) <EOL> if link : <EOL> link = '<STR_LIT>' % django_urlquote ( link ) <EOL> return '<STR_LIT>' % ( link , django_urlencode ( get , doseq = True ) ) <EOL> else : <EOL> return django_urlquote ( link ) <EOL> def validate_path ( path ) : <EOL> if not os . path . exists ( path ) : <EOL> try : <EOL> os . mkdir ( path ) <EOL> except Exception as exception : <EOL> logger . debug ( '<STR_LIT>' , exception ) <EOL> return False <EOL> try : <EOL> fd , test_filepath = tempfile . mkstemp ( dir = path ) <EOL> os . close ( fd ) <EOL> os . unlink ( test_filepath ) <EOL> except Exception as exception : <EOL> logger . debug ( '<STR_LIT>' , exception ) <EOL> return False <EOL> return True </s>
