#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    How to load user specific config file/module


    Hi,

    I hope I manage to make this clear.

    We have a program with all the code for execution stored in one directory structure. (/opt/Repro/Emulator)

    This program requires the use of user specific information. This information if currently stored in a "config" module with the file containing (config.py) it living in /opt/Repro/Emulator.

    The file looks a bit like this:
    Code:
    configs = {} 
    configs['pest_path'] = "/home/pest1/code" 
    configs['pest_ips'] = ["10.224.243.159","10.224.243.160","10.224.243.161", 
    "10.224.243.162","10.224.243.163","10.224.243.164",
    "10.224.243.165","10.224.243.166","10.224.243.167",   
    "10.224.243.168","10.224.243.169","10.224.243.170"] 
    configs['user_ip'] = "10.222.17.1"
    ....
    ....
    ....
    Currently we just import this into the modules that require it and all is fine and dandy.

    However, unfortunately, people have started to realise that this tool is useful and so I need to update it to allow multiple users to execute it at once. This requires each user to have their own version of the config file and implementing this is where I am getting stuck.

    Each user has his own user directory:
    /home/ReproUser1
    /home/ReproUser2
    etc..

    requirements:

    • For ease of use, the config file should be stored in this directory.
      All the parameters in it can be global parameters.
      It would be nice if the paramters could still be in the form of a dictionary as they were previously to limit the required code changes but this is not crucial.
      Security isn't really a problem as it is an internal tool and mainly written by interns so is full of rubbish security already.
      Parameters need to be available to modules both in the code home directory and a subdirectory of this and that is all.
      If possible, I would like to just import it once.


    I am currently trying to use execfile but am a bit confused about what this actually does. When this reads in variables from a file how are they stored and what modules are they available to?

    So my main question for now is:
    Is execfile the right way to go (I know this was removed in python 3 but we are unlikely to upgrade)?
    Is it possible to load a file from a completely different directory structure as a module? My Google searches suggested probably not?
    If I am no totally the wrong track, can you suggest another starting point?

    I haven't included any code yet as (other than the execfile command), I don't really have any, but if I progress further on my own, I will update the issue with my attempts.

    Thanks and sorry for the waffle..
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Sorry, I think I'm actually making this too hard for myself.

    I think I can just add the working directory to the path and then everything is fine, so something like:

    Code:
    import sys
    import os
    sys.path.append(os.getcwd())
    from config import configs
    Are there any problems with this?

    Yep, that seems to work, sorry for the pointless thread (it's late my brain was on strike) thanks anyway.

IMN logo majestic logo threadwatch logo seochat tools logo