NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

imageCacheSvc.py (2804B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\control\browser\imageCacheSvc.py
      2 import os.path
      3 import sys
      4 import blue
      5 from carbon.common.script.sys.service import Service
      6 from carbonui.util.settings import SettingSection
      7 
      8 class ImageCache(Service):
      9     __guid__ = 'svc.imageCache'
     10 
     11     def Run(self, *etc):
     12         super(ImageCache, self).Run(*etc)
     13         self.imgindex = None
     14         paths = [blue.paths.ResolvePath(u'cache:/'), blue.paths.ResolvePathForWriting(u'cache:/Browser'), blue.paths.ResolvePathForWriting(u'cache:/Browser/Img')]
     15         for path in paths:
     16             try:
     17                 os.makedirs(path)
     18             except OSError as e:
     19                 sys.exc_clear()
     20 
     21         self.Load()
     22 
     23     def Stop(self, *etc):
     24         super(ImageCache, self).Stop(*etc)
     25         self.Save()
     26 
     27     def GetFromCache(self, cacheID):
     28         if self.imgindex is None:
     29             self.Load()
     30         f = self.imgindex.img.Get(cacheID, None)
     31         if f:
     32             if os.path.exists(f[0].replace('cache:/', blue.paths.ResolvePath(u'cache:/'))):
     33                 return f
     34             self.ClearCache(cacheID)
     35 
     36     def Cache(self, cacheID, cacheData, fromWhere = None):
     37         if not self.imgindex:
     38             self.Load()
     39         self.ClearCache(cacheID)
     40         self.imgindex.img.Set(cacheID, cacheData)
     41 
     42     def ClearCache(self, cacheID):
     43         if not self.imgindex:
     44             self.Load()
     45         self.imgindex.img.Delete(cacheID)
     46 
     47     def InvalidateImage(self, url):
     48         self.CheckForIndexFile()
     49         self.ClearCache(url)
     50 
     51     def Load(self):
     52         if self.imgindex:
     53             self.imgindex.Unload()
     54             self.imgindex = None
     55         self.imgindex = SettingSection('cache', blue.paths.ResolvePathForWriting(u'cache:/Browser/Img/index.dat'), 62, service=self)
     56         self.imgindex.CreateGroup('img')
     57         cachefiles = [ self.imgindex.img.Get(each, None)[0].replace('cache:/', blue.paths.ResolvePath(u'cache:/')) for each in self.imgindex.img.keys() ]
     58         for f in os.listdir(blue.paths.ResolvePathForWriting(u'cache:/Browser/Img')):
     59             if f == 'index.dat':
     60                 continue
     61             f1 = blue.paths.ResolvePathForWriting(u'cache:/Browser/Img') + f
     62             if f1 not in cachefiles:
     63                 try:
     64                     if os.path.exists(f1):
     65                         os.unlink(f1)
     66                 except:
     67                     sys.exc_clear()
     68 
     69     def GetImgIndex(self):
     70         return self.imgindex
     71 
     72     def Save(self):
     73         self.imgindex.Unload()
     74         self.imgindex = None
     75 
     76     def CheckForIndexFile(self):
     77         file = blue.ResFile()
     78         if not file.Open(blue.paths.ResolvePathForWriting(u'cache:/Browser/Img/index.dat')):
     79             self.Load()
     80         else:
     81             file.Close()