PythonScript: Script not working
-
My script runs through each file and checks for possible regex patterns that can be used to adjust the file accordingly.
# encoding=utf-8 from Npp import * import re import os history_files = 'C:/Program Files (x86)/Steam/steamapps/common/Europa Universalis IV/history/countries' base_tech = 3 def replace(percentage): amount = float((100 - percentage) * 0.01) editor.rereplace(r'(?<=adm_tech = )(\d+)|(?<=dip_tech = )(\d+)|(?<=mil_tech = )(\d+)', str(int(round(base_tech * ((((float(value.group(0)) / base_tech) - 1) * amount) + 1))))); notepad.save() def adjust(): notepad.open(history_files+'\\'+country_file) colonial = re.search(r'(?<=.)(is_former_colonial_nation = yes)|(?<=.)(is_colonial_nation = yes)', editor.getText()) tech_group = re.search(r'(?<=technology_group = )(\w+)', editor.getText()) notepad.close() if tech_group.group(0) == 'western' or colonial: notepad.close() console.write('\nClosed: "' + filename + '" because of "western" or "colonial"') elif tech_group.group(0) == 'eastern': replace(20) for (filename, bufferID, index, view) in notepad.getFiles(): notepad.activateBufferID(bufferID) pattern = re.compile(r'(?<=adm_tech = )(\d+)|(?<=dip_tech = )(\d+)|(?<=mil_tech = )(\d+)') value = pattern.search(editor.getText()) mission_tag = re.search(r'\s*potential = \{[\s\S]*(?<=tag = )(\w+)', editor.getText()) religion_group = re.search(r'\s*potential = \{[\s\S]*(?<=religion_group = )(\w+)', editor.getText()) for country_file in os.listdir(history_files): if mission_tag and re.match(mission_tag.group(1), country_file): adjust() if religion_group and not mission_tag: if religion_group.group(1) == 'christian': notepad.close() console.write('\nClosed: "' + filename + '" because of "christian"') elif religion_group.group(1) == 'dharmic': replace(40) elif mission_tag: console.write('\nCould not match "' + filename + '" to any country_file') elif not religion_group and not mission_tag: console.write('\nNo identifiers found in: ' + filename)
Among other weird behaviors, why am I getting contradictory messages in the console like this?
Closed: "C:\Users\JB452503\Documents\files test\missions\DOM_French_Missions.txt" because of "western" or "colonial" Matched mission_tag and country_file for: "C:\Users\JB452503\Documents\files test\missions\DOM_French_Missions.txt" Could not match "C:\Users\JB452503\Documents\files test\missions\DOM_French_Missions.txt" to any country_file
How is it matching the contents of the file if it just closed it? How can it have a match and also no match?
-
@Fuel-DZN said in PythonScript: Script not working:
How is it matching the contents of the file if it just closed it?
Either a bug in your logic, or a race condition. If it’s a bug in your logic, it’s not really on-topic for a Notepad++
But if it’s a race condition, it’s got to do with the way that Plugins ask Notepad++ to perform actions: In case you didn’t know, Notepad++ actions take time (dozens or hundreds of milliseconds are not uncommon action times), and the Plugin Communication messages that the
notepad
andeditor
objects wrap around often don’t wait until the action is complete before returning control to the plugin (which in this case is the PythonScript plugin, which then continues executing your script).So your script tells Notepad++ to close the file, the application says “okay, I will”, and then proceeds to take a significant fraction of a second to close the file. In the meantime, your script gets the “okay, I will” message and then says “good, I’ll continue running the next commands”.
If you were going to continue to do something more after the file were closed, I would say that you should probably put in a 500ms - 1000ms wait after doing the close, just to make sure that Notepad++ has time to finish it.
If you are not planning on doing anything after, then I would suggest adding in logic to exit the script (warning:
exit()
doesn’t do what you think it does in PythonScript [when it kills the Python interpreter, it also closes Notepad++], so you’ll want flags andif
-statements to just quickly exit out of your script, orthrow
an exception, which PythonScript handles more gracefully (without exiting Notepad++).How can it have a match and also no match?
Either because of the race condition (above) or because of another logic bug. If it’s the race condition, then it’s explained above. If it’s a logic bug on your part, that’s just part of programming, and not specific to Notepad++ or the PythonScript interface with Notepad++, so is not on topic here.
-
This post is deleted!