def safe_filename(name, extension=None, digest=None, max_length=_MAX_FILENAME_LENGTH):
 if (os.path.basename(name) != name):
  raise ValueError(u'Name must be a filename, handed a path: {}'.format(name))
 ext = (extension or u'')
 filename = (name + ext)
 if (len(filename) <= max_length):
  return filename
 else:
  digest = (digest or hashlib.sha1())
  digest.update(name)
  safe_name = (digest.hexdigest() + ext)
  if (len(safe_name) > max_length):
   raise ValueError(u'Digest {} failed to produce a filename <= {} characters for {} - got {}'.format(digest, max_length, filename, safe_name))
  return safe_name

def safe_filename ( name , extension = None , digest = None , max_length = _MAX_FILENAME_LENGTH ) : if ( os . path . basename ( name ) != name ) : raise ValueError ( u'Name must be a filename, handed a path: {}' . format ( name ) ) ext = ( extension or u'' ) filename = ( name + ext ) if ( len ( filename ) <= max_length ) : return filename else : digest = ( digest or hashlib . sha1 ( ) ) digest . update ( name ) safe_name = ( digest . hexdigest ( ) + ext ) if ( len ( safe_name ) > max_length ) : raise ValueError ( u'Digest {} failed to produce a filename <= {} characters for {} - got {}' . format ( digest , max_length , filename , safe_name ) ) return safe_name
strip filesystem-unfriendly characters from a filename .