Skip to content

Instantly share code, notes, and snippets.

@AdamsGH
Created September 14, 2024 10:29
Show Gist options
  • Save AdamsGH/9f952550a1ae1c4b27d30e10b2335a0d to your computer and use it in GitHub Desktop.
Save AdamsGH/9f952550a1ae1c4b27d30e10b2335a0d to your computer and use it in GitHub Desktop.
Replace deprecated vars for VTT modules
import os
import sys
deprecations = {
"ActionTemplate": "ActivitiesTemplate",
"ActivatedEffectTemplate": "ActivitiesTemplate",
"EnchantmentField": "EnchantActivity",
"EnchantmentData": "EnchantActivity",
"SummonsField": "SummonActivity",
"SummonsData": "SummonActivity",
"Item5e#system": "ActivitiesTemplate",
"Item5e#use": "ActivityUsageResults"
}
deprecated_hooks = {
"dnd5e.preRollHitDie": "dnd5e.preRollHitDieV2",
"dnd5e.rollHitDie": "dnd5e.rollHitDieV2",
"dnd5e.preUseItem": "dnd5e.preUseActivity",
"dnd5e.preCreateItemTemplate": "dnd5e.preCreateActivityTemplate",
"dnd5e.createItemTemplate": "dnd5e.createActivityTemplate",
"dnd5e.preItemUsageConsumption": "dnd5e.preActivityConsumption",
"dnd5e.itemUsageConsumption": "dnd5e.activityConsumption",
"dnd5e.preDisplayCard": "dnd5e.preDisplayCardV2",
"dnd5e.useItem": "dnd5e.postUseActivity",
"dnd5e.preRollAttack": "dnd5e.preRollAttackV2",
"dnd5e.rollAttack": "dnd5e.rollAttackV2",
"dnd5e.preRollDamage": "dnd5e.preRollDamageV2",
"dnd5e.rollDamage": "dnd5e.rollDamageV2",
"dnd5e.preRollFormula": "dnd5e.preRollFormulaV2",
"dnd5e.rollFormula": "dnd5e.rollFormulaV2",
"dnd5e.preRollRecharge": "dnd5e.preRollRechargeV2",
"dnd5e.rollRecharge": "dnd5e.rollRechargeV2",
"dnd5e.ItemSheet5e": "dnd5e.ItemSheet5e2",
"disableExperienceTracking": "levelingMode"
}
def replace_deprecations(file_path, content, deprecations, hooks):
new_content = content
changes_made = False
for old, new in {**deprecations, **hooks}.items():
if old in new_content:
new_content = new_content.replace(old, new)
changes_made = True
print(f"Replaced: '{old}' with '{new}'")
if changes_made:
print(f"Writing changes to file: {file_path}")
try:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_content)
print(f"Changes successfully written to file: {file_path}")
except Exception as e:
print(f"Error writing to file {file_path}: {str(e)}")
else:
print(f"No changes found in file: {file_path}")
return changes_made
def check_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
found_deprecations = {term: deprecations[term] for term in deprecations if term in content}
found_hooks = {hook: deprecated_hooks[hook] for hook in deprecated_hooks if hook in content}
return found_deprecations, found_hooks, content
def scan_directory(directory):
results = {}
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.js') or file.endswith('.json'):
file_path = os.path.join(root, file)
print(f"Checking file: {file_path}")
deprecation_results, hook_results, content = check_file(file_path)
if deprecation_results or hook_results:
results[file_path] = (deprecation_results, hook_results, content)
return results
def main():
if len(sys.argv) != 2:
print("Usage: python check_deprecations.py <directory_path>")
return
directory = sys.argv[1]
if not os.path.isdir(directory):
print("The specified path is not a directory.")
return
results = scan_directory(directory)
if results:
print("Found deprecated terms and hooks:")
for file_path, (deprecations, hooks, content) in results.items():
print(f"\nFile: {file_path}")
for term, replacement in deprecations.items():
print(f" Deprecated term: '{term}' should be replaced with '{replacement}'")
for hook, replacement in hooks.items():
print(f" Deprecated hook: '{hook}' should be replaced with '{replacement}'")
auto_replace = input("\nDo you want to automatically replace deprecated values? (y/n): ")
if auto_replace.lower() == 'y':
changes_made = False
for file_path, (deprecations, hooks, content) in results.items():
if replace_deprecations(file_path, content, deprecations, hooks):
changes_made = True
if changes_made:
print("Deprecated values have been replaced.")
else:
print("No changes were made.")
else:
print("Replacement of deprecated values cancelled.")
else:
print("No deprecated terms or hooks found.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment