Skip to content

Commit

Permalink
sanitize file path better
Browse files Browse the repository at this point in the history
* replaces unsupport characters with underscores _
* github artifact upload does not support the special chars:
	- Double quote "
	- Colon :
	- Less than <
	- Greater than >
	- Vertical bar |
	- Asterisk *
	- Question mark ?
	- Carriage return \r
	- Line feed \n
* note that \r, \n are included in whitespace regex symbol \s
  • Loading branch information
hyperrealist committed Feb 23, 2024
1 parent ccca4d3 commit 7d0e104
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/srx_caproto_iocs/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import re
import textwrap
import threading
import time as ttime
Expand Down Expand Up @@ -116,12 +117,15 @@ async def stage(self, instance, value):

if value == StageStates.STAGED.value:
# Steps:
# 1. Render 'write_dir' with datetime lib and replace any blank spaces with underscores.
# 2. Render 'file_name' with .format().
# 3. Replace blank spaces with underscores.
# 1. Render 'write_dir' with datetime lib
# 2. Replace unsupported characters with underscores (sanitize).
# 3. Check if sanitized 'write_dir' exists
# 4. Render 'file_name' with .format().
# 5. Replace unsupported characters with underscores.

sanitizer = re.compile(pattern=r"[\":<>|\*\?\s]")
date = now(as_object=True)
write_dir = Path(date.strftime(self.write_dir.value).replace(" ", "_"))
write_dir = Path(sanitizer.sub("_", date.strftime(self.write_dir.value)))
if not write_dir.exists():
msg = f"Path '{write_dir}' does not exist."
print(msg)
Expand All @@ -136,8 +140,7 @@ async def stage(self, instance, value):
full_file_path = write_dir / file_name.format(
num=self.frame_num.value, uid=uid, suid=uid[:8]
)
full_file_path = str(full_file_path)
full_file_path.replace(" ", "_")
full_file_path = sanitizer.sub("_", str(full_file_path))

print(f"{now()}: {full_file_path = }")

Expand Down

0 comments on commit 7d0e104

Please sign in to comment.