Add more methods for saving and reading file data
This commit is contained in:
parent
733607a037
commit
e2502f8f7d
|
@ -434,6 +434,10 @@ self.save_file_as(file_url, new_file_name, destination_folder=None)
|
|||
|
||||
self.save_data_as(data, file_name, destination_folder=None)
|
||||
|
||||
self.append_data_to_file(data, file_name, destination_folder=None)
|
||||
|
||||
self.get_file_data(file_name, folder=None)
|
||||
|
||||
self.get_downloads_folder()
|
||||
|
||||
self.get_browser_downloads_folder()
|
||||
|
|
|
@ -6453,13 +6453,32 @@ class BaseCase(unittest.TestCase):
|
|||
)
|
||||
|
||||
def save_data_as(self, data, file_name, destination_folder=None):
|
||||
"""Saves the data specified to a file of the name specified.
|
||||
"""Saves the data specified to the file specified.
|
||||
If no destination folder is specified, the default one is used.
|
||||
(The default [Downloads Folder] = "./downloaded_files")"""
|
||||
(The default folder = "./downloaded_files")
|
||||
Use "." as the destination folder for the current directory."""
|
||||
if not destination_folder:
|
||||
destination_folder = constants.Files.DOWNLOADS_FOLDER
|
||||
page_utils._save_data_as(data, destination_folder, file_name)
|
||||
|
||||
def append_data_to_file(self, data, file_name, destination_folder=None):
|
||||
"""Appends the data specified to the file specified.
|
||||
If no destination folder is specified, the default one is used.
|
||||
(The default folder = "./downloaded_files")
|
||||
Use "." as the folder for the current directory."""
|
||||
if not destination_folder:
|
||||
destination_folder = constants.Files.DOWNLOADS_FOLDER
|
||||
page_utils._append_data_to_file(data, destination_folder, file_name)
|
||||
|
||||
def get_file_data(self, file_name, folder=None):
|
||||
"""Gets the data from the file specified.
|
||||
If no folder is specified, the default one is used.
|
||||
(The default folder = "./downloaded_files")
|
||||
Use "." as the folder for the current directory."""
|
||||
if not folder:
|
||||
folder = constants.Files.DOWNLOADS_FOLDER
|
||||
return page_utils._get_file_data(folder, file_name)
|
||||
|
||||
def get_downloads_folder(self):
|
||||
"""Returns the path of the SeleniumBase "downloaded_files/" folder.
|
||||
Calling self.download_file(file_url) will put that file in here.
|
||||
|
|
|
@ -253,11 +253,45 @@ def _download_file_to(file_url, destination_folder, new_file_name=None):
|
|||
|
||||
|
||||
def _save_data_as(data, destination_folder, file_name):
|
||||
out_file = codecs.open(
|
||||
destination_folder + "/" + file_name, "w+", encoding="utf-8"
|
||||
download_file_lock = fasteners.InterProcessLock(
|
||||
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
|
||||
)
|
||||
out_file.writelines(data)
|
||||
out_file.close()
|
||||
with download_file_lock:
|
||||
out_file = codecs.open(
|
||||
os.path.join(destination_folder, file_name), "w+", encoding="utf-8"
|
||||
)
|
||||
out_file.writelines(data)
|
||||
out_file.close()
|
||||
|
||||
|
||||
def _append_data_to_file(data, destination_folder, file_name):
|
||||
download_file_lock = fasteners.InterProcessLock(
|
||||
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
|
||||
)
|
||||
with download_file_lock:
|
||||
existing_data = ""
|
||||
if os.path.exists(os.path.join(destination_folder, file_name)):
|
||||
with open(os.path.join(destination_folder, file_name), "r") as f:
|
||||
existing_data = f.read()
|
||||
if not existing_data.split("\n")[-1] == "":
|
||||
existing_data += "\n"
|
||||
out_file = codecs.open(
|
||||
os.path.join(destination_folder, file_name), "w+", encoding="utf-8"
|
||||
)
|
||||
out_file.writelines("%s%s" % (existing_data, data))
|
||||
out_file.close()
|
||||
|
||||
|
||||
def _get_file_data(folder, file_name):
|
||||
download_file_lock = fasteners.InterProcessLock(
|
||||
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
|
||||
)
|
||||
with download_file_lock:
|
||||
if not os.path.exists(os.path.join(folder, file_name)):
|
||||
raise Exception("File not found!")
|
||||
with open(os.path.join(folder, file_name), "r") as f:
|
||||
data = f.read()
|
||||
return data
|
||||
|
||||
|
||||
def make_css_match_first_element_only(selector):
|
||||
|
|
Loading…
Reference in New Issue