User:GeneaBot/Code/module function

From Wikidata
Jump to navigation Jump to search
# -*- coding: utf-8  -*-
import pywikibot
from object_display  import *
from object_source   import *
from object_function import *
from object_human    import *




class ManagementFunction:
    #
    def __init__(self):
        DisplayMessages.call("O", "ManagementFunction..__init__", 5, "" )
        self.field = dict()
        self.field['person'] = []
        self.field['succession'] = True
    #
    def setField(self, Index, Value):
        DisplayMessages.call("O", "ManagementFunction..setField", 5, "Index = " + Index + ", Value = " + repr(Value))
        self.field[Index] = Value
    #
    def addPerson(self, Item, Source):
        DisplayMessages.call("O", "ManagementFunction..addPerson", 5, "Index = " + str(Item) + ", Source = " + repr(Source))
        n = len(self.field['person'])
        if n == 0:
            if self.field['succession']:
                if 'first' in self.field:
                    Item.currentFunction.previous = self.field['first']
        else:
            if self.field['succession']:
                self.field['person'][n-1].SetSuccessorForFunction(Item)
            self.field['person'][n-1].RecordFunction(Source)
        self.field['person'].append(Item)
    #
    def lastPerson(self, Source):
        DisplayMessages.call("O", "ManagementFunction..lastPerson", 5, "")
        n = len(self.field['person'])
        if n >= 1:
            if self.field['succession']:
                if 'last' in self.field:
                    self.field['person'][n-1].currentFunction.next = self.field['last']
            self.field['person'][n-1].RecordFunction(Source)
    #
    def getFunction(self):
        DisplayMessages.call("O", "ManagementFunction..getFunctions", 5, "")
        List = dict()
        return self.field['function']
    #
    def getDomain(self):
        DisplayMessages.call("O", "ManagementFunction..getDomain", 5, "")
        if 'domain' in self.field:
            return self.field['domain']
        else:
            return ''
    #
    def getPerson(self):
        DisplayMessages.call("O", "ManagementFunction..getPerson", 5, "")
        return self.field['person']
    #
    def getField(self, Index):
        DisplayMessages.call("O", "ManagementFunction..getField", 5, "Index = " + Index )
        if Index == 'person':
            n = len(self.field['person'])
            if n >= 1:
                return self.field['person'][n-1]
        else:
            if Index in self.field:
                return self.field[Index]
            else:
                return ""
    #
    def display(self, Format, Run):
        for key in [ 'first', 'last', 'list', 'function', 'domain']:
            if key in self.field:
                print( self.field[key].display(Format, Run) )
        if 'person' in self.field:
            for item in self.field['person']:
                print( item.display(Format, Run) )









#======================================================
# Fonction FunctionSet
#
def FunctionSet(curRequest):
    DisplayMessages.call("A", "FunctionSet", 1, "curRequest=" + curRequest )
    DisplayLog.AppendRequest( curRequest )
    EmptySource = Source()
    Item = ""
    Params = dict()
    mngtFunction = ManagementFunction()
    curRequest = curRequest[:len(curRequest)-2]  # suppress '}}' at the end request
    listArgs = curRequest.split("|")
    iArg = 1
    while iArg < len(listArgs):
        Statement = listArgs[iArg].split("=")
        Property = Statement[0].strip()
        try :
            Value = Statement[1].strip()
        except IndexError:
            DisplayMessages.warning("A", "FunctionSet", "parameter '" + Property + "' without argument")
            iArg += 1
            continue
        if not Value == '':
            if Property == 'function':
                Item = Value
            elif Property == 'domain':
                mngtFunction.setField('domain', Value)
            elif Property == 'jurisdiction':
                Params['P1001'] = [ Value ]
            elif Property == 'inception':
                Params['P571'] = [ Value ]
            elif Property == 'instance of':
                Params['P31'] = [ Value ]
            elif Property == 'first':
                mngtFunction.setField('first', Value) 
            elif Property == 'last':
                mngtFunction.setField('last',  Value)
            elif Property == 'succession':
                if Value == 'off' or Value == 'false' or Value == 'no' :
                    mngtFunction.setField('succession',  False)
                else:
                    mngtFunction.setField('succession',  True)
            else:
                DisplayMessages.warning("A", "FunctionSet", "unknown parameter '" + Property + " = " + Value + "'")
        iArg += 1
    DisplayMessages.debug("A", "FunctionSet", 4, "Params=" + str(Params) )
    #
    if not Item == "":
        DisplayMessages.debug("A", "FunctionSet", 7, "Creating object Function with ident = " + Item + " and claims wanted = " + str(Params) )
        mngtFunction.setField( 'function', Function.Get(Item, Params, EmptySource) )
    #
    return mngtFunction

    


