# -*- coding: utf-8 -*-
"""
Topic search application file.

Usage:
    $ python -m lib.query.search.topic --help
"""
from lib import database as db


def topic(searchStr='', orderByVol=False):
    """
    Search existing trends records in the db for topics matching the input
    string.

    Searches are case insensitive.

    @param searchStr: word or phrase as a string for text to search in the
        topic column of Trend table. Leave as default empty string to
        not filter results. Multi-word searches are not possible except as
        phrases.
    @orderByVol: Default False. If True, order by volumne descending instead
        of topic alphabetically.
    """
    orderBy = 'MaxVol DESC' if orderByVol else 'Trend.topic ASC'
    query = """
        SELECT Trend.topic, MAX(Trend.volume) AS MaxVol
        FROM Trend
        WHERE Trend.topic LIKE '%{0}%'
        GROUP BY Trend.topic
        ORDER BY {1}
    """.format(searchStr, orderBy)

    res = db.conn.queryAll(query)

    # Note that volume can be added up, but any null values will not be counted.
    print 'Max Volume | Topic'
    for item in res:
        # Making u'' causes errors for some reason for "Динамо"
        print '{0:10,d} | {1}'.format(item[1] if item[1] else -1, item[0])


def main(args):
    # TO DO - add order by vol as option

    if not args or set(args) & set(('-h', '--help')):
        print 'Usage: python -m lib.query.search.topic [searchStr]'
        print 'Options and arguments:'
        print 'searchStr  : Enter a topic to search for in the database. Enter'
        print '             as `ALL` to show all records.'
    else:
        searchStr = '' if args[0] == 'ALL' else args[0]
        topic(searchStr, orderByVol=True)


if __name__ == '__main__':
    import sys
    main(sys.argv[1:])

# -*- coding: utf-8 -*-
"""
Topic search application file.

Usage:
    $ python -m lib.query.search.topic --help
"""
from lib import database as db


def topic(searchStr='', orderByVol=False):
    """
    Search existing trends records in the db for topics matching the input
    string.

    Searches are case insensitive.

    @param searchStr: word or phrase as a string for text to search in the
        topic column of Trend table. Leave as default empty string to
        not filter results. Multi-word searches are not possible except as
        phrases.
    @orderByVol: Default False. If True, order by volumne descending instead
        of topic alphabetically.
    """
    orderBy = 'MaxVol DESC' if orderByVol else 'Trend.topic ASC'
    query = """
        SELECT Trend.topic, MAX(Trend.volume) AS MaxVol
        FROM Trend
        WHERE Trend.topic LIKE '%{0}%'
        GROUP BY Trend.topic
        ORDER BY {1}
    """.format(searchStr, orderBy)

    res = db.conn.queryAll(query)

    # Note that volume can be added up, but any null values will not be counted.
    print 'Max Volume | Topic'
    for item in res:
        # Making u'' causes errors for some reason for "Динамо"
        print '{0:10,d} | {1}'.format(item[1] if item[1] else -1, item[0])


def main(args):
    # TO DO - add order by vol as option

    if not args or set(args) & set(('-h', '--help')):
        print 'Usage: python -m lib.query.search.topic [searchStr] [-h|--help]'
        print 'Options and arguments:'
        print '--help    : Show help.'
        print 'searchStr : Enter a topic to search for in the database. Enter'
        print '            as `ALL` to show all records.'
    else:
        searchStr = '' if args[0] == 'ALL' else args[0]
        topic(searchStr, orderByVol=True)


if __name__ == '__main__':
    import sys
    main(sys.argv[1:])

Improve topic search command line.