-
Notifications
You must be signed in to change notification settings - Fork 44
/
fileformatter.py
122 lines (116 loc) · 5.68 KB
/
fileformatter.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python3
import argparse
import os
import re
import glob
import sys
#############################
###
### HoI 4 File Formatter by Yard1, originally for Equestria at War mod
### Written in Python 3.5.2
###
### Copyright (c) 2017 Antoni Baum (Yard1)
### Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
### The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
### THE 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.
###
### usage: hoi4Formatter.py [-h] [-r] [--extensions [EXTENSIONS [EXTENSIONS ...]]]
### input
###
### Given a file or folder, format files to follow proper PDX-style indentation. Only indentation is changed.
###
### positional arguments:
### input Technology file name/folder containing files
###
### optional arguments:
### -h, --help show this help message and exit
### -ws, --whitespace ONLY remove whitespace at the end of the line without
### formatting (Default: False)
### -ic, --ignore_comments
### Ignore lines which start with # (or whitespace #)
### (Default: False)
### -r, --recursive Format files in directories recursively (Default:
### False)
### --extensions [EXTENSIONS [EXTENSIONS ...]]
### Which file extensions should be formatted (Default:
### .txt .gfx)
###
#############################
def readable_dir(prospective_dir):
if not os.path.isdir(prospective_dir):
raise Exception("readable_dir:{0} is not a valid path".format(prospective_dir))
if os.access(prospective_dir, os.R_OK):
return prospective_dir
else:
raise Exception("readable_dir:{0} is not a readable dir".format(prospective_dir))
#############################
def formatfile(name, remove_whitespace, ignore_comments):
print("Reading file " + name + "...")
with open(name, "r") as f:
lines = f.read().splitlines()
names = list()
new_lines = list()
open_blocks = 0
for line in lines:
if ignore_comments and re.match("^\s*#", line):
new_lines.append(line)
continue
if remove_whitespace:
line = re.sub(r"\s*$", "", line)
new_lines.append(line)
continue
line = line.strip()
if line == "}":
line = ('\t' * (open_blocks-1)) + line
else:
line = ('\t' * open_blocks) + line
line = re.sub(r"\s*$", "", line)
new_lines.append(line)
open_blocks = open_blocks + re.sub(r'\".*?\"', '', line).count('{')
open_blocks = open_blocks - re.sub(r'\".*?\"', '', line).count('}')
with open(name, "w") as f:
f.writelines(str(line) + "\n" for line in new_lines)
#############################
if not sys.version_info >= (3,0):
sys.exit("Wrong Python version. Version 3.0 or higher is required to run this script!")
parser = argparse.ArgumentParser(description='Given a file or folder, format files to follow proper PDX-style indentation. Only indentation is changed.')
parser.add_argument('input', metavar='input',
help='Technology file name/folder containing files')
parser.add_argument( '-ws', '--whitespace', action='store_true', required=False, default=False,
help='ONLY remove whitespace at the end of the line without formatting (Default: False)')
parser.add_argument( '-ic', '--ignore_comments', action='store_true', required=False, default=False,
help='Ignore lines which start with # (or whitespace #) (Default: False)')
parser.add_argument( '-r', '--recursive', action='store_true', required=False, default=False,
help='Format files in directories recursively (Default: False)')
parser.add_argument( "--extensions", nargs="*", type=str, required=False,default=[".txt", ".gfx"],
help='Which file extensions should be formatted (Default: .txt .gfx)')
args = parser.parse_args()
counter = 0
names_global = list()
is_dir = False
try:
dir = readable_dir(args.input)
is_dir = True
except:
print("Not a directory, treating as file.")
if is_dir:
if args.recursive:
for file in glob.glob(dir+"/**/*.*", recursive=True):
if os.path.splitext(file)[1] in args.extensions:
try:
formatfile(file, args.whitespace, args.ignore_comments)
counter += 1
except:
pass
else:
for file in glob.glob(dir+"/*.*"):
if os.path.splitext(file)[1] in args.extensions:
try:
formatfile(file, args.whitespace, args.ignore_comments)
counter += 1
except:
pass
else:
formatfile(args.input, args.whitespace, args.ignore_comments)
counter += 1
print("Finished, %s files formatted" % counter)