Community
    • Login

    PythonScript: Script that runs on multiple files not working

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    pythonpythonscriptpythonscript
    3 Posts 2 Posters 276 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Fuel DZNF
      Fuel DZN
      last edited by

      Rather than replacing ‘value’ according to the ‘mission_tag’, ‘decision_tag’ or ‘religion_group’ found in the document, it instead replaces ‘value’ according to said variables found in the first open document from which the script is first ran from.

      The script works on single files. All that’s been added to make it multi-file is:

      for (filename, bufferID, index, view) in notepad.getFiles():
      

      I am a beginner to python so sorry if I’ve made a very basic mistake.

      # encoding=utf-8
      from Npp import *
      import re
      import os
      
      mission_tag = re.search(r'\s*potential = \{[\s\S]*(?<=tag = )(\w+)', editor.getText())
      decision_tag = re.search(r'(?<=NOT = { exists = )(\w+)', editor.getText())
      religion_group = re.search(r'\s*potential = \{[\s\S]*(?<=religion_group = )(\w+)', editor.getText())
      
      history_files = 'C:\Program Files (x86)\Steam\steamapps\common\Europa Universalis IV\history\countries'
      base_tech = 3
      
      pattern = re.compile(r'(?<=adm_tech = )(\d+)|(?<=dip_tech = )(\d+)|(?<=mil_tech = )(\d+)')
      value = pattern.search(editor.getText())
      
      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)))));
      
      def adjust():
          notepad.open(history_files+'\\'+filename)
          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()
          elif tech_group.group(0) == 'eastern':
              replace(20)
          elif tech_group.group(0) == 'ottoman':
              replace(25)
          elif tech_group.group(0) == 'muslim':
              replace(37.5)
          elif tech_group.group(0) == 'indian':
              replace(40)
          elif tech_group.group(0) == 'east_african':
              replace(45)
          elif tech_group.group(0) == 'central_african':
              replace(55)
          elif tech_group.group(0) == 'chinese':
              replace(50)
          elif tech_group.group(0) == 'nomad_group':
              replace(62.5)
          elif tech_group.group(0) == 'sub_saharan':
              replace(50)
          elif tech_group.group(0) in ('mesoamerican', 'andean'):
              replace(75)
          elif tech_group.group(0) in ('north_american', 'south_american', 'aboriginal_tech', 'polynesian_tech'):
              replace(92.5)
      
      for (filename, bufferID, index, view) in notepad.getFiles():
          notepad.activateBufferID(bufferID)
          for filename in os.listdir(history_files):
              if mission_tag and re.match(mission_tag.group(1), filename):
                  adjust()
              elif decision_tag and re.match(decision_tag.group(0), filename):
                  adjust()    
              elif religion_group:
                  if religion_group.group(1) == 'christian':
                      notepad.close()
                  elif religion_group.group(1) == 'muslim':
                      replace(37.5)
                  elif religion_group.group(1) == 'dharmic':
                      replace(40)
                  elif religion_group.group(1) == 'eastern':
                      replace(50)
                  elif religion_group.group(1) == 'pagan':
                      replace(75)
      
      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Fuel DZN
        last edited by

        @Fuel-DZN

        As the mission_tag = … (and others) definition currently involves data only from the “open doc from which the script is first ran from”, I’d say you need to have those statements inside the for loop you added, after the notepad.activateBufferID line.

        Fuel DZNF 1 Reply Last reply Reply Quote 0
        • Fuel DZNF
          Fuel DZN @Alan Kilborn
          last edited by

          @Alan-Kilborn Thanks, that worked. I had tried that but forgot to put it after the notepad.activateBufferID line.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors