2
2
import click
3
3
from bs4 import BeautifulSoup
4
4
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 *
11
6
import leetcode
12
7
import leetcode .auth
13
8
import requests
@@ -507,7 +502,7 @@ def display_question_detail(api_instance, title_slug): #did changes here
507
502
508
503
509
504
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
511
506
code_snippets = question_detail_data .get ("codeSnippets" , [])
512
507
code = next (
513
508
(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):
516
511
if code :
517
512
lang_extension = LANG_EXTENSIONS .get (lang )
518
513
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
+
521
521
if not os .path .exists (leetcode_folder ):
522
522
os .makedirs (leetcode_folder )
523
523
file_path = os .path .join (
@@ -534,6 +534,7 @@ def write_code_snippet_to_file(question_detail_data, lang, title_slug):
534
534
print (f"Code snippet for { lang } is not available for this question." )
535
535
536
536
537
+
537
538
def get_title_slug_from_filename (filepath ):
538
539
base_name = os .path .basename (filepath )
539
540
title_slug , _ = os .path .splitext (base_name )
@@ -551,32 +552,33 @@ def title_and_file_extension(file):
551
552
return title_slug , lang_name
552
553
553
554
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
557
558
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 )
580
582
581
583
582
584
def replace_files ():
@@ -611,6 +613,12 @@ def replace_files():
611
613
default = "" ,
612
614
help = "Set user preferred language (e.g., python3)" ,
613
615
)
616
+ @click .option (
617
+ "--user-path" ,
618
+ type = str ,
619
+ default = "" ,
620
+ help = "Set user preferred path" ,
621
+ )
614
622
@click .option ("--lib" , is_flag = True , default = False , help = "Show usage information" )
615
623
@click .option (
616
624
"--question" ,
@@ -640,10 +648,10 @@ def replace_files():
640
648
default = "" ,
641
649
help = "Specify the filename containing the code to be submitted" ,
642
650
)
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
647
655
if lib :
648
656
replace_files ()
649
657
exit ()
@@ -653,6 +661,9 @@ def main(config, user_lang, question, solve, test, submit, lib): # remove help_c
653
661
if user_lang :
654
662
save_user_data_to_config (user_lang )
655
663
exit ()
664
+ elif user_path :
665
+ save_user_path_to_config (user_path )
666
+ exit ()
656
667
else :
657
668
leetcode_session , csrf_token = load_credentials_from_config ()
658
669
@@ -681,8 +692,8 @@ def main(config, user_lang, question, solve, test, submit, lib): # remove help_c
681
692
leetcode_session , csrf_token
682
693
)
683
694
process_submit_file (leetcode_api_instance , api_instance , submit )
684
- # elif help_cmd :
685
- # print_help_usage()
695
+ elif help :
696
+ print_help_usage ()
686
697
else :
687
698
print (
688
699
"Please provide valid command line options. Use --help for usage information."
0 commit comments