Menu

#69 macroExpansion of "#include" directives by ParsedParameterFile class assumes relative paths and disregards environment variables

confirmed
None
normal
minor
always
none
The Library
0.5.4
general
2021-03-11
2011-06-06
No

The "macroExpansion" option in the "ParsedParameterFile" class, assumes that the path to the "#include" files are relative to the location of the dictionary. .... There are a couple of issues in this case....:

[a]. It is not always the case that a relative path is used when specifying the location of the file.... it could also be an absolute path, in which case, the line 511 in ParsedParameterFile.py would not provide a usable path.

[b]. The path may contain environment variables which get substituted correctly by OpenFOAM but are not expanded by PyFoam

[c]. Environment variables such as "$FOAM_CASE" which are only valid when actually running the case need to substituted by the right paths by PyFoam utilities. [Sample: #include "$FOAM_CASE/../inletFlowRate.dat" ]

---- additional_information ----
Location of the failure: PyFoam/RunDictionary/ParsedParameterFile.py
Line number: 510 - 521
-------------- Quote - Begin ----------------
510ŒŒŒŒ if self.doMacros:
511ŒŒŒŒŒŒŒŒŒŒŒŒ fName=path.join(self.directory(),p[2][1:-1])
512ŒŒŒŒŒŒŒŒŒŒŒŒ read=True
513ŒŒŒŒŒŒŒŒŒŒŒŒ if p[1]=="includeIfPresent" and not path.exists(fName):
514ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒ read=False
515ŒŒŒŒŒŒŒŒŒŒŒŒ if read and not path.exists(fName):
516ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒ raise PyFoamParserError("The included file "+fName+" does not exist")
517ŒŒŒŒŒŒŒŒŒŒŒŒ if read:
518ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒ data=ParsedParameterFile(fName,noHeader=True)
519ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒ into=self.dictStack[-1]
520ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒ for k in data:
521ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒ into[k]=data[k]
-------------- Quote - End ------------------

Possible solutions:

1. The python function "os.path.expandvars" could be used to substitute the contents of standard environment variables into the path.

2. Parse the "#include" directive to detect if the specified path is a relative or absolute path and always convert to an absolute path (?)

3. No robust solution idea yet for special environment variables such as "$FOAM_CASE"

Discussion

  • Bernhard Gschaider

    Will have a look at it.

    The thing with FOAM_CASE can be solved by inserting it into os.environ. Then the substitution of the variables should work.

    The detection of absolute paths can be done with os.path.isabs

     
  • David Blacher

    David Blacher - 2021-03-11

    hi,
    I have a related issue:
    When I try to include a file that is located in a parent directory as relative path with
    #include "../common/defaultParameters_mesh"
    pyFoam does not recognise this, but simply adds it to the absolute path of the case (~/OpenFoam/cyberspeck-8/tool/test):
    Error in /opt/OpenFOAM/ThirdParty-8/PyFoam/bin/pyFoamPrepareCase.py : Error in PyFoamParser: 'The included file ~/OpenFoam/cyberspeck-8/tool/test/../common/defaultParameters_mesh does not exist'
    Is there a better way to include a openFoam dict file?
    Regards,
    david

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.