def _ask(self, line): if not line: return qst, ans = self._get_qst_ans(line) msg = Practice._get_msg_base(qst) if ans.lang.hint or ans.lang.dynamic: if ans.lang.dynamic: Record = Query() results = self.db.search(Record.ln == line) # Get most recent results. num_results = 3 results = sorted(results, key=lambda r: r['dt'], reverse=True)[:num_results] oks = [r['ok'] for r in results] while len(oks) < num_results: oks.append(0.7) ratio = 0 if results: ratio = statistics.mean(oks) ans.lang.hint = dynamic_hintnum(ans.rand, ratio) if ans.lang.hint: msg += Practice._get_msg_hint(ans) talk_qst = callstop(talk) tries = 0 while True: q.alert(msg) if qst.lang.talk: talk_qst(qst.rand, qst.lang.name.short) t_start = time.time() rsp = q.ask_str("").lower().strip() sec = time.time() - t_start vld = Practice._get_valid(ans) record = self._prep_record(line, rsp, qst, ans, sec, tries) closest_orig, _ = guess_similarity(rsp, Practice._get_valid_orig(ans)) if rsp in vld: record['ok'] = 1.0 q.echo("[CORRECT] " + ans.text) if self.config.record: self.db.insert(record) else: tries += 1 _, record['ok'] = guess_similarity(rsp, vld) q.error(closest_orig) if self.config.record: self.db.insert(record) if self.config.redo: continue if tries > 0: self.miss.add(line) else: self.okay.add(line) if ans.lang.talk: say, _ = guess_similarity(rsp, Practice._get_valid_orig(ans)) talk(say, ans.lang.name.short, wait=True) return
def download_api(srcurl, dstdir): items = requests.get(srcurl).json() if op.isfile(dstdir): raise Exception("DestDirIsFile") fsys.makedirs(dstdir, ignore_extsep=True) if isinstance(items, dict) and "message" in items.keys(): qprompt.error(items['message']) return for item in items: if "file" == item['type']: fpath = op.join(dstdir, item['name']) with io.open(fpath, "w", encoding="utf-8") as fo: text = requests.get(item['download_url']).text fo.write(text) else: download_api(item['url'], op.join(dstdir, item['name']))
def updatePass(): global pragma_input conn = sqlcipher.connect('.passman.db') cur = conn.cursor() cur.execute(pragma_input) print(pd.read_sql_query( "SELECT * FROM passwords", conn)) #display all passwords so user can easily see its ID print("Select the ID of the credentials you wish to EDIT") try: ID = qprompt.ask_int("ID") #ask user for ID of credential to edit except: qprompt.error("ID NOT FOUND") #error if ID is not found retry = qprompt.ask_yesno( "Retry?") #prompt user to retry or exit to main menu if retry == False: mainMenu() else: updatePass() print( "Credential set selected! What would you like to change?" ) #ask user what about the chosen set of credentials they would like to change qprompt.alert("HINT: Use '?' for a list of options") selection = qprompt.ask_str("Edit", valid=["website", "username", "password"]) if selection.lower() == "website": new = qprompt.ask_str("Enter new value") cur.execute( "UPDATE passwords SET WEBSITE = '{}' WHERE ID = '{}'".format( new, ID)) #updates record with new info elif selection.lower() == "username": new = qprompt.ask_str("Enter new value") cur.execute( "UPDATE passwords SET USERNAME = '******' WHERE ID = '{}'".format( new, ID)) elif selection.lower() == "password": new = qprompt.ask_str("Enter new value") cur.execute( "UPDATE passwords SET PASSWORD = '******' WHERE ID = '{}'".format( new, ID)) conn.commit() cur.close() mainMenu()
def show_create_user_menu(self): # TODO: uncomment it: qprompt.ask_captcha(length=6) username = qprompt.ask_str("Enter username", valid=lambda x: len(x) > 3) password = qprompt.ask_str("Enter password", valid=lambda x: len(x) > 4, shw=False) create_user_callback = self.opts.get(self.OPTS_CREATE_USER, None) if create_user_callback is not None: status, msg = create_user_callback(username, password) if status: qprompt.status("Creating user succeeded.", time.sleep, [2]) self.username = username self.password = password self.login_status = (True, self.username) else: qprompt.error("Creating user failed: {}".format(msg)) else: qprompt.error("Cannot proceed with creating user - internal error")
def sort_file(path=None): """Alphabetically sort the contents of the given vocabulary txt file.""" if not path: path = ask_file("File to sort") if not path.endswith(".txt"): q.error("Can only sort `.txt` files!") return with open(path) as fi: lines = fi.read().splitlines() sorts = [] okay = True for num, line in enumerate(lines): line = line.strip() try: if not line: continue l1, l2 = line.split(";") l1x = "" l2x = "" if l1.find("(") > -1: l1, l1x = l1.split("(") l1 = l1.strip() l1x = " (" + l1x if l2.find("(") > -1: l2, l2x = l2.split("(") l2 = l2.strip() l2x = " (" + l2x l1 = "/".join(sorted(l1.split("/"))) l2 = "/".join(sorted(l2.split("/"))) sorts.append("%s%s;%s%s" % (l1, l1x, l2, l2x)) except: okay = False temp = unicodedata.normalize('NFKD', line).encode('ascii', 'ignore').strip() q.warn("Issue splitting `%s` line %u: %s" % (path, num, temp)) sorts.append(line) with open(path, "w") as fo: for line in sorted(set(sorts)): fo.write(line + "\n") return okay
def edit_scale(pad, verbose): global config rbank = int((pad - 1) / _PADS) rsubpad = pad - (_PADS * rbank) - 1 ptype = config[1][rbank][rsubpad]['type'] if ptype == "BANK": qprompt.error("Pad %d (Bank %s-%d) is configured as a BANK" % (pad, chr(65 + rbank), rsubpad + 1)) return menu = qprompt.Menu() x = 0 for y in Scale._SCALE_PATTERNS: menu.add(str(x), y) x = x + 1 stype = menu.show(hdr="Pad %d (Bank %s-%d):" % (pad, chr(65 + rbank), rsubpad + 1), msg="Scale", returns="desc") root = qprompt.ask_int("Note", vld=list(range(0, 128)), dft=config[1][rbank][rsubpad]['note']) count = qprompt.ask_int("Count", vld=[0] + list(range(1, _PTOTAL + 2 - pad)), dft=0) same = qprompt.ask_yesno(msg="Config all as per Pad %d?" % pad, dft='N') scale = Scale.build_scale(Note.from_midi_num(root), stype) scale_lst = [] for note in scale: scale_lst.append(note.midi_note_number()) if count: while len(scale_lst) < count: root = scale_lst.pop() scale = Scale.build_scale(Note.from_midi_num(root), stype) for note in scale: scale_lst.append(note.midi_note_number()) else: count = len(scale_lst) if pad + count > _PTOTAL: count = _PTOTAL + 1 - pad for note in scale_lst[:count]: bank = int((pad - 1) / _PADS) subpad = pad - (_PADS * bank) - 1 if same and ptype == "NOTE": config[1][bank][subpad]['type'] = config[1][rbank][rsubpad]['type'] config[1][bank][subpad]['channel'] = config[1][rbank][rsubpad][ 'channel'] config[1][bank][subpad]['trigger'] = config[1][rbank][rsubpad][ 'trigger'] config[1][bank][subpad]['aftertouch'] = config[1][rbank][rsubpad][ 'aftertouch'] if same and ptype == "PROG": config[1][bank][subpad]['type'] = config[1][rbank][rsubpad]['type'] config[1][bank][subpad]['channel'] = config[1][rbank][rsubpad][ 'channel'] if ptype == "NOTE": config[1][bank][subpad]['note'] = note else: config[1][bank][subpad]['program'] = note if verbose: print("Setting Pad %d (Bank %s-%d) to %d (%s)" % (pad, chr(65 + bank), subpad + 1, note, Note.from_midi_num(note))) pad = pad + 1
from qprompt import alert, echo, error, warn, info echo("Just a message.") alert("Heads up!") info("Hmm...") warn("Uh oh...") error("OMG this is bad!") error("REALLY BAD", end="!!!!!!!\n")