def step_next(verbose=False): ''' Show the next step. If at the end, the next step is a congratulatory message and advice on what to do now. ''' lesson_name, current_step_index = get_latest() lesson_specs = load_lesson_specs(lesson_name) max_step_index = len(lesson_specs['prompts']) - 1 if current_step_index == max_step_index: # There are no more steps, so the lesson is done. lesson_title = lesson_specs['title'] update_history(lesson_name, 'completed') # print() print(Fore.BLACK + Back.YELLOW + '') print(f'\n + + + + Congrats! You have completed "{lesson_title}". + + + +') print(f'\n You should now be able to:') for obj in lesson_specs['objectives']: print(f' - {obj}') print('\n You can review this lesson (and any notes you added) by re-starting the lesson.') print(' Otherwise, onward and upward to a new lesson!') print(Style.RESET_ALL) stop(completed_lesson_name=lesson_name) sys.exit(0) new_step_index = current_step_index + 1 update_lesson_progress(lesson_name, new_step_index) display_prompt(lesson_name, lesson_specs, new_step_index, verbose=verbose)
def step_current(verbose=False): ''' Show the current step. ''' lesson_name, current_step_index = get_latest() lesson_specs = load_lesson_specs(lesson_name) display_prompt(lesson_name, lesson_specs, current_step_index, verbose=verbose)
def step_previous(verbose=False): ''' Show the previous step. ''' lesson_name, current_step_index = get_latest() if current_step_index > 0: new_step_index = current_step_index - 1 else: new_step_index = 0 lesson_specs = load_lesson_specs(lesson_name) update_lesson_progress(lesson_name, new_step_index) display_prompt(lesson_name, lesson_specs, new_step_index, verbose=verbose)
def step_jump(step_number, verbose=False): ''' Jump to a specified step number. ''' lesson_name, current_step_index = get_latest() lesson_specs = load_lesson_specs(lesson_name) max_step_number = len(lesson_specs['prompts']) if (int(step_number) > max_step_number) or (int(step_number) < 1): print('ERROR: That step number does not exist.') sys.exit(1) new_step_index = int(step_number) - 1 update_lesson_progress(lesson_name, new_step_index) display_prompt(lesson_name, lesson_specs, new_step_index, verbose=verbose)
def step_add_note(): ''' Add a note to current step_index in the progress.csv ''' lesson_name, current_step_index = get_latest() lesson_specs = load_lesson_specs(lesson_name) # Get note from the user. note = str(input('Please enter your note: ')) # Record note in progress.csv. update_lesson_progress(lesson_name, current_step_index, note=note) # Display the current step with the new note. display_prompt(lesson_name, lesson_specs, current_step_index) print(f'Added note to {os.path.join(LESSONS_DIR, lesson_name, "progress.csv")}') print('To edit or delete the note, please do so in the csv file itself.')
def start(lesson_name): ''' Starts a new lesson by setting up the feedstock and condarc. Also checks if a user already started the specified lesson and handles accordingly. ''' lesson_specs = load_lesson_specs(lesson_name) feedstock_url = lesson_specs['feedstock_url'] commit = lesson_specs['commit'] # Check whether a progress.csv already exists (which would mean # they've started this lesson before). # If so, ask whether they want to resume, start over, or cancel. if os.path.exists(os.path.join(LESSONS_DIR, lesson_name, 'progress.csv')): while True: user_response = str(input(f'You previously started "{lesson_name}". \nDo you wish to (r)esume, (s)tart over, or (c)ancel? ')) if user_response.lower() not in ['c', 's']: print('Sorry, I did not understand.') else: break if user_response.lower() == 'r': # Resume setup_feedstock_and_condarc(lesson_name) update_history(lesson_name, 'resume') step_current(verbose=True) elif user_response.lower() == 's': # Start over setup_feedstock_and_condarc(lesson_name) update_history(lesson_name, 'start over') update_lesson_progress(lesson_name, 0) step_current(verbose=True) elif user_response.lower() == 'c': # Cancel sys.exit(0) else: setup_feedstock_and_condarc(lesson_name) update_history(lesson_name, 'start') create_lesson_progress(lesson_name) display_prompt(lesson_name, lesson_specs, 0, verbose=True)
def setup_feedstock_and_condarc(lesson_name): lesson_specs = load_lesson_specs(lesson_name) feedstock_url = lesson_specs['feedstock_url'] commit = lesson_specs['commit'] # Clone feedstock and checkout to specifiec commit. clone_checkout_feedstock(feedstock_url, commit) # Set up dojo_channels (only if "dojo_channels_pkgs.txt" file exists and it's not empty): dojo_channels_pkgs = os.path.join(LESSONS_DIR, lesson_name, 'dojo_channels_pkgs.txt') if os.path.exists(dojo_channels_pkgs) and os.stat(dojo_channels_pkgs).st_size > 0: print('\nSetting up dojo_channels...') # Parse each URL to get its channel, subdir, and filename. For example: # { # 'https://repo.anaconda.com/pkgs/main/linux-64/python-3.9.2-hdb3f193_0.conda': # { # 'channel': 'main', # 'subdir' : 'linux-64', # 'fn' : 'python-3.9.2-hdb3f193_0.conda' # } # } url_parsed_dict = {} with open(dojo_channels_pkgs, 'r') as url_list: urls = url_list.read().splitlines() for url in urls: channel = url.split('/')[-3] subdir = url.split('/')[-2] fn = url.split('/')[-1] url_parsed_dict[url] = {'channel': channel, 'subdir': subdir, 'fn': fn} # Download each URL to the appropriate destination path. for pkg_url, url_parts in url_parsed_dict.items(): destination_path = os.path.join(LESSONS_DIR, lesson_name, 'dojo_channels', url_parts['channel'], url_parts['subdir'], url_parts['fn']) download_package(pkg_url, destination_path) # Run `conda index` on each dojo channel. from glob import glob dojo_channels = glob(os.path.join(LESSONS_DIR,lesson_name, 'dojo_channels', '*')) for dojo_channel_path in dojo_channels: import subprocess print(f'Running "conda index" on {dojo_channel_path}') subprocess.run(['conda', 'index', dojo_channel_path]) # If a .condarc exists, back it up. home_path = os.environ['HOME'] condarc_path = os.path.join(home_path, '.condarc') if os.path.exists(condarc_path): ts = get_timestamp_for_file() renamed_condarc_path = os.path.join(home_path, f'.condarc_bak_{ts}') os.rename(condarc_path, renamed_condarc_path) print('Found an existing .condarc file.') print(f'Backing it up to: {renamed_condarc_path}') # Create a .condarc that points to the lesson's dojo_channels. with open(condarc_path, 'w') as new_condarc: new_condarc.write('channels: \n') # Must have a space after the colon. See: https://stackoverflow.com/a/9055411 for channel in dojo_channels: new_condarc.write(f' - {channel}\n') print('...successfully set up dojo_channels!')