mirror of
https://github.com/1j01/textual-paint.git
synced 2024-12-27 00:33:28 +03:00
"a+"?
This commit is contained in:
parent
86753aee5e
commit
f397de47c6
@ -58,36 +58,49 @@ def get_recent_files():
|
|||||||
|
|
||||||
|
|
||||||
def load_recent_files():
|
def load_recent_files():
|
||||||
|
# Create an empty recent files list
|
||||||
|
recent_files = []
|
||||||
|
|
||||||
# Lock the recent file for reading
|
# Lock the recent file for reading
|
||||||
with open(RECENT_FILE_PATH, "r") as file:
|
with open(RECENT_FILE_PATH, "a+") as file:
|
||||||
fcntl.lockf(file, fcntl.LOCK_SH)
|
|
||||||
try:
|
try:
|
||||||
# Parse the XML document
|
# Try to read the file content
|
||||||
root = ET.parse(file).getroot()
|
file.seek(0)
|
||||||
return root.findall("RecentItem")
|
fcntl.lockf(file, fcntl.LOCK_SH)
|
||||||
except ET.ParseError:
|
content = file.read()
|
||||||
# Return an empty list if the file is empty or invalid
|
|
||||||
return []
|
if content:
|
||||||
|
# Parse the XML document
|
||||||
|
root = ET.fromstring(content)
|
||||||
|
recent_files = root.findall("RecentItem")
|
||||||
|
except (ET.ParseError, FileNotFoundError):
|
||||||
|
# Handle file not found or invalid XML format
|
||||||
|
pass
|
||||||
finally:
|
finally:
|
||||||
# Unlock the recent file after reading
|
# Unlock the recent file after reading
|
||||||
fcntl.lockf(file, fcntl.LOCK_UN)
|
fcntl.lockf(file, fcntl.LOCK_UN)
|
||||||
|
|
||||||
|
return recent_files
|
||||||
|
|
||||||
|
|
||||||
def save_recent_files(recent_files):
|
def save_recent_files(recent_files):
|
||||||
|
# Create the root element
|
||||||
|
root = ET.Element("RecentFiles")
|
||||||
|
|
||||||
|
# Add each recent item to the root element
|
||||||
|
for item in recent_files:
|
||||||
|
root.append(item)
|
||||||
|
|
||||||
|
# Serialize the XML document
|
||||||
|
xml_data = ET.tostring(root, encoding="utf-8")
|
||||||
|
|
||||||
# Lock the recent file for writing
|
# Lock the recent file for writing
|
||||||
with open(RECENT_FILE_PATH, "w") as file:
|
with open(RECENT_FILE_PATH, "w") as file:
|
||||||
fcntl.lockf(file, fcntl.LOCK_EX)
|
fcntl.lockf(file, fcntl.LOCK_EX)
|
||||||
try:
|
try:
|
||||||
# Create the root element
|
|
||||||
root = ET.Element("RecentFiles")
|
|
||||||
|
|
||||||
# Add each recent item to the root element
|
|
||||||
for item in recent_files:
|
|
||||||
root.append(item)
|
|
||||||
|
|
||||||
# Write the XML document to the file
|
# Write the XML document to the file
|
||||||
file.write("<?xml version=\"1.0\"?>\n")
|
file.write("<?xml version=\"1.0\"?>\n")
|
||||||
file.write(ET.tostring(root, encoding="utf-8").decode())
|
file.write(xml_data.decode())
|
||||||
finally:
|
finally:
|
||||||
# Unlock the recent file after writing
|
# Unlock the recent file after writing
|
||||||
fcntl.lockf(file, fcntl.LOCK_UN)
|
fcntl.lockf(file, fcntl.LOCK_UN)
|
||||||
|
Loading…
Reference in New Issue
Block a user