8000 file creation support · Coderbeep/LeetCode-CLI@549a71b · GitHub
[go: up one dir, main page]

Skip to content

Commit 549a71b

Browse files
committed
file creation support
1 parent 5a16a62 commit 549a71b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

leetcode/models/graphql_question_of_today.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from leetcode.models import *
22
from leetcode.models.graphql_question_content import QuestionContent
33
from leetcode.models.graphql_question_info_table import QuestionInfoTable
4+
from leetcode.models.graphql_get_question_detail import GetQuestionDetail
45

56
@dataclass
67
class QueryResult(JSONWizard):
@@ -40,6 +41,7 @@ def __init__(self):
4041
# Instance specific variables
4142
self.contentFlag: bool = False
4243
self.browserFlag: bool = False
44+
self.fileFlag: bool = False
4345
self.title_slug: str = None
4446

4547
self.data = None
@@ -72,7 +74,11 @@ def _execute(self, args) -> None:
7274
self.data = self.leet_API.post_query(self.graphql_query)
7375
self.data = QueryResult.from_dict(self.data['data'])
7476
self.title_slug = self.data.question.titleSlug
77+
7578
self.show()
79+
80+
if self.fileFlag:
81+
self.create_file(self.title_slug)
7682

7783
def show(self) -> None:
7884
""" Shows the question information and content or opens the question in a browser.
@@ -91,6 +97,15 @@ def show(self) -> None:
9197
self.open_in_browser(link)
9298
else:
9399
print(question_info_table)
100+
101+
@classmethod
102+
def create_file(cls, title_slug: str) -> None:
103+
""" Creates a file with the question content. """
104+
question = GetQuestionDetail(title_slug)
105+
filename = f"{question.question_id}.{question.title_slug}.py"
106+
with open(filename, 'w') as file:
107+
file.write(question.code_snippet)
108+
console.print(f"File '{filename}' has been created.")
94109

95110
def __parse_args(self, args) -> None:
96111
""" Parses the command line arguments.
@@ -102,6 +117,9 @@ def __parse_args(self, args) -> None:
102117
self.browserFlag = True
103118
if getattr(args, 'contents'):
104119
self.contentFlag = True
120+
if getattr(args, 'file'):
121+
self.fileFlag = True
122+
105123

106124

107125

0 commit comments

Comments
 (0)
0