Skip to content

Commit af5b527

Browse files
initial commit
0 parents  commit af5b527

File tree

11 files changed

+8208
-0
lines changed

11 files changed

+8208
-0
lines changed

.gitignore

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
110+
.pdm.toml
111+
.pdm-python
112+
.pdm-build/
113+
114+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115+
__pypackages__/
116+
117+
# Celery stuff
118+
celerybeat-schedule
119+
celerybeat.pid
120+
121+
# SageMath parsed files
122+
*.sage.py
123+
124+
# Environments
125+
.env
126+
.venv
127+
env/
128+
venv/
129+
ENV/
130+
env.bak/
131+
venv.bak/
132+
133+
# Spyder project settings
134+
.spyderproject
135+
.spyproject
136+
137+
# Rope project settings
138+
.ropeproject
139+
140+
# mkdocs documentation
141+
/site
142+
143+
# mypy
144+
.mypy_cache/
145+
.dmypy.json
146+
dmypy.json
147+
148+
# Pyre type checker
149+
.pyre/
150+
151+
# pytype static type analyzer
152+
.pytype/
153+
154+
# Cython debug symbols
155+
cython_debug/
156+
157+
# PyCharm
158+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160+
# and can be added to the global gitignore or merged into this file. For a more nuclear
161+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162+
#.idea/

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# TPL-Parser License
2+
3+
## Personal Use License
4+
5+
This software is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License.
6+
You are free to use, modify, and distribute this software for personal, non-commercial projects,
7+
as long as you provide attribution to the original author, Dev Jones.
8+
9+
For more details, visit https://creativecommons.org/licenses/by-nc/4.0/
10+
11+
## Commercial Use License
12+
13+
If you wish to use this software in a commercial project, you must obtain a commercial license.
14+
Please contact Dev Jones at [[email protected]](mailto:[email protected]) to discuss licensing terms and fees.
15+
16+
By using this software in a commercial project without obtaining a commercial license,
17+
you are in violation of the terms and may be subject to legal action.
18+
19+
## Attribution
20+
21+
When using this software for personal projects, you must include the following attribution in any copies or substantial portions of the software:
22+
23+
```
24+
TPL-Parser © 2024 Dev Jones. Licensed under the Creative Commons Attribution-NonCommercial 4.0 International License. For commercial use, contact [[email protected]](mailto:[email protected]).
25+
```
26+
27+
## Disclaimer
28+
29+
This software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# TPLParserPy
2+
3+
<p align="center">
4+
<img src="./assets/logo.png" alt="Logo" height="128px">
5+
</p>
6+
7+
<p align="center">
8+
<img src="https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54" />
9+
<img src="https://img.shields.io/badge/adobe%20photoshop-%2331A8FF.svg?style=for-the-badge&logo=adobe%20photoshop&logoColor=white"/>
10+
<img src="https://img.shields.io/github/license/DavyJonesCodes/TPLParserPy?style=for-the-badge"/>
11+
</p>
12+
13+
# 🎨 TPLParserPy
14+
15+
Welcome to **TPLParserPy**! 🛠️ This Python package is designed to help you parse Photoshop TPL (Tool Preset) files and extract the data into a friendly JSON format. Perfect for anyone who wants to dive deep into TPL files and understand their inner workings! 💡
16+
17+
## ✨ Features
18+
19+
- 🔍 **Parse Photoshop TPL files** with ease.
20+
- 🗂️ **Extract tool names, types, and properties** into JSON format.
21+
- 💾 **Save the extracted data** for further use or analysis.
22+
23+
## 🚀 Installation
24+
25+
You can easily install TPLParserPy via pip:
26+
27+
```bash
28+
pip install tpl-parser
29+
```
30+
31+
## 🛠️ Usage
32+
33+
### Importing and Using the Library
34+
35+
Here's a quick example of how to use TPLParserPy in your Python project:
36+
37+
```python
38+
from TPLParser import TPLReader
39+
40+
file_path = "path/to/yourfile.tpl"
41+
reader = TPLReader(file_path)
42+
tpl_data = reader.read_tpl()
43+
reader.save_to_json("output.json")
44+
```
45+
46+
### Command-Line Interface
47+
48+
TPLParserPy also includes a handy command-line interface for quick parsing:
49+
50+
```bash
51+
tpl-parser path/to/yourfile.tpl -o output.json
52+
```
53+
54+
## 🤝 Contributions
55+
56+
Contributions are welcome! 🎉 If you'd like to contribute to TPLParserPy, feel free to fork the repository and submit a pull request. If you have any questions or need guidance, don't hesitate to contact me at [[email protected]](mailto:[email protected]).
57+
58+
## 📄 License
59+
60+
This project is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License. For more details, see the [LICENSE](./LICENSE) file. For commercial use, please contact [Dev Jones](mailto:[email protected]).
61+
62+
## 📬 Contact
63+
64+
If you have any questions, suggestions, or just want to say hi, feel free to reach out via email: [[email protected]](mailto:[email protected]). We'd love to hear from you! 😊

TPLParser/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .tpl_reader import TPLReader
2+
3+
__all__ = ['TPLReader']

TPLParser/cli.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import argparse
2+
from .tpl_reader import TPLReader
3+
4+
5+
def main():
6+
parser = argparse.ArgumentParser(description="Parse Photoshop TPL files and save the output as JSON.")
7+
8+
parser.add_argument(
9+
"input_file",
10+
help="Path to the TPL file to be parsed."
11+
)
12+
13+
parser.add_argument(
14+
"-o", "--output",
15+
default="output.json",
16+
help="Path to save the parsed JSON data. Defaults to 'output.json'."
17+
)
18+
19+
args = parser.parse_args()
20+
21+
tpl_reader = TPLReader(args.input_file)
22+
tpl_reader.read_tpl()
23+
tpl_reader.save_to_json(args.output)
24+
25+
print(f"Parsed TPL data has been saved to {args.output}")
26+
27+
28+
if __name__ == "__main__":
29+
main()

TPLParser/tpl_reader.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import json
2+
from .utils import (
3+
extract_text, extract_label, extract_property,
4+
validate_tpl_header, move_to_tool_data_section
5+
)
6+
7+
8+
class TPLReader:
9+
"""Class for reading and parsing Photoshop TPL files."""
10+
11+
def __init__(self, file_path):
12+
self.file_path = file_path
13+
self.tpl_data = {}
14+
15+
def read_tool(self, file):
16+
"""
17+
Reads tool data from the TPL file and extracts relevant properties.
18+
19+
Parameters:
20+
file (BinaryIO): The file object of the TPL file to be read.
21+
22+
Returns:
23+
dict: A dictionary containing the tool data extracted from the file.
24+
"""
25+
tpl = {}
26+
27+
while True:
28+
# Extract the tool name
29+
tool_name = extract_text(file)
30+
# Skip 10 bytes (typically padding or non-essential data)
31+
file.read(10)
32+
33+
# Extract the tool type
34+
tool_type = extract_label(file).decode('ascii', errors='ignore')
35+
36+
# Initialize the tool type in the dictionary if not already present
37+
if tool_type not in tpl:
38+
tpl[tool_type] = []
39+
40+
# Extract the number of properties and their values
41+
count = int(file.read(4).hex(), 16)
42+
properties = [extract_property(file) for _ in range(count)]
43+
44+
# Append the tool data to the dictionary
45+
tpl[tool_type].append({
46+
"name": tool_name.split("=")[-1],
47+
"properties": properties
48+
})
49+
50+
# Check if there are more tools to read
51+
if len(file.read(4)) != 4:
52+
break
53+
file.seek(-4, 1)
54+
55+
return tpl
56+
57+
def read_tpl(self):
58+
"""
59+
Reads and parses the TPL file.
60+
61+
Returns:
62+
dict: A dictionary containing the parsed TPL data.
63+
"""
64+
with open(self.file_path, 'rb') as file:
65+
# Validate the TPL file header and move the cursor to the tool data section
66+
if not validate_tpl_header(file) or not move_to_tool_data_section(file):
67+
return {}
68+
69+
# Extract the tool data
70+
self.tpl_data = self.read_tool(file)
71+
return self.tpl_data
72+
73+
def save_to_json(self, output_file):
74+
"""
75+
Saves the parsed TPL data to a JSON file.
76+
77+
Parameters:
78+
output_file (str): The path to the JSON file where the data will be saved.
79+
"""
80+
with open(output_file, "w+", encoding="utf-8") as f:
81+
json.dump(self.tpl_data, f, indent=2)

0 commit comments

Comments
 (0)