-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraints.py
70 lines (65 loc) · 2.36 KB
/
constraints.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
#import pywikibot
import json
import re
import codecs
import pywikibot
filename = '/home/amir/constraints.json'
site = pywikibot.Site('en', 'wikipedia')
repo = site.data_repository()
property_id = 1234567
Qid_constraints = {
'Type': 123475,
'Value type': 1224,
}
qualifers_dict = {
'property': 12345,
'class': 124565,
'relation': 1234,
'instance': 1223,
'constraint_status': 1223,
'mandatory': 1234,
}
class Constraint(object):
"""docstring for Constraint."""
def fromJSON(self, dict_name):
self.type = dict_name['Constraint']
self.parameters = dict_name['Constraint_Parameters']
self.page = pywikibot.PropertyPage(repo, 'P' + dict_name['Property'])
def treat(self):
claim = pywikibot.Claim(repo, 'P%d' % property_id)
constraint_item = pywikibot.ItemPage(repo, Qid_constraints[self.type])
claim.setTarget(constraint_item)
for case in self.parameters:
qualifer = pywikibot.Claim(repo, 'P%d' % qualifers_dict[case])
if re.search('^Q\d+?$', self.parameters[case]):
qualifer_target = pywikibot.ItemPage(repo, self.parameters[case])
elif self.parameters[case] in qualifers_dict:
qualifer_target = pywikibot.ItemPage(repo, 'Q%d' % qualifers_dict[self.parameters[case]])
else:
qualifer_target = self.parameters[case]
qualifer.setTarget(qualifer_target)
claim.addQualifier(qualifer)
#Flusing away anything
self.page.addClaim(claim)
with codecs.open(filename, 'r', 'utf-8') as f:
constraints = json.loads(f.read())
for i in constraints[:10]:
res = None
for case in i['Constraint_Parameters']:
if re.search('^Q\d+?,', i['Constraint_Parameters'][case]):
res = i['Constraint_Parameters'][case].split(',')
if res is not None:
cases = []
for case in i['Constraint_Parameters']:
if re.search('^Q\d+?,', i['Constraint_Parameters'][case]):
for item in i['Constraint_Parameters'][case].split(','):
case_temp = i
case_temp['Constraint_Parameters'][case] = item
cases.append(case_temp)
else:
cases = [i]
for case in cases:
constraint = Constraint()
constraint.fromJSON(case)
constraint.treat()
print(constraints[0])