Skip to content

Commit e4c8bb7

Browse files
committed
replaced f-strings in execute statements with placeholders
1 parent 9b8e061 commit e4c8bb7

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

stag

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class StagFS(Operations):
8282
# the file is valid if its path is a subset of its corresponding tags
8383
if is_file:
8484
if check_valid:
85-
res = self.cur.execute(f"SELECT tags FROM files WHERE path LIKE '{basename}'").fetchone()
85+
res = self.__execute("SELECT tags FROM files WHERE path LIKE", basename).fetchone()
8686
res_tags = [tag for tag in res[0].split("/") if tag]
8787

8888
tags = path.split("/")[1:-1]
@@ -115,6 +115,22 @@ class StagFS(Operations):
115115
else:
116116
return is_root, is_file, is_tag
117117

118+
def __execute(self, *args):
119+
query = []
120+
params = []
121+
122+
for i, arg in enumerate(args):
123+
if i % 2 == 0:
124+
query.append(arg)
125+
else:
126+
query.append("?")
127+
params.append(arg)
128+
129+
query = " ".join(query)
130+
params = tuple(params)
131+
132+
return self.cur.execute(query, params)
133+
118134
# ---------------------------------------------------------------------------------------------------------------
119135
# Initialization and destruction
120136
# ---------------------------------------------------------------------------------------------------------------
@@ -290,7 +306,7 @@ class StagFS(Operations):
290306
# if it's a file, check if the subfolder it's in is a valid combination of tags
291307
elif os.path.isfile(real_path):
292308
# fetch the tags associated with this file in the database
293-
res = self.cur.execute(f"SELECT tags FROM files WHERE path LIKE '{basename}'").fetchone()
309+
res = self.__execute("SELECT tags FROM files WHERE path LIKE", basename).fetchone()
294310
res_tags = [tag for tag in res[0].split("/") if tag]
295311

296312
# if the tags on the requested path is a subset of the tags on the database, then the path is valid
@@ -381,12 +397,12 @@ class StagFS(Operations):
381397
raise FuseOSError(errno.ENOENT)
382398

383399
# get current tags and add the ones from new_tags
384-
res = self.cur.execute(f"SELECT tags FROM files WHERE path LIKE '{target_name}'").fetchone()
400+
res = self.__execute("SELECT tags FROM files WHERE path LIKE", target_name).fetchone()
385401
tags = set([tag for tag in res[0].split("/") if tag] + new_tags)
386402
tags = "/" + "//".join(tags) + "/"
387403

388404
# write it to the database
389-
self.cur.execute(f"UPDATE files SET tags = '{tags}' WHERE path = '{target_name}'")
405+
self.__execute("UPDATE files SET tags =", tags, "WHERE path =", target_name)
390406
self.con.commit()
391407

392408
return
@@ -487,14 +503,14 @@ class StagFS(Operations):
487503

