User:GeneaBot/Code/object claim
< User:GeneaBot | Code
# -*- coding: utf-8 -*-
import pywikibot
from util_date import *
from util_pwb import *
from object_display import *
from object_basic_claim import *
from object_source import *
import time
class Claim:
#
#
# Fields :
# self.error
#
# self.naturalValue
# self.mainClaim type object_basic_claim
# self.qualifiers list of object_basic_claim
# self.sources list of object_basic_claim
#
#
# Methods :
# self.__init__ (pCommand, pProperty, pValue, pReferences=[], pQualifiers=[])
# self.__str__ ()
# self.AddQualifier (pAdd, pProperty, pValue)
# self.AddSource (pAdd, pProperty, pValue)
# self.ProcessDate (pAdd, pProperty, pValue)
# self.GetError ()
#
# Static attributes :
#
# static methods :
# Claim.InitClass(Repo)
#
#
#
#
#
#
#
#
#
def __init__(self, pCommand, pProperty, pValue, pSource, pQualifiers=[], pViolation="", pWarning=""):
# pCommand = "info", "add"
# pProperty = ident of a property (P...)
# pValue = value for the property
# pReferences and pQualifiers : array of structure
# - bolean = true if to add
# - property
# - value
# - violation message
#
DisplayMessages.call("O", "Claim..__init__", 5, "command=" + pCommand + \
", property=" + pProperty + \
", value=" + pValue + \
", source=" + repr(pSource) + \
", qualifier=" + repr(pQualifiers) + \
", pViolation=" + pViolation + \
", pWarning=" + pWarning + ")")
self.error = False
self.warning = False
if not pWarning == '':
self.warning = True
self.msgWarning = pWarning
toAdd = False
if pCommand == "add":
toAdd = True
#
self.naturalValue = pValue
self.qualifiers = []
self.sources = []
#
vProp = pywikibot.Claim(WikiData.GetRepositery(), pProperty)
#
if vProp.getType() == 'time':
if pValue == "None":
self.mainClaim = BasicClaim(toAdd, pProperty, pValue, "value", pViolation, pWarning)
else:
self.ProcessDate(toAdd, pProperty, pValue, pViolation, pWarning)
else:
self.mainClaim = BasicClaim(toAdd, pProperty, pValue, "value", pViolation, pWarning)
if self.mainClaim.error:
self.SetError()
else:
sourceToAdd = True
if self.mainClaim.reprValue == "None":
sourceToAdd = False
#
for qualifier in pQualifiers:
self.AddQualifier(qualifier[0] or toAdd, qualifier[1], qualifier[2], qualifier[3])
sourceToAdd = True
#
if sourceToAdd:
self.AddSource(toAdd, pSource)
#
#
#
#
def __str__(self):
return self.display("long")
#
#
#
#
def __repr__(self):
return self.display("normal")
#
#
#
#
def display(self, Format, Run="sumi"):
beg = {'long':":", 'table':"|-\n|", 'normal':"{", 'short':"{" }
col = {'long':"", 'table':"\n|", 'normal':"", 'short':"" }
sep = {'long':"\n:::", 'table':"\n", 'normal':" ; ", 'short':" ; " }
end = {'long':"", 'table':"", 'normal':"}", 'short':"}" }
text = self.mainClaim.display(Format, Run)
text += col[Format]
for qualifier in self.qualifiers:
text += sep[Format] + qualifier.display(Format, Run)
text += col[Format]
for source in self.sources:
text += sep[Format] + source.display(Format, Run)
if Format == 'table':
ListError = []
ListWarning = []
if self.mainClaim.error:
ListError.append( "claim : " + self.mainClaim.GetErrorMessage() )
if self.mainClaim.warning:
ListWarning.append("claim : " + self.mainClaim.GetWarningMessage() )
for qualifier in self.qualifiers:
if Format == 'table':
if qualifier.error:
ListError.append( "qualifier " + qualifier.property + " : " + qualifier.GetErrorMessage() )
if qualifier.warning:
ListWarning.append( "qualifier " + qualifier.property + " : " + qualifier.GetWarningMessage() )
for source in self.sources:
if Format == 'table':
if source.error:
ListError.append( "source " + source.property + " : " + source.GetErrorMessage() )
if source.warning:
ListWarning.append( "source " + source.property + " : " + source.GetWarningMessage() )
if not ListError == [] or not ListWarning == []:
text += '\n|-\n| colspan=3 |'
for msg in ListError:
text += "[[File:Red x.svg|15px]] " + msg
for msg in ListWarning:
text += "[[File:Farm-Fresh error.png|15px]] " + msg
return beg[Format] + text + end[Format]
#
#
#
#
#
#
#
#
def AddQualifier(self, pAdd, pProperty, pValue, pViolation):
DisplayMessages.call("O", "Claim..AddQualifier", 5, "pAdd=" + str(pAdd) + \
", pProperty=" + pProperty + \
", pValue=" + pValue + \
", pViolation=" + pViolation )
present = False
newQualifier = BasicClaim(pAdd, pProperty, pValue, "qualifier", pViolation)
DisplayMessages.debug("O", "Claim..AddQualifier", 7, "newQualifier.GetError() = " + str(newQualifier.GetError()) + ".")
for qualifier in self.qualifiers:
if newQualifier.property == qualifier.property and newQualifier.wikiValue == qualifier.wikiValue:
DisplayMessages.debug("O", "Claim..AddQualifier", 7, "qualifier already set.")
present = True
if not present:
#if self.GetError():
# newQualifier.toAdd = False
self.qualifiers.append( newQualifier )
if newQualifier.GetError():
self.SetError()
DisplayMessages.debug("O", "Claim..AddQualifier", 7, "self.error = " + str(self.error) + ".")
return True
#
#
#
#
#
#
#
#
def AddSource(self, toAdd, pSource):
DisplayMessages.call("O", "Claim..AddSource", 5, "toAdd=" + str(toAdd) + \
", pSource=" + repr(pSource) )
present = False
# Modifier pour récupérer directement de l'objet pSource
DisplayMessages.debug("O", "Claim..AddSource", 9, "pSource.GetList() = " + str(pSource.GetList()) + ".")
for newSource in pSource.GetList():
DisplayMessages.debug("O", "Claim..AddSource", 7, "newSource = " + repr(newSource) + ".")
DisplayMessages.debug("O", "Claim..AddSource", 7, "newSource.GetError() = " + str(newSource.GetError()) + ".")
for source in self.sources:
if newSource.property == source.property and newSource.wikiValue == source.wikiValue:
DisplayMessages.debug("O", "Claim..AddSource", 7, "source already set.")
present = True
if not present:
#if self.GetError():
# newSource.toAdd = False
self.sources.append( newSource )
if newSource.error:
self.SetError()
DisplayMessages.debug("O", "Claim..AddSource", 7, "self.error = " + str(self.error) + ".")
#
#
#
#
#
#
#
#
#
def ProcessDate(self, pAdd, pProperty, pValue, pViolation, pWarning):
DisplayMessages.call("O", "Claim..ProcessDate", 5, "pAdd=" + str(pAdd) + ", pProperty=" + pProperty + ", pValue=" + pValue + ", pViolation=" + pViolation )
elt = pValue.split(" ")
while 1:
if "" in elt:
elt.remove("")
else:
break
if elt[0] == "circa":
self.mainClaim = BasicClaim(pAdd, pProperty, pValue[6:], "value", pViolation, pWarning)
self.AddQualifier(pAdd, 'P1480', 'Q5727902', "") # sourcing circumstances = circa
elif elt[0] == "before":
self.mainClaim = BasicClaim(pAdd, pProperty, "None", "value", pViolation, pWarning)
self.AddQualifier(pAdd, 'P1326', pValue[7:], "") # latest date = dateMax
elif elt[0] == "after":
self.mainClaim = BasicClaim(pAdd, pProperty, "None", "value", pViolation, pWarning)
self.AddQualifier(pAdd, 'P1319', pValue[6:], "") # earliest date = dateMax
elif elt[0] == "between" :
self.mainClaim = BasicClaim(pAdd, pProperty, "None", "value", pViolation, pWarning)
date = pValue[8:].split("and")
if len(date) == 2:
self.AddQualifier(pAdd, 'P1319', date[0].strip(), "") # earliest date = dateMax
self.AddQualifier(pAdd, 'P1326', date[1].strip(), "") # latest date = dateMax
else:
DisplayMessages.debug("O", "Claim..ProcessDate", 9, "date = " + str(date))
self.AddQualifier(pAdd, 'P1319', date[0].strip(), "") # earliest date = dateMax
self.AddQualifier(pAdd, 'P1326', '', "") # latest date = dateMax
else:
self.mainClaim = BasicClaim(pAdd, pProperty, pValue, "value", pViolation, pWarning)
#
#
#
#
def Test(self, Property, Value):
DisplayMessages.call("O", "Claim..Test", 5, "" )
return self.mainClaim.Test(Property, Value)
#
#
#
#
def SetError(self):
DisplayMessages.call("O", "Claim..SetError", 5, "" )
self.error = True
for qualifier in self.qualifiers:
qualifier.toAdd = False
for source in self.sources:
source.toAdd = False
#
#
#
#
def GetError(self):
return self.error
#
#
#
#
#
#
def WriteToWD(self, item, display):
DisplayMessages.call("O", "Claim..WriteToWD", 5, "self=" + repr(self) + "item, display=" + display )
if self.error:
DisplayMessages.warning("O", "Claim..WriteToWD", "error on Item, claims not written on Wikidata" )
else:
claim = self.mainClaim.WriteMainClaimToWD(item, display)
#
if not claim == None:
for qualifier in self.qualifiers:
qualifier.WriteQualifierToWD(claim, display, repr(self.mainClaim))
#
sources = []
text = ""
for source in self.sources:
elt = source.WriteSourceToWD()
if not str(elt) == "":
sources.append(elt)
text += ", " + repr(source)
if not sources == []:
DisplayMessages.message("O", "Claim..WriteToWD", "item " + display + \
", claim " + repr(self.mainClaim) + \
" : adding source " + text[2:])
claim.addSources(sources)
#
#
#
#
#
#
def WriteCount(self):
tpsWrite = 0
if not self.error:
if self.mainClaim.toAdd:
tpsWrite += 10
toAdd = False
for qualifier in self.qualifiers:
if qualifier.toAdd:
toAdd = True
if toAdd:
tpsWrite += 6
toAdd = False
for source in self.sources:
if source.toAdd:
toAdd = True
if toAdd:
tpsWrite += 10
return tpsWrite
def testClaim(NumTest, Format, Run, pCommand, pProperty, pValue, pSource, pQualifiers=[], pViolation=""):
print( "" )
print( "---------------------------" )
text = "test " + str(NumTest) + ":\n"
text += "Claim(command=" + pCommand + ", property=" + pProperty + ", value=" + pValue + ", source=" + str(pSource)
if not pQualifiers == []:
text += ", qualifier=" + str(pQualifiers)
if not pViolation == "":
text += ", violation=" + str(pViolation)
text += ")"
print( text )
print( "" )
localClaim = Claim(pCommand, pProperty, pValue, pSource, pQualifiers, pViolation)
print( localClaim.display(Format, Run) )
print( "" )
def mainTestClaim(*args):
param = CallParameter(*args)
NumCase = param.GetValue('case', 0)
Display = param.GetValue('display', 'short')
DisplayMessages.SetFileName("")
ListDisplay = Display.split(':')
Format = ListDisplay[0]
if len(ListDisplay) >= 2:
Run = ListDisplay[1]
else:
Run = 'simu'
eSource = Source()
if NumCase == 0 or NumCase == 1 :
testClaim(1, Format, Run, 'info', 'P7', 'Q822486', eSource)
if NumCase == 0 or NumCase == 2 :
testClaim(2, Format, Run, 'add', 'P9', 'Q21153688', eSource)
if NumCase == 0 or NumCase == 3 :
source = Source()
source.AddReference('P248', 'Q13419312', True)
source.AddReference('P854', 'http://fmg.ac/Projects/MedLands/GASCONY.htm', True)
source.AddReference('P813', '15 june 2015', True)
testClaim(3, Format, Run, 'info', 'P97', 'Q21153436', source, \
[ [True, 'P580', '1039', ''] , \
[True, 'P582', '1062', ''] , \
[False, 'P155', 'Q264821', ''] , \
[False, 'P156', 'Q462896', ''] ])
if NumCase == 0 or NumCase == 4 :
testClaim(4, Format, Run, 'info', 'P570', 'circa 1095', eSource)
if NumCase == 0 or NumCase == 5 :
testClaim(5, Format, Run, 'info', 'P570', 'before 1270', eSource)
if NumCase == 0 or NumCase == 6 :
testClaim(6, Format, Run, 'info', 'P570', 'after 1315', eSource)
if NumCase == 0 or NumCase == 7 :
testClaim(7, Format, Run, 'info', 'P570', 'between 1064 and 1090', eSource)
if NumCase == 0 or NumCase == 8 :
testClaim(8, Format, Run, 'info', 'P570', 'between 1064 1090', eSource)
if NumCase == 0 or NumCase == 9 :
testClaim(9, Format, Run, 'info', 'P570', 'after 1315', eSource, [], 'test of constraint violation message')
if NumCase == 0 or NumCase == 10 :
testClaim(10, Format, Run, 'add', 'P570', '1095', eSource, [ [False, 'P1480', 'Q5727902', 'test of constraint violation message on qualifier' ] ])
if NumCase == 0 or NumCase == 11 :
vSource = Source()
vSource.AddReference('P248', 'Q822486', False)
testClaim(11, Format, Run, 'add', 'P570', '1095', vSource)
DisplayMessages.End()
if __name__ == "__main__":
mainTestClaim()