Ensuring calls default to the values found by the Settings object in indicoio.config

This commit is contained in:
Madison May
2015-02-27 16:33:22 -05:00
parent 110abaf7a6
commit e9fc025e95
11 changed files with 67 additions and 20 deletions
+1 -1
View File
@@ -18,4 +18,4 @@ v0.4.12, Fri Dec 19 -- Added batch support interface
v0.4.13, Fri Dec 19 -- Added optional arguments to text tags API v0.4.13, Fri Dec 19 -- Added optional arguments to text tags API
v0.4.14, Sat Dec 20 -- Fix for batch image features preprocessing, increased test coverage v0.4.14, Sat Dec 20 -- Fix for batch image features preprocessing, increased test coverage
v0.4.15, Sat Dec 20 -- Bug fix release v0.4.15, Sat Dec 20 -- Bug fix release
v0.5.0, Friday Feb 27 -- Updated to support private cloud, allows for indicorc file to reduce redundant authorization calls v0.5.0, Friday Feb 27 -- Updated to support private cloud, allows for indicorc file to reduce redundant authorization calls, README updates
+10 -2
View File
@@ -12,8 +12,16 @@ from indicoio.images.fer import fer
from indicoio.images.features import facial_features from indicoio.images.features import facial_features
from indicoio.images.features import image_features from indicoio.images.features import image_features
apis = ['political', 'posneg', 'sentiment', 'language', 'fer', apis = [
'facial_features', 'image_features', 'text_tags'] 'political',
'posneg',
'sentiment',
'language',
'fer',
'facial_features',
'image_features',
'text_tags'
]
apis = dict((api, globals().get(api)) for api in apis) apis = dict((api, globals().get(api)) for api in apis)
for api in apis: for api in apis:
+4 -4
View File
@@ -44,11 +44,11 @@ class Settings(ConfigParser.ConfigParser):
os.getenv("INDICO_PASSWORD") or self.auth_settings.get('password') os.getenv("INDICO_PASSWORD") or self.auth_settings.get('password')
) )
settings = Settings(files=[ SETTINGS = Settings(files=[
os.path.expanduser("~/.indicorc"), os.path.expanduser("~/.indicorc"),
os.path.join(os.getcwd(), '.indicorc') os.path.join(os.getcwd(), '.indicorc')
]) ])
auth = settings.auth() AUTH = SETTINGS.auth()
cloud = settings.cloud() CLOUD = SETTINGS.cloud()
public_api_host = 'apiv1.indico.io' PUBLIC_API_HOST = 'apiv1.indico.io'
+2 -2
View File
@@ -6,7 +6,7 @@ import numpy as np
from indicoio.utils import image_preprocess, api_handler from indicoio.utils import image_preprocess, api_handler
import indicoio.config as config import indicoio.config as config
def facial_features(image, cloud=config.cloud, batch=False, auth=None, **kwargs): def facial_features(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
""" """
Given an grayscale input image of a face, returns a 48 dimensional feature vector explaining that face. Given an grayscale input image of a face, returns a 48 dimensional feature vector explaining that face.
Useful as a form of feature engineering for face oriented tasks. Useful as a form of feature engineering for face oriented tasks.
@@ -30,7 +30,7 @@ def facial_features(image, cloud=config.cloud, batch=False, auth=None, **kwargs)
""" """
return api_handler(image, cloud=cloud, api="facialfeatures", batch=batch, auth=auth, **kwargs) return api_handler(image, cloud=cloud, api="facialfeatures", batch=batch, auth=auth, **kwargs)
def image_features(image, cloud=config.cloud, batch=False, auth=None, **kwargs): def image_features(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
""" """
Given an input image, returns a 2048 dimensional sparse feature vector explaining that image. Given an input image, returns a 2048 dimensional sparse feature vector explaining that image.
Useful as a form of feature engineering for image oriented tasks. Useful as a form of feature engineering for image oriented tasks.
+1 -1
View File
@@ -6,7 +6,7 @@ import numpy as np
from indicoio.utils import api_handler from indicoio.utils import api_handler
import indicoio.config as config import indicoio.config as config
def fer(image, cloud=config.cloud, batch=False, auth=None, **kwargs): def fer(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
""" """
Given a grayscale input image of a face, returns a probability distribution over emotional state. Given a grayscale input image of a face, returns a probability distribution over emotional state.
Input should be in a list of list format, resizing will be attempted internally but for best Input should be in a list of list format, resizing will be attempted internally but for best
+1 -1
View File
@@ -1,7 +1,7 @@
from indicoio.utils import api_handler from indicoio.utils import api_handler
import indicoio.config as config import indicoio.config as config
def language(text, cloud=config.cloud, batch=False, auth=None, **kwargs): def language(text, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
""" """
Given input text, returns a probability distribution over 33 possible Given input text, returns a probability distribution over 33 possible
languages of what language the text was written in. languages of what language the text was written in.
+2 -2
View File
@@ -2,7 +2,7 @@ from indicoio import JSON_HEADERS
from indicoio.utils import api_handler from indicoio.utils import api_handler
import indicoio.config as config import indicoio.config as config
def political(text, cloud=config.cloud, batch=False, auth=None, **kwargs): def political(text, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
""" """
Given input text, returns a probability distribution over the political alignment of the speaker. Given input text, returns a probability distribution over the political alignment of the speaker.
@@ -30,7 +30,7 @@ def political(text, cloud=config.cloud, batch=False, auth=None, **kwargs):
return api_handler(text, cloud=cloud, api="political", batch=batch, auth=auth, **kwargs) return api_handler(text, cloud=cloud, api="political", batch=batch, auth=auth, **kwargs)
def posneg(text, cloud=config.cloud, batch=False, auth=None, **kwargs): def posneg(text, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
""" """
Given input text, returns a scalar estimate of the sentiment of that text. Given input text, returns a scalar estimate of the sentiment of that text.
Values are roughly in the range 0 to 1 with 0.5 indicating neutral sentiment. Values are roughly in the range 0 to 1 with 0.5 indicating neutral sentiment.
+1 -1
View File
@@ -1,7 +1,7 @@
from indicoio.utils import api_handler from indicoio.utils import api_handler
import indicoio.config as config import indicoio.config as config
def text_tags(text, cloud=config.cloud, batch=False, auth=None, **kwargs): def text_tags(text, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
""" """
Given input text, returns a probability distribution over 100 document categories Given input text, returns a probability distribution over 100 document categories
+5 -3
View File
@@ -13,16 +13,18 @@ def api_handler(arg, cloud, api, batch=False, auth=None, **kwargs):
json_data = json.dumps(data) json_data = json.dumps(data)
if cloud: if cloud:
host = "%s.indico.domains" host = "%s.indico.domains" % cloud
else: else:
# default to indico public cloud # default to indico public cloud
host = config.public_api_host host = config.PUBLIC_API_HOST
url = "http://%s/%s" % (host, api) url = "http://%s/%s" % (host, api)
if batch: if batch:
url += "/batch" url += "/batch"
if not auth:
auth = config.AUTH
response = requests.post(url, data=json_data, headers=JSON_HEADERS, auth=auth).json() response = requests.post(url, data=json_data, headers=JSON_HEADERS, auth=auth).json()
results = response.get('results', False) results = response.get('results', False)
if results is False: if results is False:
+39 -2
View File
@@ -8,24 +8,36 @@ from indicoio.config import Settings
class TestConfigureEnv(unittest.TestCase): class TestConfigureEnv(unittest.TestCase):
"""
Ensure that environment variables are handled by the `Settings` parser
"""
def setUp(self): def setUp(self):
os.environ = {} os.environ = {}
def test_set_cloud_from_env_var(self): def test_set_cloud_from_env_var(self):
"""
Ensure cloud hostname is read in from environment variables
"""
cloud = "invalid/cloud" cloud = "invalid/cloud"
os.environ["INDICO_CLOUD"] = cloud os.environ["INDICO_CLOUD"] = cloud
assert config.settings.cloud() == cloud assert config.SETTINGS.cloud() == cloud
def test_set_auth_from_env_var(self): def test_set_auth_from_env_var(self):
"""
Ensure cloud authentication credentials are read in from environment variables
"""
username = "test" username = "test"
password = "password" password = "password"
os.environ["INDICO_USERNAME"] = username os.environ["INDICO_USERNAME"] = username
os.environ["INDICO_PASSWORD"] = password os.environ["INDICO_PASSWORD"] = password
assert config.settings.auth() == (username, password) assert config.SETTINGS.auth() == (username, password)
class TestConfigurationFile(unittest.TestCase): class TestConfigurationFile(unittest.TestCase):
"""
Ensure that the `Settings` parser reads in configuration files properly
"""
def setUp(self): def setUp(self):
self.username = "test" self.username = "test"
@@ -45,13 +57,22 @@ class TestConfigurationFile(unittest.TestCase):
os.environ = {} os.environ = {}
def test_set_cloud_from_config_file(self): def test_set_cloud_from_config_file(self):
"""
Ensure cloud hostname is read in from file
"""
assert self.settings.cloud() == self.cloud assert self.settings.cloud() == self.cloud
def test_set_auth_from_config_file(self): def test_set_auth_from_config_file(self):
"""
Ensure cloud authentication credentials are read in from file
"""
assert self.settings.auth() == (self.username, self.password) assert self.settings.auth() == (self.username, self.password)
class TestPrecedence(unittest.TestCase): class TestPrecedence(unittest.TestCase):
"""
Ensure that environment variables take precedence to config files
"""
def setUp(self): def setUp(self):
self.file_username = "file-username" self.file_username = "file-username"
@@ -79,13 +100,23 @@ class TestPrecedence(unittest.TestCase):
self.settings = Settings(files=[config_file]) self.settings = Settings(files=[config_file])
def test_set_cloud_from_config_file(self): def test_set_cloud_from_config_file(self):
"""
Ensure cloud hosts set in environment variables are used over those in config files
"""
assert self.settings.cloud() == self.env_cloud assert self.settings.cloud() == self.env_cloud
def test_set_auth_from_config_file(self): def test_set_auth_from_config_file(self):
"""
Ensure cloud authentication credentials set in environment variables
are used over those in config files
"""
assert self.settings.auth() == (self.env_username, self.env_password) assert self.settings.auth() == (self.env_username, self.env_password)
class TestConfigFilePrecedence(unittest.TestCase): class TestConfigFilePrecedence(unittest.TestCase):
"""
Ensure that files passed in to a `Settings` object are assigned proper priority
"""
def setUp(self): def setUp(self):
self.high_priority_username = "high-priority-username" self.high_priority_username = "high-priority-username"
@@ -132,7 +163,13 @@ class TestConfigFilePrecedence(unittest.TestCase):
]) ])
def test_cloud_config_file_priority(self): def test_cloud_config_file_priority(self):
"""
Ensure the cloud subdomain priority is handled properly
"""
assert self.settings.cloud() == self.high_priority_cloud assert self.settings.cloud() == self.high_priority_cloud
def test_auth_config_file_priority(self): def test_auth_config_file_priority(self):
"""
Ensure the cloud auth priority is handled properly
"""
assert self.settings.auth() == (self.high_priority_username, self.high_priority_password) assert self.settings.auth() == (self.high_priority_username, self.high_priority_password)
+1 -1
View File
@@ -16,7 +16,7 @@ DIR = os.path.dirname(os.path.realpath(__file__))
class BatchAPIRun(unittest.TestCase): class BatchAPIRun(unittest.TestCase):
def setUp(self): def setUp(self):
self.auth = config.auth self.auth = config.AUTH
if not all(self.auth): if not all(self.auth):
raise SkipTest raise SkipTest