Skip to content

Commit 43fb452

Browse files
committed
v5.3
1 parent b1c367a commit 43fb452

File tree

5 files changed

+123
-99
lines changed

5 files changed

+123
-99
lines changed

.vs/create_structure/v16/.suo

3 KB
Binary file not shown.

.vs/slnx.sqlite

0 Bytes
Binary file not shown.

bin/create_structure.py

Lines changed: 113 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,157 +4,173 @@
44
If there was any type of problem you can contact me on my help email: help@castellanidavide.it
55
"""
66
# Some imports
7-
import os
8-
from datetime import *
9-
from github import Github
10-
import pygit2
7+
from datetime import datetime as dt
118
from getpass import getpass
12-
import requests
9+
from github import Github
10+
from re import escape, compile
11+
from requests import get as wget
12+
from threading import Thread
1313

1414
__author__ = "help@castellanidavide.it"
15-
__version__ = "5.0 2020-11-21"
15+
__version__ = "5.3 2020-11-29"
1616

1717
TOKEN = "TODO"
1818
SOUCES_OF_TEMPLATES = ["TODO", "CastellaniDavide"]
1919
ORGANIZATION_NAME = "TODO" # Leave empty if you want to add into your personal GitHub account
20+
IGNORE_FOLDERS = ["TODO", ".vs"]
2021

2122
class create_structure:
2223
def __init__ (self):
2324
"""Main function
2425
"""
25-
g = create_structure.login() # Login
26+
27+
Thread(target = self.login()).start() # Login
2628

2729
# Make questions
30+
self.asks()
31+
32+
# Make repo
33+
Thread(target = self.create_repo()).start()
34+
35+
# Get template
36+
self.choose_template()
37+
38+
# Get changes
39+
self.change_map()
40+
41+
# Make all
42+
Thread(target = self.scan_and_elaborate()).start()
43+
44+
def login(self):
45+
"""Made the login in GitHub
46+
"""
47+
self.g = Github(TOKEN)
48+
49+
def asks(self):
2850
questions = [["name", "Name of the project (es. create_structure): "],
2951
["extention", "Extenction of the main programm (es. py): "],
3052
["descr", "Description of the project: "],
31-
["prefix", "Insert the prefix of the repository (or don't insert something): "],
53+
["prefix", "Insert the prefix of the repository (or don't insert anything): "],
3254
["team", "Do you want insert this repo into a team? [Y/n]: "],
33-
["private", "Is that private?(Y/N): "],
55+
["private", "Is that private? [Y/n]: "],
3456
]
35-
results = {}
36-
57+
self.results = {}
58+
3759
# Get infos
3860
for question_tag, current_quest in questions:
3961
if question_tag == "team":
62+
self.results["team"] = "" # default value
4063
if ORGANIZATION_NAME != "": # If there is an organization
4164
if input(current_quest) == "Y":
42-
create_structure.choose_team(g, results)
43-
else:
44-
results["team"] = ""
65+
self.choose_team()
4566
else:
46-
results[question_tag] = input(current_quest)
47-
48-
print()
67+
self.results[question_tag] = input(current_quest)
4968

50-
# Make repo
51-
repo = create_structure.create_repo(results, g)
52-
print(f"Repo built")
53-
54-
typerepo = create_structure.choose_template(results, g)
55-
print(f"Template founded ({typerepo})")
56-
57-
create_structure.scan_and_elaborate(repo, g.get_repo(typerepo), "", typerepo, results)
58-
59-
def login():
60-
"""Made the login in GitHub
61-
"""
62-
return Github(TOKEN)
69+
print()
6370

64-
def choose_team(g, results):
71+
def choose_team(self):
6572
"""Choose a team
6673
"""
67-
teams = g.get_organization('CastellaniDavide').get_teams()
68-
for i, team in enumerate(teams):
69-
print(f"{i})\t{team.name}")
70-
71-
try:
72-
results["team"] = teams[int(input("Insert your team number: "))].id
74+
try: # No teams
75+
teams = self.g.get_organization(ORGANIZATION_NAME).get_teams()
76+
enumerate(teams)[0]
77+
for i, team in enumerate(teams):
78+
print(f"{i})\t{team.name}")
79+
80+
try:
81+
self.results["team"] = teams[int(input("Insert your team number: "))].id
82+
except:
83+
print("This team didn't exist, try again")
84+
self.choose_team()
7385
except:
74-
print("This team didn't exist, try again")
75-
create_structure.choose_team(g, results)
86+
print("Sorry, you didn't have any team. Create a new team to use this option")
7687

77-
def create_repo(results, g):
78-
"""Create the repo in CastellaniDavide repository
88+
def create_repo(self):
89+
"""Create the repo
7990
"""
8091
if ORGANIZATION_NAME == "":
81-
return g.get_user().create_repo(results['name'] if(results['prefix'] == "") else f"{results['prefix']}-{results['name']}", description=results['descr'], private=results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False)
92+
self.repo = self.g.get_user().create_repo(self.results['name'] if(self.results['prefix'] == "") else f"{self.results['prefix']}-{self.results['name']}", description=self.results['descr'], private=self.results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False)
8293
else:
83-
if results["team"] == "":
84-
return g.get_organization(ORGANIZATION_NAME).create_repo(results['name'] if(results['prefix'] == "") else f"{results['prefix']}-{results['name']}", description=results['descr'], private=results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False)
94+
if self.results["team"] == "":
95+
self.repo = self.g.get_organization(ORGANIZATION_NAME).create_repo(self.results['name'] if(self.results['prefix'] == "") else f"{self.results['prefix']}-{self.results['name']}", description=self.results['descr'], private=self.results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False)
8596
else:
86-
return g.get_organization(ORGANIZATION_NAME).create_repo(results['name'] if(results['prefix'] == "") else f"{results['prefix']}-{results['name']}", description=results['descr'], private=results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False, team_id=results["team"])
97+
self.repo = self.g.get_organization(ORGANIZATION_NAME).create_repo(self.results['name'] if(self.results['prefix'] == "") else f"{self.results['prefix']}-{self.results['name']}", description=self.results['descr'], private=self.results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False, team_id=self.results["team"])
98+
99+
print(f"Repo built")
87100

