Skip to content

Commit ba033be

Browse files
committed
realpath: Reorganize code to reduce branches (for pylint checks)
1 parent 1935f32 commit ba033be

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/realpath.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77
# Note: os.path is used instead of pathlib because certain functionality such as
88
# os.path.normpath() lack a pathlib equivalent.
99

10+
def resolve_filename(opts, name: str) -> str:
11+
if opts.symlink_mode:
12+
if opts.symlink_mode == "L":
13+
# resolve instances of ".." first
14+
name = os.path.normpath(name)
15+
16+
name = os.path.realpath(name, strict=opts.can_mode == "e")
17+
18+
if not opts.can_mode:
19+
# raise an error if directory missing
20+
os.path.realpath(os.path.dirname(name), strict=True)
21+
else:
22+
if opts.can_mode == "e":
23+
# raise an error if missing
24+
os.path.realpath(name, strict=True)
25+
26+
name = os.path.abspath(name)
27+
28+
return name
1029

1130
def realpath(opts, filenames: list[str]):
1231
endchar = "\0" if opts.zero else "\n"
@@ -18,22 +37,7 @@ def realpath(opts, filenames: list[str]):
1837

1938
for name in filenames:
2039
try:
21-
if not opts.symlink_mode:
22-
if opts.can_mode == "e":
23-
# raise an error if missing
24-
os.path.realpath(name, strict=True)
25-
26-
name = os.path.abspath(name)
27-
else:
28-
if opts.symlink_mode == "L":
29-
# resolve instances of ".." first
30-
name = os.path.normpath(name)
31-
32-
name = os.path.realpath(name, strict=opts.can_mode == "e")
33-
34-
if not opts.can_mode:
35-
# raise an error if directory missing
36-
os.path.realpath(os.path.dirname(name), strict=True)
40+
name = resolve_filename(opts, name)
3741
except OSError as e:
3842
failed = True
3943

0 commit comments

Comments
 (0)