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:
@@ -32,6 +32,7 @@ except ImportError:
|
|||||||
import MySQLdb as mysqldb
|
import MySQLdb as mysqldb
|
||||||
|
|
||||||
from pywikibot import config
|
from pywikibot import config
|
||||||
|
|
||||||
import jogobot
|
import jogobot
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
118
redfam.py
118
redfam.py
@@ -32,6 +32,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
import pywikibot
|
import pywikibot
|
||||||
|
|
||||||
|
import jogobot
|
||||||
from .mysqlred import MysqlRedFam
|
from .mysqlred import MysqlRedFam
|
||||||
|
|
||||||
|
|
||||||
@@ -40,34 +41,72 @@ class RedFam:
|
|||||||
Basic class for RedFams, containing the basic data structure
|
Basic class for RedFams, containing the basic data structure
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__( self, fam_hash=None, articlesList=None, red_page_id=None,
|
def __init__( self, articlesList, beginning, ending=None, red_page_id=None,
|
||||||
beginning=None, ending=None, status=0 ):
|
status=0, fam_hash=None, heading=None ):
|
||||||
"""
|
"""
|
||||||
Generates a new RedFam object
|
Generates a new RedFam object
|
||||||
|
|
||||||
@param articlesList list List of articles
|
@param articlesList list List of articles
|
||||||
@param beginning datetime Beginning date
|
@param beginning datetime Beginning date
|
||||||
@param ending datetime Ending 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 ):
|
def __repr__( self ):
|
||||||
|
"""
|
||||||
|
Returns repression str of RedFam object
|
||||||
|
|
||||||
if( self._beginning ):
|
@returns str repr() string
|
||||||
beginning = ", beginning=" + repr( self._beginning )
|
"""
|
||||||
else:
|
|
||||||
beginning = ""
|
|
||||||
|
|
||||||
if( self._ending ):
|
__repr = "RedFam( " + \
|
||||||
ending = ", ending=" + repr( self._ending )
|
"articlesList=" + repr( self._articlesList ) + \
|
||||||
else:
|
", heading=" + repr( self._heading ) + \
|
||||||
ending = ""
|
", beginning=" + repr( self._beginning ) + \
|
||||||
|
", ending=" + repr( self._ending ) + \
|
||||||
__repr = "RedFam( " + repr( self._articlesList ) + beginning +\
|
", red_page_id=" + repr( self._red_page_id ) + \
|
||||||
ending + ", status=" + repr( self._status ) + " )"
|
", status=" + repr( self._status ) + \
|
||||||
|
", fam_hash=" + repr( self._fam_hash ) + \
|
||||||
|
", heading=" + repr( self._heading ) + \
|
||||||
|
" )"
|
||||||
|
|
||||||
return __repr
|
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 ):
|
class RedFamParser( RedFam ):
|
||||||
"""
|
"""
|
||||||
@@ -126,7 +165,7 @@ class RedFamParser( RedFam ):
|
|||||||
|
|
||||||
# Calculates the sha1 hash over self._articlesList to
|
# Calculates the sha1 hash over self._articlesList to
|
||||||
# rediscover known redundance families
|
# rediscover known redundance families
|
||||||
self.fam_hash()
|
self.calc_fam_hash()
|
||||||
|
|
||||||
# Open database connection, ask for data if existing,
|
# Open database connection, ask for data if existing,
|
||||||
# otherwise create entry
|
# otherwise create entry
|
||||||
@@ -182,19 +221,6 @@ class RedFamParser( RedFam ):
|
|||||||
|
|
||||||
self._articlesList = self._articlesList[:8]
|
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 ):
|
def add_beginning( self, beginning ):
|
||||||
"""
|
"""
|
||||||
Adds the beginning date of a redundance diskussion to the object
|
Adds the beginning date of a redundance diskussion to the object
|
||||||
@@ -362,3 +388,39 @@ class RedFamWorker( RedFam ):
|
|||||||
where discussion is finished
|
where discussion is finished
|
||||||
"""
|
"""
|
||||||
pass
|
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 )
|
||||||
|
|||||||
Reference in New Issue
Block a user