Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import tarfile, StringIO, re, json, os
  2.  
  3. class dirfs(object):
  4. """
  5. author:Martin Robinson
  6. mail:nornibor.nitram@gmail.com
  7. minimal use case of directory as storage
  8. will list, open and add file
  9. """
  10. def __init__(self, dirname):
  11. self.dirname = dirname
  12.  
  13. def ls(self):
  14. """
  15. will list file available
  16. """
  17. for name in os.listdir(self.dirname):
  18. if os.path.isfile(os.path.join(self.dirname, name)):
  19. yield name
  20.  
  21. def open(self,name):
  22. """
  23. will try to open a file inside tar file and return content as string
  24. """
  25. with open(name) as file:
  26. return file.read()
  27.  
  28. def add(self,name,data):
  29. """
  30. will try to add a file in the tar file
  31. """
  32. with open(name,'wb') as file:
  33. file.write(data)
  34.  
  35. class targzfs(object):
  36. """
  37. author:Martin Robinson
  38. mail:nornibor.nitram@gmail.com
  39. minimal use case of tar file as storage
  40. will list, open and add file
  41. """
  42. def __init__(self, filename):
  43. self.compression = 'gz'
  44. self.readmode = 'r'+self.compression
  45. self.writemode = 'w'+self.compression
  46. tarfile.open(self.filename, self.writemode).close()
  47.  
  48. def ls(self):
  49. """
  50. will list file available
  51. """
  52. with tarfile.open(self.filename, self.readmode) as tar:
  53. for name in tar.getnames():
  54. yield name
  55.  
  56. def open(self,name):
  57. """
  58. will try to open a file inside tar file and return content as string
  59. """
  60. with tarfile.open(self.filename, self.readmode) as tar:
  61. return tar.extractfile(name).read()
  62.  
  63. def add(self,name,data):
  64. """
  65. will try to add a file in the tar file
  66. """
  67. try:
  68. with tarfile.open(self.filename, self.writemode) as tar:
  69. info = tarfile.TarInfo(name)
  70. info.size = len(data)
  71. tar.addfile(info,StringIO.StringIO(data))
  72. return True
  73. except IOError:
  74. return False
  75.  
  76. class jsonForFs(object):
  77. """
  78. author:Martin Robinson
  79. mail:nornibor.nitram@gmail.com
  80. forwarding decorator
  81. will encode or decode every object in tar file as json
  82. """
  83. def __init__(self,fs=dirfs(os.getcwd())):
  84. self.fs = fs
  85.  
  86. def ls(self):
  87. """
  88. list files
  89. """
  90. return self.fs.ls()
  91.  
  92. def open(self,name):
  93. """
  94. open and decode json file
  95. """
  96. if not re.match('^.+\.json$',name):
  97. name += '.json'
  98. return json.JSONDecoder().decode(self.fs.open(name))
  99.  
  100. def add(self,name,data):
  101. """
  102. encode and add json file
  103. """
  104. if not re.match('^.+\.json$',name):
  105. name += '.json'
  106. return self.fs.add(name, json.JSONEncoder().encode(data))
  107.  
  108. if __name__ == '__main__':
  109. test = dirfs(os.getcwd())
  110. print 'first'
  111. for name in test.ls():
  112. print name
  113. test.add('hello',str(['hello','world']))
  114. print 'then'
  115. for name in test.ls():
  116. if name=='hello':
  117. print name
  118. print test.open(name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement