# Path: context/config.py
# def configuration():
#     """Return SafeConfigParser for config file"""
#     global _config
#     if _config is None:
#         _config = ConfigParser.SafeConfigParser()
#         path = config_file_path()
#         with open(path) as f:
#             _config.readfp(f)
#     return _config
# 
# Path: context/query.py
# def twitter_query(keywords=None, entities=None):
#     """
#     Form a twitter query string from keywords and/or entities.
#     See: https://dev.twitter.com/rest/public/search
# 
#     Testing shows that OR has precedence over AND:
# 
#     a b OR c d      is          a AND (b OR c) AND d
#                     is not      (a AND b) OR (c AND d)
# 
#     a OR b c OR d   is          (a OR b) AND (c OR d)
#                     is not      a OR (b AND c) OR d
# 
#     For example, the query:
# 
#         free OR good beer OR shirt
# 
#     is interpreted as:
# 
#         (free OR good) AND (beer OR shirt)
# 
#     so it will return tweets containing:
# 
#                 "free" and "beer"
#         or      "free" and "shirt"
#         or      "good" and "beer"
#         or      "good" and "shirt"
# 
#     """
#     q_entities = []
#     if entities:
#         q_entities = collapse_entities(
#             [d for d in entities if d['score'] > 0.01])
# 
#     q_keywords = []
#     if keywords:
#         q_keywords = [d['keyword'] for d in keywords \
#             if d['count'] > 2 and d['keyword'] not in q_entities]
# 
#     q_keywords = quoted_terms(q_keywords[:5])
#     q_entities = quoted_terms(q_entities[:5])
#     q = ''
# 
#     if q_keywords and q_entities:
#         # at least one keyword and at least one entity
#         q = '%s %s' % \
#             (' OR '.join(q_keywords), ' OR '.join(q_entities))
#     elif q_keywords:
#         # top keyword and at least one other keyword
#         q = q_keywords.pop(0)
#         if len(q_keywords) > 0:
#             q += ' %s' % ' OR '.join(q_keywords)
#     elif q_entities:
#         # top entity and at least one other entity
#         q = q_entities.pop(0)
#         if len(q_entities) > 0:
#             q += ' %s' % ' OR '.join(q_entities)
#     return q
# 
# Path: context/config.py
# def get_twitter_keys(section='context'):
#     """Return namedtuple of Twitter config data"""
#     config = configuration()
#     TwitterKeys = collections.namedtuple('TwitterKeys', [
#         'consumer_key',
#         'consumer_secret',
#         'access_token',
#         'access_token_secret'])
#     k = TwitterKeys(
#         config.get(section, 'twitter_consumer_key'),
#         config.get(section, 'twitter_consumer_secret'),
#         config.get(section, 'twitter_access_token'),
#         config.get(section, 'twitter_access_token_secret'))
#     return k
# 
# Path: context/services/twitter.py
# def search(params, credentials=None):
#     """
#     Execute twitter search
#     
#     @params = dictionary of search parameters
#     @credentials = dictionary containing token/secret overrides
#     """
#     client = get_client(credentials)
#     return client.api.search.tweets.get(**params).data


import re
from context.config import configuration
from context.query import twitter_query
from context.config import get_twitter_keys
from context.services.twitter import search as twitter_search


CONFIG_LIST_REGEX = re.compile(r'[, \s]+')


def get_category_pundits(category):
    """Return list of pundits in category"""
    config = configuration()
    pundit_panel = config.get('punditpanels', category)
    return [s.strip() for s in
        CONFIG_LIST_REGEX.split(pundit_panel) if s.strip()]


def get_pundit_categories():
    """Return list of pundit categories"""

# Path: context/config.py
# def configuration():
#     """Return SafeConfigParser for config file"""
#     global _config
#     if _config is None:
#         _config = ConfigParser.SafeConfigParser()
#         path = config_file_path()
#         with open(path) as f:
#             _config.readfp(f)
#     return _config
# 
# Path: context/query.py
# def twitter_query(keywords=None, entities=None):
#     """
#     Form a twitter query string from keywords and/or entities.
#     See: https://dev.twitter.com/rest/public/search
# 
#     Testing shows that OR has precedence over AND:
# 
#     a b OR c d      is          a AND (b OR c) AND d
#                     is not      (a AND b) OR (c AND d)
# 
#     a OR b c OR d   is          (a OR b) AND (c OR d)
#                     is not      a OR (b AND c) OR d
# 
#     For example, the query:
# 
#         free OR good beer OR shirt
# 
#     is interpreted as:
# 
#         (free OR good) AND (beer OR shirt)
# 
#     so it will return tweets containing:
# 
#                 "free" and "beer"
#         or      "free" and "shirt"
#         or      "good" and "beer"
#         or      "good" and "shirt"
# 
#     """
#     q_entities = []
#     if entities:
#         q_entities = collapse_entities(
#             [d for d in entities if d['score'] > 0.01])
# 
#     q_keywords = []
#     if keywords:
#         q_keywords = [d['keyword'] for d in keywords \
#             if d['count'] > 2 and d['keyword'] not in q_entities]
# 
#     q_keywords = quoted_terms(q_keywords[:5])
#     q_entities = quoted_terms(q_entities[:5])
#     q = ''
# 
#     if q_keywords and q_entities:
#         # at least one keyword and at least one entity
#         q = '%s %s' % \
#             (' OR '.join(q_keywords), ' OR '.join(q_entities))
#     elif q_keywords:
#         # top keyword and at least one other keyword
#         q = q_keywords.pop(0)
#         if len(q_keywords) > 0:
#             q += ' %s' % ' OR '.join(q_keywords)
#     elif q_entities:
#         # top entity and at least one other entity
#         q = q_entities.pop(0)
#         if len(q_entities) > 0:
#             q += ' %s' % ' OR '.join(q_entities)
#     return q
# 
# Path: context/config.py
# def get_twitter_keys(section='context'):
#     """Return namedtuple of Twitter config data"""
#     config = configuration()
#     TwitterKeys = collections.namedtuple('TwitterKeys', [
#         'consumer_key',
#         'consumer_secret',
#         'access_token',
#         'access_token_secret'])
#     k = TwitterKeys(
#         config.get(section, 'twitter_consumer_key'),
#         config.get(section, 'twitter_consumer_secret'),
#         config.get(section, 'twitter_access_token'),
#         config.get(section, 'twitter_access_token_secret'))
#     return k
# 
# Path: context/services/twitter.py
# def search(params, credentials=None):
#     """
#     Execute twitter search
#     
#     @params = dictionary of search parameters
#     @credentials = dictionary containing token/secret overrides
#     """
#     client = get_client(credentials)
#     return client.api.search.tweets.get(**params).data


# Path: web/pundits.py
import re
from context.config import configuration
from context.query import twitter_query
from context.config import get_twitter_keys
from context.services.twitter import search as twitter_search



CONFIG_LIST_REGEX = re.compile(r'[, \s]+')


def get_category_pundits(category):
    """Return list of pundits in category"""
    config = configuration()
    pundit_panel = config.get('punditpanels', category)
    return [s.strip() for s in
        CONFIG_LIST_REGEX.split(pundit_panel) if s.strip()]


def get_pundit_categories():
    """Return list of pundit categories"""

    config = configuration()