88-
def choose_template(results, g):
101+
def choose_template(self):
89102
"""This helps to find the correct template
90103
"""
104+
# If there wasn't any other template for your type of extention and no one default into SOURCES list, give my default code
105+
self.template_name = "CastellaniDavide/default-template"
106+
91107
# Check if there is my template
92108
for source in SOUCES_OF_TEMPLATES:
93-
if source != "TODO":
109+
if source != "TODO" and self.template_name == "CastellaniDavide/default-template":
94110
try:
95-
return g.get_repo(f"{source}/{results['extention']}-template").full_name
111+
self.template_name = self.g.get_repo(f"{source}/{results['extention']}-template").full_name
112+
break
96113
except:
97114
pass
98115

99116
# Check if there was a default template
100-
for source in SOUCES_OF_TEMPLATES:
101-
if source != "TODO":
102-
try:
103-
return g.get_repo(f"{source}/default-template").full_name
104-
except:
105-
pass
106-
107-
# If there wasn't any template for your type of extention and no one default into SOURCES list, give my default code
108-
return "CastellaniDavide\default"
109-
110-
def scan_and_elaborate(repo, template, loc, typerepo, results):
117+
if self.template_name == "CastellaniDavide/default-template":
118+
for source in SOUCES_OF_TEMPLATES:
119+
if source != "TODO" and self.template_name == "CastellaniDavide/default-template":
120+
try:
121+
self.template_name = self.g.get_repo(f"{source}/default-template").full_name
122+
break
123+
except:
124+
pass
125+
126+
self.template = self.g.get_repo(self.template_name)
127+
print(f"Template founded ({self.template_name})")
128+
129+
def scan_and_elaborate(self, loc=""):
111130
"""Scan all files in the repository and push it in the new directory (cahanging the necessary)
112131
"""
113-
contents = template.get_contents(f"{loc}")
132+
contents = self.template.get_contents(f"{loc}")
114133
for content_file in contents:
115-
if not content_file.path in [".castellanidavide", "", ".vs"]:
116-
print(content_file.path)
117-
if "." in content_file.path and not content_file.path[0] == "." and not "default" in content_file.path:
118-
try:
119-
create_structure.scan_and_elaborate(repo, template, content_file.path, typerepo, results)
120-
except:
121-
create_structure.elaborate_file(repo, typerepo, content_file.path, results)
134+
if not content_file.path in [".castellanidavide", ""] + IGNORE_FOLDERS:
135+
if content_file.type == "file":
136+
Thread(target = self.create_file, args = (self.change(content_file.path), f"{self.change(wget(f'https://raw.githubusercontent.com/{self.template_name}/master/{content_file.path}').text)}")).start()
122137
else:
123-
try:
124-
create_structure.scan_and_elaborate(repo, template, content_file.path, typerepo, results)
125-
except:
126-
create_structure.elaborate_file(repo, typerepo, content_file.path, results)
138+
Thread(target = self.scan_and_elaborate, args = (content_file.path, )).start()
139+
140+
def change_map(self):
141+
"""Returns a map of changes
142+
"""
143+
time = dt.now()
144+
145+
# repo changes
146+
change_map = eval(wget(f"https://raw.githubusercontent.com/{self.template_name}/master/.castellanidavide/change.json").text)
127147

128-
def elaborate_file(repo, typerepo, filepath, results):
129-
"""Elaborate the file
130-
"""
131-
page = requests.get(f"https://raw.githubusercontent.com/{typerepo}/master/{filepath}").text
132-
133-
try:
134-
page = create_structure.change(page, typerepo, results)
135-
repo.create_file(filepath.replace("template", results['name']), f"Created {filepath}", f"{page}")
136-
print(f"Created {filepath.replace('template', results['name'])}")
137-
except:
138-
print(f"Error {filepath.replace('template', results['name'])}\t{type(page)}\n{page}")
148+
# answer changes
149+
for key, value in self.results.items():
150+
change_map[f"sol{key}sol"] = value
139151