def FunctionAdd(curRequest, MngtFunction, Reference):
    DisplayMessages.call("A", "FunctionAdd", 1, "curRequest=" + curRequest + ", MngtFunction, Reference" )
    DisplayLog.AppendRequest( curRequest )
    Ident = ""
    Qualifiers = dict()
    EmptyDict  = dict()
    Ident       = ''
    BegDate     = ''
    EndDate     = ''
    Predecessor = ''
    Successor   = ''
    Ref         = Reference
    curRequest = curRequest[:len(curRequest)-2]  # suppress '}}' at the end request
    listArgs = curRequest.split("|")
    iArg = 1
    while iArg < len(listArgs):
        Statement = listArgs[iArg].split("=")
        Property = Statement[0].strip()
        try :
            Value = Statement[1].strip()
        except IndexError:
            DisplayMessages.warning("A", "FunctionAdd", "parameter '" + Property + "' without argument")
            iArg += 1
            continue
        if not Value == '':
            if Property == 'person':
                Ident = Value
            elif Property == 'date of begin':
                BegDate = Value
            elif Property == 'date of end':
                EndDate = Value
            elif Property == 'page':
                RefPage = Source()
                RefPage.AddReference('P304', Value, True)
                Ref = Reference + RefPage
            else:
                DisplayMessages.warning("A", "FunctionAdd", "unknown parameter '" + Property + " = " + Value + "'")
        iArg += 1
        
    DisplayMessages.debug("A", "FunctionAdd", 4, "Ident = " + Ident + "\nBegDate = " + BegDate + "\nEndDate = " + EndDate )

    if not Ident == '':
        Person = Human.Get(Ident, {}, Ref)
        Person.SetFunction(MngtFunction.getFunction(), MngtFunction.getDomain(), BegDate, EndDate)
        DisplayMessages.debug("A", "FunctionAdd", 4, "create item : '" + str(Person))
        MngtFunction.addPerson( Person, Ref )
        
    return MngtFunction        






def FunctionEnd(MngtFunction, Reference):
    DisplayMessages.call("A", "FunctionEnd", 3, "MngtFunction, Reference" )
    MngtFunction.lastPerson( Reference )
        









def TestFunctionSet(curRequest, Format, Run):
    print( "" )
    print( "---------------------------" )
    print( "test FunctionSet(curRequest=" + curRequest + ")" )
    print( "" )
    MngtFunction = FunctionSet(curRequest)
    MngtFunction.display(Format, Run)
    return MngtFunction



def TestFunctionAdd(curRequest, MngtFunction, Reference, Format, Run):
    print( "" )
    print( "---------------------------" )
    print( "test FunctionAdd(curRequest=" + curRequest + ", MngtFunction, Reference)" )
    print( "" )
    FunctionAdd(curRequest, MngtFunction, Reference)
    Persons = MngtFunction.getPerson()
    if len(Persons) >=2:
        print( Persons[len(Persons)-2].display(Format, Run) )



def TestFunctionEnd(MngtFunction, Reference, Format, Run):
    print( "" )
    print( "---------------------------" )
    print( "test FunctionAdd(MngtFunction, Reference)" )
    print( "" )
    FunctionEnd(MngtFunction, Reference)
    Persons = MngtFunction.getPerson()
    if len(Persons) >=2:
        print( Persons[len(Persons)-1].display(Format, Run) )



def mainTestModuleFunction(*args):
    param = CallParameter(*args)
    NumCase = param.GetValue('case', 0)
    Display = param.GetValue('display', 'short')
    
    ListDisplay = Display.split(':')
    Format = ListDisplay[0]
    if len(ListDisplay) >= 2:
        Run = ListDisplay[1]
    else:
        Run = 'simu'
    
    if NumCase == NumCase + 1:
        print( "=====================   TEST 1   =====================" )
        source = Source()
        source.AddReference('P248', 'Q13419312', True)
        source.AddReference('P854', 'http://fmg.ac/Projects/MedLands/GASCONY.htm', True)
        mngtFunction = TestFunctionSet('{{/FunctionSet|function for man=Q21123038 |function for woman=Q21513679 |man subclass of=Q3519259 |woman subclass of=Q21101501 |part of=Q612322}}', Format, Run)
        request = '{{/FunctionAdd|person=Q2506439 |date of begin= |date of end= }}'
        TestFunctionAdd(request, mngtFunction, source, Format, Run)
        request = '{{/FunctionAdd|person=Q4155186 |date of begin= |date of end= }}'
        TestFunctionAdd(request, mngtFunction, source, Format, Run)
        request = '{{/FunctionAdd|person=Q4269614 |date of begin= |date of end= }}'
        TestFunctionAdd(request, mngtFunction, source, Format, Run)
        request = '{{/FunctionAdd|person=Q2509451 |date of begin= |date of end=956 }}'
        TestFunctionAdd(request, mngtFunction, source, Format, Run)
        request = '{{/FunctionAdd|person=Q4268340 |date of begin=956 |date of end=1000  }}'
        TestFunctionAdd(request, mngtFunction, source, Format, Run)
        request = '{{/FunctionAdd|person=Q4133930 |date of begin=1000 |date of end= }}'
        TestFunctionAdd(request, mngtFunction, source, Format, Run)
        request = '{{/FunctionAdd|person=Q11923812 |date of begin= |date of end=1032 }}'
        TestFunctionAdd(request, mngtFunction, source, Format, Run)
        TestFunctionEnd(mngtFunction, source, Format, Run)


if __name__ == "__main__":
    mainTestModuleFunction()