Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit def438d

Browse files
authored
Update hoi4fileformatter.py
Add arguments to allow for only removing trailing whitespace and for ignoring of lines which start with # (or whitespace #)
1 parent a6b5133 commit def438d

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

‎python3/hoi4fileformatter.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
#!/usr/bin/python3
22
import argparse
33
import os
4+
import re
45
import glob
56
import sys
67

78
#############################
89
###
910
### HoI 4 File Formatter by Yard1, originally for Equestria at War mod
10-
### Warning: this script overwrites files without creating backups! While it has been tested and should not cause any in-game errors, please keep that in mind.
1111
### Written in Python 3.5.2
1212
###
1313
### Copyright (c) 2017 Antoni Baum (Yard1)
1414
### 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:
1515
### The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
1616
### 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.
1717
###
18-
### usage: hoi4fileformatter.py [-h] [-r] [--extensions [EXTENSIONS [EXTENSIONS ...]]]
18+
### usage: hoi4Formatter.py [-h] [-r] [--extensions [EXTENSIONS [EXTENSIONS ...]]]
1919
### input
2020
###
2121
### Given a file or folder, format files to follow proper PDX-style indentation. Only indentation is changed.
@@ -25,6 +25,11 @@
2525
###
2626
### optional arguments:
2727
### -h, --help show this help message and exit
28+
### -ws, --whitespace ONLY remove whitespace at the end of the line without
29+
### formatting (Default: False)
30+
### -ic, --ignore_comments
31+
### Ignore lines which start with # (or whitespace #)
32+
### (Default: False)
2833
### -r, --recursive Format files in directories recursively (Default:
2934
### False)
3035
### --extensions [EXTENSIONS [EXTENSIONS ...]]
@@ -43,19 +48,27 @@ def readable_dir(prospective_dir):
4348

4449
#############################
4550

46-
def formatfile(name):
51+
def formatfile(name, remove_whitespace, ignore_comments):
4752
print("Reading file " + name + "...")
4853
with open(name, "r") as f:
4954
lines = f.read().splitlines()
5055
names = list()
5156
new_lines = list()
5257
open_blocks = 0
5358
for line in lines:
59+
if ignore_comments and re.match("^\s*#", line):
60+
new_lines.append(line)
61+
continue
62+
if remove_whitespace:
63+
line = re.sub(r"\s*$", "", line)
64+
new_lines.append(line)
65+
continue
5466
line = line.strip()
5567
if line == "}":
5668
line = ('\t' * (open_blocks-1)) + line
5769
else:
5870
line = ('\t' * open_blocks) + line
71+
line = re.sub(r"\s*$", "", line)
5972
new_lines.append(line)
6073
open_blocks = open_blocks + line.count('{')
6174
open_blocks = open_blocks - line.count('}')
@@ -65,10 +78,13 @@ def formatfile(name):
6578
#############################
6679
if not sys.version_info >= (3,0):
6780
sys.exit("Wrong Python version. Version 3.0 or higher is required to run this script!")
68-
6981
parser = argparse.ArgumentParser(description='Given a file or folder, format files to follow proper PDX-style indentation. Only indentation is changed.')
7082
parser.add_argument('input', metavar='input',
7183
help='Technology file name/folder containing files')
84+
parser.add_argument( '-ws', '--whitespace', action='store_true', required=False, default=False,
85+
help='ONLY remove whitespace at the end of the line without formatting (Default: False)')
86+
parser.add_argument( '-ic', '--ignore_comments', action='store_true', required=False, default=False,
87+
help='Ignore lines which start with # (or whitespace #) (Default: False)')
7288
parser.add_argument( '-r', '--recursive', action='store_true', required=False, default=False,
7389
help='Format files in directories recursively (Default: False)')
7490
parser.add_argument( "--extensions", nargs="*", type=str, required=False,default=[".txt", ".gfx"],
@@ -83,24 +99,24 @@ def formatfile(name):
8399
is_dir = True
84100
except:
85101
print("Not a directory, treating as file.")
86-
87102
if is_dir:
88103
if args.recursive:
89104
for file in glob.glob(dir+"/**/*.*", recursive=True):
90105
if os.path.splitext(file)[1] in args.extensions:
91106
try:
92-
formatfile(file)
93-
counter =counter+1
107+
formatfile(file, args.whitespace, args.ignore_comments)
108+
counter +=1
94109
except:
95110
pass
96111
else:
97112
for file in glob.glob(dir+"/*.*"):
98113
if os.path.splitext(file)[1] in args.extensions:
99114
try:
100-
formatfile(file)
101-
counter =counter+1
115+
formatfile(file, args.whitespace, args.ignore_comments)
116+
counter +=1
102117
except:
103118
pass
104119
else:
105-
formatfile(args.input)
120+
formatfile(args.input, args.whitespace, args.ignore_comments)
121+
counter += 1
106122
print("Finished, %s files formatted" % counter)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /