178 lines
5.0 KiB
Python
178 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
#
|
||
# redpage.py
|
||
#
|
||
# Copyright 2015 GOLDERWEB – Jonathan Golder <jonathan@golderweb.de>
|
||
#
|
||
# This program is free software; you can redistribute it and/or modify
|
||
# it under the terms of the GNU General Public License as published by
|
||
# the Free Software Foundation; either version 3 of the License, or
|
||
# (at your option) any later version.
|
||
#
|
||
# This program is distributed in the hope that it will be useful,
|
||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
# GNU General Public License for more details.
|
||
#
|
||
# You should have received a copy of the GNU General Public License
|
||
# along with this program; if not, write to the Free Software
|
||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
# MA 02110-1301, USA.
|
||
#
|
||
#
|
||
"""
|
||
Provides a class for handling redundance discussion pages and archives
|
||
"""
|
||
|
||
import pywikibot # noqa
|
||
import mwparserfromhell as mwparser
|
||
|
||
import jogobot # noqa
|
||
|
||
from lib.mysqlred import MysqlRedPage, relationship
|
||
from lib.redfam import RedFam, RedFamParser
|
||
from sqlalchemy.orm.collections import attribute_mapped_collection
|
||
|
||
|
||
class RedPage( MysqlRedPage ):
|
||
"""
|
||
Class for handling redundance discussion pages and archives
|
||
"""
|
||
|
||
#TODO POLYMORPHISM? of BASEClass
|
||
redfams = relationship(
|
||
"RedFamParser", order_by=RedFamParser.famhash,
|
||
back_populates="redpage",
|
||
collection_class=attribute_mapped_collection( "famhash" ) )
|
||
|
||
def __init__( self, page=None, pageid=None, archive=False ):
|
||
"""
|
||
Generate a new RedPage object based on the given pywikibot page object
|
||
|
||
@param page Pywikibot/MediaWiki page object for page
|
||
@type page pywikibot.Page
|
||
@param pageid MW-Pageid for related page
|
||
@type pageid int
|
||
"""
|
||
|
||
# Safe the pywikibot page object
|
||
if page:
|
||
self._page = page
|
||
|
||
super().__init__(
|
||
pageid=pageid,
|
||
revid=self.page._revid,
|
||
pagetitle=self.page.title(),
|
||
status=MutableSet() ) #TODO EMPTY MutableSet() necessary?
|
||
#~ self._status = set()
|
||
|
||
self.is_archive()
|
||
|
||
self.session.add(self)
|
||
|
||
def update( self, page ):
|
||
self._page = page
|
||
self.revid = page._revid
|
||
self.pagetitle = page.title()
|
||
self.is_archive()
|
||
|
||
@property
|
||
def page(self):
|
||
if not hasattr(self,"_page"):
|
||
self._page = pywikibot.Page( pywikibot.Site(), self.pagetitle )
|
||
|
||
return self._page
|
||
|
||
@property
|
||
def archive(self):
|
||
return self.has_status("archived")
|
||
|
||
def is_archive( self ):
|
||
"""
|
||
Detects wether current page is an archive of discussions
|
||
"""
|
||
if( self.archive or ( u"/Archiv" in self.page.title() ) or
|
||
( "{{Archiv}}" in self.page.text ) or
|
||
( "{{Archiv|" in self.page.text ) ):
|
||
|
||
return True
|
||
else:
|
||
return False
|
||
|
||
def is_parsing_needed( self ):
|
||
"""
|
||
Decides wether current RedPage needs to be parsed or not
|
||
"""
|
||
return self.changedp() or not self.has_status("parsed")
|
||
|
||
def parse( self ):
|
||
"""
|
||
Handles the parsing process
|
||
"""
|
||
|
||
# Generate Wikicode object
|
||
self.wikicode = mwparser.parse( self.page.text )
|
||
|
||
# Select RedFam-sections
|
||
# matches=Regexp or
|
||
# function( gets heading content as wikicode as param 1)
|
||
# include_lead = if true include first section (intro)
|
||
# include_heading = if true include heading
|
||
fams = self.wikicode.get_sections(
|
||
matches=RedFamParser.is_section_redfam_cb,
|
||
include_lead=False, include_headings=True )
|
||
|
||
# Iterate over RedFam
|
||
for fam in fams:
|
||
|
||
yield fam
|
||
|
||
else:
|
||
self.status.add("parsed")
|
||
self._parsed = True
|
||
|
||
@classmethod
|
||
def flush_db_cache( cls ):
|
||
"""
|
||
Calls flush method of Mysql Interface class
|
||
"""
|
||
cls.session.commit()
|
||
|
||
def add_status(self, status):
|
||
"""
|
||
Adds a status specified by status, to status set
|
||
|
||
@param status Statusstring to add
|
||
@type status str
|
||
"""
|
||
self.status.add(status)
|
||
|
||
def remove_status(self, status, weak=True):
|
||
"""
|
||
Removes a status, specified by status from set. If weak is set to
|
||
False it will throw a KeyError when trying to remove a status not set.
|
||
|
||
@param status Statusstring to add
|
||
@type status str
|
||
@param weak Change behavior on missing status
|
||
@type bool
|
||
"""
|
||
if weak:
|
||
self.status.discard(status)
|
||
else:
|
||
self.status.remove(status)
|
||
|
||
def has_status(self, status):
|
||
"""
|
||
Returns True, if redfam has given status
|
||
|
||
@param status Statusstring to check
|
||
@type status str
|
||
@returns True if status is present else False
|
||
"""
|
||
if status in self.status:
|
||
return True
|
||
else:
|
||
return False
|