1- """
2- Author: Roberto Chiosa
3- Copyright: Roberto Chiosa, © 2024
4- Email: roberto.chiosa@pinvision.it
5-
6- Created: 12/01/24
7- Script Name: util_app.py
8- Path: utils
9-
10- Script Description:
11-
12-
13- Notes:
14- """
151import argparse
162import importlib
173import os
2713from .utils .util_qualify import BasicValidationInterface
2814from .utils .util_qualify import BuildingMotifValidationInterface
2915
30- # todo if the user want to setup a different folder?
3116# create app folder if not exists
32- APP_FOLDER = os .path .join (os .getcwd (), 'app' )
17+ MODULE_BASEPATH = os .path .dirname (__file__ )
18+ USER_BASEPATH = os .getcwd ()
19+ APP_FOLDER = os .path .join (USER_BASEPATH , 'app' )
20+
3321if not os .path .exists (APP_FOLDER ):
3422 os .makedirs (APP_FOLDER , exist_ok = True )
3523 logger .info (f'Created app folder in { APP_FOLDER } ' )
3624
37- MODULE_BASEPATH = os .path .dirname (__file__ )
38- USER_BASEPATH = os .getcwd ()
39-
4025
4126class Application :
4227 """
4328 Application class
4429 """
4530
46- def __init__ (self , metadata = None , app_name = None ):
31+ def __init__ (self , metadata = None , app_name = None , base_path = None ):
4732 # Class specific logger
4833 self .logger = logger
4934 # The graph_path and datasource are external to the configuration file.
@@ -53,7 +38,14 @@ def __init__(self, metadata=None, app_name=None):
5338 self .res_fetch = None
5439 self .res_preprocess = None
5540 self .res_analyze = None
56- self .path_to_app = os .path .join (USER_BASEPATH , APP_FOLDER , app_name )
41+
42+ # Resolve the app folder based on provided base_path or default
43+ if base_path :
44+ self .app_folder = os .path .join (base_path , 'app' )
45+ else :
46+ self .app_folder = APP_FOLDER
47+
48+ self .path_to_app = os .path .join (self .app_folder , app_name )
5749
5850 '''
5951 The config folder should be structured as follows
@@ -64,25 +56,29 @@ def __init__(self, metadata=None, app_name=None):
6456 '''
6557
6658 # Class variable to store available app names
67- available_app = os .listdir (os . path . join ( USER_BASEPATH , APP_FOLDER ) ) # Add your app names here
59+ available_app = os .listdir (self . app_folder ) # Add your app names here
6860 # get only directories that start with app
6961 available_app_names = [app for app in available_app if app .startswith ('app' )]
7062
63+ # Log if no apps are found in the resolved path
64+ if not available_app_names :
65+ self .logger .warning (f'No apps found in { self .app_folder } . Check if the base_path is correct.' )
66+
7167 if app_name not in available_app_names :
7268 raise ValueError (f"Invalid app name. Available app names: { available_app_names } " )
7369
74- if os .path .join (USER_BASEPATH , APP_FOLDER , app_name , 'config.yaml' ) is None :
70+ if not os .path .exists ( os . path . join (self . app_folder , app_name , 'config.yaml' )) :
7571 raise FileNotFoundError ('config.yaml not found' )
76- elif os .path .join (USER_BASEPATH , APP_FOLDER , app_name , 'manifest.yaml' ) is None :
77- raise FileNotFoundError ('manifest.yaml not found' )
78- elif os .path .join (USER_BASEPATH , APP_FOLDER , app_name , 'query.ttl' ) is None :
79- raise FileNotFoundError ('query.ttl not found' )
72+ elif not os .path .exists ( os . path . join (self . app_folder , app_name , 'manifest.ttl' )) :
73+ raise FileNotFoundError ('manifest.ttl not found' )
74+ elif not os .path .exists ( os . path . join (self . app_folder , app_name , 'query.rq' )) :
75+ raise FileNotFoundError ('query.rq not found' )
8076 else :
81- config_file = load_file (os .path .join (USER_BASEPATH , APP_FOLDER , app_name , 'config.yaml' ), yaml_type = True )
77+ config_file = load_file (os .path .join (self . app_folder , app_name , 'config.yaml' ), yaml_type = True )
8278 self .details = config_file ['details' ]
8379 self .parameters = config_file ['parameters' ]
84- self .manifest = os .path .join (USER_BASEPATH , APP_FOLDER , app_name , 'manifest.ttl' )
85- self .query = load_file (os .path .join (USER_BASEPATH , APP_FOLDER , app_name , 'query.rq' ))
80+ self .manifest = os .path .join (self . app_folder , app_name , 'manifest.ttl' )
81+ self .query = load_file (os .path .join (self . app_folder , app_name , 'query.rq' ))
8682
8783 def qualify (self ) -> bool :
8884 """
@@ -196,7 +192,6 @@ def analyze(self, *args, **kwargs):
196192 self .logger .error (f"Function { analyze_fn } not found in analyze module." )
197193 return None
198194
199-
200195def app_name_validation (answer , current ):
201196 """
202197 Validate the app name in the inquirer prompt
0 commit comments