-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectSetup.py
More file actions
165 lines (136 loc) · 7.12 KB
/
ProjectSetup.py
File metadata and controls
165 lines (136 loc) · 7.12 KB
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
# Import system library
import sys
sys.path.append('.')
# load environment variables
import os
import json
from dotenv import load_dotenv, set_key
# Set working directory and filename
current_filename = os.path.basename(__file__) # The name of the current file
project_dir = os.path.dirname(__file__) # The project directory
project_name = os.path.basename(project_dir).strip() # The name of the project
project_env_file = os.path.join(project_dir, 'project.env')
project_csv_file = os.path.join(project_dir, 'project.csv')
# load project.env file
load_dotenv(project_env_file)
# Project directories
print('Project Name :\n', project_name)
print('File :\n', current_filename)
print('Project Directory :\n', project_dir)
print('Environment Variables :\n', project_env_file)
# Create environment variables
os.environ['ProjectName'] = project_name
os.environ['ProjectDir'] = project_dir
os.environ['ProjectEnv'] = project_env_file
os.environ['ProjectCsv'] = project_csv_file
# Write to project.env file
set_key(project_env_file, "ProjectName", os.environ["ProjectName"])
set_key(project_env_file, "ProjectDir", os.environ["ProjectDir"])
set_key(project_env_file, "ProjectEnv", os.environ["ProjectEnv"])
set_key(project_env_file, "ProjectCsv", os.environ["ProjectCsv"])
# List all subfolders
subfolders = [ f.path for f in os.scandir(project_dir) if f.is_dir() ]
# Create PYTHONPATH to include locals modules:
os.environ['PYTHONPATH'] = project_dir
# Create a python configuration setup for vscode:
python_vscode_debug_config = {"name": "{}: {}: {}".format("Python","Module", project_name),"type": "{}".format("debugpy"),"request": "{}".format("launch"),"module": "{}".format(project_name),"env":{"PYTHONPATH": project_dir},"console": "{}".format("integratedTerminal")}
python_vscode_debug_config = json.dumps(python_vscode_debug_config) # , indent=4
# Create vscode_config to include locals modules when working with vscode:
os.environ['python_vscode_debug_config'] = python_vscode_debug_config
# Write PYTHONPATH to .env file.
set_key(project_env_file, "PYTHONPATH", os.environ["PYTHONPATH"])
# Write configuration setup for vscode to .env file.
set_key(project_env_file, "python_vscode_debug_config", os.environ["python_vscode_debug_config"])
# Set vscode workspace
def vscode_workspace(project_name, subfolders, project_env_file):
try:
# load project_template.code-workspace
for folder in subfolders:
if os.path.basename(folder) == ".vscode":
workspace_template = os.path.join(folder, 'workspace_template.json')
project_workspace = os.path.join(folder, '{}.code-workspace'.format(project_name))
# Open template json file and write data
with open(workspace_template, 'r', encoding='utf-8') as workspace:
# loda json data
data = json.load(workspace)
# Update workspace data
# Folders
for folder in subfolders:
name = os.path.basename(folder)
data["folders"].append({"path": name})
# Settings
data["settings"]["terminal.integrated.cwd"] = project_dir # Update
data["settings"]["python.envFile"] = project_env_file # Update
data["settings"]["projectManager.any.baseFolders"].append(project_dir) # Update
data["settings"]["projectManager.git.baseFolders"].append(project_dir) # Update
data["settings"]["projectManager.vscode.baseFolders"].append(project_dir) # Update
# Launch for debuging
python_launch_debug_configuration = os.getenv("python_vscode_debug_config")
#python_launch_debug_configuration = python_launch_debug_configuration.replace('\"', '')
data["launch"]["configurations"].append(python_launch_debug_configuration ) # Python
# Tasks
# write data to workspace
data = json.dumps(data, indent=4)
with open(project_workspace, 'w', encoding='utf-8') as workspace:
workspace.write(data)
else:
pass
# Create environment variables
os.environ['vscode_workspace'] = project_workspace
# Write configuration setup for vscode to .env file.
set_key(project_env_file, "vscode_workspace", os.environ["vscode_workspace"])
except (OSError, NameError, ImportError, SyntaxError, UnicodeError) as error:
print(error.args)
# Add comment to workspace file
def AddComment(workspace_file):
comment = {
"settings":"// Controls the settings that apply to all profiles :",
"launch":"// Add debuging configurations to project :",
"tasks":"// Automate project tasks"
}
try:
with open(workspace_file, 'r', encoding='utf-8') as workspace:
data = workspace.read()
# Update data with comments location
update_data = data[:]
# serach pattern settings
m_settings = update_data.find("settings")
update_data = f"{update_data[:m_settings-2]}\n// Controls the settings that apply to all profiles :\n{update_data[m_settings-2:]}"
# serach pattern launch
m_launch = update_data.find("launch")
update_data = f"{update_data[:m_launch-2]}\n// Add debuging configurations to project :\n{update_data[m_launch-2:]}"
# serach pattern tasks
m_tasks = update_data.find("tasks")
update_data = f"{update_data[:m_tasks-2]}\n// Automate project tasks :\n{update_data[m_tasks-2:]}"
with open(workspace_file, 'w', encoding='utf-8') as workspace:
# Write updates to file
workspace.write(update_data)
except (OSError, NameError, ImportError, SyntaxError, UnicodeError) as error:
print(error.args)
# Create a project csv file
import pandas as pd
def ProjectCsv(project_csv_file):
projectInfo = {
"ProjectName":["TemplateProject"],
"LinkProjectRepo":["https://github.com/pira245/FlaskTasksManager"],
"ImageName":["FlaskTasksManager.png"],
"LinkImageRepo":["https://github.com/pira245/FlaskTasksManager/media/FlaskTasksManager.png"],
"Category":["Web"],
"ProjectAbout":["This is a basic web aplication created with Python and Flask to manage tasks."]
}
try:
# Convert Python dic to csv
df = pd.DataFrame.from_dict(projectInfo, orient='columns')
# Write CSV to file and add to OS env file
df.to_csv(project_csv_file, index=False)
except (OSError, NameError, ImportError, SyntaxError, UnicodeError) as error:
print(error.args)
if __name__ == "__main__":
# Set vscode workspace
vscode_workspace(project_name, subfolders, project_env_file)
# Add comment to workspace
workspace_file = os.getenv("vscode_workspace")
AddComment(workspace_file)
# Create a project csv
csv_file = os.getenv("ProjectCsv")
ProjectCsv(csv_file)