def make_dir(inpath, tmpldict, outpath=None, pathsubs=None): pathsubs = pathsubs or [] inpath = op.abspath(inpath) bpath = op.basename(inpath) if not outpath: outpath = os.getcwd() dname = render_str(bpath, tmpldict) if not dname: return False mpath = op.abspath(op.join(outpath, dname)) if not mpath: return False for sub in pathsubs: mpath = mpath.replace(*sub) if inpath == mpath: qprompt.fatal("Output cannot overwrite input template!") mpath = render_str(mpath, tmpldict) qprompt.status("Making dir `%s`..." % (mpath), fsys.makedirs, [mpath]) # Iterate over files and directories IN PARENT ONLY. for r, ds, fs in os.walk(inpath): for f in fs: ipath = op.join(r, f) fname = render_str(f, tmpldict) opath = op.join(mpath, fname) if not make_file(ipath, tmpldict, opath): return False for d in ds: ipath = op.join(r, d) if not make_dir(ipath, tmpldict, mpath, pathsubs=pathsubs): return False break # Prevents from walking beyond parent. return True
def make_file(inpath, tmpldict, outpath=None): inpath = op.abspath(inpath) if outpath: outpath = render_str(outpath, tmpldict) if op.isdir(outpath): outpath = op.join(outpath, op.basename(inpath)) outpath = render_str(outpath, tmpldict) if is_binary(inpath): qprompt.status("Copying `%s`..." % (outpath), fsys.copy, [inpath, outpath]) return text = render_file(inpath, tmpldict) if text == None: return False # Handle rendered output. if outpath: outpath = op.abspath(outpath) if inpath == outpath: qprompt.fatal("Output cannot overwrite input template!") fsys.makedirs(op.dirname(outpath)) with io.open(outpath, "w", encoding="utf-8") as f: qprompt.status("Writing `%s`..." % (outpath), f.write, [text]) else: qprompt.echo(text) return True
def test_status_3(self): for delay in DELAYS: t_start = get_time() rand1 = random.randint(1, 100) rand2 = random.randint(1, 100) result = status("Doing another...", do_another, [delay, rand1], {'c': rand2}) self.assertAlmostEqual(delay, get_time() - t_start, places=PLACES) self.assertEqual(rand1+rand2, result)
def test_status_2(self): for delay in DELAYS: t_start = get_time() rand1 = random.randint(1, 100) rand2 = random.randint(1, 100) result = status("Doing something...", do_something, [delay, rand1, rand2]) self.assertAlmostEqual(delay, get_time() - t_start, places=PLACES) self.assertEqual(rand1+rand2, result)
def test_status_3(test): for delay in DELAYS: t_start = get_time() rand1 = random.randint(1, 100) rand2 = random.randint(1, 100) result = status("Doing another...", do_another, [delay, rand1], {'c': rand2}) test.assertAlmostEqual(delay, get_time() - t_start, places=PLACES) test.assertEqual(rand1+rand2, result)
def test_status_2(test): for delay in DELAYS: t_start = get_time() rand1 = random.randint(1, 100) rand2 = random.randint(1, 100) result = status("Doing something...", do_something, [delay, rand1, rand2]) test.assertAlmostEqual(delay, get_time() - t_start, places=PLACES) test.assertEqual(rand1+rand2, result)
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_all(dirpath): """Alphabetically sort the contents of all found vocabulary txt files in the given directory path.""" okay = True for f in listdir(dirpath): if f.endswith(".txt"): okay &= q.status("Sorting `%s`..." % op.basename(f), sort_file, [f]) msg = "All files sorted successfully." if okay else "Issue sorting some files!" char = "-" if okay else "!" q.wrap(msg, char=char)
def run(): global PROCS if is_running(): warn("App server already running!") return with Cwd("app"): if not op.isdir("node_modules"): status("Running NPM install...", silent, ["npm install"]) PROCS.append( start( "node node_modules/rollup/dist/bin/rollup -w -c rollup.config.js" )) PROCS.append(start("python app.py")) alert("Application starting...") time.sleep(3) if all(map(lambda p: p.isrunning(), PROCS)): alert("Application started.") browse() else: warn("Issue starting application!") stop()
def create_picnotes(dirpath, confirm=True, shrink=False): """Attempts to extract notes from all pics found under the given directory and write them to a file.""" dirpath = op.abspath(dirpath) titlepath = os.sep.join(dirpath.split(os.sep)[-2:]) pics = list(auxly.filesys.walkfiles(dirpath, ".(png|jpg)", recurse=True)) if not pics: qprompt.warn("No pics found under directory!") return pics = sort_pics(pics) qprompt.alert(f"Found {len(pics)} pics found under directory.") doc = auxly.filesys.File(dirpath, "pic_notes.adoc") existing_notes = {} if doc.exists(): existing_notes = parse_picnotes(doc) if confirm: if not qprompt.ask_yesno( f"The `pic_notes.adoc` file already exists with {len(existing_notes)} pic notes found, overwrite it?" ): return doc.empty() qprompt.alert(f"Initialized file `{doc.path}`") doc.appendline(f"= PIC NOTES: `{titlepath}`") doc.appendline(":date: " + datetime.now().strftime("%d %B %Y %I:%M%p")) doc.appendline(":toc:") doc.appendline("") doc.appendline("NOTE: Entries sorted by base filename.") doc.appendline("") count = {'reused': 0, 'scanned': 0} for idx, picpath in enumerate(pics, 1): relpath = op.relpath(picpath, dirpath) msg = f"({idx} of {len(pics)})" if relpath in existing_notes.keys() and auxly.filesys.checksum( picpath) == existing_notes[relpath]['md5']: qprompt.alert(f"{msg} Reusing `{picpath}`.") notes = existing_notes[relpath]['note'] if shrink: attempt_shrink(picpath, notes) line = format_adoc_line(relpath, picpath, notes) count['reused'] += 1 else: notes = qprompt.status(f"{msg} Scanning `{picpath}`...", scan_notes, [picpath]) or "NA" if shrink: attempt_shrink(picpath, notes) line = format_adoc_line(relpath, picpath, notes) count['scanned'] += 1 doc.appendline(line) return count
def test_status_1(self): for delay in DELAYS: t_start = get_time() status("Sleeping...", time.sleep, [delay], fin="Awake.") self.assertAlmostEqual(delay, get_time() - t_start, places=PLACES)
import time from qprompt import status, echo status("Sleeping...", time.sleep, [1], fin="Awake.") result = status("Adding...", reduce, [lambda x, y: x + y, range(10)]) echo("result = {0}".format(result))
import qprompt sys.path.append("..") sys.dont_write_bytecode = True from _Check_Versions import VERCHK from _Install_Package import generate_readme, cleanup_readme ##==============================================================# ## SECTION: Main Body # ##==============================================================# if __name__ == '__main__': pause = True if len(sys.argv) > 1 and "nopause" == sys.argv[1]: pause = False ver = VERCHK.run() if not ver: qprompt.alert("Issue with version info!") sys.exit(1) if 0 != qprompt.status("Running tests...", sh.silent, [r"..\tests\_Run_Tests.py nopause"]): qprompt.alert("Issue running tests!") sys.exit(1) if qprompt.ask_yesno("Upload version `%s`?" % (ver)): generate_readme() sh.call("python setup.py sdist upload") cleanup_readme() if pause: qprompt.pause() sys.exit(0)
import time from qprompt import status, echo status("Sleeping...", time.sleep, [1], fin="Awake.") result = status("Adding...", sum, [range(10)]) echo("result = {0}".format(result))
def test_status_1(test): for delay in DELAYS: t_start = get_time() status("Sleeping...", time.sleep, [delay], fin="Awake.") test.assertAlmostEqual(delay, get_time() - t_start, places=PLACES)