Back to index

plone3  3.1.7
Public Member Functions | Private Attributes
PlacelessTranslationService.GettextMessageCatalog.MoFileCache Class Reference

List of all members.

Public Member Functions

def __init__
def storeMoFile
def retrieveMoFile
def getPath
def isCacheHit
def compilePo
def cachedPoFile
def purgeCache

Private Attributes

 _path

Detailed Description

Cache for mo files

Definition at line 449 of file GettextMessageCatalog.py.


Constructor & Destructor Documentation

Definition at line 453 of file GettextMessageCatalog.py.

00453 
00454     def __init__(self, path):
00455         if not os.path.isdir(path):
00456             try:
00457                 os.makedirs(path)
00458             except (IOError, OSError):
00459                 log("No permission to create directory %s" % path, logging.INFO)
00460                 path = None
00461         self._path = path
        

Member Function Documentation

Cache a po file (public api)

Returns a file handler on a mo file

Definition at line 530 of file GettextMessageCatalog.py.

00530 
00531     def cachedPoFile(self, catalog):
00532         """Cache a po file (public api)
00533         
00534         Returns a file handler on a mo file
00535         """
00536         path = self._path
00537         if path is None:
00538             return None, self.compilePo(catalog)
00539         hit = self.isCacheHit(catalog)
00540         if hit:
00541             mof = self.retrieveMoFile(catalog)
00542             if mof is None:
00543                 mof = self.compilePo(catalog)
00544         else:
00545             mof = self.storeMoFile(catalog)
00546         return hit, mof
        

Here is the call graph for this function:

compile a po file to mo

returns a file handler

Definition at line 522 of file GettextMessageCatalog.py.

00522 
00523     def compilePo(self, catalog):
00524         """compile a po file to mo
00525         
00526         returns a file handler
00527         """
00528         mo = Msgfmt(catalog._readFile(), catalog.getId())
00529         return mo.getAsFile()
        

Here is the caller graph for this function:

Get the mo file path (cache path + file name)

Definition at line 491 of file GettextMessageCatalog.py.

00491 
00492     def getPath(self, catalog):
00493         """Get the mo file path (cache path + file name)
00494         """
00495         id = catalog.getId()
00496         if id.endswith('.po'):
00497             id = id[:-3]
00498         return os.path.join(self._path, '%s.mo' % id)
        

Here is the caller graph for this function:

Cache hit?

True: file exists and mod time is newer than mod time of the catalog
False: file exists but mod time is older
None: file doesn't exist

Definition at line 499 of file GettextMessageCatalog.py.

00499 
00500     def isCacheHit(self, catalog):
00501         """Cache hit?
00502         
00503         True: file exists and mod time is newer than mod time of the catalog
00504         False: file exists but mod time is older
00505         None: file doesn't exist
00506         """
00507         f = self.getPath(catalog)
00508         ca_mtime = catalog._getModTime()
00509         try:
00510             mo_mtime = os.stat(f)[ST_MTIME]
00511         except (IOError, OSError):
00512             mo_mtime = 0
00513         
00514         if mo_mtime == 0:
00515             return None
00516         elif ca_mtime == 0:
00517             return None
00518         elif mo_mtime > ca_mtime:
00519             return True
00520         else:
00521             return False
        

Here is the call graph for this function:

Here is the caller graph for this function:

Purge the cache and remove all compiled mo files

Definition at line 547 of file GettextMessageCatalog.py.

00547 
00548     def purgeCache(self):
00549         """Purge the cache and remove all compiled mo files
00550         """
00551         log("Purging mo file cache", logging.INFO)
00552         if not os.access(self._path, os.W_OK):
00553             log("No write permission on folder %s" % self._path, logging.INFO)
00554             return False
00555         pattern = os.path.join(self._path, '*.mo')
00556         for mo in glob.glob(pattern):
00557             if not os.access(mo, os.W_OK):
00558                 log("No write permission on file %s" % mo, logging.INFO)
00559                 continue
00560             try:
00561                 os.unlink(mo)
00562             except IOError:
00563                 log("Failed to unlink %s" % mo, logging.INFO)

Load a mo file file for a catalog from disk

Definition at line 480 of file GettextMessageCatalog.py.

00480 
00481     def retrieveMoFile(self, catalog):
00482         """Load a mo file file for a catalog from disk
00483         """
00484         f = self.getPath(catalog)
00485         if os.path.isfile(f):
00486             if os.access(f, os.R_OK):
00487                 return open(f, 'rb')
00488             else:
00489                 log("No permission to read file %s" % f, logging.INFO)
00490                 return None
        

Here is the call graph for this function:

Here is the caller graph for this function:

compile and save to mo file for catalog to disk

return value: mo file as file handler

Definition at line 462 of file GettextMessageCatalog.py.

00462 
00463     def storeMoFile(self, catalog):
00464         """compile and save to mo file for catalog to disk
00465         
00466         return value: mo file as file handler
00467         """
00468         f = self.getPath(catalog)
00469         mof = self.compilePo(catalog)
00470         moExists = os.path.exists(f)
00471         if (not moExists and os.access(self._path, os.W_OK)) \
00472           or (moExists and os.access(f, os.W_OK)):
00473             fd = open(f, 'wb')
00474             fd.write(mof.read()) # XXX efficient?
00475             fd.close()
00476         else:
00477             log("No permission to write file %s" % f, logging.INFO)
00478         mof.seek(0)
00479         return mof
        

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 460 of file GettextMessageCatalog.py.


The documentation for this class was generated from the following file: