Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ae32f28

Browse files
authored
Update main.py
added user path, help and made changes toward default path
1 parent 717aa36 commit ae32f28

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

main.py

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
import click
33
from bs4 import BeautifulSoup
44
from color import Colors
5-
from config_setup import (
6-
save_credentials_to_config,
7-
load_credentials_from_config,
8-
load_user_data_from_config,
9-
save_user_data_to_config,
10-
)
5+
from config_setup import *
116
import leetcode
127
import leetcode.auth
138
import requests
@@ -507,7 +502,7 @@ def display_question_detail(api_instance, title_slug): #did changes here
507502

508503

509504

510-
def write_code_snippet_to_file(question_detail_data, lang, title_slug):
505+
def write_code_snippet_to_file(question_detail_data, lang, title_slug): # tags:path
511506
code_snippets = question_detail_data.get("codeSnippets", [])
512507
code = next(
513508
(snippet["code"] for snippet in code_snippets if snippet["langSlug"] == lang),
@@ -516,8 +511,13 @@ def write_code_snippet_to_file(question_detail_data, lang, title_slug):
516511
if code:
517512
lang_extension = LANG_EXTENSIONS.get(lang)
518513
if lang_extension:
519-
home_directory = os.path.expanduser("~")
520-
leetcode_folder = os.path.join(home_directory, ".leetcode")
514+
leetcode_path = load_user_path_from_config()
515+
if not leetcode_path:
516+
home_directory = os.path.expanduser("~")
517+
leetcode_folder = os.path.join(home_directory, ".leetcode")
518+
else:
519+
leetcode_folder = leetcode_path
520+
521521
if not os.path.exists(leetcode_folder):
522522
os.makedirs(leetcode_folder)
523523
file_path = os.path.join(
@@ -534,6 +534,7 @@ def write_code_snippet_to_file(question_detail_data, lang, title_slug):
534534
print(f"Code snippet for {lang} is not available for this question.")
535535

536536

537+
537538
def get_title_slug_from_filename(filepath):
538539
base_name = os.path.basename(filepath)
539540
title_slug, _ = os.path.splitext(base_name)
@@ -551,32 +552,33 @@ def title_and_file_extension(file):
551552
return title_slug, lang_name
552553

553554

554-
# def print_help_usage():
555-
# help_message = """
556-
# IMPORTANT: python main.py --lib
555+
def print_help_usage():
556+
help_message = """
557+
IMPORTANT: python main.py --lib
557558
558-
# Usage:
559-
# python main.py --config
560-
# python main.py --config --user-lang <language>
561-
# python main.py --question/-q <question_id_or_title>
562-
# python main.py --solve <question_id_or_title>
563-
# python main.py --test/-t <filename>
564-
# python main.py --submit/-sb <filename>
565-
566-
# Examples:
567-
# python main.py --config --user-lang=python3
568-
# python main.py --question 1
569-
# python main.py --question add-two-numbers
570-
# python main.py --question 10:20
571-
# python main.py --solve/-s add-two-numbers
572-
# python main.py --solve 1
573-
# python main.py --test test_file.py
574-
# python main.py --submit submit_file.py
575-
576-
# For any issues or feature requests, please visit:
577-
# https://github.com/hrdkmishra/leetcode.py
578-
# """
579-
# print(help_message)
559+
Usage:
560+
python main.py --config
561+
python main.py --config --user-lang <language>
562+
python main.py --question/-q <question_id_or_title>
563+
python main.py --solve <question_id_or_title>
564+
python main.py --test/-t <filename>
565+
python main.py --submit/-sb <filename>
566+
567+
Examples:
568+
python main.py --config --user-lang=python3
569+
python main.py --config --user-path=/d/prog/lc
570+
python main.py --question 1
571+
python main.py --question add-two-numbers
572+
python main.py --question 10:20
573+
python main.py --solve/-s add-two-numbers
574+
python main.py --solve 1
575+
python main.py --test test_file.py
576+
python main.py --submit submit_file.py
577+
578+
For any issues or feature requests, please visit:
579+
https://github.com/hrdkmishra/leetcode.py
580+
"""
581+
print(help_message)
580582

581583

582584
def replace_files():
@@ -611,6 +613,12 @@ def replace_files():
611613
default="",
612614
help="Set user preferred language (e.g., python3)",
613615
)
616+
@click.option(
617+
"--user-path",
618+
type=str,
619+
default="",
620+
help="Set user preferred path",
621+
)
614622
@click.option("--lib", is_flag=True, default=False, help="Show usage information")
615623
@click.option(
616624
"--question",
@@ -640,10 +648,10 @@ def replace_files():
640648
default="",
641649
help="Specify the filename containing the code to be submitted",
642650
)
643-
# @click.option(
644-
# "--kelp","-k" ,is_flag=True, default=False, help="Show usage information"
645-
# )
646-
def main(config, user_lang, question, solve, test, submit, lib): # remove help_cmd
651+
@click.option(
652+
"--help","-h" ,is_flag=True, default=False, help="Show usage information"
653+
)
654+
def main(config, user_lang, user_path ,question, solve, test, submit, lib,help): # remove help_cmd
647655
if lib:
648656
replace_files()
649657
exit()
@@ -653,6 +661,9 @@ def main(config, user_lang, question, solve, test, submit, lib): # remove help_c
653661
if user_lang:
654662
save_user_data_to_config(user_lang)
655663
exit()
664+
elif user_path:
665+
save_user_path_to_config(user_path)
666+
exit()
656667
else:
657668
leetcode_session, csrf_token = load_credentials_from_config()
658669

@@ -681,8 +692,8 @@ def main(config, user_lang, question, solve, test, submit, lib): # remove help_c
681692
leetcode_session, csrf_token
682693
)
683694
process_submit_file(leetcode_api_instance, api_instance, submit)
684-
# elif help_cmd:
685-
# print_help_usage()
695+
elif help:
696+
print_help_usage()
686697
else:
687698
print(
688699
"Please provide valid command line options. Use --help for usage information."

0 commit comments

Comments
 (0)