def tell_exposed_algonames(self): """ This function will tell what algorithms will be exposed as an endpoint finally when deployed. Also it checks if the algorithm scripts do not have any imports of ui.R and server.R """ if self.shiny_app_dir and self.algos_folder: for file in os.listdir(self.algos_folder): if file.endswith(".R"): file_name = file.split('.')[0] my_regex = re.compile(file_name + r'[ ]+(<-)[ ]+(function)') ui_regex = re.compile(r'ui.R') server_regex = re.compile(r'server.R') with open(self.algos_folder + '/' + file, 'r') as myfile: r_code = myfile.read() result = re.search(my_regex, r_code) ui_result = re.search(ui_regex, r_code) server_result = re.search(server_regex, r_code) if (result is not None): logger.info( "%s will be exposed finally as an Algorithm\n", file_name) else: logger.info( "%s will not be exposed as an Algorithm \n", file_name) if (ui_result or server_result): logger.error( "%s has server.R or ui.R imported. Please remove it and resolve the dependency \n", file_name)
def __init__(self): self.shiny_app_name, self.state_path = hlp.search_state_file( os.getcwd()) if (not self.state_path): self.shiny_app_dir, self.algos_folder = "", "" logger.error(cnst.NOT_IN_SHINY_APP_OR_NO_STATE_FILE) elif (self.shiny_app_name and self.state_path): self.shiny_app_dir = self.state_path.split( self.shiny_app_name)[0] + self.shiny_app_name self.algos_folder = self.shiny_app_dir + cnst.ALGORITHMS_FOLDER
def check_directory_structure(self): """ This function checks if the "CORRECT" folder structure is follwed in the shiny app. """ if (self.shiny_app_dir and self.algos_folder): correct_shiny_directory = [ 'algorithms', 'ui.R', 'server.R', 'Readme.md', 'tests', '.shiny' ] actual_directory = os.listdir(self.shiny_app_dir) state_file_glob = self.shiny_app_dir + cnst.SHINY_STATE_FILE for filepath in glob.iglob(state_file_glob): if not filepath: logger.error(cnst.NO_SHINY_STATE_FILE) if set(actual_directory) != set(correct_shiny_directory): logger.warn(cnst.INCORRECT_FOLDER_STRUCTURE)
def make_algo_file(self, shiny_path, algo_name): """ Creates an algorithm file in the algorithms folder with the name of the file being the same as that of the algorithm provided by the user and also creates a function in the same file of the same name. Arguments: shiny_path {str} -- The path to your shiny app. algo_name {str} -- The name of the algorithm you want to add. """ if not os.path.isfile(shiny_path + '/algorithms/' + algo_name + '.R'): Path(shiny_path + '/algorithms/' + algo_name + '.R').touch() else: logger.error(cnst.SAME_ALGORITHM_EXISTS) return algo_path = os.path.realpath(shiny_path + '/algorithms/' + algo_name + '.R') documentation_template = """ ######################################################################### ##### ##### ##### PLEASE WHEN IMPORTING ANYTHING, USE RELATIVE PATHS INSTEAD ##### ##### OF ABSOLUTE PATHS. ##### ##### ##### ######################################################################### \n """ function_template = """ # Explain what the function does ... #' @param1 .... #' @param2 ... #' #' @return ... %s <- function(param1 , param2) { \n }""" % algo_name with open(algo_path, "r+") as file_pointer: file_pointer.write(documentation_template) file_pointer.write(function_template)
def create_algo_template(self): """ The entry point function to create an algorithm template after searching for the shiny state file and getiing the app_name and making the algorithm file in the required folder. """ if (not self.algo_name): logger.error(cnst.GIVE_ALGORITHM_NAME) else: shiny_app_name, state_file_path = hlp.search_state_file( os.getcwd()) if (shiny_app_name and state_file_path): shiny_app_dir = state_file_path.split( shiny_app_name)[0] + shiny_app_name if not os.path.exists(shiny_app_dir + "/algorithms"): logger.error(cnst.NO_ALGORITHMS_FOLDER) else: self.make_algo_file(shiny_app_dir, self.algo_name) else: logger.error(cnst.NOT_IN_SHINY_APP)