<s> import io <EOL> import os <EOL> from functools import partial <EOL> from clize import parser , errors <EOL> @ parser . value_converter ( name = '<STR_LIT>' ) <EOL> def datetime ( arg ) : <EOL> from dateutil import parser as dparser <EOL> return dparser . parse ( arg ) <EOL> class _FileOpener ( object ) : <EOL> def __init__ ( self , arg , kwargs ) : <EOL> self . arg = arg <EOL> self . kwargs = kwargs <EOL> self . validate_permissions ( ) <EOL> def validate_permissions ( self ) : <EOL> mode = self . kwargs . get ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> exists = os . access ( self . arg , os . F_OK ) <EOL> if not exists : <EOL> if '<STR_LIT:r>' in mode and '<STR_LIT:+>' not in mode : <EOL> raise errors . CliValueError ( <EOL> '<STR_LIT>' . format ( self . arg ) ) <EOL> else : <EOL> dirname = os . path . dirname ( self . arg ) <EOL> if os . access ( dirname , os . W_OK ) : <EOL> return <EOL> if not os . path . exists ( dirname ) : <EOL> raise errors . CliValueError ( <EOL> '<STR_LIT>' . format ( self . arg ) ) <EOL> elif os . access ( self . arg , os . W_OK ) : <EOL> return <EOL> raise errors . CliValueError ( <EOL> '<STR_LIT>' . format ( self . arg ) ) <EOL> def __enter__ ( self ) : <EOL> try : <EOL> self . f = io . open ( self . arg , ** self . kwargs ) <EOL> except IOError as exc : <EOL> raise _convert_ioerror ( self . arg , exc ) <EOL> return self . f <EOL> def __exit__ ( self , * exc_info ) : <EOL> self . f . close ( ) <EOL> def file ( ** kwargs ) : <EOL> return parser . value_converter ( <EOL> partial ( _FileOpener , kwargs = kwargs ) , <EOL> name = '<STR_LIT>' ) <EOL> def _convert_ioerror ( arg , exc ) : <EOL> nexc = errors . ArgumentError ( '<STR_LIT>' . format ( exc , arg ) ) <EOL> nexc . __cause__ = exc <EOL> return nexc </s>
