User:GeneaBot/Code/util pwb

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



class WikiData:
    Init = False
    ItemWithoutLabel = {}
    #
    #
    #
    #
    def GetRepositery():
        if not WikiData.Init:
            Site = pywikibot.Site("wikidata", "wikidata")
            WikiData.Value = Site.data_repository()
            WikiData.Init = True
            WikiData.Labels = dict()
            WikiData.Namespace = dict()
            WikiData.Namespace['P'] = 'Property:'
            WikiData.Namespace['Q'] = ''
        return WikiData.Value
    GetRepositery = staticmethod(GetRepositery)
    #
    #
    #
    #
    def GetLabel(Ident, Type, Lang='en'):
        Repo = WikiData.GetRepositery()
        Text = ""
        if Lang in WikiData.Labels:
            if Ident in WikiData.Labels[Lang]:
                Text = WikiData.Labels[Lang][Ident]
        else:
            WikiData.Labels[Lang] = dict()
        if Text == "":
            correct = True
            if Type == 'Q':
                if Ident[0] == 'Q' :
                    Item = pywikibot.ItemPage(Repo, Ident)
                else:
                    correct = False
                    Text = '#' + Ident
            elif Type == 'P' :
                Item = pywikibot.PropertyPage(Repo, Ident)
            else:
                DisplayMessages.warning("U", "GetIdentAndLabel", "bad type :" + Type)
                correct = False
                Text = ""
    
            if correct:
                try:
                    Item.get()
                except NotImplementedError:
                    Text = Ident 
                else:
                    try:
                        Text = Item.labels[Lang]
                    except KeyError :
                        Text = Ident
                        if not Lang in WikiData.ItemWithoutLabel:
                            WikiData.ItemWithoutLabel[Lang] = []
                        WikiData.ItemWithoutLabel[Lang].append(Ident)
                        DisplayMessages.warning("U", "GetIdentAndLabel", "no label (lang=" + Lang + ") for element '" + Ident + "'")
                    except UnicodeEncodeError :
                        Text = Ident

                    if not Lang == 'fr':
                        Text2 = WikiData.TestLabel(Item, Ident, 'fr')

                    if not Lang == 'en':
                        Text2 = WikiData.TestLabel(Item, Ident, 'en')
                    
            List = Text.split('œ')
            Text = "oe".join(List)
            List = Text.split('—')
            Text = "-".join(List)
            WikiData.Labels[Lang][Ident] = Text
        return Text
    GetLabel = staticmethod(GetLabel)
    #
    #
    #
    #
    def TestLabel(Item, Ident, Lang):
        try:
            Text = Item.labels[Lang]
        except KeyError :
            if not Lang in WikiData.ItemWithoutLabel:
                WikiData.ItemWithoutLabel[Lang] = []
            WikiData.ItemWithoutLabel[Lang].append(Ident)
            Text = Ident
        except UnicodeEncodeError :
            Text = Ident
        return Text
    TestLabel = staticmethod(TestLabel)
    #
    #
    #
    #
    def GetItemWithoutLabel(lang):
        if lang in WikiData.ItemWithoutLabel:
            return WikiData.ItemWithoutLabel[lang]
        else:
            return []
    GetItemWithoutLabel = staticmethod(GetItemWithoutLabel)





#======================================================
# Fonction ExtractItem
#
# aim : [[wikidata:Q6581097]] -> Q6581097
def ExtractItem(Claim):
    DisplayMessages.call("U", "ExtractItem", 7, "" )
    Value = Claim.getTarget()
    Text  = str(Value)
    Type  = Claim.getType()
    #DisplayMessages.debug("U", "ExtractItem", 7, "Value=" + str(Value) + ", Type=" + Type)
    if Type == 'wikibase-item':
        if Claim.getSnakType() == 'value':
            Text = Text[11:]
            Text = Text[:len(Text)-2]
        else:
            Text="None"
        
    elif Type == 'time':
        if Claim.getSnakType() == 'value':
            Text = ExtractDate(Value)
            if 'P1480' in Claim.qualifiers:
                iQua = 0
                while 1:
                    try:
                        vQua = Claim.qualifiers['P1480'][iQua].getTarget()
                    except IndexError:
                        break
                    if str(vQua) == "[[wikidata:Q5727902]]":
                        Text = "circa " + Text
                    iQua += 1
        else:
            Text="None"
            DateMin = ""
            if 'P1319' in Claim.qualifiers:
                DateMin = ExtractDate(Claim.qualifiers['P1319'][0].getTarget())
            DateMax = ""
            if 'P1326' in Claim.qualifiers:
                DateMax = ExtractDate(Claim.qualifiers['P1326'][0].getTarget())
            if not DateMin == "" and DateMax == "":
                Text = "after " + DateMin
            if DateMin == "" and not DateMax == "":
                Text = "before " + DateMax
            if not DateMin == "" and not DateMax == "":
                Text = "between " + DateMin + " and " + DateMax
    
    elif Type == 'url':
        Text = Claim._formatValue()
            
    elif Type == 'unicode' or Type == 'string':
        Text = Claim._formatValue()
        if not Text[:7] == 'http://':
            Text = TestString( Text, "ExtractItem, Type=" + Type )
            
    else:
        DisplayMessages.message("U", "ExtractItem", "unknown type : " + Type )
        Text = TestString( Text, "ExtractItem, Type=" + Type )
    return Text



def TestString(text, comment):
    try:
        Comment = DisplayMessages.Format("TestString", "calling" , comment + ", Text=" )
        print(Comment + text)
        Text = text
        DisplayMessages.call("U", "TestString", 7, comment )
    except UnicodeEncodeError :
        DisplayMessages.message("U", "TestString", comment + ", UnicodeEncodeError" )
        Text = ""
    return Text




def ExtractShortItem(Claim):
    DisplayMessages.call("U", "ExtractShortItem", 7, "" )
    Value = Claim.getTarget()
    Text  = str(Value)
    Type  = Claim.getType()
    #DisplayMessages.debug("U", "ExtractItem", 7, "Value=" + str(Value) + ", Type=" + Type)
    if Type == 'wikibase-item':
        if Claim.getSnakType() == 'value':
            Text = Text[11:]
            Text = Text[:len(Text)-2]
        else:
            Text="None"
        
    elif Type == 'time':
        if Claim.getSnakType() == 'value':
            Text = ExtractDate(Value)
        else:
            Text="None"
    
    elif Type == 'unicode' or Type == 'string' or Type == 'url':
        text = Claim._formatValue()
        
    else:
        print( "util_pwb.py : " + str(type(Value)) + ":" + Text )
        print( Claim._formatValue() )
    return Text








def GetIdentAndLabel(Ident, Type):
    #DisplayMessages.call("U", "GetIdentAndLabel", 7, "Ident=" + Ident + ", Type=" + Type )
    Label = WikiData.GetLabel(Ident, Type)
    if Label == "":
        Text = ""
    elif Label == Ident:
        Text = '(' + Ident + ')'
    elif Label == '#' + Ident:
        Text = Ident 
    else:
        Text = Label + ' (' + Ident + ')'
    return Text



def GetLinkAndLabel(Ident, Type):
    #DisplayMessages.call("U", "GetLinkAndLabel", 7, "Ident=" + Ident + ", Type=" + Type )
    Label = WikiData.GetLabel(Ident, Type)
    if Label == "":
        Text = ""
    elif Label == Ident:
        Text = '{{' + Type + '|' + Ident + '}}'
    elif Label == '#' + Ident:
        Text = Ident 
    else:
        Text = '[[' + WikiData.Namespace[Type] + Ident + '|' + Label + ' (' + Ident + ')]]'
    return Text








def testUtilPwb(*args):
    param = CallParameter(*args)
    DisplayMessages.SetFileName("")

    print("TestString = '" + TestString('Bonjour', 'testUtilPwb') + "'")
    text = TestString('Mercœur', 'testUtilPwb')
    print("TestString = '" + text + "'")
    text = TestString('Мария Терезия', 'testUtilPwb')
    print("TestString = '" + text + "'")
    text = TestString('Great Soviet Encyclopedia (1969—1978)', 'testUtilPwb')
    print("TestString = '" + text + "'")

    repo = WikiData.GetRepositery()
    for ident in ['Q822486', 'Q729206', 'Q90']:
        print( "" )
        print( GetLinkAndLabel(ident, 'Q') )
        item = pywikibot.ItemPage(repo, ident)
        try:
            item.get()
        except:
            print("Read error : not implemented")
        else:
            for prop in ['P26', 'P569', 'P570' ]:
                if prop in item.claims :
                    txtProp = GetLinkAndLabel(prop, 'P')
                    txtVal  = ExtractItem(item.claims[prop][0])
                    if txtVal[0] == 'Q' :
                        print( ' ' + txtProp + ' = ' + GetLinkAndLabel(txtVal, 'Q') )
                    else:
                        print( ' ' + txtProp + ' = ' + txtVal )
    print(WikiData.Labels)
    DisplayMessages.End()



if __name__ == "__main__":
    testUtilPwb()