<s> from django . core . files . storage import Storage <EOL> from django . core . files . base import ContentFile <EOL> class PathDoesNotExist ( Exception ) : <EOL> pass <EOL> class InMemoryNode ( object ) : <EOL> """<STR_LIT>""" <EOL> parent = None <EOL> def add_child ( self , name , child ) : <EOL> child . parent = self <EOL> self . children [ name ] = child <EOL> class InMemoryFile ( InMemoryNode ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , contents = '<STR_LIT>' , parent = None ) : <EOL> self . contents = contents <EOL> self . parent = parent <EOL> class InMemoryDir ( InMemoryNode ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dirs = None , files = None , parent = None ) : <EOL> self . children = { } <EOL> self . parent = parent <EOL> def resolve ( self , path , create = False ) : <EOL> path_bits = path . strip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> current = path_bits [ <NUM_LIT:0> ] <EOL> rest = path_bits [ <NUM_LIT:1> ] if len ( path_bits ) > <NUM_LIT:1> else None <EOL> if not rest : <EOL> if current == '<STR_LIT>' : <EOL> return self <EOL> if current in self . children . keys ( ) : <EOL> return self . children [ current ] <EOL> if not create : <EOL> raise PathDoesNotExist ( ) <EOL> node = InMemoryFile ( ) <EOL> self . add_child ( current , node ) <EOL> return node <EOL> if current in self . children . keys ( ) : <EOL> return self . children [ current ] . resolve ( rest , create = create ) <EOL> if not create : <EOL> raise PathDoesNotExist ( ) <EOL> node = InMemoryDir ( ) <EOL> self . add_child ( current , node ) <EOL> return self . children [ current ] . resolve ( rest , create ) <EOL> def ls ( self , path = '<STR_LIT>' ) : <EOL> return self . resolve ( path ) . children . keys ( ) <EOL> def listdir ( self , dir ) : <EOL> nodes = tuple ( self . resolve ( dir ) . children . iteritems ( ) ) <EOL> dirs = [ k for ( k , v ) in nodes if isinstance ( v , InMemoryDir ) ] <EOL> files = [ k for ( k , v ) in nodes if isinstance ( v , InMemoryFile ) ] <EOL> return [ dirs , files ] <EOL> def delete ( self , path ) : <EOL> node = self . resolve ( path ) <EOL> for name , child in node . parent . children . iteritems ( ) : <EOL> if child is node : <EOL> del node . parent . children [ name ] <EOL> break <EOL> def exists ( self , name ) : <EOL> try : <EOL> self . resolve ( name ) <EOL> except PathDoesNotExist : <EOL> return False <EOL> else : <EOL> return True <EOL> def size ( self , name ) : <EOL> return len ( self . resolve ( name ) . contents ) <EOL> def open ( self , path ) : <EOL> return ContentFile ( self . resolve ( path , create = True ) . contents ) <EOL> def save ( self , path , content ) : <EOL> file = self . resolve ( path , create = True ) <EOL> file . contents = content <EOL> return path <EOL> class InMemoryStorage ( Storage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filesystem = None ) : <EOL> self . filesystem = filesystem or InMemoryDir ( ) <EOL> def listdir ( self , dir ) : <EOL> return self . filesystem . listdir ( dir ) <EOL> def delete ( self , path ) : <EOL> return self . filesystem . delete ( path ) <EOL> def exists ( self , name ) : <EOL> return self . filesystem . exists ( name ) <EOL> def size ( self , name ) : <EOL> return self . filesystem . size ( name ) <EOL> def _open ( self , name , mode = None ) : <EOL> return self . filesystem . open ( name ) <EOL> def _save ( self , name , content ) : <EOL> return self . filesystem . save ( name , content . read ( ) ) </s>