140-
def change(page, typerepo, results):
141-
"""Returns the changed page
142-
"""
143-
data = eval(requests.get(f"https://raw.githubusercontent.com/{typerepo}/master/.castellanidavide/change.json").text)
144-
advanced_values = { f"sol{i}sol" : j for i, j in results.items()}
145-
time = datetime.now()
146-
time = f"{str(time.year)}-{str(time.month)}-{str(time.day)}"
152+
# special changes
153+
change_map["time__now"] = f"{str(time.year)}-{str(time.month)}-{str(time.day)}"
154+
change_map["time_now"] = f"{str(time.year)}{str(time.month)}{str(time.day)}"
147155

148-
for i in data.keys():
149-
page = page.replace(i.replace("'''", '"'), data[i].replace("'''", '"'))
156+
# re dict, because I can use it faster (eg. for changes)
157+
self.change_map = dict((escape(k), v) for k, v in change_map.items())
150158

151-
for i in advanced_values.keys():
152-
page = page.replace(i, advanced_values[i])
159+
def change(self, text):
160+
"""Returns the changed page
161+
Change two times for special keys
162+
"""
163+
return compile("|".join(self.change_map.keys())).sub(lambda m: self.change_map[escape(m.group(0))], compile("|".join(self.change_map.keys())).sub(lambda m: self.change_map[escape(m.group(0))], text))
153164

154-
page = page.replace("time__now", time)
155-
page = page.replace("time_now", time.replace("_", "").replace("-", ""))
156-
157-
return page
165+
def create_file (self, path, file):
166+
"""Create the file into the repo
167+
"""
168+
try:
169+
self.repo.create_file(path, f"Created {path}", file)
170+
print(f"Created {path}")
171+
except:
172+
# If it's an error, possible with multitreading, try again
173+
self.create_file (path, file)
158174

159175
if __name__ == "__main__":
160176
assert TOKEN != "TODO", "You must to put your tocken into TOKEN variable"

docs/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# create_structure
2-
[![GitHub license](https://img.shields.io/badge/licence-GNU-green?style=flat)](https://github.com/CastellaniDavide/cpp-create_structure/blob/master/LICENSE) ![Author](https://img.shields.io/badge/author-Castellani%20Davide-green?style=flat) ![Version](https://img.shields.io/badge/version-v5.2-blue?style=flat) ![Language Python](https://img.shields.io/badge/language-Python-yellowgreen?style=flat) ![sys.platform supported](https://img.shields.io/badge/OS%20platform%20supported-Linux,%20Windows%20&%20Mac%20OS-blue?style=flat) [![On GitHub](https://img.shields.io/badge/on%20GitHub-True-green?style=flat&logo=github)](https://github.com/CastellaniDavide/create_structure)
2+
[![GitHub license](https://img.shields.io/badge/licence-GNU-green?style=flat)](https://github.com/CastellaniDavide/cpp-create_structure/blob/master/LICENSE) ![Author](https://img.shields.io/badge/author-Castellani%20Davide-green?style=flat) ![Version](https://img.shields.io/badge/version-v5.3-blue?style=flat) ![Language Python](https://img.shields.io/badge/language-Python-yellowgreen?style=flat) ![sys.platform supported](https://img.shields.io/badge/OS%20platform%20supported-Linux,%20Windows%20&%20Mac%20OS-blue?style=flat) [![On GitHub](https://img.shields.io/badge/on%20GitHub-True-green?style=flat&logo=github)](https://github.com/CastellaniDavide/create_structure)
33

44
## Description
55
This is the magic "robottino" by Castellani Davide
@@ -56,6 +56,7 @@ With this programm you can easly create a repository on GitHub with a basic temp
5656
- Try to execute this programm and check correct use
5757

5858
# Changelog
59+
- [5.3_2020-11-29](#53_2020-11-29)
5960
- [5.2_2020-11-28](#52_2020-11-28)
6061
- [5.1_2020-11-28](#51_2020-11-28)
6162
- [5.0_2020-11-21](#50_2020-11-21)
@@ -65,6 +66,14 @@ With this programm you can easly create a repository on GitHub with a basic temp
6566
- [2.0_2020-3-24](#20_2020-3-24)
6667
- [1.0_2020-3-24](#10_2020-3-24)
6768

69+
### 5.3_2020-11-29
70+
- removed some untils lines
71+
- accelerated the code
72+
- parametrized the folder to be ignored
73+
- start to use multitreading (41 sec. -> 10 sec.) (I made this test using [this template](https://github.com/CastellaniDavide/py-template))
74+
- optimized imports
75+
- optimised variables
76+
6877
### 5.2_2020-11-28
6978
- Added the website documentation
7079
- Fixed a bug

requirements/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22

33
DateTime>=4.3
44
PyGithub>=1.53
5-
pygit2>=1.2.1
65
requests>=2.24.0

0 commit comments

Comments
 (0)