Just whipped up a simple way to divide up module sources into multiple files within a seperate folder per module_*.py and thought I should share it.
... Probably already exists in some form, but the search function is terrible and it only took a few lines, so here:
Also Python3 compliant, by the way. I actually developed them with a Python3.3 executable.
... Probably already exists in some form, but the search function is terrible and it only took a few lines, so here:
Code:
import os
import shutil
from os import path
def parse_directory(dir, maxDepth):
entries = os.listdir(dir)
dirs = [ path.join(dir, d) for d in entries if path.isdir (path.join(dir, d)) ]
files = [ path.join(dir, f) for f in entries if path.isfile(path.join(dir, f)) and path.splitext(f)[1] == ".py" ]
for name in files:
# write from source files
file = open(name, 'r')
for line in file:
# keep the comments out
if line.find('#') != -1:
line = line.split('#')[0] + '\n'
# keep the empty lines out
if not line.isspace(): # "empty" lines still contain the line separator
module.write(line)
file.close()
if(maxDepth > 0):
for name in dirs:
# parse subfolders
parse_directory(name, maxDepth - 1)
modules = [ m for m in os.listdir(".") if path.isfile(m) and m.startswith("module_") ]
# remove files nonsensical to parse
modules.remove("module_info.py")
modules.remove("module_constants.py") # not to be appended to, as slots et al. need to be conflict free; checking easier in place
for m in modules:
# fetch directory name for module file: capitalised right side of file name without extension
dir = path.splitext(m.partition("_")[2])[0].capitalize()
# if path exists, parse
if(path.isdir(dir)):
print("Importing " + dir.lower() + "...") # Python3 compliant
# save original module_scripts.py
shutil.copy2(m, m + ".bak")
# append all source files to module_scripts.py
module = open(m, 'a')
parse_directory(dir, 10) # arbitrary limit; may be raised considerably before errors occur
module.close()
Code:
import os
from os import path
print("Performing cleanup operations...")
files = [ f for f in os.listdir(".") if path.isfile(f) and path.splitext(f)[1] == ".bak" ]
for name in files:
orig = path.splitext(name)[0]
# restore original source files
os.remove(orig)
os.rename(name, orig)
Add before process_init.py:
Add after process_postfx.py:
Code:
<Python path>\python.exe process_imports.py
Code:
<Python path>\python.exe process_cleanup.py
Use "<array>.extend([[...]])" to append to arrays (such as array "scripts" in "module_scripts.py").
When defining local constants, do not use indentation! These are interpreted as Python code and indentations are semantic information for Python. If you want to write functions, I assume you know your trade.
Files are fetched from the directory associated with the main file ("Animations" for "module_animations.py", "Scripts" for "module_scripts.py" etc.) and any subdirectories down to a depth of 10.
Any name will do. As long as the file has the correct suffix ".py", it will be fetched.
When defining local constants, do not use indentation! These are interpreted as Python code and indentations are semantic information for Python. If you want to write functions, I assume you know your trade.
Files are fetched from the directory associated with the main file ("Animations" for "module_animations.py", "Scripts" for "module_scripts.py" etc.) and any subdirectories down to a depth of 10.
Any name will do. As long as the file has the correct suffix ".py", it will be fetched.
Code:
scripts.extend([
#script_set_adjacency_between_centers
# INPUT : arg0 = center1, arg1 = center2, arg2 = new adjacency value
# OUTPUT: none
("set_adjacency_between_centers",[
(store_script_param_1, ":center1"),
(store_script_param_2, ":center2"),
(store_script_param, ":adjacent", 3),
(party_get_slot, ":adjacencies", ":center1", slot_center_adjacencies),
(party_count_members_of_type, ":adjacencyValue", ":adjacencies", ":center2"), # hack: put partyId as troopId
(party_remove_members, ":adjacencies", ":center2", ":adjacencyValue"),
(party_force_add_members, ":adjacencies", ":center2", ":adjacent"),
(party_get_slot, ":adjacencies", ":center2", slot_center_adjacencies),
(party_count_members_of_type, ":adjacencyValue", ":adjacencies", ":center1"),
(party_remove_members, ":adjacencies", ":center1", ":adjacencyValue"),
(party_force_add_members, ":adjacencies", ":center1", ":adjacent"),
]),
])