예제 #1
0
    def updateDict(self, propDict):
        """
		このオブジェクトのpropertyをdictで更新する。
		dictのキーはASCII文字列でなければならない。
		更新されたpropertyのdictを返す。
		"""

        filePath = os.path.abspath(
            os.path.normpath(self._getPropertyFilePath()))

        #全てのkeyがASCII文字列であるかチェック。
        for key in propDict.keys():
            if not isinstance(key, basestring):
                msg = 'Bug found. Tried to update a property file %s with non-string key "%s".' % (
                    filePath, repr(key))
                logging.error(msg)
                raise MyException(msg)

        #propertyファイル更新。
        with FileLock(filePath):  #propertyファイルのリードとライトをアトミックに行う。
            currentDict = self.__updateDict(propDict)

        #キャッシュ更新。
        self.__cache.set(filePath, currentDict)

        return currentDict
예제 #2
0
    def clear(self):
        """
		このオブジェクトのpropertyを削除する。
		親オブジェクトのpropertyは削除しないので継承された値は削除されない。
		"""
        filePath = os.path.abspath(
            os.path.normpath(self._getPropertyFilePath()))

        with FileLock(filePath):
            self.__clear(filePath)

        self.__cache.set(filePath, {})
예제 #3
0
    def remove(self, key):
        """
		このオブジェクトのkeyで指定されたpropertyを削除する。存在しなければ何もしない。
		キーはASCII文字列でなければならない。
		更新されたpropertyのdictを返す。
		"""
        filePath = os.path.abspath(
            os.path.normpath(self._getPropertyFilePath()))

        if not isinstance(key, basestring):
            msg = 'Bug found. Tried to remove a property from file %s with non-string key "%s".' % (
                filePath, repr(key))
            logging.error(msg)
            raise MyException(msg)

        #propertyファイル更新。
        with FileLock(filePath):  #propertyファイルのリードとライトをアトミックに行う。
            currentDict = self.__remove(key, filePath)

        #キャッシュ更新。
        self.__cache.set(filePath, currentDict)

        return currentDict
예제 #4
0
 def failMethod():
     with FileLock(self.__getFilePath(), 0):  #No wait.
         pass
예제 #5
0
 def testLockWaitInThread(self):
     import thread
     th = threading.Thread(target=self.__writeLockInThread, args=(0, 0.1))
     time.sleep(0.05)
     with FileLock(self.__getFilePath()):
         self.assertEqual(self.__readFile(), 'tako')
예제 #6
0
 def testLock(self):
     with FileLock(self.__getFilePath()):
         pass
예제 #7
0
 def __writeLockInThread(self, sleepFirst, sleepAfterWrite):
     time.sleep(sleepFirst)
     with FileLock(self.__getFilePath()):
         self.__writeFile('tako')
         time.sleep(sleepAfterWrite)
예제 #8
0
 def isLocked(self):
     return FileLock.isLocked(self._getPropertyFilePath())