@contextlib.contextmanager
def fpopen(*args, **kwargs):
 uid = kwargs.pop('uid', (-1))
 gid = kwargs.pop('gid', (-1))
 mode = kwargs.pop('mode', None)
 with fopen(*args, **kwargs) as fhandle:
  path = args[0]
  d_stat = os.stat(path)
  if hasattr(os, 'chown'):
   if (((d_stat.st_uid != uid) or (d_stat.st_gid != gid)) and [i for i in (uid, gid) if (i != (-1))]):
    os.chown(path, uid, gid)
  if (mode is not None):
   mode_part = S_IMODE(d_stat.st_mode)
   if (mode_part != mode):
    os.chmod(path, ((d_stat.st_mode ^ mode_part) | mode))
  (yield fhandle)

@ contextlib . contextmanager def fpopen ( * args , ** kwargs ) : uid = kwargs . pop ( 'uid' , ( - 1 ) ) gid = kwargs . pop ( 'gid' , ( - 1 ) ) mode = kwargs . pop ( 'mode' , None ) with fopen ( * args , ** kwargs ) as fhandle : path = args [ 0 ] d_stat = os . stat ( path ) if hasattr ( os , 'chown' ) : if ( ( ( d_stat . st_uid != uid ) or ( d_stat . st_gid != gid ) ) and [ i for i in ( uid , gid ) if ( i != ( - 1 ) ) ] ) : os . chown ( path , uid , gid ) if ( mode is not None ) : mode_part = S_IMODE ( d_stat . st_mode ) if ( mode_part != mode ) : os . chmod ( path , ( ( d_stat . st_mode ^ mode_part ) | mode ) ) ( yield fhandle )
shortcut for fopen with extra uid .