User:GeneaBot/Code/object basic claim
< User:GeneaBot | Code
# -*- coding: utf-8 -*-
import pywikibot
from util_date import *
from util_pwb import *
from object_display import *
class BasicClaim:
#
#
# Fields :
# self.error
# self.message
# self.warning
# self.toAdd
# self.nature
# self.type
# self.property
# self.strValue
# self.reprValue
# self.wikiValue
#
# Methods :
# self.__init__ (pAdd, pProperty, pValue)
# self.__str__ ()
# self.__repr__ ()
# self.GetError ()
#
# Static attributes :
#
# static methods :
#
#
#
#
#
#
def __init__(self, pAdd, pProperty, pValue, pNature, pViolation, pWarning=""):
# pAdd = type boolean
# pProperty = ident of a property (P...)
# pValue = value for the property
# pNature = "value", "qualifier" or "source"
#
DisplayMessages.call("O", "BasicClaim..__init__", 5, "pAdd=" + str(pAdd) + ", pProperty=" + pProperty + \
", pValue=" + pValue + ", pNature=" + pNature + ", pViolation=" + pViolation )
self.error = False
self.nature = pNature
self.message = ""
self.warning = pWarning
self.toAdd = pAdd
self.property = pProperty
vProp = pywikibot.Claim(WikiData.GetRepositery(), pProperty)
self.type = vProp.getType()
self.value = pValue
#
if self.type == 'wikibase-item':
if pValue == "None":
self.strValue = "None"
self.reprValue = "None"
self.wikiValue = "None"
elif pValue[0] == 'Q':
self.strValue = GetLinkAndLabel(pValue, 'Q')
self.reprValue = GetIdentAndLabel(pValue, 'Q')
self.wikiValue = pywikibot.ItemPage(WikiData.GetRepositery(), pValue)
else:
self.error = True
self.reprValue = pValue
self.strValue = pValue
self.message = "invalid value (" + pValue + ") for property " + pProperty + " (type = wikibase-item)"
DisplayMessages.error("O", "BasicClaim..__init__", self.message)
#
#
elif self.type == 'commonsMedia':
self.strValue = "[[File:" + pValue + "|75px]]"
self.reprValue = pValue
self.wikiValue = pywikibot.FilePage(pywikibot.Site('commons', 'commons'), pValue)
#
#
elif self.type == 'string' or self.type == 'url':
self.strValue = pValue
self.reprValue = pValue
self.wikiValue = pValue
#
#
elif self.type == 'time':
if pValue == "None":
self.strValue = "None"
self.reprValue = "None"
self.wikiValue = "None"
else:
self.strValue = pValue
self.reprValue = pValue
self.wikiValue = DateToWiki(pValue)
if not str(type(self.wikiValue)) == "<class 'pywikibot.WbTime'>":
self.error = True
self.message = "invalid date (" + str(pValue) + ") for property " + pProperty + "."
DisplayMessages.error("O", "BasicClaim..ProcessDate", self.message)
#
#
else:
self.error = True
self.strValue = pValue
self.reprValue = pValue
self.message = "invalid type for property " + pProperty + " (" + str(vProp.getType()) + ")."
DisplayMessages.error("O", "BasicClaim..__init__", self.message)
#
if not pViolation == "":
if not self.message == "":
self.message += " ; "
self.message += "constraint violation : " + pViolation
self.error = True
#
#
#
#
def __repr__(self):
return self.display('normal')
#
#
#
#
def __str__(self):
return self.display('long')
#
#
#
#
#
#
#
def display(self, Format='normal', Run='simu'):
# Format = short, normal, long, large
# Run = update, simu, error, normal
if Format == 'short':
text = self.display_short()
elif Format == 'normal':
text = self.display_normal()
elif Format == 'table':
text = self.display_table(Run)
elif Format == 'long':
text = self.display_long(Run)
else:
text = self.display_short()
return text
#
#
def display_short(self):
if self.nature == "value":
text = self.property + "=" + self.value
if self.error:
text += " (ERR)"
elif not self.warning == '':
text += " (wng)"
elif self.toAdd:
text += " (Add)"
else:
text = self.property
return text
#
#
def display_normal(self):
text = self.property + "=" + self.value
if self.error:
text += " (ERROR)"
elif not self.warning == '':
text += " (warning)"
elif self.toAdd:
text += " (toAdd)"
return text
#
#
def display_table(self, Run='simu'):
if self.type == 'url':
lstVal = self.strValue.split('/')
text = GetLinkAndLabel(self.property, 'P') + "=[" + self.strValue + " " + lstVal[len(lstVal)-1] + "]"
else:
text = GetLinkAndLabel(self.property, 'P') + "=" + self.strValue
if self.error:
text = "[[File:Red x.svg|15px]] " + text
elif not self.warning == '':
text = "[[File:Farm-Fresh error.png|15px]] " + text
elif self.toAdd:
if Run == 'update' :
text = "[[File:Yes check.svg|15px]] " + text
else:
text = "[[File:Check-lightgreen.svg|15px]] " + text
else:
text = "[[File:SemiTransBlack x.svg|15px]] " + text
if not self.nature == "value":
text = "*" + text
return text
#
#
def display_long(self, Run='simu'):
text = GetLinkAndLabel(self.property, 'P') + " = " + self.strValue
if self.error:
text = "[[File:Red x.svg|15px]] Try to add " + self.nature + " : " + text
elif not self.warning == '':
if Run == 'update' :
text = "[[File:Farm-Fresh error.png|15px]] '''add " + self.nature + " : " + text + "'''"
else:
text = "[[File:Farm-Fresh error.png|15px]] OK to add " + self.nature + " : " + text
elif self.toAdd:
if Run == 'update' :
text = "[[File:Yes check.svg|15px]] '''add " + self.nature + " : " + text + "'''"
else:
text = "[[File:Check-lightgreen.svg|15px]] OK to add " + self.nature + " : " + text
else:
text = "[[File:SemiTransBlack x.svg|15px]] " + self.nature + " already set : " + text
if self.nature == "value":
if self.error:
text += ", but " + self.message + "."
elif not self.warning == '':
text += ", but warning : " + self.warning
return text
#
#
#
#
#
#
def Test(self, Property, Value):
DisplayMessages.call("O", "BasicClaim..Test", 5, "self={" + self.property + ":" + self.value + \
"}, Property=" + Property + ", Value=" + Value)
test = False
if self.property == Property:
if self.value == Value:
test = True
DisplayMessages.debug("O", "BasicClaim..Test", 6, "return " + str(test) )
return test
#
#
#
#
#
#
def GetError(self):
DisplayMessages.call("O", "BasicClaim..GetError", 5, str(self.error) )
return self.error
#
#
#
#
def GetErrorMessage(self):
return self.message
#
#
#
#
def GetWarningMessage(self):
return self.warning
#
#
#
#
#
def WriteMainClaimToWD(self, item, display):
DisplayMessages.call("O", "BasicClaim..WriteMainClaimToWD", 5, "self=" + repr(self) + ", item, display=" + display )
claim = None
if self.error:
DisplayMessages.warning("O", "BasicClaim..WriteMainClaimToWD", "error on Item, claims not written on Wikidata" )
else:
DisplayMessages.debug("O", "BasicClaim..WriteQualifierToWD", 8, "self.nature=" + self.nature + ", self.toAdd=" + str(self.toAdd) )
if self.nature == "value":
if self.toAdd:
repo = WikiData.GetRepositery()
claim = pywikibot.Claim(repo, self.property)
if self.value == "None":
claim.setSnakType('somevalue')
else:
claim.setTarget(self.wikiValue)
DisplayMessages.message("O", "Claim.WriteMainClaimToWD", "item " + display + " : adding claim " + repr(self))
item.addClaim(claim)
else:
cpt = 0
while cpt < len(item.claims[self.property]):
if str(ExtractShortItem(item.claims[self.property][cpt])) == str(self.value):
claim = item.claims[self.property][cpt]
cpt += 1
if claim == None:
DisplayMessages.warning("O", "BasicClaim..WriteMainClaimToWD", "claim not found" )
cpt = 0
while cpt < len(item.claims[self.property]):
DisplayMessages.debug("O", "BasicClaim..WriteQualifierToWD", 8, \
"\nextractItem=" + ExtractShortItem(item.claims[self.property][cpt]) + \
"\nself.value=" + str(self.value) )
if item.claims[self.property][cpt].getTarget() == self.wikiValue:
claim = item.claims[self.property][cpt]
cpt += 1
else:
DisplayMessages.warning("O", "BasicClaim..WriteMainClaimToWD", "bad nature for claim" )
return claim
#
#
#
#
#
def WriteQualifierToWD(self, claim, dispItem, dispClaim):
DisplayMessages.call("O", "BasicClaim..WriteQualifierToWD", 5, "self=" + repr(self) + "claim=" + repr(claim) )
if self.error:
DisplayMessages.warning("O", "BasicClaim..WriteQualifierToWD", "error on Item, claims not written on Wikidata" )
else:
DisplayMessages.debug("O", "BasicClaim..WriteQualifierToWD", 8, "self.nature=" + self.nature + ", self.toAdd=" + str(self.toAdd) )
if self.nature == "qualifier":
if self.toAdd:
DisplayMessages.message("O", "Claim.WriteQualifierToWD", "item " + dispItem + \
", claim " + dispClaim + \
" : adding qualifier " + repr(self))
repo = WikiData.GetRepositery()
qualifier = pywikibot.Claim(repo, self.property)
qualifier.setTarget(self.wikiValue)
claim.addQualifier(qualifier)
else:
DisplayMessages.warning("O", "BasicClaim..WriteQualifierToWD", "bad nature for claim" )
#
#
#
#
#
def WriteSourceToWD(self):
DisplayMessages.call("O", "BasicClaim..WriteSourceToWD", 5, "self=" + repr(self) )
elt = ""
if self.error:
DisplayMessages.warning("O", "BasicClaim..WriteSourceToWD", "error on Item, claims not written on Wikidata" )
else:
DisplayMessages.debug("O", "BasicClaim..WriteSourceToWD", 8, "self.nature=" + self.nature + ", self.toAdd=" + str(self.toAdd) )
if self.nature == "source":
if self.toAdd:
repo = WikiData.GetRepositery()
elt = pywikibot.Claim(repo, self.property)
elt.setTarget(self.wikiValue)
else:
DisplayMessages.warning("O", "BasicClaim..WriteSourceToWD", "bad nature for claim" )
return elt
def testBasicClaim(NumTest, Format, Run, pAdd, pProperty, pValue, pNature, pViolation=""):
print( "" )
print( "---------------------------" )
text = "test " + str(NumTest) + ":\n"
text += "BasicClaim(pAdd=" + pAdd + ", pProperty=" + pProperty + \
", pValue=" + pValue + ", pNature=" + pNature
if not pViolation == "":
text += ", violation=" + str(pViolation)
text += ")"
print( text )
print( "" )
if pAdd == 'add':
bAdd = True
else:
bAdd = False
localClaim = BasicClaim(bAdd, pProperty, pValue, pNature, pViolation)
print(localClaim.display(Format, Run))
def mainTestBasicClaim(*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'
if NumCase == 0 or NumCase == 1 :
testBasicClaim(1, Format, Run, 'info', 'P7', 'Q822486', 'claim')
if NumCase == 0 or NumCase == 2 :
testBasicClaim(2, Format, Run, 'add', 'P9', 'Q21153688', 'claim')
if NumCase == 0 or NumCase == 3 :
testBasicClaim(3, Format, Run, 'info', 'P570', '1904', 'claim')
if NumCase == 0 or NumCase == 4 :
testBasicClaim(4, Format, Run, 'info', 'P570', 'None', 'claim')
if NumCase == 0 or NumCase == 5 :
testBasicClaim(5, Format, Run, 'info', 'P570', '14 july 1789', 'claim')
if NumCase == 0 or NumCase == 6 :
testBasicClaim(6, Format, Run, 'info', 'P1326', '1270', 'qualifier')
if NumCase == 0 or NumCase == 7 :
testBasicClaim(7, Format, Run, 'info', 'P155', 'Q1350089', 'qualifier')
if NumCase == 0 or NumCase == 8 :
testBasicClaim(8, Format, Run, 'info', 'P155', '1270', 'qualifier')
if NumCase == 0 or NumCase == 9 :
testBasicClaim(9, Format, Run, 'info', 'P854', 'http://fmg.ac/Projects/MedLands/GASCONY.htm', 'source')
if NumCase == 0 or NumCase == 10 :
testBasicClaim(10, Format, Run, 'info', 'P7', 'Q822486', 'claim', 'violation message')
if NumCase == 0 or NumCase == 11 : # test display
ListFormat = ['short', 'normal', 'table', 'long' ]
ListRun = ['simu', 'update' ]
claim1 = BasicClaim(False, 'P7', 'Q822486', 'qualifier', '')
claim2 = BasicClaim(True, 'P854', 'http://genealogy.euweb.cz/lorraine/lorraine4.html', 'source', '')
claim3 = BasicClaim(False, 'P7', 'Q822486', 'value', 'violation message')
for iFormat in ListFormat:
for iRun in ListRun:
print('iFormat:' + iFormat + ', iRun:' + iRun)
print(' claim1=' + claim1.display(iFormat, iRun))
print(' claim2=' + claim2.display(iFormat, iRun))
print(' claim3=' + claim3.display(iFormat, iRun))
DisplayMessages.End()
if __name__ == "__main__":
mainTestBasicClaim()