def execute_sync(direction, option): """ run the sync according to user select. direction: up or down option: conf or plugin sync_settings: dictionary for sync_settings.json, contain gist id and auth. """ config = Config('sync_settings') sync_settings = config.readData() gist = sync_settings.get('gist', None) auth = sync_settings.get('auth', None) if direction == 'up': if not auth: click.echo( td(text= "<a> Set up Auth Token to Upload settings. </a>\nRefer to: [https://github.com/settings/tokens]" )) return data = extract_settings(option) write_gist(data, auth, gist, option) return elif direction == 'down': if not gist: click.echo( td(text= "<a> Set up gist ID to download settings. </a>\nRefer to: [https://gist.github.com/]" )) return data = extract_gist(gist, option) write_settings(data, option) return
def print_settings(ctx, param, value): if not value or ctx.resilient_parsing: return data = Config('sync_settings').readData() td = TableDisplay(({"[]": "fG"})) click.echo( td(title=">>> [Sync Settings] <<<", text=json.dumps(data, indent=2).strip('{}'))) ctx.exit()
def update_Sync_settings(gist=None, auth=None): config = Config('sync_settings') data = config.readData() if gist is not None: data.update(gist=gist.strip()) click.echo(td(text=f"Saved Gist ID: [<{gist}>].")) if auth is not None: data.update(auth=auth.strip()) click.echo(td(text=f"Saved auth: [<{auth}>].")) config.saveData(data)
def extract_settings(settings): """ extract all config files to a dictionary """ data = {} if settings == 'conf': for name in Config.list_config(): if name != 'sync_settings': data[name] = Config(name).readData() elif settings == 'plugin': for filename in os.listdir(plugin_folder): if filename.endswith('.py'): with open(os.path.join(plugin_folder, filename), 'rt') as f: data[filename] = f.read() return data
def write_settings(data, settings): """ write all the config files to conf folder. """ if settings == 'conf': for name, settings in data.items(): if name != 'sync_settings': config = Config(name) config.saveData(settings) click.echo( td(text= f"<g>Success!</g> <y>{name}</y> configuration imported." )) elif settings == 'plugin': for name, txt in data.items(): with open(os.path.join(plugin_folder, name), 'wt') as f: f.write(txt) click.echo( td(text=f"<g>Success!</g> Plugin <y>{name[:-3]}</y> imported.") )
import click import requests from cli.utils import ColorText,TableDisplay,Config import json APIs = Config('api') _data = APIs.readData() COL_API_KEY=_data.get("COL_API_KEY",None) MED_API_KEY=_data.get("MED_API_KEY",None) URBAN_API_KEY=_data.get("URBAN_API_KEY",None) def lookupMW(word,limit): url1=f"https://www.dictionaryapi.com/api/v3/references/collegiate/json/{word}?key={COL_API_KEY}" url2=f"https://www.dictionaryapi.com/api/v3/references/medical/json/{word}?key={MED_API_KEY}" try: if COL_API_KEY is None or (MED_API_KEY is None): raise ValueError ("Missing API key for M-W dictionary. Run def --config to setup.") r1 = requests.request("GET", url1, ).json() r2 = requests.request("GET", url2, ).json() def process(r): result = [] for k,d in enumerate(r[0:limit]): if not isinstance(d,dict): continue _def = [("<gDef {}.g> [{}]".format(k+1,d.get('fl','None')))] for i,l in enumerate(d.get('shortdef',[])): _def.append(f" [{i+1}]. {l}") result.append(_def) return result res1 = process(r1) res2 = process(r2)
import click import subprocess as sub from cli.utils import TableDisplay, Config import os config = Config('folders') td = TableDisplay() def displayMenu(data): data = data.get('favorites', {}) dis = [] for k in sorted(data.keys()): dis.append(f" >><g> [ {k} ] </g><< {data[k]} ") click.echo(td(title="[>> Favorite Folders <<]", text=[dis])) def mark_fav(ctx, param, value): if not value or ctx.resilient_parsing: return cwd = os.getcwd() key = click.prompt( td(text=f'Enter name for favorite folder\n@ [{cwd}]') + '\n') data = config.readData() if data.get('favorites', None) is None: data['favorites'] = {} todis = [] for k in key.split():
import click import subprocess from cli.utils import TableDisplay, Config from cli._version_ import __version__ from cli.ok import print_version TL_CONFIG = Config('tools') td = TableDisplay() def displayMenu(data): dis = [] for k in sorted(data.keys()): dis.append(f" >><g> [ {k} ] </g><< {data[k]['name']} ") click.echo(td(title= "[>>Tools Menu<<]", text=[dis])) def displayCommand(data,items): if not items: items = sorted(data.keys()) dis = [] for k in items: if k in data: dis.extend([f" >><g> [ {k} ] </g><< {data[k]['name']}", f" {data[k]['command']}"]) else: dis.append(f" >><r> [ {k} ] </r><< is not in your tool list.") click.echo(td(title= "[>>Tools Menu<<]", text=[dis])) def run_tool(key,data): """ key is the key to command dictionary sotred in data dict.