11# -*- coding: utf-8 -*-
22# -*- coding: cp1252 -*-
3-
3+ from filelock import FileLock
44import configparser , os
55from . import FileUtilities as FL
6+ from pathlib import Path
7+ from datetime import date
8+ from configobj import ConfigObj
9+ import traceback
610
711"""constants"""
812file_name = "settings.conf"
9-
13+ settings_file_lock = FileLock (os .getcwd ().split ("Framework" )[0 ] + os .sep + "Framework" + os .sep + file_name + ".lock" )
14+ settings_conf_path = os .getcwd ().split ("Framework" )[0 ] + os .sep + "Framework" + os .sep + file_name
1015remote_config = {
1116 "threading" : False ,
1217 "local_run" : False ,
1520 "upload_log_file_only_for_fail" : True ,
1621}
1722
18-
23+ @settings_file_lock
24+ def create_settings_config_file ():
25+ if Path (settings_conf_path ).exists ():
26+ return
27+
28+ today = date .today ().strftime ("%Y-%m-%d" )
29+
30+ config = ConfigObj ()
31+ config ["Authentication" ] = {"username" : "" , "api-key" : "" , "server_address" : "" }
32+ config ["Advanced Options" ] = {
33+ "log_delete_interval" : 7 ,
34+ "last_module_update_date" : today ,
35+ "last_log_delete_date" : today ,
36+ "element_wait" : 10 ,
37+ "available_to_all_project" : False ,
38+ "_file" : "temp_config.ini" ,
39+ "_file_upload_path" : "TestExecutionLog" ,
40+ "stop_live_log" : False ,
41+ }
42+ config ["Inspector" ] = {
43+ "Window" : "" ,
44+ "No_of_level_to_skip" : 0 ,
45+ "ai_plugin" : True ,
46+ }
47+ config ["server" ] = {"port" : 0 }
48+ config .filename = str (settings_conf_path )
49+ config .write ()
50+ print (f"Created settings.conf at { settings_conf_path } " )
51+
52+ def remove_settings_lock_file ():
53+ """Remove stale lock file if the process that created it is no longer running."""
54+ try :
55+ lock_file_path = Path (settings_file_lock .lock_file )
56+ if not lock_file_path .exists ():
57+ return
58+ lock_file_path .unlink (missing_ok = True )
59+ except Exception :
60+ traceback .print_exc ()
61+
62+ @settings_file_lock
1963def get_config_value (section , key , location : os .PathLike | None = None ):
2064 """
2165 :param section: name of section
@@ -30,7 +74,7 @@ def get_config_value(section, key, location: os.PathLike | None = None):
3074 config = configparser .ConfigParser ()
3175 config .optionxform = str # Retain text case (default is to change to lowercase without this line)
3276 if not location :
33- _file_name = os .getcwd () + os .sep + file_name
77+ _file_name = os .getcwd (). split ( "Framework" )[ 0 ] + os . sep + "Framework" + os .sep + file_name
3478 else :
3579 _file_name = location
3680 try :
@@ -46,7 +90,7 @@ def get_config_value(section, key, location: os.PathLike | None = None):
4690 # print "No option in that name: %s"%key
4791 return ""
4892
49-
93+ @ settings_file_lock
5094def remove_config_value (section , value , location = False ):
5195 try :
5296 config = configparser .ConfigParser ()
@@ -69,7 +113,7 @@ def remove_config_value(section, value, location=False):
69113 # print "No section in that name: %s"%section
70114 return ""
71115
72-
116+ @ settings_file_lock
73117def add_config_value (section , key , value , location : os .PathLike | None = None ):
74118 try :
75119 config = configparser .ConfigParser ()
@@ -98,44 +142,16 @@ def add_config_value(section, key, value, location: os.PathLike | None = None):
98142 else :
99143 config .set (section , key , value )
100144
101- with ( open (_file_name , "w" ) ) as open_file :
145+ with open (_file_name , "w" ) as open_file :
102146 config .write (open_file ) # Write all configuration to file
103147 open_file .close ()
104148 return True
105149 except configparser .NoSectionError :
106- # print "No section in that name: %s"%section
107150 return ""
108151 except configparser .NoOptionError :
109- # print "No option in that name: %s"%key
110152 return ""
111153
112154
113- def get_all_option (section_name , location : os .PathLike | None = None ):
114- """
115- :param section_name: given section name
116- :return: list of all option on that section
117- """
118- try :
119- config = configparser .ConfigParser ()
120- config .optionxform = str # Retain text case (default is to change to lowercase without this line)
121- if not location :
122- _file_name = os .getcwd () + os .sep + file_name
123- else :
124- _file_name = location
125- try :
126- config .read (_file_name ) # Read current configuration, if the file exists
127- except :
128- FL .DeleteFile (location )
129- config .read (_file_name )
130- return config .options (section_name )
131- except configparser .NoSectionError as e :
132- # print("Found no section with name %s" % section_name)
133- return []
134- except configparser .NoOptionError as e :
135- # print("Found no options on the section %s" % section_name)
136- return []
137-
138-
139155def add_section (section_name , location : os .PathLike | None = None ):
140156 """
141157 :param section_name: name of the section to add
@@ -184,46 +200,3 @@ def clean_config_file(location: os.PathLike | None = None):
184200 except Exception as e :
185201 print (e )
186202 return False
187-
188-
189- def get_all_sections (location : os .PathLike | None = None ):
190- try :
191- config = configparser .ConfigParser ()
192- config .optionxform = str # Retain text case (default is to change to lowercase without this line)
193- if not location :
194- _file_name = os .getcwd () + os .sep + file_name
195- else :
196- _file_name = location
197- try :
198- config .read (_file_name ) # Read current configuration, if the file exists
199- except :
200- FL .DeleteFile (location )
201- config .read (_file_name )
202- return config .sections ()
203- except configparser .NoSectionError as e :
204- print ("found no sections" )
205- return []
206- except configparser .NoOptionError as e :
207- print ("found no options" )
208- return []
209-
210- def has_section (section_name , location : os .PathLike | None = None ):
211- try :
212- config = configparser .ConfigParser ()
213- config .optionxform = str # Retain text case (default is to change to lowercase without this line)
214- if not location :
215- _file_name = os .getcwd () + os .sep + file_name
216- else :
217- _file_name = location
218- try :
219- config .read (_file_name ) # Read current configuration, if the file exists
220- except :
221- FL .DeleteFile (location )
222- config .read (_file_name )
223- return config .has_section (section_name )
224- except configparser .NoSectionError as e :
225- print ("found no sections" )
226- return []
227- except configparser .NoOptionError as e :
228- print ("found no options" )
229- return []
0 commit comments