User:GeneaBot/Code/object source

From Wikidata
Jump to navigation Jump to search
# -*- coding: utf-8  -*-
import pywikibot
from util_date                      import *
from util_pwb                       import *
from object_constraint_violation    import *
from object_display                 import *
from object_basic_claim             import *




class Source:
    #
    # Fields :
    #   self.error
    #   self.list
    #
    #
    # Methods :
    #   self.__init__   ()
    #   self.__str__    ()
    #   self.__repr__   ()
    #   self.__add__    (source)
    #   self.AddReference(pProperty, pValue, toAdd)
    #   self.GetList    ()
    #
    #
    # static attributes :
    #
    #
    # static methods :
    #
    #
    #
    ListOfProperty = [
        'P248', # affirmed by
        'P478', # tome
        'P304', # page
        'P854', # url
        'P813', # retrieved
        ]
    #
    #
    #
    #
    def __init__(self):
        DisplayMessages.call("O", "Source..__init__", 5, "" )
        self.error  = False
        self.list   = []
    #
    #
    #
    #
    def __repr__(self):
        return self.display("normal")
    #
    #
    #
    #
    def __str__(self):
        return self.display("long")
    #
    #
    #
    #
    def display(self, Format, Run='simu'):
        beg = {'long':":::",   'table':"\n", 'normal':"{",   'short':"{"   }
        sep = {'long':"\n:::", 'table':"\n", 'normal':" ; ", 'short':" ; " }
        end = {'long':"",      'table':"",   'normal':"}",   'short':"}"   }
        
        text = ""
            
        for claim in self.list:
            if text == "":
                text = claim.display(Format, Run)
            else:
                text += sep[Format] + claim.display(Format, Run)
        return beg[Format] + text + end[Format]
    #
    #
    #
    #
    def __add__(self, source):
        DisplayMessages.call("O", "Source..__add__", 5, "self=" + repr(self) + ", self=" + repr(source) )
        somme = Source()
        for elt1 in self.list:
            somme.list.append(elt1)
            if elt1.error:
                somme.error = True
        for elt2 in source.list:
            eltPresent = False
            for elt1 in self.list:
                if elt1.property == elt2.property:
                    if elt1.value == elt2.value:
                        eltPresent = True
            if not eltPresent:
                somme.list.append(elt2)
                if elt2.error:
                    somme.error = True
        return somme
    #
    #
    #
    #
    def AddReference(self, pProperty, pValue, toAdd):
        DisplayMessages.call("O", "Source..AddReference", 5, "pProperty=" + pProperty + ", pValue=" + pValue )
        Check = ConstraintViolation("Source", pProperty, pValue, [], 0, [], {}, False, True)
        violation = str(Check)
        elt = BasicClaim(toAdd, pProperty, pValue, "source", violation)
        self.list.append(elt)
        if elt.error:
            self.error = True
    #
    #
    #
    #
    def GetList(self):
        return self.list
    #
    #
    #
    #
    def GetListProperty(self):
        List = []
        for elt in self.list:
            List.append(elt.property)
        return List




        
        


def testSource(NumTest, VarName, Format, Run, source, prop1, val1, prop2, val2, prop3, val3):
    print( "" )
    print( "---------------------------" )
    print( "test " + str(NumTest) + ":" )
    print( "Source(" + VarName + ', ' + Format + ', ' + Run + ", " + repr(source) + ", " + \
                          prop1 + " , " + val1 + " , " + \
                          prop2 + " , " + val2 + " , " + \
                          prop3 + " , " + val3 + ")" )
    source.AddReference(prop1, val1, True)
    source.AddReference(prop2, val2, True)
    source.AddReference(prop3, val3, True)
    print( source.display(Format, Run) )
    print( source.GetListProperty() )
    print( "" )
        




def mainTestSource(*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:
        print( "" )
        print( "---------------------------" )
        print( "source1.Source()" )
        source1 = Source()
        print( source1.display(Format, Run) )
        testSource(1, "source1", Format, Run, source1, 'P248', 'Q13409396', 'P478', '1', 'P304', '132-7')
        
    if NumCase == 0 or NumCase == 2 or NumCase == 3:
        source2 = Source()
        testSource(2, "source2", Format, Run, source2, 'P248', 'Q13419312', 'P854', 'http://fmg.ac/Projects/MedLands/GASCONY.htm', 'P813', '15 june 2015')
    
    if NumCase == 0 or NumCase == 3:
        source3 = Source()
        testSource(3, "source3", Format, Run, source3, 'P248', 'Q13419312', 'P854', 'http://fmg.ac/Projects/MedLands/SAVOY.htm', 'P813', '9 september 2015')
        print( "" )
        print( "source4 = source2 + source3" )
        source4 = source2 + source3
        print( "source4 = " + source4.display(Format, Run) )
        print( "source2 = " + source2.display(Format, Run) )
        print( "source3 = " + source3.display(Format, Run) )
    
    if NumCase == 0 or NumCase == 4:
        source4 = Source()
        testSource(4, "source4", Format, Run, source4, 'P248', 'Q13419312', 'P155', 'Q13409396', 'P813', '9 september 2015')
    DisplayMessages.End()



if __name__ == "__main__":
    mainTestSource()