Skip to main content
tajinders0001
June 26, 2024
Solved

List of environment constants

  • June 26, 2024
  • 7 replies
  • 0 views

Hello Techies,

I am looking for ways to extract a list of environment constants in my application. I know I can export entire list of constants and look up the properties file but is there a better way within designer to achieve it?

Thank you!

Best answer by tajinders0001

Unfortunately there isn't any (especially within Appian)

I, therefore have developed a python executable to identify constants and move those as appropriate. If you have a basic Python experience, you will be able to use it.

Python

-----------------------------------------------------------

from fileinput import close
from hashlib import new
from lib2to3.pgen2 import grammar
from posixpath import join
import shutil
import zipfile
from tkinter import TRUE
from zipfile import ZipFile
import os, re


#get folders in director
def get_folders_in_directory(directory_path):
    all_items=os.listdir(directory_path)

    #filter dirs
    folders = [item for item in all_items if os.path.isdir(os.path.join(directory_path,item))]
    return folders
#----------------------------------------------------------------

#create zip file
def zip_files_and_folders(source_dir, output_zip):
    with ZipFile(output_zip,'w') as zipf:
        for root, dirs, files in os.walk(source_dir):
            for file in files:
                file_path=os.path.join(root,file)
                arcname=os.path.relpath(file_path,source_dir)
                zipf.write(file_path,arcname)

            for dir in dirs:
                dir_path=os.path.join(root,dir)
                arcname=os.path.relpath(dir_path,source_dir) + os.sep
                zipf.write(dir_path,arcname)

#----------------------------------------------------------------



#verify if this is a xml is of constant object
def is_constant_type_object(fileContent):
    constant_start_keyword = ''
    constant_end_keyword = ''
    if fileContent.find(constant_start_keyword) != -1 and fileContent.find(constant_end_keyword) != -1:
        return TRUE

#----------------------------------------------------------------

#change version for constant
def change_version_for_constant(filename, fileContent):
    start_string = ''
    end_string = ''

    #current version details
    version_string = extract_between_keywords (fileContent, start_string, end_string)

    last_letter = str(int(version_string[-1])+1)
    new_version = version_string[:-1]+last_letter
    print(f'old version:{version_string} and new version is {new_version}')

    #create a pattern to match text between start and end string
    pattern = f'{re.escape(start_string)}(.*?){re.escape(end_string)}'
    new_content=re.sub(pattern,f'{start_string}{new_version}{end_string}',fileContent, flags=re.DOTALL)


    #write back to the file
    with open(filename, 'w') as file:
        file.write(new_content)
    
#--------------------------------------------------------------------

#change folder for constant and version upgrade
def change_folder_for_constant (filename, fileContent, newFolderUuid ):
    start_string = ''
    end_string = ''
    
    #create a pattern to match text between start and end string
    pattern = f'{re.escape(start_string)}(.*?){re.escape(end_string)}'

    if extract_between_keywords (fileContent, start_string, end_string) == newFolderUuid:
        print(f'Constant is in the right folder')
    else:
        print(f'Moving constant to the right folder')
        #replace the matched text with the newFolderUuid
        new_content=re.sub(pattern,f'{start_string}{newFolderUuid}{end_string}',fileContent, flags=re.DOTALL)
        #write back to the file
        with open(filename, 'w') as file:
            file.write(new_content)
        file.close
        change_version_for_constant(filename, new_content)
#----------------------------------------------------------------

    

def extract_between_keywords (text, start_keyword, end_keyword):
    start_index = text.find(start_keyword)
    if start_index == -1:
        return "Name not found" #start keyword not found

    start_index +=len(start_keyword) #move to the end of start keyword
    end_index = text.find(end_keyword, start_index)

    if end_index == -1:
        return "Name not found" #end keyword not found
    return text[start_index:end_index].strip()    
#----------------------------------------------------------------
def contains_substring(main_string, substring):
    return substring in main_string
#----------------------------------------------------------------
def unzip_and_read_and_change (zip_file_path, envtFolderUuid, nonEnvtFolderUuid):
    #open the zip file
    with zipfile.ZipFile(zip_file_path,'r') as zip_ref:
        #extract all contents
        temp_dir = "temp_extracted"
        zip_ref.extractall(temp_dir)
        print (f"contents of {zip_file_path}:")
        
        #walk through the directory
        for root, dirs, files in os.walk(temp_dir):
            #get relative path
            rel_path = os.path.relpath(root, temp_dir)
            
            #print current dir
            if rel_path == 'content':
                print (f"\nFolder: {rel_path}")
                
                #print files in the current directory
                for file in files:
                    file_path =os.path.join(root,file)
                    #print(f"File: {file}")
                
                    #check if it is a constant xml file, otherwise delete it
                    if file.endswith('.xml'):
                        with open(file_path,'r', encoding='utf-8') as f:
                            file_content = f.read().replace('\n',' ')
                            if is_constant_type_object(file_content) == True:
                                if contains_substring(file_content,"true"):
                                    file_name = extract_between_keywords(file_content,"","")
                                    print(f" Environment constant: {file_name}")
                                    change_folder_for_constant(file_path, file_content, envtFolderUuid)
                                    
                                else:
                                    file_name = extract_between_keywords(file_content,"","")
                                    print(f" Non-environment constant: {file_name}")
                                    change_folder_for_constant(file_path, file_content, nonEnvtFolderUuid)
                                    
                            else: 
                                f.close()
                                os.remove(file_path)
                    else: 
                        f.close()
                        os.remove(file_path)

            
            
            #remove all other dirs
            if rel_path != 'META-INF' and rel_path!='content':
                print (f"\nFolder: {rel_path}")
                
                #print files in the current directory
                for file in files:
                    file_path =os.path.join(root,file)
                    #print(f"File: {file}")
                
                #read and print content
                    if file.endswith(('.txt','.csv','.py','.xml','.jpg','.png','.docx','.pdf','.gif','.csv','.py','.xml','html','xsd')):
                        try:
                            with open(file_path,'r', encoding='utf-8') as f:                            
                                f.close()                            
                                os.remove(file_path)
                        except Exception as e:
                            continue

    #zip it
    zip_files_and_folders(temp_dir,zip_file_path + "_outputzip.zip")




    #clean up: remove dir
    for root, dirs, files in os.walk(temp_dir, topdown=False):
        for file in files:
            os.remove(os.path.join(root,file))
        for dir in dirs:
            os.rmdir(os.path.join(root,dir))
    os.rmdir(temp_dir)

#----------------------------------------------------------------
def tj_graffiti():
    graffiti="""
    Created by TJ - Persistent Systems    
    """
    print(graffiti)
    
#---------------------------------------------------------------- 


#use it
tj_graffiti()
zip_file_path=input("please enter complete path to the application package file (zip)")

envt_folder_uuid=input("please enter UUID for environment constants")
app_folder_uuid=input("please enter UUID for application constants")
unzip_and_read_and_change(zip_file_path,envt_folder_uuid,app_folder_uuid)

7 replies

shubhama926776
June 26, 2024
I know I can export entire list of constants and look up the properties file

This is the optimal and most straightforward solution

tajinders0001
June 27, 2024

never mind, I created a python utility to scan through contents and return list of environment constants. Thank you for your response.