-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwiz.py
252 lines (201 loc) · 7.28 KB
/
wiz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import os
import click
import requests
from flask import Flask
from bs4 import BeautifulSoup
import subprocess
db_options = {'1':'mongodb', '2':'sqlite3', '3':'mysql', '4':'postgresql'}
value_key = {v: k for k,v in db_options.items()} #created reverse mapping of values to keys
#to display options
def display_options():
options = "\n".join([f"{key}: {value}" for key, value in db_options.items()])
return f"Select database system:\n{options}\n(Enter either key or value)"
def create_app():
app = Flask(__name__)
@app.route('/')
def index():
return 'Namaste Duniya! This is a Flask app generated by Flask-Wiz.'
return app
@click.group()
def cli():
pass
# To fetch user's repositories from github
@cli.command()
@click.argument('username')
def fetch(username):
url = "https://github.com/"
tab = '?tab=repositories'
user_url = f"{url}{username}{tab}"
click.echo(f'fetching repositories for user : {username}')
user_file = username + '.txt'
try:
if not os.path.exists(user_file):
response = requests.get(user_url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
repo_links = soup.find_all('a',itemprop = 'name codeRepository')
repo_names = [repo.text.strip() for repo in repo_links]
if repo_names:
click.echo(f"Repositories for user '{username}' : ")
for repo in repo_names:
click.echo(repo)
with open(user_file, 'a') as f:
f.write(repo+"\n")
else:
click.echo(f"No repositories found for user '{username}'")
else:
click.echo(f"User file already exists for user '{username}'")
click.echo("Use show command to see files")
except requests.exceptions.RequestException as e:
click.echo(f'Error fetching repositories: {e}')
except Exception as e:
click.echo(f"An unexpected error occured: {e}")
# To show and get user's repositories
@cli.command()
@click.argument('username')
def get(username):
user_file = username + '.txt'
try:
if os.path.exists(user_file):
with open(user_file) as f:
click.echo(f.read())
else:
click.echo(f"No user file found for user '{username}'")
return # Exit if no user file is found
repo = click.prompt("Enter repo name to get")
if not repo.strip(): # Check if repo name is empty
click.echo("Repo name cannot be empty.")
return
repo_url = f"https://github.com/{username}/{repo}".strip()
click.echo(f"Fetching repo: {repo_url}")
# Attempt to clone the repository
try:
subprocess.run(['git','clone', repo_url], check=True)
click.echo(f'{repo} cloned successfully.')
except subprocess.CalledProcessError:
click.echo('Failed to clone repository')
except Exception as e:
click.echo(f'An error occured : {e}')
except Exception as e:
click.echo(f"An unexpected error occurred: {e}")
# To create new project
@cli.command()
def new():
while True:
name = click.prompt('Enter project name')
if os.path.exists(name):
click.echo(f"Project {name} already exists. Please choose another name.")
else:
break
click.echo(display_options())
while True:
db_input = click.prompt('db selection')
if db_input in db_options:
db_key = db_input
break
elif db_input in value_key:
db_key = value_key[db_input]
break
else:
click.echo('Invalid input. Choose valid Database')
os.makedirs(name)
os.chdir(name)
os.makedirs('templates')
os.chdir('templates')
with open('home.html', 'w') as home_file:
home_file.write("""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flask-app</title>
</head>
<body>
<h1>flask app generated with flask-wiz</h1>
<p>{{ message }}</p>
</body>
</html>""")
os.chdir('..')
os.makedirs('static')
os.chdir('static')
os.makedirs('css')
os.makedirs('js')
os.makedirs('img')
os.chdir('..')
with open('.gitignore', 'w') as gitignore_file:
gitignore_file.write("# Default .gitignore for Flask project\n")
gitignore_file.write("venv/\n")
gitignore_file.write("__pycache__/\n")
gitignore_file.write(".vscode/\n")
with open('.env', 'w') as env_file:
env_file.write("# Default .env file for Flask project\n")
env_file.write("SECRET_KEY = \n")
env_file.write("DATABASE_URL = \n")
env_file.write("API_KEY = \n")
with open('app.py', 'w') as app_file:
db_module = None
match db_key:
case '1':
db_module = 'pymongo'
app_file.write(
"""from flask import Flask, render_template, redirect
from pymongo import MongoClient
app = Flask(__name__)
client = MongoClient('mongodb://localhost:27017/')
db = client['my_database']
@app.route('/')
def index():
return render_template('home.html', message = 'Namaste Duniya! This is a Flask app generated by Flask-Wiz for MongoDB.')
if __name__ == '__main__':
app.run()
""")
case '2':
db_module = 'sqlite3'
app_file.write(
"""from flask import Flask, render_template, redirect
import sqlite3
app = Flask(__name__)
conn = sqlite3.connect('database.db')
@app.route('/')
def index():
return render_template('home.html', message = 'Namaste Duniya! This is a Flask app generated by Flask-Wiz for SQLite.')
if __name__ == '__main__':
app.run()
""")
case '3':
db_module = 'pymysql'
app_file.write(
"""from flask import Flask, render_template, redirect
import pymysql
app = Flask(__name__)
conn = pymysql.connect(host='localhost', user='root', password='', database='my_database')
@app.route('/')
def index():
return render_template('home.html', message = 'Namaste Duniya! This is a Flask app generated by Flask-Wiz MySQL.')
if __name__ == '__main__':
app.run()
""")
case '4':
db_module = 'psycopg2'
app_file.write(
"""from flask import Flask, render_template, redirect
import psycopg2
app = Flask(__name__)
conn = psycopg2.connect(host='localhost', user='postgres', password='', dbname='my_database')
@app.route('/')
def index():
return render_template('home.html', message = 'Namaste Duniya! This is a Flask app generated by Flask-Wiz PostgreSQL.')
if __name__ == '__main__':
app.run()
""")
case _:
print('Invalid choice. Please choose a valid database system.')
if db_module:
os.system("pip install flask")
if db_module != 'sqlite3':
os.system(f"pip install {db_module}") # Install the required database module
# to create a requirements file
os.system(f"pip freeze > requirements.txt")
click.echo(f'New Flask project "{name}" created successfully with {db_module} database!')
if __name__ == '__main__':
cli()