Implementing project management/Opening copy files



  • I am trying to implement a project management system (As simple for the user as possible)
    It works like this;
    I have a PROJECT.BAT file in my folder with my source files (Or in the parent folder)
    This PROJECT.BAT file is executed before I compile or run my my current source file so the user can specify any environment variables for the compiler or runtime by modifying PROJECT.BAT.
    Compile and running is accomplished by executing a cbl_function.bat file.
    I use NppExec to issue the command line $(SYS.COBOL)\cbl_function.bat COMPILE $(CURRENT_DIRECTORY) $(NAME_PART) $(EXT_PART)
    So the first parameter is COMPILE or RUN depending on what the user wants, then the PATH, NAME and EXTENSION parameters.
    cbl_function.bat will then run PROJECT.BAT (If it exists) and use any predefined variables that are set in PROJECT.BAT

    So this works fine.

    My issue is this;
    I have a python script that will parse a COBOL COPY filename statement and open the copy file
    So the pyhon script will need to extract the COPYPATH from the PROJECT.BAT file in order to find the COPY file
    This COPYPATH may contain references to other variables (%VARIABLE%)

    I am concerned that this will take too long. How can i parse the project file (when the source file is opened) and some how set a GLOBAL COPYPATH variable in python that will be available to my python script that opens my COPY files.



  • @Anthony-Blinco said in Implementing project management/Opening copy files:

    I have a python script that will parse

    I am assuming this to mean “I am using the PythonScript plugin to run a python script”. I make this clarification because using the PythonScript plugin to run a script has different implications as to the environment compared to calling an external python interpreter from the NppExec script. (I feel justified in this interpretation because later you say “parse the project file (when the source file is opened)”, which I infer to mean that the “python script” has access to a file that’s opened in Notepad++, which says to me that you’re using PythonScript plugin.)

    I am concerned that this will take too long. How can i parse the project file (when the source file is opened) and some how set a GLOBAL COPYPATH variable in python that will be available to my python script that opens my COPY files.

    I think this might not be the best way to go about it. Since the variables would not be set in the environment of the Notepad++ (which was run before the batch file, so maintains the original, unmodified environment), the values of the %Variables% wouldn’t be available to the python script , so you would basically have to write a batch-file interpreter in Python to get it to figure out the right value for each %Variable% before piecing together the final COPYPATH.

    As an alternative, I might suggest that you add a line at the end of the cbl_function.bat (or after running cbl_function.bat from the NppExec script) that will output a quick config-output file which just contains the value of COPYPATH. Maybe something like

    REM main contents , ending with:
    CALL PROJECT.BAT
    REM new code to put the final value of COPYPATH into a known file location
    echo %COPYPATH% > ./COPYPATH.out
    

    Then you could just have your python code read ./COPYPATH.out and use the text from that file to “set a GLOBAL COPYPATH variable in python”



  • Awesome

    1. Yes i am running notepad++ python script
    2. How do i set a global variable in notepad++ python script?


  • @Anthony-Blinco said in Implementing project management/Opening copy files:

    How do i set a global variable in notepad++ python script?

    The same way you do in normal python.

    The PythonScript environment has one instance for the whole run of Notepad++, so if you set the global variable in your script, it will stay set, and if you change the value, it will change. There is a trick you can use to make sure that it gets an initial value, and stays global all the time:

    global myGlobal
    try:
        myGlobal
    except NameError:
        myGlobal = "initial value"
    

    (That’s if you’re worried that you’re going to try to read a value from myGlobal before you set it)

    However, really, I cannot see a need for something that fancy, In your script, you should just be able to do

    def read_copypath_file:
        ...  # function to read the contents of the COPYPATH.out file
    
    COPYPATH = read_copypath_file()
    ... # remainder of your python script, where you want to be able to use the COPYPATH value
    

    And after that, COPYPATH will have the value that was read from the file.



  • Ok. Thanks very much for your help. I thought i might have to parse the .bat file to get an expanded variable but think i am going to go with your suggestion and make a script that runs when the source file is loaded that executes the project.bat and saves of the variables i need.
    That was a very good suggestion indeed. Thanks!


Log in to reply