diff options
Diffstat (limited to 'data/wikidata/wikidata.py')
-rw-r--r-- | data/wikidata/wikidata.py | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/data/wikidata/wikidata.py b/data/wikidata/wikidata.py new file mode 100644 index 0000000..55ed496 --- /dev/null +++ b/data/wikidata/wikidata.py @@ -0,0 +1,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)) |