summaryrefslogtreecommitdiffstats
path: root/data/wikidata/wikidata.py
blob: 55ed496f829335aad63e3680c8ac985bdaf533a1 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/env python
# Copyright (C) 2020 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import os

# We don't need a configuration as we only need read-only access to the data
os.environ['PYWIKIBOT_NO_USER_CONFIG'] = '1'
import pywikibot
import pywikibot.config2

# Workaround the following bug:
# https://phabricator.wikimedia.org/T242081
pywikibot.config2.maxlag = 60

from tabulate import tabulate

from cache import *

class Wikidata(object):
    def __init__(self, cache=False):
        self.cache = cache
        if not self.cache:
            site = pywikibot.Site('wikidata', 'wikidata')
            self.repo = site.data_repository()
            replicant_id = 'Q7314062'
            self.replicant_page = pywikibot.ItemPage(self.repo, replicant_id).get()
        self.replicant_releases_data = self.get_all_compatible_devices()

    def print_variants_infos(self, variants):
        def append_header(headers, text):
            if text not in headers:
                headers.append(text)

        idx = 0
        headers = []
        table = []
        for variant, variant_data in variants.items():
            device_name = variant_data.get('device_name', None)
            modem = variant_data.get('modem', None)
            soc = variant_data.get('soc', None)
    
            table.append([])

            if device_name:
                append_header(headers, "Device")
                table[idx].append(device_name)
    
                append_header(headers, "Variant")
                table[idx].append(variant)
            else:
                append_header(headers, "Variant")
                table[idx].append(variant_name)
    
            if soc:
                append_header(headers, "SOC")
                table[idx].append(soc)
    
            if modem:
                append_header(headers, "Modem")
                table[idx].append(modem)

            idx += 1

        print(tabulate(table, headers, tablefmt="psql"))

    def print_compatible_variants(self, requested_variants):
        # Example: { 'GT-I9300': { 'device_name' : 'Galaxy SIII'}}
        results = {}
        data = self.replicant_releases_data

        for replicant_version in data.keys():
            for variant in data[replicant_version].keys():
                if variant in requested_variants:

                    results[variant] = data[replicant_version][variant]
        self.print_variants_infos(results)

    def get_variant_infos(self, variant_data, variant):
        device_name = variant_data.get('device_name', None)
        modem = variant_data.get('modem', None)
        soc = variant_data.get('soc', None)
    
        string = ''
        args = []
    
        if device_name:
            string += '- {} ({})'
            args += [device_name, variant]
        else:
            string += '- {}'
            args.append(variant_name)
    
        if soc:
            string += ': {}'
            args.append(soc)
    
        if modem:
            args.append(modem)
            if soc:
                string += ', {}'
            else:
                string += ': {}'
    
        return string.format(*args)

    def get_all_compatible_devices(self):
        # Example: {'6.0 0004': { 'GT-I9300': { 'device_name' : 'Galaxy SIII'}}}
        results = {}
    
        if self.cache:
            cache = Cache()
            #if cache.read():
            results = cache.load()
            return results
    
        replicant_releases_data = self.replicant_page.get('claims', {}).get('P348', [])
        for release_data in replicant_releases_data:
            replicant_version = release_data.getTarget()
            assert (replicant_version != None), 'replicant_version == None'
    
            if replicant_version not in results:
                results[replicant_version] = {}
    
            release_qualifiers = release_data.toJSON().get('qualifiers', {})
            compatible_variants = release_qualifiers.get('P400', [])
            for compatible_variant in compatible_variants:
                variant_id = get_id(compatible_variant)
                variant_page = pywikibot.ItemPage(self.repo, variant_id).get()
                variant_name = get_label(variant_page)
                model_name = get_variant_device_name(variant_page)
                assert (variant_name != None), 'variant_name == None'
    
                if  variant_name not in results[replicant_version]:
                    results[replicant_version][variant_name] = {}
    
                variant_results = results[replicant_version][variant_name]
    
                # Enable model_name to be None as it's not essential
                if model_name not in results[replicant_version][variant_name]:
                    variant_results['device_name'] = model_name
    
                specifications = get_variant_device_specifications(self.repo, variant_page)
                variant_results.update(specifications)
        if self.cache:
            cache.store(results)
            cache.close()
    
        return results

    def print_compatible_devices(self):
        # Example: {'6.0 0004': { 'GT-I9300': { 'device_name' : 'Galaxy SIII'}}}
        data = self.replicant_releases_data
        for replicant_version in data.keys():
            print('Replicant {}:'.format(replicant_version))
            for variant in data[replicant_version].keys():
                variant_data = data[replicant_version][variant]
                print(self.get_variant_infos(variant_data, variant))