fopen_ld.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. def cache(self,cache, path):
  2. self.fc = None
  3. if not use.cache:return
  4. if path.count(':/'): return
  5. if cache:
  6. mkdir(cache%'')
  7. else:
  8. cache = '%s'
  9. try:
  10. os.stat( path)
  11. print('hit',end=' ')
  12. return path
  13. except:
  14. self.fc = path
  15. print('miss',end=' ')
  16. print(path)
  17. def store(self):
  18. if self.valid:
  19. print('cacheget',self.fc,end=' ')
  20. try:
  21. with open(self.fc,'wb') as f:
  22. f.write( self.u.read() )
  23. print('ok')
  24. except Exception as e:
  25. print('fail',e)
  26. return self
  27. def __init__(self, path, mode="rb"):
  28. hit=''
  29. self.valid = False
  30. if path.count('/')==2:
  31. hit = '/'.join( ('',path.split('/')[1] , '%s', ) )
  32. hit = self.cache(hit, path)
  33. if hit: return
  34. if not path.count(':/'):
  35. path = ''.join( (use.srv , path, ) )
  36. print('GET',path)
  37. try:
  38. self.u = await_(klib.urlopen(path))
  39. self.valid = True
  40. except Exception as e:
  41. self.u = str(e)
  42. if e.args[0]==2:
  43. pdb('404',path)
  44. return
  45. pdb('50:',e)