def launch_program(): """Code that allows OSCAR to launch a program he has been told about""" oscar_functions.command = oscar_functions.command.lower() confirmed_aliases = [] alias_paths = [] #Searches the "command" for aliases, and adds their info to the above lists for alias_group in oscar_functions.groups[0][0]: for alias in alias_group: alias = "\\b" + alias + "\\b" if re.search(alias, oscar_functions.command): contains_alias = False for path in alias_paths: if path == oscar_functions.groups[0][1][ oscar_functions.groups[0][0].index(alias_group)]: contains_alias = True if not contains_alias: #Add the alias to the list confirmed_aliases.append(alias) #Add its file path to the list alias_paths.append(oscar_functions.groups[0][1][ oscar_functions.groups[0][0].index(alias_group)]) for path in alias_paths: oscar_functions.subprocess_cmd(path) if (len(confirmed_aliases) > 1): print(oscar_functions.get_response(28)) elif (len(confirmed_aliases) > 0): print(oscar_functions.get_response(29)) else: print(oscar_functions.get_response(30))
def should(): """Tells the user if someone should or should not perform an action. Variations ---------- "Or" mode: Tells the user if they should perform one action, "or" another "Yes/no" mode: Responds as to whether the user should or should not do one particular action. """ #If we're in yes/no mode if " or " not in oscar_functions.command.lower(): if randint(0, 1): print(oscar_functions.get_response(17)) else: print(oscar_functions.get_response(18)) elif " or " in oscar_functions.command: #Cut out the "should i" part of the string should_index = oscar_functions.command.find("should ") #The string "should " is 7 characters should_index += 7 end_of_next_word = oscar_functions.command.find(" ", should_index) string = oscar_functions.command[end_of_next_word + 1:] if string.endswith("?"): string = string[:-1] options = string.split(" or ") randIndex = randint(0, len(options) - 1) option = options[randIndex] print(oscar_functions.get_response(14, "<option>", option))
def stopwatch(): """Manages the starting and stopping of the stopwatch""" print(oscar_functions.get_response(45)) start_stopwatch() input() final_time = stop_stopwatch() formatted_time = oscar_functions.convert_and_format_time(final_time) print(oscar_functions.get_response(46, "<time_string>", formatted_time))
def configure_settings(): """Allows the user to view and adjust their settings""" print(oscar_functions.get_response(32)) setting_changed = 0 print("Name: " + oscar_functions.settings[0]) clock_type = "12-hour" if oscar_functions.settings[1] else "24-hour" print("Clock type: " + clock_type) path_chooser = "File manager" if oscar_functions.settings[2] else "Let me type it out" print("File path chooser: " + path_chooser) to_change = input("").lower() if "name" in to_change: oscar_functions.settings[0] = input("Name: ") setting_changed = 1 if "clock" in to_change: clock_type = None clock_type_raw = input("New clock type: ") for clock_string in oscar_functions.inputs[12][0]: if re.search(clock_string, clock_type_raw): clock_type = 0 if clock_type == None: for clock_string in oscar_functions.inputs[13][0]: if re.search(clock_string, clock_type_raw): clock_type = 1 oscar_functions.settings[1] = clock_type setting_changed = 1 if "file" in to_change or "path" in to_change: path_type = None path_type_raw = input("File path chooser: ") for path_string in oscar_functions.inputs[16][0]: if re.search(path_string, path_type_raw): path_type = 1 if path_type == None: for path_string in oscar_functions.inputs[17][0]: if re.search(path_string, path_type_raw): path_type = 0 oscar_functions.settings[2] = path_type setting_changed = 1 if setting_changed: settings_file = None if sys.platform == "win32": directory = "C:\\Program Files(x86)\\Oscar" settings_file = Path(directory + "\\settings") elif sys.platform == "darwin": directory = str(Path.home()) + "/Library/Preferences/Oscar" settings_file = Path(directory + "/settings") else: directory = str(Path.home()) + "/.config/oscar" settings_file = Path(directory + "/settings") final_settings = open(settings_file, 'w') final_settings.write(json.dumps(oscar_functions.settings, indent=4)) print(oscar_functions.get_response(36)) else: print(oscar_functions.get_response(37))
def schedule_command(): """Allows OSCAR to schedule a command to be executed later""" time = oscar_functions.schedule() if time != None: bash_command = None if "\"" in oscar_functions.command: index1 = oscar_functions.command.find("\"") + 1 index2 = oscar_functions.command.find("\"", index1 + 1, len(oscar_functions.command)) bash_command = "sleep " + str( time) + " && " + oscar_functions.command[index1:index2] oscar_functions.subprocess_cmd(bash_command) else: bash_command = "sleep " + str(time) + " && " + input( oscar_functions.get_response(22)) oscar_functions.subprocess_cmd(bash_command) print(oscar_functions.get_response(21))
def give_time(): """Gives the user the time and date.""" day = datetime.now().day weekday = datetime.now().weekday month = datetime.now().month hour = datetime.now().hour minute = datetime.now().minute time = None #If 12-hour clock if (oscar_functions.settings[1]): time = datetime.now().strftime("%A, %B %d, at %I:%M %p") #If 24-hour clock else: time = datetime.now().strftime("%A, %B %d, at %H:%M") print(oscar_functions.get_response(5, "<time>", time))
def search(): """Searches and interprets a given string. Can extract summaries from some sites and services. Uses duckduckgo""" identifier_string = None for string in oscar_functions.inputs[1][0]: if re.search(string, oscar_functions.command): identifier_string = string break index = re.search(identifier_string, oscar_functions.command).end() query = oscar_functions.command[index:] if query.endswith("?"): query = query[:-1] if query != "": answer = duckduckgo.get_zci(query) duck_query = duckduckgo.query(query) if answer != "": print(answer + "\n") if duck_query.type != "nothing": confirm = input(oscar_functions.get_response(4)).lower() if oscar_functions.get_yes_no(confirm): oscar_functions.open_in_browser(duck_query.related[0].url) else: print(oscar_functions.get_response(19)) elif answer.startswith("http"): if answer.startswith("https://www.youtu.be") or answer.startswith("https://www.youtube.com"): confirm = input(oscar_functions.get_response(31)) else: confirm = input(oscar_functions.get_response(3)).lower() if oscar_functions.get_yes_no(confirm): oscar_functions.open_in_browser(answer) else: print(oscar_functions.get_response(20)) else: confirm = input(oscar_functions.get_response(3)).lower() if oscar_functions.get_yes_no(confirm): for c in query: if c == ' ': c = '+' oscar_functions.open_in_browser("https://www.duckduckgo.com/?q=" + query) else: print(oscar_functions.get_response(20)) else: print(oscar_functions.get_response(2))
def walkthrough(): """Walks the user through the available setting options, and assigns them based on user choice""" inputs_array = oscar_defaults.inputs_array settings_array = oscar_defaults.settings_array introduction = "" username_raw = input(oscar_functions.get_response(24)) for intro in inputs_array[14][0]: if re.search(intro, username_raw): introduction = intro username = username_raw if introduction != "": username = username_raw[(re.search(introduction, username_raw).end()) + 1:] settings_array[0] = username clock_type = None while clock_type == None: clock_type_raw = input( oscar_functions.get_response(25, "<user>", username)) for clock_string in inputs_array[12][0]: if re.search(clock_string, clock_type_raw): clock_type = 0 if clock_type == None: for clock_string in inputs_array[13][0]: if re.search(clock_string, clock_type_raw): clock_type = 1 if clock_type: print(oscar_functions.get_response(27, "<user>", username)) else: print(oscar_functions.get_response(26, "<user>", username)) settings_array[1] = clock_type path_type = None while path_type == None: path_type_raw = input(oscar_functions.get_response(33)) for path_string in inputs_array[16][0]: if re.search(path_string, path_type_raw): path_type = 1 if path_type == None: for path_string in inputs_array[17][0]: if re.search(path_string, path_type_raw): path_type = 0 if path_type: print(oscar_functions.get_response(34)) else: print(oscar_functions.get_response(35)) settings_array[2] = path_type
def thanks(): """Responds to the user thanking OSCAR""" print(oscar_functions.get_response(6))
def add_program(): """Method that prompts the user to add a new program to OSCAR's list""" print(oscar_functions.get_response(38)) #If the user wants a file manager file_path = None if oscar_functions.settings[2]: file_path = oscar_functions.open_file_manager(Path.home()) #If the user didn't choose a file if len(file_path) == 0: return #If the user prefers to type out paths manually else: while True: file_path = input() program = file_path.split(" ")[0] #If the file is there, break out of the loop if os.path.isfile(program): break #If it isn't, prompt the user to reselect the file else: print(oscar_functions.get_response(39)) #If the user has already registered this program before if file_path in oscar_functions.groups[0][1]: print(oscar_functions.get_response(43)) return #Add the file path to the groups array oscar_functions.groups[0][1].append(file_path) #Prompt the user for the program's name print(oscar_functions.get_response(40)) print(oscar_functions.get_response(41)) while True: aliases_raw = input() #If the user entered more than one name if ", " in aliases_raw: aliases = aliases_raw.split(", ") already_exists = False existing_aliases = [] for alias_group in oscar_functions.groups[0][0]: for alias in alias_group: if alias in aliases: existing_aliases.append(alias) already_exists = True if already_exists: print(oscar_functions.get_response(44)) else: oscar_functions.groups[0][0].append(aliases) break else: already_exists = False for alias_group in oscar_functions.groups[0][0]: for alias in alias_group: if alias == aliases_raw: already_exists = True if already_exists: print(oscar_functions.get_response(44)) else: aliases_raw = [aliases_raw] oscar_functions.groups[0][0].append(aliases_raw) break groups_array = oscar_defaults.groups_array directory = None groups_file = None if sys.platform == "win32": directory = "C:\\Program Files(x86)\\Oscar" groups_file = Path(directory + "\\groups") elif sys.platform == "darwin": directory = str(Path.home()) + "/Library/Preferences/Oscar" groups_file = Path(directory + "/groups") else: directory = str(Path.home()) + "/.config/oscar" groups_file = Path(directory + "/groups") if not os.path.exists(directory): os.makedirs(directory) updated_groups = open(groups_file, 'w') updated_groups.write(json.dumps(groups_array, indent=4)) print(oscar_functions.get_response(42))