66from github import Github
77import pygit2
88from getpass import getpass
9+ import requests
910
1011__author__ = "davidecastellani@castellanidavide.it" , "chiara@sabaini.com"
11- __version__ = "03.01 2020-08-15 "
12+ __version__ = "3.1 2020-08-18 "
1213
13- class create_structure_03_01 :
14+ class create_structure :
1415 def __init__ (self ):
1516 """Main function
1617 """
17- questions = [ "Inserisci nome progetto (es. create_structur ): " , #0
18+ questions = [ "Inserisci nome progetto (es. create_structure ): " , #0
1819 "Inserisci estensione file principale (es. py): " , #1
1920 "Inserisci una descrizione del progetto: " , #2
2021 "Il progetto è con Boscaini (<nome della classe>-<numero consegna>), Bellini (b) o qualcunaltro(lasciare vuoto)? " , #3
@@ -33,24 +34,33 @@ def __init__ (self):
3334
3435 print ()
3536
37+ g = create_structure .login (results )
38+ print (f"Credentials OK" )
39+
3640 folder_name = f"./{ results [0 ].lower ()} _01_01" if (results [3 ] == "b" ) else (f"./{ results [0 ]} " if (results [3 ] == "" ) else f"./{ results [3 ]} -{ results [0 ]} " )
37- create_structure_03_01 .create_folder (folder_name )
38- print (f"Folder built" )
41+ if results [5 ] == "N" :
42+ create_structure .create_folder (folder_name )
43+ print (f"Folder built" )
3944
40- repo = create_structure_03_01 .create_repo (results )
45+ repo = create_structure .create_repo (results , g )
4146 print (f"Repo built" )
4247
43- create_structure_03_01 .create_source (repo , results )
44- print (f"Source file built" )
45-
46- create_structure_03_01 .create_README (repo , results )
47- print (f"README.md built" )
48+ typerepo = create_structure .choose_template (results , g )
49+ print (f"Template founded ({ typerepo } )" )
4850
49- create_structure_03_01 . create_log ( repo )
50- print ( f"trace.log built" )
51+ template = g . get_repo ( f"CastellaniDavide/ { typerepo } -template" )
52+ create_structure . scan_and_elaborate ( repo , template , "" , typerepo , results )
5153
52- create_structure_03_01 .download_repo (repo , folder_name )
53- print (f"repo downloaded" )
54+ if results [5 ] == "N" :
55+ create_structure .download_repo (repo , folder_name )
56+ print (f"repo downloaded" )
57+ else :
58+ print (f"Your repo is redy online" )
59+
60+ def login (results ):
61+ """Made the login in GitHub
62+ """
63+ return Github (results [6 ], results [7 ])
5464
5565 def create_folder (directory ):
5666 """Creates a folder
@@ -61,74 +71,68 @@ def create_folder(directory):
6171 except OSError :
6272 print ('Error: Creating directory. ' + directory )
6373
64- def create_repo (results ):
74+ def create_repo (results , g ):
6575 """Create the repo in CastellaniDavide repository
6676 """
6777 if results [4 ] == "Y" :
68- repo = Github ( results [6 ], results [7 ]). get_organization ( "CastellaniDavide" ). create_repo ( results [0 ] , description = results [2 ], private = results [5 ] == "Y" , has_issues = False , has_wiki = False , has_downloads = True , has_projects = False , team_id = 4008430 )
78+ repo = g . get_organization ( "CastellaniDavide" ). create_repo ( results [0 ] if ( results [3 ] == "" ) else f" { results [3 ] } - { results [ 0 ] } " , description = results [2 ], private = results [5 ] == "Y" , has_issues = True , has_wiki = False , has_downloads = True , has_projects = False , team_id = 4008430 )
6979 else :
70- repo = Github ( results [6 ], results [7 ]). get_organization ( "CastellaniDavide" ). create_repo ( results [0 ] , description = results [2 ], private = results [5 ] == "Y" , has_issues = False , has_wiki = False , has_downloads = True , has_projects = False )
71-
80+ repo = g . get_organization ( "CastellaniDavide" ). create_repo ( results [0 ] if ( results [3 ] == "" ) else f" { results [3 ] } - { results [ 0 ] } " , description = results [2 ], private = results [5 ] == "Y" , has_issues = True , has_wiki = False , has_downloads = True , has_projects = False )
81+
7282 return repo
7383
74- def create_source ( repo , results ):
75- """Writes the standards line of code based on the extension
84+ def choose_template ( results , g ):
85+ """This helps to find the correct template
7686 """
77- time = datetime .now ()
78-
79- if (results [1 ] == "py" ):
80- text = f'"""{ results [0 ]} \n """\n \n __author__ = "davidecastellani@castellanidavide.it", "chiara@sabaini.com"\n __version__ = "01.01 { str (time .day )} -{ str (time .month )} -{ str (time .day )} "\n \n ' # first part
81- text += f'class { results [0 ]} :\n \t def __init__ (self):\n \n if __name__ == "__main__":\n \t { results [0 ]} ()'
82- elif (results [1 ] == "js" ):
83- text = f'/**\n * @file { results [0 ]} .{ results [1 ]} \n *\n * @version 01.01 { str (time .year )} { str (time .month )} { str (time .day )} \n *\n * @brief desc\n *\n * @ingroup { results [0 ]} \n * (Note: this needs exactly one @defgroup somewhere)\n *\n * @author Castellani Davide & Sabaini Chiara\n *\n *Contact: davidecastellani@castellanidavide.it, chiara@sabaini.com\n *\n */'
84- elif (results [1 ] == "c" ):
85- init = f'/**\n * @file { results [0 ]} .{ results [1 ]} \n *\n * @version 01.01 { str (time .year )} { str (time .month )} { str (time .day )} \n *\n * @brief desc\n *\n * @ingroup { results [0 ]} \n * (Note: this needs exactly one @defgroup somewhere)\n *\n * @author Castellani Davide & Sabaini Chiara\n *\n *Contact: davidecastellani@castellanidavide.it, chiara@sabaini.com\n *\n */'
86- text = init + ""
87- elif (results [1 ] == "cpp" ):
88- init = f'/**\n * @file { results [0 ]} .{ results [1 ]} \n *\n * @version 01.01 { str (time .year )} { str (time .month )} { str (time .day )} \n *\n * @brief desc\n *\n * @ingroup { results [0 ]} \n * (Note: this needs exactly one @defgroup somewhere)\n *\n * @author Castellani Davide & Sabaini Chiara\n *\n *Contact: davidecastellani@castellanidavide.it, chiara@sabaini.com\n *\n */'
89- text = init + f'\n \n #include <bits/stdc++.h>\n \n using namespace std;\n \n int main() { "{" } \n return 0;\n { "}" } '
90- elif (results [1 ] == "cs" ):
91- init = f'/**\n * @file { results [0 ]} .{ results [1 ]} \n *\n * @version 01.01 { str (time .year )} { str (time .month )} { str (time .day )} \n *\n * @brief desc\n *\n * @ingroup { results [0 ]} \n * (Note: this needs exactly one @defgroup somewhere)\n *\n * @author Castellani Davide & Sabaini Chiara\n *\n *Contact: davidecastellani@castellanidavide.it, chiara@sabaini.com\n *\n */'
92- elif (results [1 ] == "html" ):
93- text = f'<!DOCTYPE html>\n <!--\n @author: Castellani Davide & Sabaini Chiara 3CI a.s. 2019/2020\n @version: 01.01 { str (time .year )} -{ str (time .month )} -{ str (time .day )} \n -->\n <html>\n \n <head>\n \n \t <title>{ results [0 ]} </title>\n \n \t <meta charset="utf-8">\n \n \t <meta name="viewport" content="width=device-width, initial-scale=1">\n \n \t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">\n \n \t <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>\n \n \t <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>\n \n \t <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>\n \n \t <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>\n \n </head>\n \n \n \n <body>\n \n \t <div class="container">\n \n \t </div>\n \n </body>\n \n </html>'
94- elif (results [1 ] == "bat" ):
95- text = f"@ECHO OFF\n CLS\n \n REM\n { results [0 ]} \n REM Made by Castellani Davide & Sabaini Chiara 3CI a.s. 2019/2020\n "
96- elif (results [1 ] == "ino" ):
97- text = f"// Author: Castellani Davide & Sabaini Chiara\n // Version: 01.01 { str (time .year )} -{ str (time .month )} -{ str (time .day )} \n void setup(){ '{' } \n Serial.begin(9600);\n { '}' } \n void loop(){ '{' } \n \n { '}' } "
98- else :
99- text = f"# { results [0 ]} "
100-
101- if (results [4 ] == "N" ):
102- text = text .replace (" & Sabaini Chiara" , "" ).replace (', "chiara@sabaini.com"' , "" )
103-
104- repo .create_file (f"bin/{ results [0 ]} .{ results [1 ]} " , f"Created { results [0 ]} .{ results [1 ]} " , text )
87+ try :
88+ g .get_repo (f"CastellaniDavide/{ results [1 ]} -template" )
89+ return f"{ results [1 ]} "
90+ except :
91+ return "default"
10592
106- def create_README (repo , results ):
107- """Creates the README.md
93+ def scan_and_elaborate (repo , template , loc , typerepo , results ):
94+ """Scan all files in the repository and push it in the new directory (cahanging the necessary)
95+ """
96+ contents = template .get_contents (f"{ loc } " )
97+ for content_file in contents :
98+ if not content_file .path in [".castellanidavide" , "" , ".vs" ]:
99+ if "." in content_file .path and not content_file .path [0 ] == "." :
100+ create_structure .elaborate_file (repo , typerepo , content_file .path , results )
101+ else :
102+ try :
103+ create_structure .scan_and_elaborate (repo , template , content_file .path , typerepo , results )
104+ except :
105+ create_structure .elaborate_file (repo , typerepo , content_file .path , results )
106+
107+ def elaborate_file (repo , typerepo , filepath , results ):
108+ """Elaborate the file
109+ """
110+ try :
111+ page = requests .get (f"https://raw.githubusercontent.com/CastellaniDavide/{ typerepo } -template/master/{ filepath } " ).text
112+ page = create_structure .change (page , typerepo , results )
113+ repo .create_file (filepath .replace ("template" , results [0 ]), f"Created { filepath } " , f"{ page } " )
114+ print (f"Created { filepath .replace ('template' , results [0 ])} " )
115+ except :
116+ print (f"Error { filepath .replace ('template' , results [0 ])} \t { type (page )} \n { page } " )
117+
118+ def change (page , typerepo , results ):
119+ """Returns the changed page
108120 """
121+ data = eval (requests .get (f"https://raw.githubusercontent.com/CastellaniDavide/{ typerepo } -template/master/.castellanidavide/change.json" ).text )
122+ advanced_values = { f"sol{ i } sol" : j for i , j in enumerate (results )}
109123 time = datetime .now ()
124+ time = f"{ str (time .year )} -{ str (time .month )} -{ str (time .day )} "
110125
111- init = f"# { results [0 ]} \n "
112- tags = f"\n ## Tags\n #tag1, #tag2\n "
113- desc = f"\n ## Description\n { results [2 ]} \n "
114- req = f"\n ## Required\n - \n "
115- dir_structure = f"\n ### Directories structure\n - bin\n \t - { results [0 ]} .{ results [1 ]} \n - doc\n \t - README.md\n - log\n \t - trace.log\n "
116- exe_examples = f"\n ### Execution examples\n - { results [0 ]} .{ results [1 ]} \n \t - \n "
117- changelog = f"\n ## Changelog\n - [01.01_{ str (time .year )} -{ str (time .month )} -{ str (time .day )} ](#01.01_{ str (time .year )} -{ str (time .month )} -{ str (time .day )} )\n "
118- version = f"\n ### 01.01_{ str (time .year )} -{ str (time .month )} -{ str (time .day )} \n - first version\n "
119- finish = f"\n Made by Castellani Davide & Sabaini Chiara\n If you have any problem please contact us:\n - davidecastellani@castellanidavide.it\n - chiara@sabaini.com"
120- sep = f"\n ---"
121- text = init + tags + desc + req + sep + dir_structure + sep + exe_examples + sep + changelog + sep + version + sep + finish
122-
123- if (results [4 ] == "N" ):
124- text = text .replace (" & Sabaini Chiara" , "" ).replace ("\n - chiara@sabaini.com" , "" )
125-
126- repo .create_file ("docs/README.md" , f"Created README.md" , text )
126+ for i in data .keys ():
127+ page = page .replace (i .replace ("'''" , '"' ), data [i ].replace ("'''" , '"' ))
127128
128- def create_log (repo ):
129- """Creates the trace.log
130- """
131- repo .create_file ("log/trace.log" , f"Created trace.log" , "" )
129+ for i in advanced_values .keys ():
130+ page = page .replace (i , advanced_values [i ])
131+
132+ page = page .replace ("time__now" , time )
133+ page = page .replace ("time_now" , time .replace ("_" , "" ))
134+
135+ return page
132136
133137 def download_repo (repo , folder_name ):
134138 """Downloads the repo into folder_name
@@ -137,6 +141,6 @@ def download_repo(repo, folder_name):
137141
138142if __name__ == "__main__" :
139143 try :
140- create_structure_03_01 ()
144+ create_structure ()
141145 except :
142146 print ("There is an error, try again." )
0 commit comments