Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alternative configuration file option fixed, python3 compatible, adds start_lims function #202

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 59 additions & 18 deletions genologics/config.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,69 @@
import os
import sys
import warnings
from genologics.lims import Lims
from sys import version_info

if version_info.major == 2:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not compatible with python 2.6, please use version_info[0]

import ConfigParser
pyvers = 2
else:
from configparser import ConfigParser
pyvers = 3


import ConfigParser

'''
If config file is in a default location
Usage:
from genologics.config import BASEURI, USERNAME, PASSWORD

Alternate Usage:
from genologics import config
BASEURI, USERNAME, PASSWORD, VERSION, MAIN_LOG = config.load_config(specified_config = <path to config file>)
To use config from alternative location
Usage:
from genologics import start_lims

lims = start_lims(path_to_config)

# you won't need this once you have your lims object, but here they are:
BASEURI = lims.baseuri
PASSWORD = lims.password
USERNAME = lims.username
'''

spec_config = None

def get_config_info(config_file):
config = ConfigParser.SafeConfigParser()
if pyvers==2:
config = ConfigParser.SafeConfigParser()
else:
config = ConfigParser()

config.readfp(open(config_file))



BASEURI = config.get('genologics', 'BASEURI').rstrip()
USERNAME = config.get('genologics', 'USERNAME').rstrip()
PASSWORD = config.get('genologics', 'PASSWORD').rstrip()

if config.has_section('genologics') and config.has_option('genologics','VERSION'):
VERSION = config.get('genologics', 'VERSION').rstrip()
else:
VERSION = 'v2'

if config.has_section('logging') and config.has_option('logging','MAIN_LOG'):
MAIN_LOG = config.get('logging', 'MAIN_LOG').rstrip()
else:
MAIN_LOG = None
return BASEURI, USERNAME, PASSWORD, VERSION, MAIN_LOG


def load_config(specified_config = None):
if specified_config != None:

def load_config(specified_config = None, startup=True):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but would you mind keeping the same spacing rule for the two arguments ?

config_file = None
if specified_config is not None:
config_file = specified_config
else:
config = ConfigParser.SafeConfigParser()
if pyvers == 2:
config = ConfigParser.SafeConfigParser()
else:
config = ConfigParser()
try:
conf_file = config.read([os.path.expanduser('~/.genologicsrc'), '.genologicsrc',
'genologics.conf', 'genologics.cfg', '/etc/genologics.conf'])
Expand All @@ -49,12 +72,30 @@ def load_config(specified_config = None):
config_file = conf_file[0]

except:
warnings.warn("Please make sure you've created or indicated your own Genologics configuration file (i.e: ~/.genologicsrc) as stated in README.md")
sys.exit(-1)
if not startup:
warnings.warn("config file not specified or found in the expected locations. Please provide create your own Genologics configuration file and provide a config path or place it in a default location (i.e: ~/.genologicsrc) as stated in README.md")
sys.exit(-1)

if startup and config_file is None:
return warnings.warn("Config File Not Found"), None, None, None, None
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warnings.warn returns None, and if there is no exit, then the caller will see a warning, but still end up with seemingly populated BASEURI, USERNAME, PASSWORD, VERSION, MAIN_LOG, but they will all contain None.

I don't really agree with that, as the current method hard-exits if there is no configuration file, it should be the same for providing a different config file.


BASEURI, USERNAME, PASSWORD, VERSION, MAIN_LOG = get_config_info(config_file)

return BASEURI, USERNAME, PASSWORD, VERSION, MAIN_LOG

return BASEURI, USERNAME, PASSWORD, VERSION, MAIN_LOG


def start_lims(config=None):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that code related to instantiating a LIMS object belongs in the config module.

'''start genologics lims object

Args:
config (str): path to genologics configuration file
Returns:
lims class object
'''
BASEURI, USERNAME, PASSWORD, VERSION, MAIN_LOG = load_config(config, startup=False)
lims = Lims(BASEURI, USERNAME, PASSWORD, VERSION)
#lims.check_version()
return lims


BASEURI, USERNAME, PASSWORD, VERSION, MAIN_LOG = load_config(specified_config = spec_config)
BASEURI, USERNAME, PASSWORD, VERSION, MAIN_LOG = load_config(specified_config = spec_config, startup=True)