summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2020-04-15 05:02:19 +0200
committerDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2020-09-10 14:59:55 +0200
commitdc6b73a4c72b0d4ea3a5c1baa6c00fde39e6bad8 (patch)
treea37c8d350609aabf00894c045b3d0c4dc740a3d4
parent2630ec59947816d2449a42e433e39b21573be3e6 (diff)
downloadvendor_replicant-scripts-dc6b73a4c72b0d4ea3a5c1baa6c00fde39e6bad8.tar.gz
vendor_replicant-scripts-dc6b73a4c72b0d4ea3a5c1baa6c00fde39e6bad8.tar.bz2
vendor_replicant-scripts-dc6b73a4c72b0d4ea3a5c1baa6c00fde39e6bad8.zip
WIP: improve wikidata
Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
-rwxr-xr-xdata/wikidata/devices.py141
1 files changed, 108 insertions, 33 deletions
diff --git a/data/wikidata/devices.py b/data/wikidata/devices.py
index 8a0a5a7..8fc588a 100755
--- a/data/wikidata/devices.py
+++ b/data/wikidata/devices.py
@@ -15,6 +15,10 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
+
+from optparse import OptionParser
+from tabulate import tabulate
+
# 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
@@ -53,7 +57,7 @@ def is_a_smartphone_or_tablet_model(page):
return False
-def get_variant_device_specifications(variant_page):
+def get_variant_device_specifications(repo, variant_page):
results = {}
parts = variant_page.get('claims', {}).get('P527', [])
for part in parts:
@@ -92,7 +96,7 @@ def get_variant_device_name(variant_page):
return None
-def get_all_compatible_devices(replicant_page):
+def get_all_compatible_devices(repo, replicant_page):
# Example: {'6.0 0004': { 'GT-I9300': { 'device_name' : 'Galaxy SIII'}}}
results = {}
@@ -122,51 +126,122 @@ def get_all_compatible_devices(replicant_page):
if model_name not in results[replicant_version][variant_name]:
variant_results['device_name'] = model_name
- specifications = get_variant_device_specifications(variant_page)
+ specifications = get_variant_device_specifications(repo, variant_page)
variant_results.update(specifications)
return results
-def print_compatible_devices(data):
- # Example: {'6.0 0004': { 'GT-I9300': { 'device_name' : 'Galaxy SIII'}}}
+def get_variant_infos(variant_data, variant):
+ device_name = variant_data.get('device_name', None)
+ modem = variant_data.get('modem', None)
+ soc = variant_data.get('soc', None)
- for replicant_version in data.keys():
- print('Replicant {}:'.format(replicant_version))
- for variant in data[replicant_version].keys():
- variant_data = data[replicant_version][variant]
- device_name = variant_data.get('device_name', None)
- modem = variant_data.get('modem', None)
- soc = variant_data.get('soc', None)
+ string = ''
+ args = []
- string = ''
- args = []
+ if device_name:
+ string += '- {} ({})'
+ args += [device_name, variant]
+ else:
+ string += '- {}'
+ args.append(variant_name)
- if device_name:
- string += '- {} ({})'
- args += [device_name, variant]
- else:
- string += '- {}'
- args.append(variant_name)
+ if soc:
+ string += ': {}'
+ args.append(soc)
- if soc:
- string += ': {}'
- args.append(soc)
+ if modem:
+ args.append(modem)
+ if soc:
+ string += ', {}'
+ else:
+ string += ': {}'
- if modem:
- args.append(modem)
- if soc:
- string += ', {}'
- else:
- string += ': {}'
+ return string.format(*args)
- print(string.format(*args))
+def print_variant_infos_2(variant_data, variant):
+ device_name = variant_data.get('device_name', None)
+ modem = variant_data.get('modem', None)
+ soc = variant_data.get('soc', None)
+ headers = []
+ table = []
+ table.append([])
-if __name__ == '__main__':
+ if device_name:
+ headers.append("Device")
+ table[0].append(device_name)
+
+ headers.append("Variant")
+ table[0].append(variant)
+ else:
+ headers.append("Variant")
+ table[0].append(variant_name)
+
+ if soc:
+ headers.append("SOC")
+ table[0].append(soc)
+
+ if modem:
+ headers.append("Modem")
+ table[0].append(modem)
+
+ print(tabulate(table, headers, tablefmt="psql"))
+
+def print_compatible_devices(data):
+ # Example: {'6.0 0004': { 'GT-I9300': { 'device_name' : 'Galaxy SIII'}}}
+ 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(get_variant_infos(variant_data, variant))
+
+def print_compatible_variant(data, requested_variant):
+ # Example: { 'GT-I9300': { 'device_name' : 'Galaxy SIII'}}
+ for replicant_version in data.keys():
+ for variant in data[replicant_version].keys():
+ if variant == requested_variant:
+ variant_data = data[replicant_version][variant]
+ print_variant_infos_2(variant_data, variant)
+ return
+
+def print_device_infos(variant):
wikidata = pywikibot.Site('wikidata', 'wikidata')
repo = wikidata.data_repository()
replicant_id = 'Q7314062'
replicant_page = pywikibot.ItemPage(repo, replicant_id).get()
- replicant_releases_data = get_all_compatible_devices(replicant_page)
- print_compatible_devices(replicant_releases_data)
+ # TODO: obtimize that, either through caching or mimizing the querry
+ # caching is better for privacy and speed
+ replicant_releases_data = get_all_compatible_devices(repo, replicant_page)
+ print_compatible_variant(replicant_releases_data, variant)
+
+if __name__ == '__main__':
+ parser = OptionParser()
+ parser.add_option('-t', '--match-token', dest='match_token',
+ action='append',
+ help='Search for information with tokens named NAME that have the value value,',
+ metavar='NAME=value')
+
+ (options, args) = parser.parse_args()
+
+ parser_options = options.__dict__
+ if parser_options.get('match_token', None):
+ # TODO: handle multiple constraints
+ for match_token in options.match_token:
+ # TODO: check format
+ (key, value) = match_token.split('=')
+ if key == 'device':
+ # TODO: sanity check on value?
+ print_device_infos(value)
+ pass
+ else:
+ #TODO
+ pass
+ else:
+ wikidata = pywikibot.Site('wikidata', 'wikidata')
+ repo = wikidata.data_repository()
+ replicant_id = 'Q7314062'
+ replicant_page = pywikibot.ItemPage(repo, replicant_id).get()
+ replicant_releases_data = get_all_compatible_devices(repo, replicant_page)
+ print_compatible_devices(replicant_releases_data)