mirror of
				https://github.com/Thor77/TeamspeakStats.git
				synced 2025-11-03 23:32:45 -05:00 
			
		
		
		
	Add funcdocs to Cache-class and methods
This commit is contained in:
		
							parent
							
								
									74444edd55
								
							
						
					
					
						commit
						7b3ecb039b
					
				
					 1 changed files with 48 additions and 0 deletions
				
			
		| 
						 | 
					@ -10,18 +10,48 @@ CachedLog = namedtuple('CachedLog', ['path', 'hash', 'events'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _calculate_hash(path):
 | 
					def _calculate_hash(path):
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
 | 
					    Calculate hash of a file using sha256
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    :param path: path to file
 | 
				
			||||||
 | 
					    :type path: str
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    :return: hash of the file
 | 
				
			||||||
 | 
					    :rtype: str
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
    with open(path, 'rb') as f:
 | 
					    with open(path, 'rb') as f:
 | 
				
			||||||
        return hashlib.sha256(f.read()).hexdigest()
 | 
					        return hashlib.sha256(f.read()).hexdigest()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Cache(MutableMapping):
 | 
					class Cache(MutableMapping):
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
 | 
					    Cache for parsed events from a logfile
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
    def __init__(self, path, data={}):
 | 
					    def __init__(self, path, data={}):
 | 
				
			||||||
 | 
					        '''
 | 
				
			||||||
 | 
					        Initialize Cache at `path`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        :param path: path to cache file
 | 
				
			||||||
 | 
					        :param data: initial cached data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        :type path: str
 | 
				
			||||||
 | 
					        :type data: dict
 | 
				
			||||||
 | 
					        '''
 | 
				
			||||||
        self.path = path
 | 
					        self.path = path
 | 
				
			||||||
        self.store = data
 | 
					        self.store = data
 | 
				
			||||||
        self.initial_store_version = self.store.setdefault('version', 0)
 | 
					        self.initial_store_version = self.store.setdefault('version', 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def read(cls, path):
 | 
					    def read(cls, path):
 | 
				
			||||||
 | 
					        '''
 | 
				
			||||||
 | 
					        Create a new Cache-instance with data read from `path`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        :param path: path to cache file
 | 
				
			||||||
 | 
					        :type path: str
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        :return: Cache-instance with data from `path`
 | 
				
			||||||
 | 
					        :rtype: tsstats.cache.Cache
 | 
				
			||||||
 | 
					        '''
 | 
				
			||||||
        data = {}
 | 
					        data = {}
 | 
				
			||||||
        if os.path.exists(path):
 | 
					        if os.path.exists(path):
 | 
				
			||||||
            logger.debug('Reading cache from %s', path)
 | 
					            logger.debug('Reading cache from %s', path)
 | 
				
			||||||
| 
						 | 
					@ -33,6 +63,15 @@ class Cache(MutableMapping):
 | 
				
			||||||
        return cls(path, data)
 | 
					        return cls(path, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def write(self, path=None):
 | 
					    def write(self, path=None):
 | 
				
			||||||
 | 
					        '''
 | 
				
			||||||
 | 
					        Dump cache to `path`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        but only if there were any changes, if not, it will silently skip
 | 
				
			||||||
 | 
					        writing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        :param path: path to cache file
 | 
				
			||||||
 | 
					        :type path: str
 | 
				
			||||||
 | 
					        '''
 | 
				
			||||||
        if not path:
 | 
					        if not path:
 | 
				
			||||||
            path = self.path
 | 
					            path = self.path
 | 
				
			||||||
        if self.initial_store_version == self.store['version']:
 | 
					        if self.initial_store_version == self.store['version']:
 | 
				
			||||||
| 
						 | 
					@ -43,6 +82,15 @@ class Cache(MutableMapping):
 | 
				
			||||||
            pickle.dump(self.store, f)
 | 
					            pickle.dump(self.store, f)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def needs_parsing(self, path):
 | 
					    def needs_parsing(self, path):
 | 
				
			||||||
 | 
					        '''
 | 
				
			||||||
 | 
					        Determine if a logfile needs parsing (=> is not cached or was updated)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        :param path: path to logfile
 | 
				
			||||||
 | 
					        :type path: str
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        :return: if parsing is necessary for logfile at `path`
 | 
				
			||||||
 | 
					        :rtype: bool
 | 
				
			||||||
 | 
					        '''
 | 
				
			||||||
        if path not in self.store:
 | 
					        if path not in self.store:
 | 
				
			||||||
            return True
 | 
					            return True
 | 
				
			||||||
        return _calculate_hash(path) != self.store[path].hash
 | 
					        return _calculate_hash(path) != self.store[path].hash
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue