File: yaml_splitter

package info (click to toggle)
triplea 1.9.0.0.7062-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 25,964 kB
  • sloc: java: 143,218; xml: 70,206; sh: 133; makefile: 7
file content (39 lines) | stat: -rwxr-xr-x 1,308 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python3
import yaml
import sys
import os
import re

# This script splits the triplea_maps.yaml file into multiple files which can be handled by jekyll
# This way we can have a page for each map without having to manage each one individually

if len(sys.argv) != 3:
	print("Usage: " + sys.argv[0] +" [Source_File] [Target_Dir]");
	exit(1);
sourceFile = sys.argv[1];
targetDir = sys.argv[2];
if not os.path.isfile(sourceFile):
	print("'" + sourceFile + "' is not a File");
	exit(1);
if not os.path.isdir(targetDir):
	print("'" + targetDir + "' is not a directory");
	exit(1);

with open(sourceFile, 'r') as stream:
	try:
		for map in yaml.safe_load(stream):
			for key, value in map.items():
				if isinstance(value, str):
					map[key] = value.strip();
			map['slug'] = re.sub(r'[^a-zA-Z\-0-9]', '', map['mapName'].lower().replace(' ', '-').replace('_', '-'));
			with open(os.path.join(targetDir, map['slug'] + '.html'), 'w+') as mapFile:
				mapFile.write('---\n');
				map['title'] = map['mapName'] + ' | TripleA Map';
				description = map.pop('description', '');
				map['downloadUrl'] = map.pop('url', '');
				yaml.dump(map, mapFile, default_flow_style=False);
				mapFile.write('---\n');
				mapFile.write(description);
				mapFile.write('\n');
	except yaml.YAMLError as exc:
		print(exc);