Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tarfile, StringIO, re, json, os
- class dirfs(object):
- """
- author:Martin Robinson
- mail:nornibor.nitram@gmail.com
- minimal use case of directory as storage
- will list, open and add file
- """
- def __init__(self, dirname):
- self.dirname = dirname
- def ls(self):
- """
- will list file available
- """
- for name in os.listdir(self.dirname):
- if os.path.isfile(os.path.join(self.dirname, name)):
- yield name
- def open(self,name):
- """
- will try to open a file inside tar file and return content as string
- """
- with open(name) as file:
- return file.read()
- def add(self,name,data):
- """
- will try to add a file in the tar file
- """
- with open(name,'wb') as file:
- file.write(data)
- class targzfs(object):
- """
- author:Martin Robinson
- mail:nornibor.nitram@gmail.com
- minimal use case of tar file as storage
- will list, open and add file
- """
- def __init__(self, filename):
- self.compression = 'gz'
- self.readmode = 'r'+self.compression
- self.writemode = 'w'+self.compression
- tarfile.open(self.filename, self.writemode).close()
- def ls(self):
- """
- will list file available
- """
- with tarfile.open(self.filename, self.readmode) as tar:
- for name in tar.getnames():
- yield name
- def open(self,name):
- """
- will try to open a file inside tar file and return content as string
- """
- with tarfile.open(self.filename, self.readmode) as tar:
- return tar.extractfile(name).read()
- def add(self,name,data):
- """
- will try to add a file in the tar file
- """
- try:
- with tarfile.open(self.filename, self.writemode) as tar:
- info = tarfile.TarInfo(name)
- info.size = len(data)
- tar.addfile(info,StringIO.StringIO(data))
- return True
- except IOError:
- return False
- class jsonForFs(object):
- """
- author:Martin Robinson
- mail:nornibor.nitram@gmail.com
- forwarding decorator
- will encode or decode every object in tar file as json
- """
- def __init__(self,fs=dirfs(os.getcwd())):
- self.fs = fs
- def ls(self):
- """
- list files
- """
- return self.fs.ls()
- def open(self,name):
- """
- open and decode json file
- """
- if not re.match('^.+\.json$',name):
- name += '.json'
- return json.JSONDecoder().decode(self.fs.open(name))
- def add(self,name,data):
- """
- encode and add json file
- """
- if not re.match('^.+\.json$',name):
- name += '.json'
- return self.fs.add(name, json.JSONEncoder().encode(data))
- if __name__ == '__main__':
- test = dirfs(os.getcwd())
- print 'first'
- for name in test.ls():
- print name
- test.add('hello',str(['hello','world']))
- print 'then'
- for name in test.ls():
- if name=='hello':
- print name
- print test.open(name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement