Skip to content

Commit 2ba69df

Browse files
committed
linting and formating python
1 parent f973cf2 commit 2ba69df

File tree

4 files changed

+290
-216
lines changed

4 files changed

+290
-216
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ To easily convert all SVGs in a folder, you can use a terminal and Inkscape like
6464

6565
## Contribute
6666

67+
```bash
68+
# install uv
69+
uv run ruff check --fix
70+
uv run ruff format
71+
```
72+
6773
Contributions to this project are welcome. Feel free to report bugs or post ideas.
6874

6975
To update the python code for the website, you have to update the python wheel in the website folder. To update it, just run: ```python setup.py bdist_wheel --universal --dist-dir=website```

download_factorio_raw_data.py

Lines changed: 106 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,60 @@
55
# ]
66
# ///
77

8-
import requests
9-
import re
10-
import luadata
118
import json
129
import os
10+
import re
11+
12+
import luadata
13+
import requests
14+
1315

1416
def download_and_parse_data():
1517
# Download the data
1618
url = "https://gist.githubusercontent.com/Bilka2/6b8a6a9e4a4ec779573ad703d03c1ae7/raw"
1719
response = requests.get(url)
1820
if response.status_code != 200:
1921
raise Exception(f"Failed to download data: {response.status_code}")
20-
22+
2123
# Get the content
2224
content = response.text
2325

2426
# Clean up the content to make it valid Lua
2527
# Remove the script header if present
26-
content = re.sub(r'^Script.*?: ', '', content)
27-
28+
content = re.sub(r"^Script.*?: ", "", content)
29+
2830
# Remove Lua multi-line comments
29-
content = re.sub(r'--\[=\[.*?\]=\]', '', content, flags=re.DOTALL)
30-
31+
content = re.sub(r"--\[=\[.*?\]=\]", "", content, flags=re.DOTALL)
32+
3133
# Convert scientific notation to decimal notation
3234
def convert_scientific(match):
3335
num = float(match.group(0))
34-
return f"{num:.10f}".rstrip('0').rstrip('.')
35-
36-
content = re.sub(r'-?\d+\.?\d*[eE][+-]?\d+', convert_scientific, content)
37-
36+
return f"{num:.10f}".rstrip("0").rstrip(".")
37+
38+
content = re.sub(r"-?\d+\.?\d*[eE][+-]?\d+", convert_scientific, content)
39+
3840
# Remove any trailing commas in arrays/tables
39-
content = re.sub(r',(\s*[}\]])', r'\1', content)
40-
41+
content = re.sub(r",(\s*[}\]])", r"\1", content)
42+
4143
# Ensure proper line endings
42-
content = content.replace('\r\n', '\n')
43-
44+
content = content.replace("\r\n", "\n")
45+
4446
try:
4547
a = luadata.unserialize(content, encoding="utf-8", multival=False)
4648
return a
4749
except Exception as e:
48-
print(f"Error position in content: {str(e)}")
50+
print(f"Error position in content: {e}")
4951
# Save problematic content to a file for inspection
50-
with open('debug_lua_content.txt', 'w') as f:
52+
with open("debug_lua_content.txt", "w") as f:
5153
f.write(content)
5254
raise
5355

56+
5457
def remove_layers_recursively(data):
5558
if isinstance(data, dict):
5659
# Remove 'layers' and 'sprites' keys if present
57-
data.pop('layers', None)
58-
data.pop('sprites', None)
60+
data.pop("layers", None)
61+
data.pop("sprites", None)
5962
# Recursively process all values
6063
for value in data.values():
6164
remove_layers_recursively(value)
@@ -65,115 +68,110 @@ def remove_layers_recursively(data):
6568
remove_layers_recursively(item)
6669
return data
6770

71+
6872
if __name__ == "__main__":
6973
data = download_and_parse_data()
70-
data.pop('achievement', None)
71-
data.pop('technology', None)
72-
data.pop('tips-and-tricks-item', None)
73-
data.pop('rail-planner', None)
74-
75-
recipe_data = {
76-
'recipe': data.pop('recipe', {}),
77-
'recipe-category': data.pop('recipe-category', {})
78-
}
74+
data.pop("achievement", None)
75+
data.pop("technology", None)
76+
data.pop("tips-and-tricks-item", None)
77+
data.pop("rail-planner", None)
78+
79+
recipe_data = {"recipe": data.pop("recipe", {}), "recipe-category": data.pop("recipe-category", {})}
7980

8081
item_data = {
81-
'item': data.pop('item', {}),
82-
'item-group': data.pop('item-group', {}),
83-
'item-subgroup': data.pop('item-subgroup', {}),
82+
"item": data.pop("item", {}),
83+
"item-group": data.pop("item-group", {}),
84+
"item-subgroup": data.pop("item-subgroup", {}),
8485
}
8586

8687
filtered_keys = [
87-
'accumulator',
88-
'agricultural-tower',
89-
'arithmetic-combinator',
90-
'assembling-machine',
91-
'asteroid-collector',
92-
'beacon',
93-
'boiler',
94-
'burner-generator',
95-
'cargo-bay',
96-
'cargo-landing-pad',
97-
'constant-combinator',
98-
'container',
99-
'curved-rail-a',
100-
'curved-rail-b',
101-
'decider-combinator',
102-
'electric-pole',
103-
'electric-turret',
104-
'elevated-curved-rail-a',
105-
'elevated-curved-rail-b',
106-
'elevated-half-diagonal-rail',
107-
'elevated-straight-rail',
108-
'fluid-turret',
109-
'furnace',
110-
'fusion-generator',
111-
'fusion-reactor',
112-
'gate',
113-
'generator',
114-
'half-diagonal-rail',
115-
'heat-interface', # TODO: check if this is needed
116-
'heat-pipe',
117-
'highlight-box',
118-
'inserter',
119-
'lab',
120-
'lamp',
121-
'lightning-attractor',
122-
'logistic-container',
123-
'mining-drill',
124-
'offshore-pump',
125-
'pipe',
126-
'pipe-to-ground',
127-
'power-switch',
128-
'programmable-speaker',
129-
'pump',
130-
'radar',
131-
'rail-chain-signal',
132-
'rail-ramp',
133-
'rail-remnants', # TODO: check if this is needed
134-
'rail-signal',
135-
'rail-support',
136-
'reactor',
137-
'roboport',
138-
'rocket-silo',
139-
'selector-combinator',
140-
'solar-panel',
141-
'splitter',
142-
'storage-tank',
143-
'straight-rail',
144-
'thruster',
145-
'train-stop',
146-
'transport-belt',
147-
'underground-belt',
148-
'wall'
88+
"accumulator",
89+
"agricultural-tower",
90+
"arithmetic-combinator",
91+
"assembling-machine",
92+
"asteroid-collector",
93+
"beacon",
94+
"boiler",
95+
"burner-generator",
96+
"cargo-bay",
97+
"cargo-landing-pad",
98+
"constant-combinator",
99+
"container",
100+
"curved-rail-a",
101+
"curved-rail-b",
102+
"decider-combinator",
103+
"electric-pole",
104+
"electric-turret",
105+
"elevated-curved-rail-a",
106+
"elevated-curved-rail-b",
107+
"elevated-half-diagonal-rail",
108+
"elevated-straight-rail",
109+
"fluid-turret",
110+
"furnace",
111+
"fusion-generator",
112+
"fusion-reactor",
113+
"gate",
114+
"generator",
115+
"half-diagonal-rail",
116+
"heat-interface", # TODO: check if this is needed
117+
"heat-pipe",
118+
"highlight-box",
119+
"inserter",
120+
"lab",
121+
"lamp",
122+
"lightning-attractor",
123+
"logistic-container",
124+
"mining-drill",
125+
"offshore-pump",
126+
"pipe",
127+
"pipe-to-ground",
128+
"power-switch",
129+
"programmable-speaker",
130+
"pump",
131+
"radar",
132+
"rail-chain-signal",
133+
"rail-ramp",
134+
"rail-remnants", # TODO: check if this is needed
135+
"rail-signal",
136+
"rail-support",
137+
"reactor",
138+
"roboport",
139+
"rocket-silo",
140+
"selector-combinator",
141+
"solar-panel",
142+
"splitter",
143+
"storage-tank",
144+
"straight-rail",
145+
"thruster",
146+
"train-stop",
147+
"transport-belt",
148+
"underground-belt",
149+
"wall",
149150
]
150151
filtered_data = {key: data[key] for key in filtered_keys}
151152

152153
filtered_data = data
153154

154155
filtered_data = remove_layers_recursively(filtered_data)
155-
156-
157-
156+
158157
# Create factorio_data directory if it doesn't exist
159-
os.makedirs('factorio_data', exist_ok=True)
160-
158+
os.makedirs("factorio_data", exist_ok=True)
159+
161160
# Extract recipe-related data
162-
163161

164162
print(data.keys())
165-
163+
166164
# Save recipe data to a separate file
167-
with open('factorio_data/recipe_data.json', 'w', encoding='utf-8') as f:
165+
with open("factorio_data/recipe_data.json", "w", encoding="utf-8") as f:
168166
json.dump(recipe_data, f, indent=2, ensure_ascii=False)
169-
print(f"Recipe data saved to factorio_data/recipe_data.json")
167+
print("Recipe data saved to factorio_data/recipe_data.json")
170168

171169
# Save item data to a separate file
172-
with open('factorio_data/item_data.json', 'w', encoding='utf-8') as f:
170+
with open("factorio_data/item_data.json", "w", encoding="utf-8") as f:
173171
json.dump(item_data, f, indent=2, ensure_ascii=False)
174-
print(f"Item data saved to factorio_data/item_data.json")
175-
172+
print("Item data saved to factorio_data/item_data.json")
173+
176174
# Save remaining data
177-
with open('factorio_data/factorio_data.json', 'w', encoding='utf-8') as f:
175+
with open("factorio_data/factorio_data.json", "w", encoding="utf-8") as f:
178176
json.dump(filtered_data, f, indent=2, ensure_ascii=False)
179-
print(f"Main data saved to factorio_data/factorio_data.json")
177+
print("Main data saved to factorio_data/factorio_data.json")

0 commit comments

Comments
 (0)