def mercIDValidation(option): # mercID validation/confirmation while True: try: mercID = int(input('Enter a merchandiseID to {}: '.format(option))) # Check if mercID is found in the database if mercID not in [ item['mercID'] for item in globalData['merchandise'] ]: message( 'MerchandiseID:{} does not exist in the database'.format( mercID)) else: mercName, mercPrice = data.getMercInfo(mercID) confirm = False while confirm == False: yn = input( 'MerchandiseID:{} {} for ${}. Is this what you want to {}?(Y/N) ' .format(mercID, mercName, mercPrice, option)).upper() confirm = ui.confirm(yn) if yn == 'Y': break except ValueError: message('Enter a valid merchandiseID') return mercID
def gameIDValidation(option): # GameID confirmation/validation while True: try: gameID = int(input('Enter a GameID to {}: '.format(option))) if gameID not in [game['gameID'] for game in globalData['games']]: message( 'GameID:{} does not exist in the database'.format(gameID)) else: for game in globalData['games']: if game['gameID'] == gameID: date = game['dates'] venueID = game['venueID'] venueName = data.getVenueName(venueID) confirm = False while confirm == False: yn = input( 'GameID:{} record is for the game on {} at {}. Is this what you want to {}?(Y/N) ' .format(gameID, date, venueName, option)).upper() confirm = ui.confirm(yn) if yn == 'Y': break except ValueError: message('Enter a valid number') return gameID
def salesDataValidation(): # GameID validation/confirmation while True: try: gameID = int(input('Enter a gameID: ')) gameDate = data.getDates(gameID) if gameDate is None: message("There are no data with that GameID") else: confirm = False while confirm == False: yn = input( 'ID:{} is for the game on {}. Is this correct?(Y/N) '. format(gameID, gameDate)).upper() confirm = ui.confirm(yn) if yn == 'Y': yn = '' break except ValueError: message('Enter a valid merchandiseID') # mercID validation/confirmation while True: try: mercID = int(input('Enter a merchandiseID: ')) mercName, mercPrice = data.getMercInfo(mercID) if (mercName or mercPrice) is None: message("There are no data with that merchandiseID") else: confirm = False while confirm == False: yn = input( 'ID:{} is for the {} that costs ${}. Is this correct?(Y/N) ' .format(mercID, mercName, mercPrice)).upper() confirm = ui.confirm(yn) if yn == 'Y': break except ValueError: message('Enter a valid merchandiseID') # create a temporary sales object to check if the sales record with the same gameID and mercID already exist tempSalesObj = Sales(gameID, mercID, 0) exist = data.checkSalesExist(tempSalesObj) return gameID, mercID, exist
def exit(): global filename y, x = 19, 3 if ui.confirm(y, x, 'SAVE?') == 0: dbmgr.save(filename) ui.exitui() quit()
def quit(): data.quit() confirm = False while confirm == False: yn = input( 'Would you like to backup database into the backup folder? (Y/N) ' ).upper() confirm = ui.confirm(yn) if yn == 'Y': data.backupDB(globalData) message('End of program')
def delete(): global ret global focus if not len(dbmgr.records) == 0: y, x = 19, 3 if ui.confirm(y, x, 'DELETE?') == 0: dbmgr.remove(dbmgr.records[ret[1]][0]) if not focus == 0: focus -= 1 else: focus = 0
def venueIDValidation(option): # venueID validation/confirmation while True: try: venueID = int(input('Enter a VenueID to {}: '.format(option))) # Check if venueID exist in the database venueName = data.getVenueName(venueID) if venueName is not None: confirm = False while confirm == False: yn = input( 'VenueID:{} {}. Is this what you want to {}?(Y/N) '. format(venueID, venueName, option)).upper() confirm = ui.confirm(yn) if yn == 'Y': break except ValueError: message('Enter a valid VenueID') return venueID
def add(): global ret global focus y, x = 18, 2 record = ui.ask(y, x,\ dbmgr.typechs, dbmgr.lengths, dbmgr.fields) if not record == ui.keyboard['ESC']: y, x = 19, 3 if ui.confirm(y, x, 'ADD?') == 0: focus = dbmgr.add(record) if focus == -1: focus = dbmgr.search(record[0]) else: focus = ret[1]
def client(hostname, port): ui.init() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((hostname, port)) except: ui.alert(2, 2, ERROR['CONNREF'][1]) ui.exit() quit() ret = '' while not ret == 'SIGNON': ret = ui.multitextwinv(2, 2,\ [ 'VARCHAR', 'VARCHAR' ],\ [ 10, 20 ],\ [ 'USERNAME', 'PASSWORD' ],\ [ False, True ]) if ret == ui.keyboard['ESC']: ui.exit() quit() else: username = ret[0] password = hashlib.sha224(ret[1].encode('ascii')).hexdigest() args = {'username': username, 'password': password} ui.message(2, 2, REQUEST['LOGON'][1]) ret = REQUEST['LOGON'][3](sock, args) ret_msg = '' if str(ret) in SUCCESS: ret_code, ret_msg = SUCCESS[ret][0], SUCCESS[ret][1] elif str(ret) in ERROR: ret_code, ret_msg = ERROR[ret][0], ERROR[ret][1] ui.alert(2, 2, ret_msg) break t = threading.Thread(target=clientthread, args=(9, 3, 16, 75, sock)) t.daemon = True t.start() while not rdy_event.is_set(): continue # wait for client thread. ui.window(2, 81, 22, 15,\ ui.curses.color_pair(ui.colors['W/BK']),\ True) ui.window(2, 2, 3, 75,\ ui.curses.color_pair(ui.colors['W/BK']),\ True) ui.message(3, 4, REQUEST['GETMSGS'][1]) REQUEST['GETMSGS'][3](sock, args) while not msg_event[1].is_set(): continue # wait for client thread. if not str(msg_event[0]) == 'NOMSGS': msg_event[1] = threading.Event() ui.optionwinh(2, 2, 3, 75,\ [ [ '\u25B2/\u25BC', 'SELECT' ],\ [ 'RET', 'CLOSE' ],\ [ 'ESC', 'CLOSE' ] ]) ui.menuwin(28, 2, 5, 0,\ [ [ 'FROM', '11', 'VARCHAR' ], [ 'MESSAGE', '76', 'VARCHAR' ] ],\ msg_event[0]) while True: ui.window(2, 2, 3, 75,\ ui.curses.color_pair(ui.colors['W/BK']),\ True) ui.optionwinv(2, 81, 22, 15,\ [ [ 'RET', 'INPUT' ],\ [ 'U', 'USERS' ],\ [ 'ESC', 'LOGOUT' ] ]) key = ui.stdscr.getch() if key == ui.keyboard['ESC']: ui.optionwinv(2, 81, 22, 15,\ [ [ '\u25C0/\u25B6', 'SELECT' ],\ [ 'RET', 'SUBMIT' ],\ [ 'ESC', 'CANCEL' ] ]) if ui.confirm(3, 4, 'LOG OUT?') == 0: break elif key == ui.keyboard['ENTER']: ui.optionwinv(2, 81, 22, 15,\ [ ['RET', 'SUBMIT'],\ ['ESC', 'CANCEL' ] ]) ret = ui.multitextwinh(2, 2,\ [ 'VARCHAR', 'VARCHAR' ],\ [ 10, 57 ],\ [ 'TO', 'MESSAGE' ],\ [ False, False ]) if not ret == ui.keyboard['ESC']: args['receiver'], args['message'] = ret[0], ret[1] ui.window(2, 2, 3, 75,\ ui.curses.color_pair(ui.colors['W/BK']),\ True) ui.message(3, 4, REQUEST['SENDMSG'][1]) REQUEST['SENDMSG'][3](sock, args) elif key == ui.keyboard['U']: ui.window(2, 81, 22, 15,\ ui.curses.color_pair(ui.colors['W/BK']),\ True) ui.message(3, 4, REQUEST['GETUSRS'][1]) REQUEST['GETUSRS'][3](sock, args) while not usr_event[1].is_set(): continue # wait for client thread. usr_event[1] = threading.Event() ui.optionwinh(2, 2, 3, 75,\ [ [ '\u25B2/\u25BC', 'SELECT' ],\ [ 'RET', 'CLOSE' ],\ [ 'ESC', 'CLOSE' ] ]) ui.menuwin(2, 81, 22, 0,\ [ ['USER', '10', 'VARCHAR' ] ],\ usr_event[0]) ui.message(3, 4, REQUEST['LOGOFF'][1]) REQUEST['LOGOFF'][3](sock, args) sock.close() ui.exit() quit()
def main(): version_string = ('{}\n' 'Version: {}\n' 'Python: {}').format(MODULE_NAME, __version__, python_version()) parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, description='{} (Version {})'.format( MODULE_NAME, __version__)) parser.add_argument('--version', action='version', version=version_string, help='display version information') parser.add_argument( '--force', '-f', action='store_true', dest='force', default=False, help='attempt to rename files without prompting for confirmation') parser.add_argument('--skip-subtitle', '-s', action='store_true', dest='skip_subtitle', default=False, help='skip files with common subtitle extensions') parser.add_argument('--remove-langs', '-l', action='store_true', dest='remove_langs', default=False, help='remove language codes from filename') parser.add_argument('--remove-noise', '-n', action='store_true', dest='remove_noise', default=False, help='remove excess chars repetition') parser.add_argument('--list-dir', '-d', action='store_true', dest='list_dir', default=False, help='list directory of renamed files') parser.add_argument('directory', nargs='?', metavar='DIRECTORY', action='store', help='directory containg the files to rename') args = parser.parse_args() target_dir = os.path.realpath( args.directory if args.directory != None else '.') if not os.path.exists(target_dir): die('{}: No such file or directory'.format(target_dir)) # Filter files excluding directories files = [ f for f in os.listdir(target_dir) if os.path.isfile(os.path.join(target_dir, f)) ] if not files: die('{}: Contains no files'.format(target_dir)) # Skip subtitle files if requested if args.skip_subtitle: files = [f for f in files if not is_subtitle(f)] # Normalize excluding hidden files acronyms_re = re.compile(build_re(args), re.IGNORECASE) normalized = [(f, normalize(f, acronyms_re, args.remove_noise)) \ for f in files if not is_hidden(os.path.join(target_dir, f))] # Remove files that don't need rename normalized = [(oldname, newname) for oldname, newname in normalized if oldname != newname] file_count = len(normalized) if file_count == 0: exit_and_hints(target_dir, args) proceed = True if not args.force: print_preview(normalized) proceed, temp_scheme = confirm(target_dir, normalized) if temp_scheme: atexit.register(lambda: remove_scheme(temp_scheme)) if proceed: if temp_scheme: normalized = load_scheme(temp_scheme, normalized) if not normalized: die("Nothing done") print('Renaming into \'{}\'...'.format(target_dir)) completed = 0 step_perc = 32 / file_count failed = [] anim = progress_anim() frame = 0 update_progess(0, step_perc, '0% 0/{}'.format(file_count), chr(anim[frame])) for n, (oldname, newname) in enumerate(normalized): time.sleep(0.1) if not try_rename(os.path.join(target_dir, oldname), os.path.join(target_dir, newname)): failed.append(oldname) completed += step_perc frame += 1 frame = 0 if frame == 4 else frame update_progess( completed, step_perc, '{}% {}/{}'.format(int(completed), n + 1, file_count), chr(anim[frame])) update_progess(100, step_perc, '100% {}/{}, done.'.format(file_count, file_count), chr(anim[frame])) print('') if failed: print_failed(failed) if args.list_dir: list_dir(target_dir)
def client(args): def fetchUpdated(sock): ret = request(sock, 'GETUPD'.encode('ascii')) if str(ret) in err: y, x = 1, 2 ui.alert(y, x, err[ret]) ui.exit() quit() else: return eval(ret) def fetchSchema(sock): ret = request(sock, 'GETSCH'.encode('ascii')) if str(ret) in err: y, x = 1, 2 ui.alert(y, x, err[ret]) ui.exit() quit() else: return eval(ret) def fetchRecords(sock): ret = request(sock, 'GETRECS'.encode('ascii')) if str(ret) in err: y, x = 1, 2 ui.alert(y, x, err[ret]) ui.exit() quit() else: return eval(ret) ui.init() cache = cachemgr.Cache(host='localhost', port=11211) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((args.host, args.p)) except: y, x = 1, 2 ui.alert(y, x, err['CONNREF']) ui.exit() quit() server_time = fetchUpdated(sock) schema = fetchSchema(sock) collect = fetchRecords(sock) cache.consolidate(server_time, schema, collect) schema = cache.getSchema() collect = cache.getRecords() sel = 0 while True: y, x = 1, 2 height = 13 ret = ui.menuwin(y, x, height, sel, schema, collect) if ui.keyboard['ESC'] in ret: sel = ret[1] y, x = 1, 2 if ui.confirm(y, x, 'EXIT?'): ret = request(sock, 'CONNEND'.encode('ascii')) if str(ret) in err: y, x = 1, 2 ui.alert(y, x, err[ret]) elif str(ret) in succ: y, x = 1, 2 ui.alert(y, x, succ[ret]) ui.exit() quit() elif ui.keyboard['S'] in ret: sel = ret[1] y, x = 1, 2 title = 'SEARCH' length = 30 typech = 'VARCHAR' ret = ui.textwin(y, x, typech, length, title, False) if not ret == ui.keyboard['ESC']: sel = 0 if not cache.getUpdated() == server_time: y, x = 1, 2 ui.alert(y, x, 'CONSOLIDATING...') collect = fetchRecords(sock) cache.consolidate(server_time, schema, collect) search = cache.search(ret) if not search: y, x = 1, 2 ui.alert(y, x, err['NOREC']) else: y, x, 1, 2 ui.alert(y, x, '{} RECORD(S) FOUND.'.format(len(search))) collect = search elif ui.keyboard['A'] in ret: sel = ret[1] attrs, lens, typechs, is_hiddens = [], [], [], [] for s in schema: attrs.append(str(s[0])) lens.append(int(s[1])) typechs.append(str(s[2])) is_hiddens.append(False) y, x = 1, 2 ret = ui.multitextwinv(y, x, typechs, lens, attrs, is_hiddens) if not ret == ui.keyboard['ESC']: req = '{}#{}'.format('ADDREC', repr(ret)) ret = request(sock, req.encode('ascii')) if str(ret) in err: y, x = 1, 2 ui.alert(y, x, err[ret]) elif str(ret) in succ: y, x = 1, 2 ui.alert(y, x, succ[ret]) sel = 0 server_time = fetchUpdated(sock) elif ui.keyboard['D'] in ret and collect: sel = ret[1] for i, rec in enumerate(collect): if i == sel: key = rec[schema[0][0]] y, x = 1, 2 if ui.confirm(y, x, 'DELETE {}?'.format(key)): req = '{}#{}'.format('DELREC', repr(key)) ret = request(sock, req.encode('ascii')) if str(ret) in err: y, x = 1, 2 ui.alert(y, x, err[ret]) elif str(ret) in succ: y, x = 1, 2 ui.alert(y, x, succ[ret]) sel = 0 server_time = fetchUpdated(sock) elif ui.keyboard['R'] in ret: y, x = 1, 2 if ui.confirm(y, x, 'REFRESH?'): server_time = fetchUpdated(sock) collect = fetchRecords(sock) cache.consolidate(server_time, schema, collect) if not cache.getUpdated() == server_time: y, x = 1, 2 ui.alert(y, x, 'CONSOLIDATING...') collect = fetchRecords(sock) cache.consolidate(server_time, schema, collect)