-
Notifications
You must be signed in to change notification settings - Fork 4
/
insert-property.py
113 lines (85 loc) · 3.03 KB
/
insert-property.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
import pywikibot, csv
import get_property_list
"""
This script matches Wikibase items with a property (used as "identifier")
and appends them a new claim with a new property.
It uses a CSV file to find the matches.
Arguments:
-identifier:<N>
Number of the property needed to identify the items
-insert:<N>
Number of the property to add
-file:<source.csv>
CSV file which has two columns:
1. the value of the property indicated in the argument -identifier;
2. the value of the property -insert, to add to the items.
Format:
X;Y
Example usage:
python insert-property.py -file:TM_EDR.csv -identifier:38 -insert:3
"""
def main():
selectedSet = None
always = False
idProp = insertProp = filename = False
# Handles command-line arguments for pywikibot.
for arg in pywikibot.handleArgs():
if arg.startswith('-identifier:'):
idProp = arg.replace('-identifier:', '')
if arg.startswith('-insert:'):
insertProp = arg.replace('-insert:', '')
if arg.startswith('-file:'):
filename = arg.replace('-file:', '')
if arg.startswith('-always'):
always=True
if idProp == None or insertProp == None or filename == None:
pywikibot.output('All arguments are required!')
return
matchDict = {}
with open(filename, 'r') as f:
reader = csv.reader(f, delimiter=";")
for row in reader:
matchDict[row[0]] = row[1]
bot = PropertyBot(always=always, idProp=idProp, insertProp=insertProp, matchDict=matchDict)
bot.run()
class PropertyBot(pywikibot.bot.Bot):
availableOptions = {
'always': False, # ask for confirmation when putting a page?
'idProp': None,
'insertProp': None,
'matchDict': None,
}
def run(self):
# pywikibot/families/eagle_family.py
site = pywikibot.Site('en', 'eagle')
repo = site.data_repository()
idProp = self.getOption('idProp')
insertProp = self.getOption('insertProp')
matchDict = self.getOption('matchDict')
itemList = get_property_list.getItemsForProperty(repo, idProp, additionalProperties=[insertProp])
for i in itemList:
if i['property'] not in matchDict:
continue
self.current_page = pywikibot.ItemPage(repo, i['title'])
pywikibot.output('Item matched by P' + idProp + ': ' + i['property'])
toInsert = matchDict[i['property']]
pywikibot.output('Attempting to insert P' + insertProp + ': ' + toInsert)
if insertProp in i['additionalProperties']:
if i['additionalProperties'][insertProp] == toInsert:
pywikibot.output('The item already has the property '
+ insertProp + ' with an identical content. Skipping')
else:
pywikibot.output('The item already has the property '
+ insertProp + ' with a different content: ' + i['additionalProperties'][insertProp] +
'. Skipping')
continue
newClaim = pywikibot.Claim(repo, 'P' + insertProp)
newClaim.setTarget(toInsert)
if self.user_confirm('Do you really want do add the claim for property P' + insertProp + '?'):
self.current_page.addClaim(newClaim)
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()