Update RedFam class to rebuild the whole structure of RedFamPaser generated object

Move fam_hash() method from RedFamParser to RedFam
Define custom Error classes
This commit is contained in:
2015-09-15 21:11:06 +02:00
parent be0041804a
commit db5bb7401e
2 changed files with 92 additions and 29 deletions

View File

@@ -32,6 +32,7 @@ except ImportError:
import MySQLdb as mysqldb
from pywikibot import config
import jogobot

118
redfam.py
View File

@@ -32,6 +32,7 @@ from datetime import datetime
import pywikibot
import jogobot
from .mysqlred import MysqlRedFam
@@ -40,34 +41,72 @@ class RedFam:
Basic class for RedFams, containing the basic data structure
"""
def __init__( self, fam_hash=None, articlesList=None, red_page_id=None,
beginning=None, ending=None, status=0 ):
def __init__( self, articlesList, beginning, ending=None, red_page_id=None,
status=0, fam_hash=None, heading=None ):
"""
Generates a new RedFam object
@param articlesList list List of articles
@param beginning datetime Beginning date
@param ending datetime Ending date
@param red_page_id int MW pageid of containing RedPage
@param status int Status of RedFam
@param fam_hash str SHA1 hash of articlesList
@param heading str Original heading of RedFam (Link)
"""
pass
# Initial attribute values
self._articlesList = articlesList
self._beginning = beginning
self._ending = ending
self._red_page_id = red_page_id
self._status = status
self._fam_hash = fam_hash
self._heading = heading
# Calculates the sha1 hash over self._articlesList to
# rediscover known redundance families
self.calc_fam_hash()
def __repr__( self ):
"""
Returns repression str of RedFam object
if( self._beginning ):
beginning = ", beginning=" + repr( self._beginning )
else:
beginning = ""
@returns str repr() string
"""
if( self._ending ):
ending = ", ending=" + repr( self._ending )
else:
ending = ""
__repr = "RedFam( " + repr( self._articlesList ) + beginning +\
ending + ", status=" + repr( self._status ) + " )"
__repr = "RedFam( " + \
"articlesList=" + repr( self._articlesList ) + \
", heading=" + repr( self._heading ) + \
", beginning=" + repr( self._beginning ) + \
", ending=" + repr( self._ending ) + \
", red_page_id=" + repr( self._red_page_id ) + \
", status=" + repr( self._status ) + \
", fam_hash=" + repr( self._fam_hash ) + \
", heading=" + repr( self._heading ) + \
" )"
return __repr
def calc_fam_hash( self ):
"""
Calculates the SHA-1 hash for the articlesList of redundance family.
Since we don't need security SHA-1 is just fine.
@returns str String with the hexadecimal hash digest
"""
h = hashlib.sha1()
h.update( str( self._articlesList ).encode('utf-8') )
if self._fam_hash and h.hexdigest() != self._fam_hash:
raise RedFamHashError( self._fam_hash, h.hexdigest() )
elif self._fam_hash:
return
else:
self._fam_hash = h.hexdigest()
class RedFamParser( RedFam ):
"""
@@ -126,7 +165,7 @@ class RedFamParser( RedFam ):
# Calculates the sha1 hash over self._articlesList to
# rediscover known redundance families
self.fam_hash()
self.calc_fam_hash()
# Open database connection, ask for data if existing,
# otherwise create entry
@@ -182,19 +221,6 @@ class RedFamParser( RedFam ):
self._articlesList = self._articlesList[:8]
def fam_hash( self ):
"""
Calculates the SHA-1 hash for the articlesList of redundance family.
Since we don't need security SHA-1 is just fine.
@returns str String with the hexadecimal hash digest
"""
h = hashlib.sha1()
h.update( str( self._articlesList ).encode('utf-8') )
self._fam_hash = h.hexdigest()
def add_beginning( self, beginning ):
"""
Adds the beginning date of a redundance diskussion to the object
@@ -362,3 +388,39 @@ class RedFamWorker( RedFam ):
where discussion is finished
"""
pass
class RedFamError( Exception ):
"""
Base class for all Errors of RedFam-Module
"""
def __init__( self, message=None ):
"""
Handles Instantiation of RedFamError's
"""
if not message:
self.message = "An Error occured while executing a RedFam action"
else:
self.message = message
def __str__( self ):
"""
Output of error message
"""
return message
class RedFamHashError( RedFamError ):
"""
Raised when given RedFamHash does not match with calculated
"""
def __init__( self, givenHash, calculatedHash ):
message = "Error: Given fam_hash ('{given}') does not match with \
calculated ('{calc}'".format( given=givenHash,
calc=calculatedHash )
super().__init__( message )