library-txt-casing-fixup 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of library-txt-casing-fixup might be problematic. Click here for more details.

File without changes
File without changes
@@ -0,0 +1,144 @@
1
+ import argparse
2
+ import pathlib
3
+ import sys
4
+
5
+ from library_txt_casing_fixup.files import (
6
+ backup_library,
7
+ cd_if_necessary,
8
+ collect_lowercased_files,
9
+ write_library,
10
+ )
11
+ from library_txt_casing_fixup.regex import (
12
+ EXPORT_LINE_REGEX,
13
+ fix_missing_asset,
14
+ fix_path_separators,
15
+ )
16
+ from library_txt_casing_fixup.verifications import (
17
+ ensure_library_exists,
18
+ ensure_linux,
19
+ )
20
+
21
+
22
+ class CommandArgs:
23
+ """The arguments passed to the script"""
24
+
25
+ autofix: bool
26
+ library_file: str
27
+
28
+
29
+ def collect_tool_args() -> CommandArgs:
30
+ """
31
+ Get the script's arguments
32
+
33
+ :return: the arguments passed to the script
34
+ """
35
+
36
+ args_parser = argparse.ArgumentParser(exit_on_error=True)
37
+
38
+ args_parser.add_argument(
39
+ "-a",
40
+ "--autofix",
41
+ action="store_true",
42
+ help="whether to attempt automatically fixing the incorrect paths - "
43
+ + "this first backs up the library file with a `.bak` extension",
44
+ )
45
+ args_parser.add_argument(
46
+ "-l",
47
+ "--library-file",
48
+ default="library.txt",
49
+ help="the library file on which to operate - `%(default)s` by default",
50
+ )
51
+
52
+ return args_parser.parse_args(namespace=CommandArgs())
53
+
54
+
55
+ def run_cmd():
56
+ """Perform the script's actions"""
57
+
58
+ # bail on windows+macOS
59
+ ensure_linux()
60
+
61
+ # parse autofix and library file flags
62
+ args = collect_tool_args()
63
+
64
+ # set up our env to match the library.txt
65
+ lib_path = pathlib.Path(args.library_file).resolve()
66
+ ensure_library_exists(lib_path)
67
+ cd_if_necessary(lib_path)
68
+
69
+ # build a lowercased file tree within the current directory so we can
70
+ # suggest potential matches
71
+ lowercased_files = collect_lowercased_files()
72
+
73
+ with open(lib_path) as lib_file:
74
+ # helps avoid autofixing when no missing assets exist, or we couldn't
75
+ # autofix
76
+ autofix_error = False
77
+ missing_assets = False
78
+
79
+ # helps avoid outputting unnecessary trailing newline
80
+ first_missing_asset = True
81
+
82
+ # keep track of file contents so we can later replace the file if
83
+ # autofix is desired
84
+ autofixed_file_contents = []
85
+
86
+ for line in lib_file:
87
+ match = EXPORT_LINE_REGEX.search(line)
88
+ # if this isn't an EXPORT line, we can skip it
89
+ if not match:
90
+ autofixed_file_contents.append(line)
91
+ continue
92
+
93
+ # fix the windows-style relative paths
94
+ obj_filename = fix_path_separators(match)
95
+
96
+ # if we find that the asset exists, we can skip trying to fix this
97
+ # line
98
+ obj_path = pathlib.Path(obj_filename)
99
+ if obj_path.exists():
100
+ autofixed_file_contents.append(line)
101
+ continue
102
+
103
+ # if we made it here, something is missing
104
+ missing_assets = True
105
+
106
+ # separation between missing asset output
107
+ if first_missing_asset:
108
+ first_missing_asset = False
109
+ else:
110
+ print()
111
+
112
+ print(f"missing asset: {obj_path}")
113
+ try:
114
+ # scan through the files in the dir+subdirs to see if we find an
115
+ # (otherwise identical) incorrectly-cased filename
116
+ potential_missing_file = lowercased_files[str(obj_path).lower()]
117
+ print(f"potential match: {potential_missing_file}")
118
+
119
+ # append the line to the autofixed file, replacing it with the
120
+ # case-corrected filename
121
+ autofixed_file_contents.append(
122
+ fix_missing_asset(match, potential_missing_file)
123
+ )
124
+ except KeyError:
125
+ # we couldn't find a matching case-corrected filename
126
+ autofix_error = True
127
+ print("no potential match found")
128
+
129
+ if not args.autofix:
130
+ # we're done if autofix isn't desired
131
+ sys.exit()
132
+ elif autofix_error:
133
+ # autofix is desired, but we encountered an error
134
+ print()
135
+ print("unable to autofix - missing assets with no potential match found")
136
+ sys.exit(1)
137
+ elif not missing_assets:
138
+ # autofix is desired, but we didn't find any missing assets
139
+ print("skipping autofix - no missing assets found")
140
+ sys.exit()
141
+
142
+ # try to back up original library file
143
+ backup_library(lib_path)
144
+ write_library(lib_path, autofixed_file_contents)
@@ -0,0 +1,110 @@
1
+ import sys
2
+ from pathlib import Path
3
+ from tkinter import filedialog, messagebox
4
+
5
+ from library_txt_casing_fixup.files import (
6
+ cd_if_necessary,
7
+ collect_lowercased_files,
8
+ )
9
+ from library_txt_casing_fixup.regex import (
10
+ EXPORT_LINE_REGEX,
11
+ fix_missing_asset,
12
+ fix_path_separators,
13
+ )
14
+ from library_txt_casing_fixup.verifications import ensure_linux
15
+
16
+
17
+ def run_gui():
18
+ ensure_linux()
19
+
20
+ # ask user where library.txt is
21
+ lib_file = filedialog.askopenfile(title="Choose library.txt")
22
+ if not lib_file:
23
+ # user cancelled dialogue - exit
24
+ return
25
+
26
+ # move to library.txt's parent dir and index the files there
27
+ cd_if_necessary(Path(lib_file.name).resolve())
28
+ lowercased_files = collect_lowercased_files()
29
+
30
+ autofixed_lines = []
31
+ num_fixed_lines = 0
32
+
33
+ for line in lib_file:
34
+ match = EXPORT_LINE_REGEX.search(line)
35
+ # if this isn't an EXPORT line, we can skip it
36
+ if not match:
37
+ autofixed_lines.append(line)
38
+ continue
39
+
40
+ # fix the windows-style relative paths
41
+ asset_filename = fix_path_separators(match)
42
+
43
+ # if we find that the asset exists, we can skip trying to fix this line
44
+ asset_path = Path(asset_filename)
45
+ if asset_path.exists():
46
+ autofixed_lines.append(line)
47
+ continue
48
+
49
+ # if we made it here, something is missing
50
+
51
+ try:
52
+ # scan through the files in the dir+subdirs to see if we find an
53
+ # (otherwise identical) incorrectly-cased filename
54
+ potential_missing_file = lowercased_files[str(asset_path).lower()]
55
+ except KeyError:
56
+ # we couldn't find a matching case-corrected filename - display
57
+ # error and exit
58
+ messagebox.showerror(
59
+ "Error correcting library.txt",
60
+ "No Potential Match",
61
+ detail="No asset with an incorrectly-cased (yet otherwise "
62
+ + f"identical) filename found for asset `{asset_path}`.",
63
+ )
64
+ sys.exit(1)
65
+ else:
66
+ # append the line to the autofixed file, replacing it with the
67
+ # case-corrected filename
68
+ autofixed_lines.append(fix_missing_asset(match, potential_missing_file))
69
+ num_fixed_lines += 1
70
+
71
+ lib_file.close()
72
+
73
+ if num_fixed_lines == 0:
74
+ # no changes necessary - inform user and exit
75
+ messagebox.showinfo(
76
+ "library.txt already correct",
77
+ "No Missing Assets",
78
+ detail=f"All assets present in `{lib_file.name}` were found. The "
79
+ + "library file will not be modified.",
80
+ )
81
+ return
82
+
83
+ messagebox.showinfo(
84
+ "library.txt needs modification",
85
+ f"{num_fixed_lines} Lines Corrected.",
86
+ detail="The next step is to choose a file in which to save the "
87
+ + "corrected library.",
88
+ )
89
+
90
+ while True:
91
+ # ask user where to save modifications
92
+ output_file = filedialog.asksaveasfile(
93
+ title="Choose file to save modifications to"
94
+ )
95
+
96
+ if output_file:
97
+ # they picked a file path - we can keep going
98
+ break
99
+ elif not messagebox.askyesno(
100
+ "No output file selected",
101
+ "Retry output file selection?",
102
+ detail="Changes will not be saved if an output filename isn't "
103
+ + "selected.",
104
+ ):
105
+ # user cancelled the dialogue, and they elected not to reattempt
106
+ # file path selection. we can bail now.
107
+ return
108
+
109
+ output_file.writelines(autofixed_lines)
110
+ output_file.close()
@@ -0,0 +1,74 @@
1
+ import os
2
+ import pathlib
3
+ import sys
4
+
5
+
6
+ def cd_if_necessary(lib: pathlib.Path):
7
+ """
8
+ Change the process working directory to the library file's parent directory
9
+ if we aren't already there
10
+
11
+ This ensures that the relative paths in the library file resolve to the
12
+ case-corrected filenames properly.
13
+ """
14
+
15
+ if lib.parent != pathlib.Path.cwd():
16
+ os.chdir(lib.parent)
17
+
18
+
19
+ def backup_library(lib: pathlib.Path):
20
+ """
21
+ Back up the existing library
22
+
23
+ The backup will be created with the existing library's filename plus a
24
+ ``.bak`` extension.
25
+
26
+ :param lib: the path to the existing library
27
+ """
28
+
29
+ backup_filename = str(lib) + ".bak"
30
+
31
+ print()
32
+ print(f"renaming `{lib}` to `{backup_filename}` ", end="")
33
+
34
+ try:
35
+ os.rename(lib, backup_filename)
36
+ print("succeeded")
37
+ except IsADirectoryError:
38
+ print("failed")
39
+ sys.exit(1)
40
+
41
+
42
+ def collect_lowercased_files() -> dict[str, str]:
43
+ """
44
+ Get lowercased and actual members of the current directory and subdirectory
45
+
46
+ :return: a :class:`dict` with the lowercased filenames as keys and the
47
+ actual filenames as values.
48
+ """
49
+
50
+ lowercased_files: dict[str, str] = {}
51
+
52
+ cwd = pathlib.Path(".")
53
+ for current_dir, _dirs, files in cwd.walk():
54
+ if current_dir == cwd or not files:
55
+ continue
56
+
57
+ for file in files:
58
+ filename = str(current_dir / file)
59
+ lowercased_files[filename.lower()] = filename
60
+
61
+ return lowercased_files
62
+
63
+
64
+ def write_library(lib: pathlib.Path, lines: list[str]):
65
+ """
66
+ Write the corrected lines to the library file
67
+
68
+ :param lib: the path to the library file
69
+ :param lines: the corrected contents to write
70
+ """
71
+
72
+ with open(lib, "w") as lib_file:
73
+ lib_file.writelines(lines)
74
+ print(f"autofixed library saved as `{lib}`")
@@ -0,0 +1,31 @@
1
+ import re
2
+
3
+ EXPORT_LINE_REGEX = re.compile("^EXPORT (\\S*)\t+(\\S*)$")
4
+ """RegEx matching lines exporting assets"""
5
+
6
+ _asset_filename_group = 2
7
+
8
+
9
+ def fix_missing_asset(match: re.Match, fixed_path: str) -> str:
10
+ """
11
+ Get an ``EXPORT`` line, updated with the case-corrected asset filename
12
+
13
+ :param match: the RegEx match containing the ``EXPORT`` line
14
+ :param fixed_path: the asset's corrected filename
15
+ :return: the ``EXPORT`` line with the new case-corrected filename
16
+ """
17
+
18
+ line = match.string
19
+ asset_filename_start, asset_filename_end = match.span(_asset_filename_group)
20
+ return line[:asset_filename_start] + fixed_path + line[asset_filename_end:]
21
+
22
+
23
+ def fix_path_separators(match: re.Match) -> str:
24
+ """
25
+ Replace the Windows-style ``\\`` path separators with POSIX-style ``/``
26
+
27
+ :param match: the RegEx match containing the ``EXPORT`` line
28
+ :return: the filename with replaced path separators
29
+ """
30
+
31
+ return match.group(_asset_filename_group).replace("\\", "/")
@@ -0,0 +1,23 @@
1
+ import pathlib
2
+ import platform
3
+ import sys
4
+
5
+
6
+ def ensure_library_exists(lib: pathlib.Path):
7
+ """Output an error and exit the script if the library file can't be found"""
8
+
9
+ if not lib.exists():
10
+ print(f"Unable to find library file `{lib}`")
11
+ sys.exit(1)
12
+
13
+
14
+ def ensure_linux():
15
+ """Output an explanation and exit the script if the host OS isn't Linux"""
16
+
17
+ if platform.system() != "Linux":
18
+ print(
19
+ "This tool is intended for use on Linux; Windows and macOS (by "
20
+ + "default) use case-insensitive filesystems and users will see no "
21
+ + "benefit from correcting their library.txt.",
22
+ )
23
+ sys.exit(1)
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.4
2
+ Name: library-txt-casing-fixup
3
+ Version: 0.0.1
4
+ Summary: Fix incorrectly-cased filenames in X-Plane 12 scenery `library.txt` files.
5
+ Author: Nicola Clark
6
+ Author-email: Nicola Clark <nicola@slottedspoon.dev>
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: End Users/Desktop
11
+ Classifier: Operating System :: POSIX :: Linux
12
+ Classifier: Topic :: Utilities
13
+ Requires-Python: >=3.14
14
+ Project-URL: Repository, https://repos.slottedspoon.dev/nicola/library-txt-casing-fixup
15
+ Description-Content-Type: text/markdown
16
+
17
+ # library-txt-casing-fixup
18
+
19
+ Fix incorrectly-cased filenames in X-Plane 12 scenery `library.txt` files.
@@ -0,0 +1,12 @@
1
+ library_txt_casing_fixup/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ library_txt_casing_fixup/apps/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ library_txt_casing_fixup/apps/cmdline.py,sha256=eFWh_MwASspJzW0rUo2vrU743GM9zvcLAY1NCOZXLsQ,4426
4
+ library_txt_casing_fixup/apps/gui.py,sha256=vzKVfJ_7mHGLZ9Un7iiTP2VviRmxbCpUGlFuY6_77Ts,3616
5
+ library_txt_casing_fixup/files.py,sha256=0s-xXu_Zo8sPFxX7rIdUpPsdys9nsw-Cdzdy7JGjOBk,1855
6
+ library_txt_casing_fixup/regex.py,sha256=0ChLGGLF7I8K6CgxoUXm9BmlKXtypONGEW_oC_LniyU,1005
7
+ library_txt_casing_fixup/verifications.py,sha256=S7k4XJVq2CfI6KJVKfecRT8_xepgRuN-wVysM3Nj7yI,673
8
+ library_txt_casing_fixup-0.0.1.dist-info/licenses/LICENSE,sha256=lK9yfVuXE6FtbxUEAOhEr6lyoovCLP6VbXkF52C7HgA,1070
9
+ library_txt_casing_fixup-0.0.1.dist-info/WHEEL,sha256=G_-rrEibZr7OEM7zdH1dSO6s-VdjfnS5PA4CLAdI9Vw,80
10
+ library_txt_casing_fixup-0.0.1.dist-info/entry_points.txt,sha256=FbAZxsWeV0farAY1FPu42e89zCBUIHJfTN0wr0aIndA,180
11
+ library_txt_casing_fixup-0.0.1.dist-info/METADATA,sha256=4KD7GJCUeOE-asCp_hO8Vmt5BHZkm9Ez8e1IkIQSeu8,700
12
+ library_txt_casing_fixup-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: uv 0.13.9
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,6 @@
1
+ [console_scripts]
2
+ library-txt-casing-fixup = library_txt_casing_fixup.apps.cmdline:run_cmd
3
+
4
+ [gui_scripts]
5
+ library-txt-casing-fixup-gui = library_txt_casing_fixup.apps.gui:run_gui
6
+
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nicola Clark
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+