Back to index

enigmail  1.4.3
util.py
Go to the documentation of this file.
00001 import os
00002 
00003 class MakeError(Exception):
00004     def __init__(self, message, loc=None):
00005         self.msg = message
00006         self.loc = loc
00007 
00008     def __str__(self):
00009         locstr = ''
00010         if self.loc is not None:
00011             locstr = "%s:" % (self.loc,)
00012 
00013         return "%s%s" % (locstr, self.msg)
00014 
00015 def normaljoin(path, suffix):
00016     """
00017     Combine the given path with the suffix, and normalize if necessary to shrink the path to avoid hitting path length limits
00018     """
00019     result = os.path.join(path, suffix)
00020     if len(result) > 255:
00021         result = os.path.normpath(result)
00022     return result
00023 
00024 def joiniter(fd, it):
00025     """
00026     Given an iterator that returns strings, write the words with a space in between each.
00027     """
00028     
00029     it = iter(it)
00030     for i in it:
00031         fd.write(i)
00032         break
00033 
00034     for i in it:
00035         fd.write(' ')
00036         fd.write(i)
00037 
00038 def checkmsyscompat():
00039     """For msys compatibility on windows, honor the SHELL environment variable,
00040     and if $MSYSTEM == MINGW32, run commands through $SHELL -c instead of
00041     letting Python use the system shell."""
00042     if 'SHELL' in os.environ:
00043         shell = os.environ['SHELL']
00044     elif 'MOZILLABUILD' in os.environ:
00045         shell = os.environ['MOZILLABUILD'] + '/msys/bin/sh.exe'
00046     elif 'COMSPEC' in os.environ:
00047         shell = os.environ['COMSPEC']
00048     else:
00049         raise DataError("Can't find a suitable shell!")
00050 
00051     msys = False
00052     if 'MSYSTEM' in os.environ and os.environ['MSYSTEM'] == 'MINGW32':
00053         msys = True
00054         if not shell.lower().endswith(".exe"):
00055             shell += ".exe"
00056     return (shell, msys)
00057 
00058 if hasattr(str, 'partition'):
00059     def strpartition(str, token):
00060         return str.partition(token)
00061 
00062     def strrpartition(str, token):
00063         return str.rpartition(token)
00064 
00065 else:
00066     def strpartition(str, token):
00067         """Python 2.4 compatible str.partition"""
00068 
00069         offset = str.find(token)
00070         if offset == -1:
00071             return str, '', ''
00072 
00073         return str[:offset], token, str[offset + len(token):]
00074 
00075     def strrpartition(str, token):
00076         """Python 2.4 compatible str.rpartition"""
00077 
00078         offset = str.rfind(token)
00079         if offset == -1:
00080             return '', '', str
00081 
00082         return str[:offset], token, str[offset + len(token):]
00083 
00084 try:
00085     from __builtin__ import any
00086 except ImportError:
00087     def any(it):
00088         for i in it:
00089             if i:
00090                 return True
00091         return False
00092 
00093 class _MostUsedItem(object):
00094     __slots__ = ('key', 'o', 'count')
00095 
00096     def __init__(self, key):
00097         self.key = key
00098         self.o = None
00099         self.count = 1
00100 
00101     def __repr__(self):
00102         return "MostUsedItem(key=%r, count=%i, o=%r)" % (self.key, self.count, self.o)
00103 
00104 class MostUsedCache(object):
00105     def __init__(self, capacity, creationfunc, verifyfunc):
00106         self.capacity = capacity
00107         self.cfunc = creationfunc
00108         self.vfunc = verifyfunc
00109 
00110         self.d = {}
00111         self.active = [] # lazily sorted!
00112 
00113     def setactive(self, item):
00114         if item in self.active:
00115             return
00116 
00117         if len(self.active) == self.capacity:
00118             self.active.sort(key=lambda i: i.count)
00119             old = self.active.pop(0)
00120             old.o = None
00121             # print "Evicting %s" % old.key
00122 
00123         self.active.append(item)
00124 
00125     def get(self, key):
00126         item = self.d.get(key, None)
00127         if item is None:
00128             item = _MostUsedItem(key)
00129             self.d[key] = item
00130         else:
00131             item.count += 1
00132 
00133         if item.o is not None and self.vfunc(key, item.o):
00134             return item.o
00135 
00136         item.o = self.cfunc(key)
00137         self.setactive(item)
00138         return item.o
00139 
00140     def verify(self):
00141         for k, v in self.d.iteritems():
00142             if v.o:
00143                 assert v in self.active
00144             else:
00145                 assert v not in self.active
00146 
00147     def debugitems(self):
00148         l = [i.key for i in self.active]
00149         l.sort()
00150         return l