summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2020-04-15 07:22:50 +0200
committerDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2020-09-10 14:59:55 +0200
commit0366c773d8a5a5791ff2fec34c680c3a1717cb83 (patch)
tree582d929815ce296e8cbcb6ebba2df2c167d40cda
parentdc6b73a4c72b0d4ea3a5c1baa6c00fde39e6bad8 (diff)
downloadvendor_replicant-scripts-0366c773d8a5a5791ff2fec34c680c3a1717cb83.tar.gz
vendor_replicant-scripts-0366c773d8a5a5791ff2fec34c680c3a1717cb83.tar.bz2
vendor_replicant-scripts-0366c773d8a5a5791ff2fec34c680c3a1717cb83.zip
WIP: add cache
Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
-rw-r--r--data/wikidata/cache.py45
-rwxr-xr-xdata/wikidata/devices.py58
2 files changed, 88 insertions, 15 deletions
diff --git a/data/wikidata/cache.py b/data/wikidata/cache.py
new file mode 100644
index 0000000..90e2d7a
--- /dev/null
+++ b/data/wikidata/cache.py
@@ -0,0 +1,45 @@
+#!/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 json
+import os
+import sys
+
+class Cache:
+ def __init__(self):
+ file_valid = False
+
+ self.path = os.path.dirname(sys.argv[0]) + os.sep + 'cache.json'
+
+ if os.path.exists(self.path):
+ self.file = open(self.path, 'r')
+ big_string = self.file.read()
+ self.json = json.loads(big_string)
+ file_valid = True
+
+ if not file_valid:
+ self.file = open(self.path, 'w')
+ def close(self):
+ self.file.close()
+ def load(self):
+ return self.json
+ def store(self, data):
+ json_str = str(json.dumps(data))
+ self.file.write(json_str)
+ def __str__(self):
+ pass
+ def __repr__(self):
+ pass
diff --git a/data/wikidata/devices.py b/data/wikidata/devices.py
index 8fc588a..61483d0 100755
--- a/data/wikidata/devices.py
+++ b/data/wikidata/devices.py
@@ -24,6 +24,8 @@ os.environ['PYWIKIBOT_NO_USER_CONFIG'] = '1'
import pywikibot
import pywikibot.config2
+from cache import *
+
# Workaround the following bug:
# https://phabricator.wikimedia.org/T242081
pywikibot.config2.maxlag = 60
@@ -96,9 +98,16 @@ def get_variant_device_name(variant_page):
return None
-def get_all_compatible_devices(repo, replicant_page):
+def get_all_compatible_devices(repo, replicant_page, caching=True):
# Example: {'6.0 0004': { 'GT-I9300': { 'device_name' : 'Galaxy SIII'}}}
results = {}
+ cache = None
+
+ if caching:
+ cache = Cache()
+ #if cache.read():
+ results = cache.load()
+ return results
replicant_releases_data = replicant_page.get('claims', {}).get('P348', [])
for release_data in replicant_releases_data:
@@ -128,6 +137,9 @@ def get_all_compatible_devices(repo, replicant_page):
specifications = get_variant_device_specifications(repo, variant_page)
variant_results.update(specifications)
+ if cache:
+ cache.store(results)
+ cache.close()
return results
@@ -206,18 +218,25 @@ def print_compatible_variant(data, requested_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()
- # 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)
+def print_device_infos(variant, cache=False):
+ replicant_releases_data = None
+ if not cache:
+ 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)
+ else:
+ replicant_releases_data = get_all_compatible_devices(None, None)
+
print_compatible_variant(replicant_releases_data, variant)
if __name__ == '__main__':
parser = OptionParser()
+ parser.add_option('-c', '--cache', dest='cache',
+ action="store_true",
+ help='Use the local cache only (offline mode)',
+ metavar='NAME=value')
parser.add_option('-t', '--match-token', dest='match_token',
action='append',
help='Search for information with tokens named NAME that have the value value,',
@@ -225,7 +244,10 @@ if __name__ == '__main__':
(options, args) = parser.parse_args()
+
parser_options = options.__dict__
+
+ cache = parser_options.get('cache', False)
if parser_options.get('match_token', None):
# TODO: handle multiple constraints
for match_token in options.match_token:
@@ -233,15 +255,21 @@ if __name__ == '__main__':
(key, value) = match_token.split('=')
if key == 'device':
# TODO: sanity check on value?
- print_device_infos(value)
+ print_device_infos(value, cache)
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)
+ replicant_releases_data = None
+ if not cache:
+ 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)
+ else:
+ replicant_releases_data = get_all_compatible_devices(None, None)
+
print_compatible_devices(replicant_releases_data)
+