def edit_dvd(db): old_title = find_dvd(db, 'edit') if not old_title: return title = Cmd.get_string('Title', 'title', default=old_title) if not title: return director, year, duration = db[title] director = Cmd.get_string('Director', 'director', default=director) year = Cmd.get_int('Year', 'year', default=year, min_val=1896, max_val=datetime.date.today().year, empty_ok=True) duration = Cmd.get_int('Duration', 'duration', default=duration, min_val=0, max_val=60 * 48, empty_ok=True) db[title] = (director, year, duration) if old_title != title: del db[old_title] db.sync()
def find_dvd(db, action): msg = '(Start of) title to {}'.format(action) while True: usr_title = Cmd.get_string(msg, 'title', empty_ok=True) if not usr_title: return (None, None) cursor = db.cursor() cursor.execute( 'SELECT title, id ' 'FROM dvds ' 'WHERE title LIKE ? ' 'ORDER BY title', (usr_title + '%', )) matches = cursor.fetchall() if not matches: print('No matches found. Try using different characters.') continue elif len(matches) == 1: return matches[0] elif len(matches) > DISPLAY_LIMIT: print('Too many matches found. Please try to be more specific.') continue else: for index, matched_title in enumerate(matches, 1): print('{}: {}'.format(index, matched_title[0])) chosen_title = Cmd.get_int('Index (0 to cancel)', 'index', min_val=1, max_val=len(matches), empty_ok=True) return matches[chosen_title - 1] if chosen_title else (None, None)
def edit_dvd(db): title, identity = find_dvd(db, 'edit') if not title: return title = Cmd.get_string('Title', 'title', default=title) if not title: return cursor = db.cursor() cursor.execute( 'SELECT dvds.year, dvds.duration, directors.name ' 'FROM dvds, directors ' 'WHERE dvds.director_id = directors.id AND ' 'dvds.id=:id', dict(id=identity)) year, duration, director = cursor.fetchone() year = Cmd.get_int('Year', 'year', default=year, min_val=1896, max_val=datetime.date.today().year, empty_ok=True) duration = Cmd.get_int('Duration', 'duration', default=duration, min_val=0, max_val=60 * 48, empty_ok=True) director_id = get_set_director(db, director) cursor.execute( 'UPDATE dvds SET title=:title, year=:year, ' 'duration=:duration, director_id=:director_id ' 'WHERE id=:identity', locals()) db.commit()
def find_dvd(db, action): msg = '(Start of) title to {}'.format(action) cursor = db.cursor() while True: title = Cmd.get_string(msg, 'title', empty_ok=True) if not title: return (None, None) cursor.execute( 'SELECT title, id FROM dvds ' 'WHERE title LIKE ? ORDER BY title', (title + "%", )) matches = cursor.fetchall() if len(matches) == 0: print('No matches. Try using different characters') continue elif len(matches) == 1: return matches[0] elif len(matches) > DISPLAY_MAX: print('Too many values to display.' 'Please narrow your search.') continue else: for enum, matched_title in enumerate(matches, 1): print('{}: {}'.format(enum, matched_title[0])) title_choice = Cmd.get_int('Number (or 0 to cancel)', min_val=1, max_val=len(matches), empty_ok=True) return matches[title_choice - 1] if title_choice else (None, None)
def find_dvd(db, action): msg = '(Start of) title to {}'.format(action) while True: matches = [] start_str = Cmd.get_string(msg, 'title', empty_ok=True) if not start_str: return for title in db.keys(): if title.lower().startswith(start_str.lower()): matches.append(title) if not matches: print('No matching titles found.\n' 'Try adding some characters.') continue elif len(matches) == 1: return matches[0] elif len(matches) > DISPLAY_LIMIT: print('Too many dvd start with {}.' 'Try adding some characters.'.format(start_str)) continue else: for id, matched_title in enumerate(matches, 1): print('{}: {}'.format(id, matched_title)) title_choice = Cmd.get_int('Number (or 0 to cancel)', 'number', min_val=1, max_val=len(matches), empty_ok=True) return matches[title_choice - 1] if title_choice else ''
def find_bookmark(db, action): msg = '(Start of) bookmark title to {}'.format(action) while True: lookup_title = Cmd.get_string(msg, 'bookmark title') if not lookup_title: return matches = [] for title in db.keys(): if title.lower().startswith(lookup_title.lower()): matches.append(title) if not matches: return None elif len(matches) == 1: return matches[0] elif len(matches) > DISPLAY_LIMIT: print('Too many bookmarks to show.' 'Please narrow your search.') continue else: for index, matched_bookm in enumerate(sorted(matches), 1): print('{}: {}'.format(index, matched_bookm)) index_choice = Cmd.get_int('Index (or 0 to cancel)', 'index', min_val=1, max_val=len(matches), empty_ok=True) return matches[index_choice-1] if index_choice else None
def add_bookm(db): bookm_title = Cmd.get_string('Bookmark title', 'bookmark title') if not bookm_title: return url = Cmd.get_string('URL', 'url') if not url: return db[bookm_title] = fix_input_url(url) db.sync()
def add_bookm(db): title = Cmd.get_string('Bookmark title', 'bookmark title') if not title: return url = Cmd.get_string('URL', 'url') if not url: return url = http_name_fix(url) db[title] = http_name_fix(url) db.sync()
def edit_bookm(db): old_title = find_bookmark(db, 'edit') if not old_title: return title = Cmd.get_string('Bookmark title', 'bookmark title', default=old_title) url = Cmd.get_string('URL', 'url', default=db[old_title]) db[title] = http_name_fix(url) if old_title != title: del db[old_title] db.sync()
def edit_bookm(db): old_name = find_bookmark(db, 'edit') if not old_name: return bookm_title = Cmd.get_string('Bookmark title', 'bookmark title', old_name) if not bookm_title: return url = Cmd.get_string('URL', 'url', db[old_name]) if not url: return db[bookm_title] = fix_input_url(url) if bookm_title != old_name: del db[old_name] db.sync()
def add_dvd(db): title = Cmd.get_string('Title', 'title') if not title: return director = Cmd.get_string('Director', 'director') if not title: return year = Cmd.get_int('Year', 'year', min_val=1896, max_val=datetime.date.today().year) duration = Cmd.get_int('Duration', 'duration', min_val=0, max_val=60 * 48) db[title] = (director, year, duration) db.sync()
def main(): filename = os.path.join(os.path.dirname(__file__), 'test2_db_sql') menu = ('(A)dd (E)dit (R)emove (L)ist List (D)irectors ' 'E(X)port (I)mport (Q)uit: ') input_to_func = dict(a=add_dvd, e=edit_dvd, r=remove_dvd, l=list_dvds, d=list_directors, x=export_xml, i=import_xml, q=quit) valid = frozenset('aerldxiq') db = None try: db = connect(filename) while True: user_choice = Cmd.get_menu_option(menu, valid) input_to_func[user_choice.lower()](db) except (EnvironmentError, IOError) as file_err: print('File Error: ', file_err) sys.exit() except sqlite3.Error as db_err: print('Database Error: ', db_err) sys.exit() finally: if db: db.close()
def _get_script_output(self, pyfile, f=None): script_path = os.path.join(self.test_dir, pyfile) assert os.path.exists(script_path), 'file ' + script_path + \ " doesn't exist" cmd = '/usr/bin/env python3 ' + script_path out = Cmd.run_cmd(cmd, ret=False) return out
def main(): filename = os.path.join(os.path.dirname(__file__), 'dvds_sql_db') menu = '(A)dd (E)dit (R)emove (L)ist List (D)irectors E(X)port (I)mport (Q)uit: ' input_to_func = dict(a=add_dvd, e=edit_dvd, r=remove_dvd, l=list_dvds, d=list_directors, x=export_xml, i=import_xml, q=quit) valid = frozenset('aerldxiq') db = None try: db = connect(filename) while True: chosen_opt = Cmd.get_menu_option(menu, valid) input_to_func[chosen_opt.lower()](db) except (EnvironmentError, IOError, sqlite3.Error) as process_err: print('Error: ', process_err) sys.exit() finally: if db: db.close()
def remove_bookm(db): title = find_bookmark(db, 'remove') if not title: return confirmation = Cmd.get_bool('Remove {}?'.format(title), 'no') if confirmation: del db[title] db.sync()
def remove_bookm(db): bookm_name = find_bookmark(db, 'remove') if not bookm_name: return confirmation = Cmd.get_bool('Remove {}?'.format(bookm_name), 'no') if confirmation: del db[bookm_name] db.sync()
def find_bookmark(db, action): msg = 'Index of bookmark to {}'.format(action) index = Cmd.get_int(msg, 'bookmark index', min_val=0, max_val=len(db)) if not index: return for idx, name in enumerate(sorted(db.keys()), 1): if index == idx: return name
def list_bookm(db): start_str = '' if len(db.keys()) > DISPLAY_LIMIT: start_str = Cmd.get_string('Start of) bookmark title to display ' '[ENTER=all]', 'bookmark title') for index, bookm_name in enumerate(sorted(db.keys()), 1): if not start_str or bookm_name.lower().startswith(start_str.lower()): print('{} {:.<30} {:.40}'.format(index, bookm_name, db[bookm_name]))
def remove_dvd(db): title, identity = find_dvd(db, 'remove') if not title: return confirm = Cmd.get_bool("Remove {0}?".format(title), "no") if confirm: cursor = db.cursor() cursor.execute("DELETE FROM dvds WHERE id=?", (identity, )) db.commit()
def javaFiles(filePaths, verbose=False, mainConfig=dict()): successfullyCompiled = list() successJavaFiles = list() failed = list() filePathsRefined = list() if ("feature_check" in mainConfig and str2bool(mainConfig["feature_check"])): for filePath in filePaths: if (not featureCheck(filePath)): #this .java file does not contain the correct features if (verbose): print("File: '" + str(filePath) + "' does not contain any of the required features.") continue filePathsRefined.append(filePath) print(" " + str(len(filePaths) - len(filePathsRefined)) + " .java files eleminated after feature_check. " + str(len(filePathsRefined)) + " .java files still remain.") else: filePathsRefined = filePaths for filePath in filePathsRefined: #compile the file given by the file path if (not filePath.endswith(".java")): #this isn't a java file, skip it if (verbose): print("File: '" + str(filePath) + "' isn't a .java file, skipping it.") continue #this must be a java file #".java" is 5 characters long, remove it from the end and replace it with ".class" classFilePath = filePath[:-5] + str(".class") if ("recompile" in mainConfig and not str2bool(mainConfig["recompile"])): #don't recompile, just use the class file if it's there #if(os.path.isfile(classFilePath)): if (verbose): print( "Recompile set to false in the main.config file, not recompiling since found preexisting class file." ) #the class file already exists successfullyCompiled.append(classFilePath) successJavaFiles.append(filePath) continue compileCmd = "javac " + str(filePath) cmdOutput, cmdErr, cmdExitStatus = Cmd.runCmd(compileCmd, verbose) if (cmdExitStatus == 0): successfullyCompiled.append(classFilePath) successJavaFiles.append(filePath) else: failed.append(filePath) return ((successJavaFiles, successfullyCompiled), failed)
def add_dvd(db): title = Cmd.get_string('Title', 'title') if not title: return director = Cmd.get_string('Director', 'director') if not director: return director_id = get_set_director(db, director) year = Cmd.get_int('Year', 'year', min_val=1896, max_val=datetime.date.today().year) duration = Cmd.get_int('Duration', 'duration', min_val=0, max_val=60 * 48) cursor = db.cursor() cursor.execute( 'INSERT INTO dvds (title, year, duration, director_id)' 'VALUES (?, ?, ?, ?)', (title, year, duration, director_id)) db.commit()
def remove_dvd(db): title, dvd_id = find_dvd(db, 'remove') if not title: return confirm = Cmd.get_bool('Remove {}?'.format(title), 'no') if confirm: cursor = db.cursor() cursor.execute('DELETE FROM dvds ' 'WHERE id=?', (dvd_id, )) db.commit()
def list_dvds(db): start_str = '' if len(db) > DISPLAY_LIMIT: start_str = Cmd.get_string('List titles starting with' ' [Enter=all]', 'title') for title in db.keys(): if not start_str or title.lower().startswith(start_str.lower()): director, year, duration = db[title] print('Title: {title} | Director: {director} |' ' Year: {year} | Duration: {duration}'.format(**locals()))
def processInput(input): submittedFullPath = input[0] referenceFullPath = input[1] submittedPath = input[2] referencePath = input[3] workingDir = input[4] verbose = input[5] command = "java -cp .. analyze.Difference " + submittedFullPath + " " + referenceFullPath output, error, status = Cmd.runCmd(command, workingDir=workingDir, verbose=verbose) return (submittedPath, referencePath, output)
def main(): filename = os.path.join(os.path.dirname(__file__), 'bookmarks2.dbm') menu = '(A)dd (E)dit (L)ist (R)emove (Q)uit' opt_to_func = dict(a=add_bookm, e=edit_bookm, l=list_bookm, r=remove_bookm, q=quit) db = None try: db = shelve.open(filename, protocol=pickle.HIGHEST_PROTOCOL) while True: print('Bookmarks ({})'.format(os.path.split(filename)[1])) list_bookm(db) choice = Cmd.get_menu_option(menu, 'aelrq', 'l' if len(db) else 'a') opt_to_func[choice.lower()](db) finally: if db: db.close()
def list_dvds(db): cursor = db.cursor() sql = ("SELECT dvds.title, dvds.year, dvds.duration, " "directors.name FROM dvds, directors " "WHERE dvds.director_id = directors.id") start = None if dvd_count(db) > DISPLAY_MAX: start = Cmd.get_string("List those starting with " "[Enter=all]", "start") sql += " AND dvds.title LIKE ?" sql += " ORDER BY dvds.title" print() if start is None: cursor.execute(sql) else: cursor.execute(sql, (start + "%", )) for record in cursor: print("{0[0]} ({0[1]}) {0[2]} minutes, by {0[3]}".format(record))
def list_directors(db): cursor = db.cursor() sql = ("SELECT directors.name " "FROM directors, dvds " "WHERE dvds.director_id = directors.id") start = None if dvd_count(db) > DISPLAY_MAX: start = Cmd.get_string("List those starting with " "[Enter=all]", "start") sql += " AND directors.name LIKE ?" sql += " ORDER BY directors.name" print() if start is None: cursor.execute(sql) else: cursor.execute(sql, (start + "%", )) for director in cursor: print('{}'.format(director[0]))
def list_directors(db): cursor = db.cursor() sql = ('SELECT directors.name from directors, dvds ' 'WHERE directors.id = dvds.director_id') start = None if directors_count(db) > DISPLAY_LIMIT: start = Cmd.get_string("List those starting with " "[Enter=all]", "start") sql += ' AND directors.name LIKE ?' sql += ' ORDER BY directors.name' print() if start is None: cursor.execute(sql) else: cursor.execute(sql, (start + '%')) directors_list = cursor.fetchall() for index, name in enumerate(directors_list, 1): print('{}: {}'.format(index, name[0]))
def main(): filename = os.path.join(os.path.dirname(__file__), 'bookmarks.dbm') menu = '(A)dd (E)dit (L)ist (R)emove (Q)uit' opt_to_func = dict(a=add_bookm, e=edit_bookm, l=list_bookm, r=remove_bookm, q=quit) valid = frozenset('aelrq') menu_choice = 'l' db = None try: db = shelve.open(filename, protocol=pickle.HIGHEST_PROTOCOL) while True: menu_choice = Cmd.get_menu_option(menu, valid, default=menu_choice) opt_to_func[menu_choice.lower()](db) except (EnvironmentError, pickle.PickleError) as err: print('Error: ', err) sys.exit() finally: if db: db.close()
def list_dvds(db): cursor = db.cursor() sql = ('SELECT dvds.title, dvds.year, dvds.duration, ' 'directors.name ' 'FROM dvds, directors ' 'WHERE dvds.director_id = directors.id') start = None if dvd_count(db) > DISPLAY_LIMIT: start = Cmd.get_string('List those starting with ' '[Enter=all]', 'start') sql += ' AND dvds.title LIKE ?' sql += ' ORDER BY dvds.title' print() if start is None: cursor.execute(sql) else: cursor.execute(sql, (start + '%')) dvds_list = cursor.fetchall() for dvd in dvds_list: print('Title: {0[0]} | Director: {0[3]} |' ' Year: {0[1]} | Duration: {0[2]}'.format(dvd))