diff --git a/main.py b/main.py index 6dd4166..b704c87 100644 --- a/main.py +++ b/main.py @@ -1,80 +1,97 @@ #encoding=utf8 - -import requests -import os.path, time -import datetime +"""Wox plugin for caniuse.com""" +# import requests moved to get_data func +import os +import time import json import collections -from collections import OrderedDict -import webbrowser -from wox import Wox,WoxAPI - -class canIUse(Wox): - - data_fn = os.path.dirname(os.path.abspath(__file__)) + "\data.json" - - # write file - def write_file(self, filename, data): - f = open(filename, 'w') - f.write(data) - f.close() - - # read file - def read_file(self, filename): - f = open(filename, 'r') - data = f.read() - f.close() +from wox import Wox + +class CanIUse(Wox): + """Request data, save, return results by query""" + abs_path = os.path.dirname(os.path.abspath(__file__)) + data_fn = os.path.join(abs_path, "data.json") + + @staticmethod + def write_file(filename, data): + """ + Write file + + :param str filename: filename + :param str data: data to save + """ + file_ = open(filename, 'w') + file_.write(data) + file_.close() + + @staticmethod + def read_file(filename): + """ + Read file + + :param str filename: filename + :return: file data + """ + file_ = open(filename, 'r') + data = file_.read() + file_.close() return data - # create supported browser versions item - def browserVersion(self,stats): - version = "0" - for (key, value) in stats.items(): - if value == 'a x': - if version.find("+*-px-") > 0 : - continue - version = key + "+*-px-" - elif value == 'a': - if version.find("+*") > 0: + @staticmethod + def browser_version(stats): + """ + Create supported browser versions string + + :param stats dict: dict with all browser versions + :return str: supported version + """ + support = "" + for (key, val) in stats.items(): + if val.find("a") != -1: + if support.find("+*") > 0: continue - version = key+"+*"; - elif value == 'y x': - if version.find("-px-") > 0: + support = "".join([key, "+*"]) + elif val.find("y x") != -1: + if support.find("-px-") > 0: continue - version = key+"-px-"; - elif value == 'y': - version = key + "+" + support = "".join([key, "-px-"]) + elif val == "y": + support = "".join([key, "+"]) break - else: - version = "n/a" - return version + return support if support else "n/a" - # create string from stats - def get_stats(self, stats): - data = ", [" - data += "FF:" + stats['FF'] - data += ", CH:" + stats['Ch'] - data += ", SF:" + stats['S'] - data += ", IE:" + stats['IE'] - data += "]" + @staticmethod + def get_stats(stats): + """ + Create all browsers feature support in one string - return data + :param stats dict: dict with all browsers support + :return str: string with all browser feature support + """ + data = (stats['FF'], stats['Ch'], stats['S'], stats['IE']) + text = ", [FF:{0[0]}, CH:{0[1]}, SF:{0[2]}, IE:{0[3]}]".format(data) + + return text - # get data from https://raw.githubusercontent.com/Fyrd/caniuse/master/data.json def get_data(self): - r = requests.get('https://raw.githubusercontent.com/Fyrd/caniuse/master/data.json') - data = r.json(object_pairs_hook=collections.OrderedDict) + """ + Get raw data from github/Fyrd/caniuse/master/data.json + parse it and save to "cache" file + """ + import requests + req = requests.get('https://raw.githubusercontent.com/Fyrd/caniuse/master/data.json') + data = req.json(object_pairs_hook=collections.OrderedDict) parsed_data = data['data'] dct = {} for (key, val) in parsed_data.items(): title = val['title'] - url = "http://caniuse.com/#feat=" + key + url = "".join(["http://caniuse.com/#feat=", key]) description = val['description'] stats = {} for (browser, stat) in val['stats'].items(): - stats[browser] = self.browserVersion(stat) + stats[browser] = self.browser_version(stat) dct[key] = { "title":title, @@ -91,18 +108,24 @@ def get_data(self): if len(dct): self.write_file(self.data_fn, json.dumps(dct)) - def query(self,key): + def query(self, key): + """ + Get query, search in file and return results + :param str key: query string + :return: results + """ # if file not exists or file is old - create data.json - if not (os.path.isfile(self.data_fn)) or (os.path.getmtime(self.data_fn) <= int( time.time()-86400*7)) : + if not (os.path.isfile(self.data_fn)) or \ + (os.path.getmtime(self.data_fn) <= int(time.time()-86400*14)): self.get_data() # read data from file data = json.loads(self.read_file(self.data_fn), object_pairs_hook=collections.OrderedDict) query = key - results = [] - bykey = collections.OrderedDict() - bytitle = collections.OrderedDict() - bydesc = collections.OrderedDict() + results = [] + bykey = collections.OrderedDict() + bytitle = collections.OrderedDict() + bydesc = collections.OrderedDict() if query == "": results.append({ @@ -110,8 +133,8 @@ def query(self,key): "SubTitle": "Support tables for HTML5, CSS3, etc", "IcoPath":"Images\\caniuse.png", "JsonRPCAction":{ - "method": "", - "parameters":"", + "method": "open_url", + "parameters":["http://caniuse.com"], "dontHideAfterAction":True } }) @@ -125,11 +148,11 @@ def query(self,key): if value.find(query) == 0: results.append({ - "Title": result['title'] + self.get_stats(result['stats']), + "Title": "".join([result['title'], self.get_stats(result['stats'])]), "SubTitle": result['description'], "IcoPath":"Images\\caniuse.png", "JsonRPCAction":{ - "method": "openUrl", + "method": "open_url", "parameters":[result['url']], "dontHideAfterAction":False } @@ -143,10 +166,11 @@ def query(self,key): for (key, keyres) in bykey.items(): results.append({ - "Title": keyres['title'] + self.get_stats(keyres['stats']), + "Title": "".join([keyres['title'], self.get_stats(keyres['stats'])]), "SubTitle": keyres['description'], - "IcoPath":"Images\\caniuse.png","JsonRPCAction":{ - "method": "openUrl", + "IcoPath": "Images\\caniuse.png", + "JsonRPCAction":{ + "method": "open_url", "parameters":[keyres['url']], "dontHideAfterAction":False } @@ -154,10 +178,11 @@ def query(self,key): for (key, titleres) in bytitle.items(): results.append({ - "Title": titleres['title'] + self.get_stats(titleres['stats']), + "Title": "".join([titleres['title'], self.get_stats(titleres['stats'])]), "SubTitle": titleres['description'], - "IcoPath":"Images\\caniuse.png","JsonRPCAction":{ - "method": "openUrl", + "IcoPath": "Images\\caniuse.png", + "JsonRPCAction":{ + "method": "open_url", "parameters":[titleres['url']], "dontHideAfterAction":False } @@ -165,11 +190,11 @@ def query(self,key): for (key, descres) in bydesc.items(): results.append({ - "Title": descres['title'] + self.get_stats(descres['stats']), + "Title": "".join([descres['title'], self.get_stats(descres['stats'])]), "SubTitle": descres['description'], "IcoPath":"Images\\caniuse.png", "JsonRPCAction":{ - "method": "openUrl", + "method": "open_url", "parameters":[descres['url']], "dontHideAfterAction":False } @@ -177,10 +202,13 @@ def query(self,key): return results - def openUrl(self,url): - webbrowser.open(url) - # WoxAPI.shell_run(url) - # os.popen('"'+self.exe_path+'"' + ' --site=0' + server) + def open_url(self, url): + """ + Function for JsonRPCAction, open url + + :param str url: url to open + """ + os.startfile(url) if __name__ == "__main__": - canIUse() \ No newline at end of file + CanIUse() diff --git a/plugin.json b/plugin.json index 4875474..33593b0 100644 --- a/plugin.json +++ b/plugin.json @@ -4,7 +4,7 @@ "Name":"caniuse", "Description":"Support tables for HTML5, CSS3, etc", "Author":"roose", - "Version":"1.0.0", + "Version":"1.1.0", "Language":"python", "Website":"https://github.com/roose/Wox.Plugin.CanIUse", "IcoPath":"Images\\caniuse.png",