|
#!/usr/bin/env python3 |
|
|
|
import argparse |
|
import re |
|
import os |
|
from os import name |
|
|
|
def clear_screen(): |
|
'''Clears the console. OS Independent.''' |
|
|
|
if name == 'nt': os.system('cls') |
|
|
|
else: os.system('clear') |
|
|
|
def gather_input(prompt,break_on_empty_input=False): |
|
'''Gathers input from the user. |
|
|
|
Arguments: |
|
prompt {str} -- The prompt to provide the user. |
|
|
|
Keyword Arguments: |
|
break_on_empty_input {bool} -- If True, the process ends when no input is provided. (default: {False}) |
|
|
|
Returns: |
|
str -- The user's input response to the prompt. |
|
''' |
|
|
|
user_response = '' |
|
|
|
while True: |
|
|
|
clear_screen() |
|
|
|
user_response = input(prompt) |
|
|
|
if ( |
|
len(user_response.strip()) > 0 or |
|
( |
|
len(user_response.strip()) == 0 and |
|
break_on_empty_input)): break |
|
|
|
return user_response |
|
|
|
def gather_inputs(prompt): |
|
'''Gathers multiple user inputs with the same prompt. |
|
|
|
Arguments: |
|
prompt {str} -- The prompt to provide the user. |
|
|
|
Returns: |
|
list -- The user's input responses to the prompt. |
|
''' |
|
|
|
user_responses = [] |
|
|
|
while True: |
|
|
|
user_response = gather_input(prompt,break_on_empty_input=True) |
|
|
|
if len(user_response) == 0: break |
|
|
|
else: user_responses.append(user_response) |
|
|
|
return user_responses |
|
|
|
def gather_methods(): |
|
'''Gathers all the method data, if the user wants to provide any. |
|
|
|
Returns: |
|
str -- The methods as one string. |
|
''' |
|
|
|
methods = [] |
|
|
|
method_template_file_name = os.path.realpath(__file__).replace('create_python_class_file.py','method_template') |
|
|
|
method_template = open(method_template_file_name).read() |
|
|
|
if gather_input('Are there any methods to add? (Y/N)').strip().upper()[0] == 'Y': |
|
|
|
while True: |
|
|
|
method_name = gather_input('Method Name?') |
|
|
|
method_params = ( |
|
'' |
|
if gather_input('Are there any method params? (Y/N)').strip().upper()[0] != 'Y' |
|
else ',' + ', '.join(gather_inputs('?'))) |
|
|
|
method = re.sub(r'!METHOD_NAME!',method_name,method_template) |
|
|
|
method = re.sub(r'!METHOD_PARAMS!',method_params,method) |
|
|
|
methods.append(method) |
|
|
|
if gather_input('Add another method? (Y/N)').strip().upper()[0] != 'Y': break |
|
|
|
return ''.join(methods) |
|
|
|
def create_class_file(target_path,class_name,base_class_name,init_params,methods): |
|
'''Creates the class file using the provided data. |
|
|
|
Arguments: |
|
target_path {str} -- The path to write the class file. |
|
class_name {str} -- The class' name in snake_case. |
|
base_class_name {str} -- The base class name the class is extending. |
|
init_params {str} -- The params to include in the class' __init__ method. |
|
methods {str} -- The class' methods as a string. |
|
''' |
|
|
|
file_name = f'{class_name}.py' |
|
|
|
class_name = class_name.replace('_',' ').title().replace(' ','') |
|
|
|
class_template_file_name = os.path.realpath(__file__).replace('create_python_class_file.py','class_template') |
|
|
|
class_template = open(class_template_file_name).read() |
|
|
|
class_data = re.sub(r'!CLASS_NAME!',class_name,class_template) |
|
|
|
class_data = re.sub(r'!BASE_CLASS!',base_class_name,class_data) |
|
|
|
class_data = re.sub(r'!INIT_PARAMS!',init_params,class_data) |
|
|
|
with open(os.path.join(target_path,file_name),'w+') as out_file: |
|
|
|
out_file.write(class_data) |
|
|
|
out_file.write(methods) |
|
|
|
def setup_argparse(): |
|
'''Sets up the argparser for this script. |
|
|
|
Returns: |
|
ArgParser -- The argparser to use for this script. |
|
''' |
|
|
|
parser = argparse.ArgumentParser(description='Create a new python class file.') |
|
|
|
parser.add_argument('target_path',type=str,help='The target path to write the class file to.') |
|
|
|
return parser |
|
|
|
if __name__ == "__main__": |
|
'''Main Method''' |
|
|
|
parser = setup_argparse() |
|
|
|
args = parser.parse_args() |
|
|
|
target_path = args.target_path |
|
|
|
while True: |
|
|
|
class_name = gather_input('Class Name? (In snake_case, E.g. class_name NOT ClassName)') |
|
|
|
base_class_name = gather_input('Base Class Name? (In TitleCase, E.g. ClassName NOT class_name. If unsure, enter "object")') |
|
|
|
init_params = ( |
|
'' |
|
if gather_input('Are there any __init__ params? (Y/N)').strip().upper()[0] != 'Y' |
|
else ',' + ', '.join(gather_inputs('?'))) |
|
|
|
methods = gather_methods() |
|
|
|
create_class_file(target_path,class_name,base_class_name,init_params,methods) |
|
|
|
if gather_input('Create another class file?').strip().upper()[0] != 'Y': break |