Read a file and extract data and assign to a variable from a python file

不想你离开。 提交于 2020-12-13 17:51:11

问题


I am trying to extract BINPATH, LIBPATH,CPPPATH from a conan.txt file which looks like:

conan = {

    "conan" : {
        "CPPPATH"     : ['something'],
        "BINPATH"     : ['something'],
        "LIBS"        : ['something'],
        "CPPDEFINES"  : [],
        "CXXFLAGS"    : [],
        "CCFLAGS"     : [],
        "SHLINKFLAGS" : [],
        "LINKFLAGS"   : [],
    },
    "conan_version" : "None",

    "boost" : {
        "CPPPATH"     : ['C:\\.conan\\123456\\1\\include'],
        "LIBPATH"     : ['C:\\.conan\\123456\\1\\lib'],
        "BINPATH"     : ['C:\\.conan\\123456\\1\\lib'],
        "LIBS"        : [],
        "CPPDEFINES"  : [],
        "CXXFLAGS"    : [],
        "CCFLAGS"     : [],
        "SHLINKFLAGS" : [],
        "LINKFLAGS"   : [],
    },
    "boost_version" : "1.69.0"
}
Return('conan')

I have a scons /python file which needs CPPPATH,BINPATH,LIBPATH values as variable. I am trying to extract these values in following function in Sconscript :

def getCPPPath():
          data = {'Return': lambda x: False}
            with open(file.txt, 'r') as f:
             exec(f.read(), data)
             return (data["conan"]["conan"]["CPPPATH"][0])
             print ("Path is:", ["conan"]["conan"]["CPPPATH"][0])

This gives me an error:

scons: *** Return of non-existent variable ''conan''

How can I achieve this?


回答1:


You can use the following code. Note that exec is insecure as it runs all the code that is in your file.txt. You also need to pass a dummy Return function into exec.

data = {"Return": lambda x: False}

with open("file.txt", "r", encoding="utf-8") as f:
    exec(f.read(), data)

print(data['conan']['conan']['BINPATH'][0])
print(data['conan']['boost']['LIBPATH'][0])
print(data['conan']['conan']['CPPPATH'][0])

Prints

['something']
['C:\\.conan\\123456\\1\\lib']
['something']



回答2:


A lot easier and w/o using exec. You need to name your file conan.py (<= note the .py ending):

import conan

data = conan.conan
print(data['conan']['BINPATH'])

=> ['something']



回答3:


Assuming you're trying to set up Conan with SCons, there's another way. If this was "normal" Python, the two other answers would've been completely correct. However, since you're clearly using the SCons generator in Conan (otherwise, you wouldn't have ended up with that file), you have a file that's compatible with SCons. SCons naturally has additions on top of Python for managing its "own" files.

Therefore, you don't need exec at all. this is all you need:

conan = env.SConscript("SConscript_conan")
# Note: you may need to change the path to SConscript_conan depending on your 
#       build system configuration. If you installed Conan in `build`, the
#       path should be "build/SConscript_conan"
env.MergeFlags(conan["conan"])

This also ensures you add all the other flags you need, not just the paths. Also saves you manual setup if you ever decide to add another library.

Return('conan') is an SCons function, which basically is equivalent to returning a variable (here: conan) from an SConscript file into whatever executed it. Incorrect execution, or of course the lacking existence of a variable, is enough to cause the function to throw. If you source the SConscript file properly, it should work as expected.

See also the Conan documentation, specifically the second code snippet, and the SCons docs on SConscript files



来源:https://stackoverflow.com/questions/60210426/read-a-file-and-extract-data-and-assign-to-a-variable-from-a-python-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!