488504
# now we can merge the tags
489505
os.rmdir(old_path)
490-
self.cur.execute(
491-
f"""
506+
self.__execute(
507+
"""
492508
UPDATE files
493509
SET tags = CASE
494-
WHEN tags LIKE '%/{new_name}/%' THEN REPLACE(tags, '/{old_name}/', '')
495-
ELSE REPLACE(tags, '/{old_name}/', '/{new_name}/')
510+
WHEN tags LIKE '%/' ||""", new_name, """|| '/%' THEN REPLACE(tags, '/' ||""", old_name, """|| '/', '')
511+
ELSE REPLACE(tags, '/' ||""", old_name, """|| '/', '/' ||""", new_name, """|| '/')
496512
END
497-
WHERE tags LIKE '%/{old_name}/%';
513+
WHERE tags LIKE '%/' ||""", old_name, """|| '/%';
498514
"""
499515
)
500516
self.con.commit()
@@ -506,7 +522,7 @@ class StagFS(Operations):
506522
# if its neither a file nor a directory, rename the corresponding tag
507523
else:
508524
os.rename(old_path, new_path)
509-
self.cur.execute(f"UPDATE files SET tags = REPLACE(tags, '/{old_name}/', '/{new_name}/') WHERE tags LIKE '%/{old_name}/%'")
525+
self.__execute("UPDATE files SET tags = REPLACE(tags, '/' ||", old_name, "|| '/', '/' ||", new_name, "|| '/') WHERE tags LIKE '%/' ||", old_name, "|| '/%'")
510526
self.con.commit()
511527

512528
if is_file:
@@ -519,7 +535,7 @@ class StagFS(Operations):
519535

520536
# otherwise, rename file
521537
os.rename(old_path, new_path)
522-
self.cur.execute(f"UPDATE files SET path = '{new_name}' WHERE path = '{old_name}'")
538+
self.__execute("UPDATE files SET path =", new_name, " WHERE path =", old_name)
523539
self.con.commit()
524540

525541
# if the tags differ, update them in the database
@@ -529,7 +545,7 @@ class StagFS(Operations):
529545
else:
530546
new_tags = ""
531547

532-
self.cur.execute(f"UPDATE files SET tags = '{new_tags}' WHERE path = '{new_name}'")
548+
self.__execute("UPDATE files SET tags =", new_tags, "WHERE path =", new_name)
533549
self.con.commit()
534550

535551
return
@@ -610,11 +626,11 @@ class StagFS(Operations):
610626
# remove tags or file depending on where the removal was issued
611627
if tags:
612628
for tag in tags:
613-
self.cur.execute(f"UPDATE files SET tags = REPLACE(tags, '/{tag}/', '') WHERE path = '{basename}'")
629+
self.__execute("UPDATE files SET tags = REPLACE(tags, '/' ||", tag, "|| '/', '') WHERE path =", basename)
614630
self.con.commit()
615631

616632
else:
617-
self.cur.execute(f"DELETE FROM files WHERE path = '{basename}'")
633+
self.__execute("DELETE FROM files WHERE path =", basename)
618634
self.con.commit()
619635
os.unlink(real_path)
620636

@@ -723,10 +739,10 @@ class StagFS(Operations):
723739
# make a query where we fetch all files that have the tags present in the variable `tags`
724740
conditions = []
725741
for tag in tags:
726-
conditions.append(f"tags LIKE '%/{tag}/%'")
742+
conditions.append("tags LIKE '%/' || ? || '/%'")
727743
query = "SELECT path, tags FROM files WHERE " + " AND ".join(conditions)
728744

729-
res = self.cur.execute(query).fetchall()
745+
res = self.cur.execute(query, tags).fetchall()
730746

731747
# go over all results, append all the files and return unique tags
732748
for line in res:
@@ -777,10 +793,10 @@ class StagFS(Operations):
777793
if len(tags) > 1:
778794
conditions = []
779795
for tag in tags:
780-
conditions.append(f"tags LIKE '%/{tag}/%'")
796+
conditions.append("tags LIKE '%/' || ? || '/%'")
781797
query = "SELECT path, tags FROM files WHERE " + " AND ".join(conditions)
782798

783-
res = self.cur.execute(query).fetchone()
799+
res = self.cur.execute(query, tags).fetchone()
784800

785801
if not res:
786802
raise FuseOSError(errno.ENOENT)
@@ -790,7 +806,7 @@ class StagFS(Operations):
790806

791807
# remove tags from files
792808
for tag in tags:
793-
self.cur.execute(f"UPDATE files SET tags = REPLACE(tags, '/{tag}/', '')")
809+
self.__execute("UPDATE files SET tags = REPLACE(tags, '/' ||", tag, "|| '/', '')")
794810
self.con.commit()
795811

796812
return
@@ -830,7 +846,7 @@ class StagFS(Operations):
830846
raise FuseOSError(errno.EEXIST)
831847

832848
# add the file to the database
833-
self.cur.execute(f"INSERT INTO files VALUES ('{basename}', '{tags}')")
849+
self.__execute("INSERT INTO files VALUES (", basename, ", ", tags, ")")
834850
self.con.commit()
835851

836852
return os.open(real_path, os.O_WRONLY | os.O_CREAT, mode)

tests/test_stag.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,28 @@ def test_create(self):
248248

249249
pass
250250

251+
# create a new file on the repository but with special characters
252+
def test_create2(self):
253+
fs, tmpdir, mnt = self.__create_stag()
254+
p = self.__mount_stag(fs, mnt)
255+
256+
filename = """ç''?\??'\##$%\&ºª=@})([]}««>»>>««"""
257+
258+
open(f"{mnt}/city/{filename}.txt", "w").write("this is a very weird file name")
259+
ls = os.listdir(mnt)
260+
ls_city = os.listdir(f"{mnt}/city")
261+
262+
try:
263+
assert f"{filename}.txt" in ls
264+
assert f"{filename}.txt" in ls_city
265+
assert ["this is a very weird file name"] == open(f"{mnt}/{filename}.txt").readlines()
266+
267+
finally:
268+
self.__unmount_stag(p)
269+
self.__remove_stag(tmpdir)
270+
271+
pass
272+
251273
# add tags to files
252274
def test_ln(self):
253275
fs, tmpdir, mnt = self.__create_stag()

0 commit comments

Comments
 (0)