<commit_before>#!/usr/bin/python

"""Command line to dump ROOT files contents to terminal"""

from cmdLineUtils import *

ANSI_BOLD = "\x1B[1m"
ANSI_BLUE = "\x1B[34m"
ANSI_GREEN = "\x1B[32m"
ANSI_END = "\x1B[0m"

def isTerminal():
    """Return True if the output is a terminal"""
    return sys.stdout.isatty()

def isWin32():
    """Return True if the platform is Windows"""
    return sys.platform == 'win32'

def isSpecial(ansiCode,string):
    """Use ansi code on 'string' if the output is the
    terminal of a not Windows platform"""
    if isTerminal() and not isWin32(): return ansiCode+string+ANSI_END
    else: return string

def write(string,indent=0,end=""):
    """Use sys.stdout.write to write the string with an indentation
    equal to indent and specifying the end character"""
    sys.stdout.write(" "*indent+string+end)

TREE_TEMPLATE = "{0:{nameWidth}}"+"{1:{titleWidth}}{2:{memoryWidth}}"

def recursifTreePrinter(tree,indent):
    """Print recursively tree informations"""
    listOfBranches = tree.GetListOfBranches()
    if len(listOfBranches) > 0: # Width informations
        maxCharName = max([len(branch.GetName()) \
            for branch in listOfBranches])
        maxCharTitle = max([len(branch.GetTitle()) \
            for branch in listOfBranches])
        dic = { \
            "nameWidth":maxCharName+2, \
            "titleWidth":maxCharTitle+4, \
            "memoryWidth":1}
    for branch in listOfBranches: # Print loop
        rec = \
            [branch.GetName(), \
            "\""+branch.GetTitle()+"\"", \
            str(branch.GetTotBytes())]
        write(TREE_TEMPLATE.format(*rec,**dic),indent,end="\n")
        recursifTreePrinter(branch,indent+2)

def prepareTime(time):
    """Get time in the proper shape
    ex : 174512 for 17h 45m 12s
    ex : 094023 for 09h 40m 23s"""
    time = str(time)
    time = '000000'+time
    time = time[len(time)-6:]
    return time

MONTH = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun', \
         7:'Jul',8:'Aug',9:'Sep',10:'Oct',11:'Nov',12:'Dec'}
LONG_TEMPLATE = \
    isSpecial(ANSI_BOLD,"{0:{classWidth}}")+"{1:{timeWidth}}" + \
    "{2:{nameWidth}}{3:{titleWidth}}"

def roolsPrintLongLs(keyList,optDict,indent):
    """Print a list of Tkey in columns
    pattern : classname, datetime, name and title"""
    if len(keyList) > 0: # Width informations
        maxCharClass = max([len(key.GetClassName()) for key in keyList])
        maxCharTime = 12
        maxCharName = max([len(key.GetName()) for key in keyList])
        dic = { \
            "classWidth":maxCharClass+2, \
            "timeWidth":maxCharTime+2, \
            "nameWidth":maxCharName+2, \
            "titleWidth":1}
    date = ROOT.Long(0)
    for key in keyList:
        time = ROOT.Long(0)
        key.GetDatime().GetDateTime(key.GetDatime().Get(),date,time)
        time = prepareTime(time)
        rec = \
            [key.GetClassName(), \
            MONTH[int(str(date)[4:6])]+" " +str(date)[6:]+ \
            " "+time[:2]+":"+time[2:4], \
            key.GetName(), \
            "\""+key.GetTitle()+"\""]
        write(LONG_TEMPLATE.format(*rec,**dic),indent,end="\n")
        if optDict['tree'] and isTreeKey(key):
            tree = key.ReadObj()
            recursifTreePrinter(tree,indent+2)

##
# The code of the getTerminalSize function can be found here : https://gist.github.com/jtriley/1108174
# Thanks jtriley !!

import os
import shlex
import struct
import platform
import subprocess

def getTerminalSize():
    """ getTerminalSize()
     - get width and height of console
     - works on linux,os x,windows,cygwin(windows)
     originally retrieved from:
     http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python"""
    current_os = platform.system()
    tuple_xy = None
    if current_os == 'Windows':
        tuple_xy = _get_terminal_size_windows()
        if tuple_xy is None:
            tuple_xy = _get_terminal_size_tput()
            # needed for window's python in cygwin's xterm!
    if current_os in ['Linux', 'Darwin'] or current_os.startswith('CYGWIN'):
        tuple_xy = _get_terminal_size_linux()
    if tuple_xy is None:
        #print "default"
        #_get_terminal_size_windows() or _get_terminal_size_tput don't work
        tuple_xy = (80, 25)      # default value
    return tuple_xy

def _get_terminal_size_windows():
    try:
        from ctypes import windll, create_string_buffer
        # stdin handle is -10
        # stdout handle is -11
        # stderr handle is -12
        h = windll.kernel32.GetStdHandle(-12)
        csbi = create_string_buffer(22)
        res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
        if res:
            (bufx, bufy, curx, cury, wattr,
             left, top, right, bottom,
             maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
            sizex = right - left + 1
            sizey = bottom - top + 1
            return sizex, sizey
    except:
        pass

def _get_terminal_size_tput():
    # get terminal width
    # src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
    try:
        cols = int(subprocess.check_call(shlex.split('tput cols')))
        rows = int(subprocess.check_call(shlex.split('tput lines')))
        return (cols, rows)
    except:
        pass

def _get_terminal_size_linux():
    def ioctl_GWINSZ(fd):
        try:
            import fcntl
            import termios
            cr = struct.unpack('hh',
                               fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
            return cr
        except:
            pass
    cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
    if not cr:
        try:
            fd = os.open(os.ctermid(), os.O_RDONLY)
            cr = ioctl_GWINSZ(fd)
            os.close(fd)
        except:
            pass
    if not cr:
        try:
            cr = (os.environ['LINES'], os.environ['COLUMNS'])
        except:
            return None
    return int(cr[1]), int(cr[0])

# End of getTerminalSize code
##

def roolsPrintSimpleLs(keyList,indent):
    """Print list of strings in columns
    - blue for directories
    - green for trees"""
    # This code is adaptated from the pprint_list function here :
    # http://stackoverflow.com/questions/25026556/output-list-like-ls
    # Thanks hawkjo !!
    if len(keyList) == 0: return
    (term_width, term_height) = getTerminalSize()
    term_width = term_width - indent
    min_chars_between = 2
    min_element_width = min( len(key.GetName()) for key in keyList ) \
                        + min_chars_between
    max_element_width = max( len(key.GetName()) for key in keyList ) \
                        + min_chars_between
    if max_element_width >= term_width: ncol,col_widths = 1,[1]
    else:
        # Start with max possible number of columns and reduce until it fits
        ncol = min( len(keyList), term_width / min_element_width  )
        while True:
            col_widths = \
                [ max( len(key.GetName()) + min_chars_between \
                for j, key in enumerate(keyList) if j % ncol == i ) \
                for i in range(ncol) ]
            if sum( col_widths ) <= term_width: break
            else: ncol -= 1
    for i, key in enumerate(keyList):
        if i%ncol == 0: write("",indent) # indentation
        # Don't add spaces after the last element of the line or of the list
        if (i+1)%ncol != 0 and i != len(keyList)-1:
            if not isTerminal(): write( \
                key.GetName().ljust(col_widths[i%ncol]))
            elif isDirectoryKey(keyList[i]): write( \
                isSpecial(ANSI_BLUE,key.GetName()).ljust(col_widths[i%ncol] + 9))
                # len(ANSI_BLUE+ANSI_END) = len("\x1B[34m"+"\x1B[0m") = 9
            elif isTreeKey(keyList[i]): write( \
                isSpecial(ANSI_GREEN,key.GetName()).ljust(col_widths[i%ncol] + 9))
                # len(ANSI_GREEN+ANSI_END) = len("\x1B[32m"+"\x1B[0m") = 9
            else: write(key.GetName().ljust(col_widths[i%ncol]))
        else: # No spaces after the last element of the line or of the list
            if not isTerminal(): write(key.GetName())
            elif isDirectoryKey(keyList[i]): write(isSpecial(ANSI_BLUE,key.GetName()))
            elif isTreeKey(keyList[i]): write(isSpecial(ANSI_GREEN,key.GetName()))
            else: write(key.GetName())
            write('\n')

def roolsPrint(keyList,optDict,indent=0):
    """Print informations given by keyList with a rools
    style choosen with optDict"""
    if optDict['long'] or optDict['tree']: \
       roolsPrintLongLs(keyList,optDict,indent)
    else: roolsPrintSimpleLs(keyList,indent)

# Help strings
COMMAND_HELP = \
    "Display ROOT files contents in the terminal " + \
    "(for more informations please look at the man page)."
LONG_PRINT_HELP = \
    "use a long listing format."
TREE_PRINT_HELP = \
    "print tree recursively and use a long listing format."

##### Beginning of the main code #####

# Collect arguments with the module argparse
parser = argparse.ArgumentParser(description=COMMAND_HELP)
parser.add_argument("sourcePatternList", help=SOURCES_HELP, nargs='+')
parser.add_argument("-l", "--long", help=LONG_PRINT_HELP, action="store_true")
parser.add_argument("-t", "--tree", help=TREE_PRINT_HELP, action="store_true")
args = parser.parse_args()

# Create a list of tuples that contain source ROOT file names
# and lists of path in these files
sourceList = \
    [tup for pattern in args.sourcePatternList \
    for tup in patternToFileNameAndPathSplitList(pattern)]

# Create a dictionnary with options
optDict = vars(args)

# Initialize a boolean and indent level
manySources = len(sourceList) > 1
indent = 2 if manySources else 0

# Loop on the ROOT files
first_round_file = True
for fileName, pathSplitList in sourceList:
    with stderrRedirected():
        rootFile = ROOT.TFile.Open(fileName)
    objList,dirList = keyClassSpliter(rootFile,pathSplitList)
    keyList = [getKey(rootFile,pathSplit) for pathSplit in objList]
    keyList.sort()
    dirList.sort()

    # Paths of file
    if manySources: write("{0} :".format(fileName)+"\n")

    # Print with the rools style
    roolsPrint(keyList,optDict,indent)

    # Initialize a boolean and indent directory level
    manyPathSplits = len(pathSplitList) > 1
    indentDir = 2 if manyPathSplits else 0

    # Loop on the directories
    for pathSplit in dirList:
        keyList = getKeyList(rootFile,pathSplit)
        keyList.sort()

        # Paths of object
        if manyPathSplits:
            write("{0} :".format("/".join(pathSplit)),indent,end="\n")

        # Print with the rools style
        roolsPrint(keyList,optDict,indent+indentDir)

    rootFile.Close()
<commit_msg>Add the -1 option<commit_after>#!/usr/bin/python

"""Command line to dump ROOT files contents to terminal"""

from cmdLineUtils import *

ANSI_BOLD = "\x1B[1m"
ANSI_BLUE = "\x1B[34m"
ANSI_GREEN = "\x1B[32m"
ANSI_END = "\x1B[0m"

def isTerminal():
    """Return True if the output is a terminal"""
    return sys.stdout.isatty()

def isWin32():
    """Return True if the platform is Windows"""
    return sys.platform == 'win32'

def isSpecial(ansiCode,string):
    """Use ansi code on 'string' if the output is the
    terminal of a not Windows platform"""
    if isTerminal() and not isWin32(): return ansiCode+string+ANSI_END
    else: return string

def write(string,indent=0,end=""):
    """Use sys.stdout.write to write the string with an indentation
    equal to indent and specifying the end character"""
    sys.stdout.write(" "*indent+string+end)

TREE_TEMPLATE = "{0:{nameWidth}}"+"{1:{titleWidth}}{2:{memoryWidth}}"

def recursifTreePrinter(tree,indent):
    """Print recursively tree informations"""
    listOfBranches = tree.GetListOfBranches()
    if len(listOfBranches) > 0: # Width informations
        maxCharName = max([len(branch.GetName()) \
            for branch in listOfBranches])
        maxCharTitle = max([len(branch.GetTitle()) \
            for branch in listOfBranches])
        dic = { \
            "nameWidth":maxCharName+2, \
            "titleWidth":maxCharTitle+4, \
            "memoryWidth":1}
    for branch in listOfBranches: # Print loop
        rec = \
            [branch.GetName(), \
            "\""+branch.GetTitle()+"\"", \
            str(branch.GetTotBytes())]
        write(TREE_TEMPLATE.format(*rec,**dic),indent,end="\n")
        recursifTreePrinter(branch,indent+2)

def prepareTime(time):
    """Get time in the proper shape
    ex : 174512 for 17h 45m 12s
    ex : 094023 for 09h 40m 23s"""
    time = str(time)
    time = '000000'+time
    time = time[len(time)-6:]
    return time

MONTH = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun', \
         7:'Jul',8:'Aug',9:'Sep',10:'Oct',11:'Nov',12:'Dec'}
LONG_TEMPLATE = \
    isSpecial(ANSI_BOLD,"{0:{classWidth}}")+"{1:{timeWidth}}" + \
    "{2:{nameWidth}}{3:{titleWidth}}"

def roolsPrintLongLs(keyList,optDict,indent):
    """Print a list of Tkey in columns
    pattern : classname, datetime, name and title"""
    if len(keyList) > 0: # Width informations
        maxCharClass = max([len(key.GetClassName()) for key in keyList])
        maxCharTime = 12
        maxCharName = max([len(key.GetName()) for key in keyList])
        dic = { \
            "classWidth":maxCharClass+2, \
            "timeWidth":maxCharTime+2, \
            "nameWidth":maxCharName+2, \
            "titleWidth":1}
    date = ROOT.Long(0)
    for key in keyList:
        time = ROOT.Long(0)
        key.GetDatime().GetDateTime(key.GetDatime().Get(),date,time)
        time = prepareTime(time)
        rec = \
            [key.GetClassName(), \
            MONTH[int(str(date)[4:6])]+" " +str(date)[6:]+ \
            " "+time[:2]+":"+time[2:4], \
            key.GetName(), \
            "\""+key.GetTitle()+"\""]
        write(LONG_TEMPLATE.format(*rec,**dic),indent,end="\n")
        if optDict['tree'] and isTreeKey(key):
            tree = key.ReadObj()
            recursifTreePrinter(tree,indent+2)

##
# The code of the getTerminalSize function can be found here : https://gist.github.com/jtriley/1108174
# Thanks jtriley !!

import os
import shlex
import struct
import platform
import subprocess

def getTerminalSize():
    """ getTerminalSize()
     - get width and height of console
     - works on linux,os x,windows,cygwin(windows)
     originally retrieved from:
     http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python"""
    current_os = platform.system()
    tuple_xy = None
    if current_os == 'Windows':
        tuple_xy = _get_terminal_size_windows()
        if tuple_xy is None:
            tuple_xy = _get_terminal_size_tput()
            # needed for window's python in cygwin's xterm!
    if current_os in ['Linux', 'Darwin'] or current_os.startswith('CYGWIN'):
        tuple_xy = _get_terminal_size_linux()
    if tuple_xy is None:
        #print "default"
        #_get_terminal_size_windows() or _get_terminal_size_tput don't work
        tuple_xy = (80, 25)      # default value
    return tuple_xy

def _get_terminal_size_windows():
    try:
        from ctypes import windll, create_string_buffer
        # stdin handle is -10
        # stdout handle is -11
        # stderr handle is -12
        h = windll.kernel32.GetStdHandle(-12)
        csbi = create_string_buffer(22)
        res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
        if res:
            (bufx, bufy, curx, cury, wattr,
             left, top, right, bottom,
             maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
            sizex = right - left + 1
            sizey = bottom - top + 1
            return sizex, sizey
    except:
        pass

def _get_terminal_size_tput():
    # get terminal width
    # src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
    try:
        cols = int(subprocess.check_call(shlex.split('tput cols')))
        rows = int(subprocess.check_call(shlex.split('tput lines')))
        return (cols, rows)
    except:
        pass

def _get_terminal_size_linux():
    def ioctl_GWINSZ(fd):
        try:
            import fcntl
            import termios
            cr = struct.unpack('hh',
                               fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
            return cr
        except:
            pass
    cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
    if not cr:
        try:
            fd = os.open(os.ctermid(), os.O_RDONLY)
            cr = ioctl_GWINSZ(fd)
            os.close(fd)
        except:
            pass
    if not cr:
        try:
            cr = (os.environ['LINES'], os.environ['COLUMNS'])
        except:
            return None
    return int(cr[1]), int(cr[0])

# End of getTerminalSize code
##

def roolsPrintSimpleLs(keyList,indent, oneColumn):
    """Print list of strings in columns
    - blue for directories
    - green for trees"""
    # This code is adaptated from the pprint_list function here :
    # http://stackoverflow.com/questions/25026556/output-list-like-ls
    # Thanks hawkjo !!
    if len(keyList) == 0: return
    (term_width, term_height) = getTerminalSize()
    term_width = term_width - indent
    min_chars_between = 2
    min_element_width = min( len(key.GetName()) for key in keyList ) \
                        + min_chars_between
    max_element_width = max( len(key.GetName()) for key in keyList ) \
                        + min_chars_between
    if max_element_width >= term_width: ncol,col_widths = 1,[1]
    else:
        # Start with max possible number of columns and reduce until it fits
        ncol = min( len(keyList), term_width / min_element_width  )
        if oneColumn: ncol =1
        else:
         while True:
               col_widths = \
                  [ max( len(key.GetName()) + min_chars_between \
                  for j, key in enumerate(keyList) if j % ncol == i ) \
                  for i in range(ncol) ]
               if sum( col_widths ) <= term_width: break
               else: ncol -= 1
    for i, key in enumerate(keyList):
        if i%ncol == 0: write("",indent) # indentation
        # Don't add spaces after the last element of the line or of the list
        if (i+1)%ncol != 0 and i != len(keyList)-1:
            if not isTerminal(): write( \
                key.GetName().ljust(col_widths[i%ncol]))
            elif isDirectoryKey(keyList[i]): write( \
                isSpecial(ANSI_BLUE,key.GetName()).ljust(col_widths[i%ncol] + 9))
                # len(ANSI_BLUE+ANSI_END) = len("\x1B[34m"+"\x1B[0m") = 9
            elif isTreeKey(keyList[i]): write( \
                isSpecial(ANSI_GREEN,key.GetName()).ljust(col_widths[i%ncol] + 9))
                # len(ANSI_GREEN+ANSI_END) = len("\x1B[32m"+"\x1B[0m") = 9
            else: write(key.GetName().ljust(col_widths[i%ncol]))
        else: # No spaces after the last element of the line or of the list
            if not isTerminal(): write(key.GetName())
            elif isDirectoryKey(keyList[i]): write(isSpecial(ANSI_BLUE,key.GetName()))
            elif isTreeKey(keyList[i]): write(isSpecial(ANSI_GREEN,key.GetName()))
            else: write(key.GetName())
            write('\n')

def roolsPrint(keyList,optDict,indent=0):
    """Print informations given by keyList with a rools
    style choosen with optDict"""
    if optDict['long'] or optDict['tree']: \
       roolsPrintLongLs(keyList,optDict,indent)
    else:
       oneColumn = True if optDict['one'] else False
       roolsPrintSimpleLs(keyList,indent, oneColumn)

# Help strings
COMMAND_HELP = \
    "Display ROOT files contents in the terminal " + \
    "(for more informations please look at the man page)."
LONG_PRINT_HELP = \
    "use a long listing format."
TREE_PRINT_HELP = \
    "print tree recursively and use a long listing format."

##### Beginning of the main code #####

# Collect arguments with the module argparse
parser = argparse.ArgumentParser(description=COMMAND_HELP)
parser.add_argument("sourcePatternList", help=SOURCES_HELP, nargs='+')
parser.add_argument("-l", "--long", help=LONG_PRINT_HELP, action="store_true")
parser.add_argument("-t", "--tree", help=TREE_PRINT_HELP, action="store_true")
parser.add_argument("-1", "--one", help="Print content in one column", action="store_true")
args = parser.parse_args()

# Create a list of tuples that contain source ROOT file names
# and lists of path in these files
sourceList = \
    [tup for pattern in args.sourcePatternList \
    for tup in patternToFileNameAndPathSplitList(pattern)]

# Create a dictionnary with options
optDict = vars(args)

# Initialize a boolean and indent level
manySources = len(sourceList) > 1
indent = 2 if manySources else 0

# Loop on the ROOT files
first_round_file = True
for fileName, pathSplitList in sourceList:
    with stderrRedirected():
        rootFile = ROOT.TFile.Open(fileName)
    objList,dirList = keyClassSpliter(rootFile,pathSplitList)
    keyList = [getKey(rootFile,pathSplit) for pathSplit in objList]
    keyList.sort()
    dirList.sort()

    # Paths of file
    if manySources: write("{0} :".format(fileName)+"\n")

    # Print with the rools style
    roolsPrint(keyList,optDict,indent)

    # Initialize a boolean and indent directory level
    manyPathSplits = len(pathSplitList) > 1
    indentDir = 2 if manyPathSplits else 0

    # Loop on the directories
    for pathSplit in dirList:
        keyList = getKeyList(rootFile,pathSplit)
        keyList.sort()

        # Paths of object
        if manyPathSplits:
            write("{0} :".format("/".join(pathSplit)),indent,end="\n")

        # Print with the rools style
        roolsPrint(keyList,optDict,indent+indentDir)

    rootFile.Close()
<|endoftext|>


<commit_before>from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth.models import AnonymousUser, User
from .queryGoogleForDirectionsv5 import return_travel_info
from .apiai import *
from .yelpfusionapi import *
import json, re

from random import randint

FIRST_TIME_MSG = "Welcome to CharlieChat! Are you new to the area?"
INTENT_CLARIFICATION = ["That doesn't quite make sense, could you word that differently?",\
                        "I'm not sure what you mean. Could you say that again?"]
TROUBLE = ["I seem to be having trouble, could you try again later?", "Uh oh, I'm having difficulties. Try again later.", \
        "I can't seem to connect, try again some time later.", "Oh no, something's wrong on my end. Could you try again later?", \
        'I don\'t seem to be working right now! Try something else, or try again later.']

def msg(msg_type,message):
    return {'author':msg_type,'msg':message}

def cc_msg(message):
    return msg('charliechat',message)

def user_msg(message):
    return msg('user',message)

def appendToDestHistory(request,start,dest):
    request.session['_historyDestinations'].append((start,dest))

def random_choice(responses):
    return responses[randint(0,len(responses) - 1 )]

def assemble_instructions_map(segments):
    code = ""

    for segment in segments:
        step = segment['html_instructions']
        print(step)
        print(segment)
        print("*****")
        if segment['travel_mode'] == 'WALKING':
            code += "
{} (about {})
".format(step,segment['duration']['text'])
        else:
            transit = segment['transit_details']
            depart = transit['departure_stop']['name']

            if 'color' in transit['line']:
                color = transit['line']['color']
                code += "
  ".format(color)
            else:
                code += "
"

            mode = transit['line']['vehicle']['type'].lower()

            if mode == 'bus':
                number = transit['line']['short_name']
                code += "{} {} from {} for {} stops".format(number,step,depart,transit['num_stops'])
            elif mode == 'heavy_rail':
                line = transit['line']['name']
                code += "Take the {} from {} towards {} for {} stops".format(line,depart,transit['headsign'],transit['num_stops'])
            elif mode == 'subway':
                line_name = transit['line']['name']
                code += "Take the {} from {} towards {} for {} stops".format(line_name,depart,transit['headsign'],transit['num_stops'])
            elif mode == 'tram':
                train_letter = transit['line']['short_name']
                code += "Take green line {} train from {} towards {} for {} stops".format(train_letter,depart,transit['headsign'],transit['num_stops'])
            else:
                code += step

            code += " ({})
".format(segment['duration']['text'])

    return code

def decide_intent(intent,response,request,query,geo_loc):
    #TODO: may need to break down into separate methods
    if intent == "help":
        request.session['_messages'].append(cc_msg("That's a great question! I do a lot of things."))
        request.session['_messages'].append(cc_msg("Ask me how to get somewhere, how to get from point A to point B, how much that costs, or how long that'll take."))
    # intent is to navigate
    elif intent == "direction":
        address1, address2 = get_addresses_from_response(response)
        if not address2:
            # if you've already asked things, assume the previous
            lastQuery = request.session['_historyQueries'][-1]
            if lastQuery['MBTA directions'] == 'Directions not found':
                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
            else:
                request.session['_messages'].append({'author':'charliechat', \
                    'msg':"Arrival time is {} if you leave at {}.".format(lastQuery['MBTA arrival time'],lastQuery['MBTA depart time'])})
                request.session['_messages'].append(cc_msg(assemble_instructions_map(lastQuery['Route segments'])))
        else:
            if (validate(address1) == "current_loc"):
                address1 = geo_loc

            info_dict = return_travel_info(address1,address2)
            if address1 == geo_loc:
                address1 = "current_loc" # make it easier to check historyDestinations later on
            appendToDestHistory(request,address1,address2)

            if info_dict['MBTA directions'] == 'Directions not found':
                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
            else:
                request.session['_messages'].append({'author':'charliechat', \
                    'msg':"Arrival time is {} if you leave at {}.".format(info_dict['MBTA arrival time'],info_dict['MBTA depart time'])})
                request.session['_messages'].append(cc_msg(assemble_instructions_map(info_dict['Route segments'])))
            request.session['_historyQueries'].append(info_dict)
            # if they asked for the price previously, also give the price
            if 'getCost' in request.session['_unfinished']:
                price = info_dict['MBTA price']
                if price != 'Directions not found':
                    request.session['_messages'].append(cc_msg('And the total cost of this trip is ${0:.2f}.'.format(price)))
                else:
                    request.session['_messages'].append(cc_msg('I can\'t seem to find any prices at the moment.'))
                del request.session['_unfinished']['getCost']
            elif 'lengthTime' in request.session['_unfinished']:
                duration = info_dict['MBTA duration']
                if duration != 'Directions not found':
                    request.session['_messages'].append(cc_msg('And the total duration of this trip would be {0}.'.format(duration)))
                else:
                    request.session['_messages'].append(cc_msg('I can\'t seem to calculate the time right now.'))
                del request.session['_unfinished']['lengthTime']
        return HttpResponseRedirect(reverse('index'))
    # get the cost
    elif intent == "getCost":
        # if asking for fare to a place
        if response['result']['parameters']['address2'] != '':
            address1, address2 = get_addresses_from_response(response)
            appendToDestHistory(request,address1,address2)
            if (validate(address1) == "current_loc"):
                address1 = geo_loc
            info_dict = return_travel_info(address1,address2)
            request.session['_historyQueries'].append(info_dict)
            price = info_dict['MBTA price']
            if price == 'Directions not found':
                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
            else:
                if (validate(address1) == geo_loc):
                    address1 = "your current location"
                request.session['_messages'].append(cc_msg('It\'ll cost you ${0:.2f} to go from {1} to {2}.'.format(price,address1,address2)))
        # if the previous request or the one before was a navigation, assume we're getting that cost
        elif request.session['_historyIntents'][-2] == "direction" or request.session['_historyIntents'][-3] == "direction":
            price = request.session['_historyQueries'][-1]['MBTA price']
            if price != 'Directions not found':
                destination = request.session['_historyDestinations'][-1][1]
                request.session['_messages'].append(cc_msg('Your trip to {0} is going to cost ${1:.2f} with an adult CharlieCard.'.format(destination,(price))))
            else:
                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
        # if it was a while back, ask if that's the one
        elif 'direction' in request.session['_historyIntents'] or 'lengthTime' in request.session['_historyIntents']:
            request.session['_messages'].append(cc_msg('Do you mean the cost of your trip to {}?'.format(request.session['_historyDestinations'][-1][1])))
            # remember unfinished so agree intent is handled properly later
            print(request.session['_unfinished'])
            request.session['_unfinished']['getCost'] = 'farBack'
        else:
            request.session['_messages'].append(cc_msg('From where to where?'))
            request.session['_unfinished']['getCost'] = 'needTrip'
        return HttpResponseRedirect(reverse('index'))
    # asked how long it takes to get somewhere
    elif intent == "lengthTime":
        # if they want duration to a specified place
        if response['result']['parameters']['address2'] != '':
            address1, address2 = get_addresses_from_response(response)
            appendToDestHistory(request,address1,address2)
            if (validate(address1) == "current_loc"):
                address1 = geo_loc
            info_dict = return_travel_info(address1,address2)
            duration = info_dict['MBTA duration']
            request.session['_historyQueries'].append(info_dict)
            if duration == 'Directions not found':
                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
            else:
                if (validate(address1) == geo_loc):
                    address1 = "where you currently are"
                request.session['_messages'].append(cc_msg('It\'ll take you {0} to go from {1} to {2}.'.format(duration,address1,address2)))
        # ask if they're referencing the last mentioned
        elif 'direction' in request.session['_historyIntents'] or 'getCost' in request.session['_historyIntents']:
            lastQuery = request.session['_historyDestinations'][-1]
            a1 = lastQuery[0]
            a2 = lastQuery[1]
            if (validate(a1) == "current_loc"):
                a1 = "where you are now"
            request.session['_messages'].append(cc_msg('Do you mean between {} and {}?'.format(a1,a2)))
            request.session['_unfinished']['lengthTime'] = 'farBack'
        else:
            equest.session['_messages'].append(cc_msg('From where to where?'))
            request.session['_unfinished']['lengthTime'] = 'needTrip'

        return HttpResponseRedirect(reverse('index'))
    elif intent == "restaurant":
        businesses = query_multiterm(["food"], geo_loc, '3')
        parsed = parseResponses(businesses)
        candidates = [name for term in parsed for name in parsed[term]]
        request.session['_messages'].append(cc_msg("Here's what I found: "+",".join(candidates)))
        return HttpResponseRedirect(reverse('index'))
    # unknown intent
    else:
        r = response["result"]["fulfillment"]["speech"]
        if r is not "" and r is not None:
            request.session['_messages'].append({'author':'charliechat','msg':r})
        else:
            request.session['_messages'].append({'author':'charliechat', \
                        'msg':random_choice(INTENT_CLARIFICATION)})
        return HttpResponseRedirect(reverse('index'))

def dialog_flow(request,query,geo_loc):
    response = apiai_request(query)
    intent = get_intent_from_response(response)
    request.session['_historyIntents'].append(intent)
    print(intent)

    # logic for first time use
    if len(request.session['_messages']) == 2 and request.session['_messages'][0] == cc_msg(FIRST_TIME_MSG):
        user = User.objects.get(username="admin") if isinstance(request.user, AnonymousUser) else request.user
        if intent == 'agree':
            user.profile.group = 1
            user.save()
            request.session['_messages'].append(cc_msg('Awesome, welcome! What do you want to know?'))
        elif intent == 'disagree':
            request.session['_messages'].append(cc_msg('Well hi! What can I do for you?'))
        else:
            request.session['_messages'].append(cc_msg('I\'ll get on that.'))
            decide_intent(intent,response,request,query,geo_loc)
        return HttpResponseRedirect(reverse('index'))
    # returning user
    else:
        if intent == "agree":
            if len(request.session['_unfinished']) == 0:
                request.session['_messages'].append(cc_msg('Yes to you too!'))
            else:
                if len(request.session['_unfinished']) == 1:
                    unfinished_request = list(request.session['_unfinished'].keys())[0]
                    if unfinished_request == 'getCost':
                        if request.session['_unfinished'][unfinished_request] == 'farBack':
                            request.session['_messages'].append(cc_msg('Got it!'))
                            price = request.session['_historyQueries'][-1]['MBTA price']
                            if price != 'Directions not found':
                                request.session['_messages'].append(cc_msg("The price was ${}.".format(price)))
                            else:
                                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
                            request.session['_historyIntents'].append('getCost')
                    elif unfinished_request == 'lengthTime':
                        if request.session['_unfinished'][unfinished_request] == 'farBack':
                            request.session['_messages'].append(cc_msg('On it!'))
                            duration = request.session['_historyQueries'][-1]['MBTA duration']
                            if duration != 'Directions not found':
                                request.session['_messages'].append(cc_msg("The trip will take {}.".format(duration)))
                            else:
                                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
                            request.session['_historyIntents'].append('lengthTrip')

                    request.session['_unfinished'] = {}

                # somehow, a bunch of unfinished queries
                else:
                    pass

        elif intent == "disagree":
            if len(request.session['_unfinished']) == 0:
                request.session['_messages'].append(cc_msg('Boo to you too.'))
            else:
                if len(request.session['_unfinished']) == 1:
                    unfinished_request = list(request.session['_unfinished'].keys())[0]
                    if unfinished_request == 'lengthTime':
                        # not the last trip mentioned
                        if request.session['_unfinished'][unfinished_request] == 'farBack':
                            request.session['_messages'].append(cc_msg('Okay. What trip do you mean?'))
                    elif unfinished_request == 'getCost':
                        # not the last trip mentioned
                        if request.session['_unfinished'][unfinished_request] == 'farBack':
                            request.session['_messages'].append(cc_msg('Okay. The cost from where to where?'))
        else:
            decide_intent(intent,response,request,query,geo_loc)
<commit_msg>buses without a number<commit_after>from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth.models import AnonymousUser, User
from .queryGoogleForDirectionsv5 import return_travel_info
from .apiai import *
from .yelpfusionapi import *
import json, re

from random import randint

FIRST_TIME_MSG = "Welcome to CharlieChat! Are you new to the area?"
INTENT_CLARIFICATION = ["That doesn't quite make sense, could you word that differently?",\
                        "I'm not sure what you mean. Could you say that again?"]
TROUBLE = ["I seem to be having trouble, could you try again later?", "Uh oh, I'm having difficulties. Try again later.", \
        "I can't seem to connect, try again some time later.", "Oh no, something's wrong on my end. Could you try again later?", \
        'I don\'t seem to be working right now! Try something else, or try again later.']

def msg(msg_type,message):
    return {'author':msg_type,'msg':message}

def cc_msg(message):
    return msg('charliechat',message)

def user_msg(message):
    return msg('user',message)

def appendToDestHistory(request,start,dest):
    request.session['_historyDestinations'].append((start,dest))

def random_choice(responses):
    return responses[randint(0,len(responses) - 1 )]

def assemble_instructions_map(segments):
    code = ""

    for segment in segments:
        step = segment['html_instructions']
        print(step)
        print(segment)
        print("*****")
        if segment['travel_mode'] == 'WALKING':
            code += "
{} (about {})
".format(step,segment['duration']['text'])
        else:
            transit = segment['transit_details']
            depart = transit['departure_stop']['name']

            if 'color' in transit['line']:
                color = transit['line']['color']
                code += "
  ".format(color)
            else:
                code += "
"

            mode = transit['line']['vehicle']['type'].lower()

            if mode == 'bus':
                if 'short_name' in transit['line']:
                    number = transit['line']['short_name']
                    code += "{} {} from {} for {} stops".format(number,step,depart,transit['num_stops'])
                else:
                    code += "{} from {} for {} stops".format(step,depart,transit['num_stops'])
            elif mode == 'heavy_rail':
                line = transit['line']['name']
                code += "Take the {} from {} towards {} for {} stops".format(line,depart,transit['headsign'],transit['num_stops'])
            elif mode == 'subway':
                line_name = transit['line']['name']
                code += "Take the {} from {} towards {} for {} stops".format(line_name,depart,transit['headsign'],transit['num_stops'])
            elif mode == 'tram':
                train_letter = transit['line']['short_name']
                code += "Take green line {} train from {} towards {} for {} stops".format(train_letter,depart,transit['headsign'],transit['num_stops'])
            else:
                code += step

            code += " ({})
".format(segment['duration']['text'])

    return code

def decide_intent(intent,response,request,query,geo_loc):
    #TODO: may need to break down into separate methods
    if intent == "help":
        request.session['_messages'].append(cc_msg("That's a great question! I do a lot of things."))
        request.session['_messages'].append(cc_msg("Ask me how to get somewhere, how to get from point A to point B, how much that costs, or how long that'll take."))
    # intent is to navigate
    elif intent == "direction":
        address1, address2 = get_addresses_from_response(response)
        if not address2:
            # if you've already asked things, assume the previous
            lastQuery = request.session['_historyQueries'][-1]
            if lastQuery['MBTA directions'] == 'Directions not found':
                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
            else:
                request.session['_messages'].append({'author':'charliechat', \
                    'msg':"Arrival time is {} if you leave at {}.".format(lastQuery['MBTA arrival time'],lastQuery['MBTA depart time'])})
                request.session['_messages'].append(cc_msg(assemble_instructions_map(lastQuery['Route segments'])))
        else:
            if (validate(address1) == "current_loc"):
                address1 = geo_loc

            info_dict = return_travel_info(address1,address2)
            if address1 == geo_loc:
                address1 = "current_loc" # make it easier to check historyDestinations later on
            appendToDestHistory(request,address1,address2)

            if info_dict['MBTA directions'] == 'Directions not found':
                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
            else:
                request.session['_messages'].append({'author':'charliechat', \
                    'msg':"Arrival time is {} if you leave at {}.".format(info_dict['MBTA arrival time'],info_dict['MBTA depart time'])})
                request.session['_messages'].append(cc_msg(assemble_instructions_map(info_dict['Route segments'])))
            request.session['_historyQueries'].append(info_dict)
            # if they asked for the price previously, also give the price
            if 'getCost' in request.session['_unfinished']:
                price = info_dict['MBTA price']
                if price != 'Directions not found':
                    request.session['_messages'].append(cc_msg('And the total cost of this trip is ${0:.2f}.'.format(price)))
                else:
                    request.session['_messages'].append(cc_msg('I can\'t seem to find any prices at the moment.'))
                del request.session['_unfinished']['getCost']
            elif 'lengthTime' in request.session['_unfinished']:
                duration = info_dict['MBTA duration']
                if duration != 'Directions not found':
                    request.session['_messages'].append(cc_msg('And the total duration of this trip would be {0}.'.format(duration)))
                else:
                    request.session['_messages'].append(cc_msg('I can\'t seem to calculate the time right now.'))
                del request.session['_unfinished']['lengthTime']
        return HttpResponseRedirect(reverse('index'))
    # get the cost
    elif intent == "getCost":
        # if asking for fare to a place
        if response['result']['parameters']['address2'] != '':
            address1, address2 = get_addresses_from_response(response)
            appendToDestHistory(request,address1,address2)
            if (validate(address1) == "current_loc"):
                address1 = geo_loc
            info_dict = return_travel_info(address1,address2)
            request.session['_historyQueries'].append(info_dict)
            price = info_dict['MBTA price']
            if price == 'Directions not found':
                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
            else:
                if (validate(address1) == geo_loc):
                    address1 = "your current location"
                request.session['_messages'].append(cc_msg('It\'ll cost you ${0:.2f} to go from {1} to {2}.'.format(price,address1,address2)))
        # if the previous request or the one before was a navigation, assume we're getting that cost
        elif request.session['_historyIntents'][-2] == "direction" or request.session['_historyIntents'][-3] == "direction":
            price = request.session['_historyQueries'][-1]['MBTA price']
            if price != 'Directions not found':
                destination = request.session['_historyDestinations'][-1][1]
                request.session['_messages'].append(cc_msg('Your trip to {0} is going to cost ${1:.2f} with an adult CharlieCard.'.format(destination,(price))))
            else:
                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
        # if it was a while back, ask if that's the one
        elif 'direction' in request.session['_historyIntents'] or 'lengthTime' in request.session['_historyIntents']:
            request.session['_messages'].append(cc_msg('Do you mean the cost of your trip to {}?'.format(request.session['_historyDestinations'][-1][1])))
            # remember unfinished so agree intent is handled properly later
            print(request.session['_unfinished'])
            request.session['_unfinished']['getCost'] = 'farBack'
        else:
            request.session['_messages'].append(cc_msg('From where to where?'))
            request.session['_unfinished']['getCost'] = 'needTrip'
        return HttpResponseRedirect(reverse('index'))
    # asked how long it takes to get somewhere
    elif intent == "lengthTime":
        # if they want duration to a specified place
        if response['result']['parameters']['address2'] != '':
            address1, address2 = get_addresses_from_response(response)
            appendToDestHistory(request,address1,address2)
            if (validate(address1) == "current_loc"):
                address1 = geo_loc
            info_dict = return_travel_info(address1,address2)
            duration = info_dict['MBTA duration']
            request.session['_historyQueries'].append(info_dict)
            if duration == 'Directions not found':
                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
            else:
                if (validate(address1) == geo_loc):
                    address1 = "where you currently are"
                request.session['_messages'].append(cc_msg('It\'ll take you {0} to go from {1} to {2}.'.format(duration,address1,address2)))
        # ask if they're referencing the last mentioned
        elif 'direction' in request.session['_historyIntents'] or 'getCost' in request.session['_historyIntents']:
            lastQuery = request.session['_historyDestinations'][-1]
            a1 = lastQuery[0]
            a2 = lastQuery[1]
            if (validate(a1) == "current_loc"):
                a1 = "where you are now"
            request.session['_messages'].append(cc_msg('Do you mean between {} and {}?'.format(a1,a2)))
            request.session['_unfinished']['lengthTime'] = 'farBack'
        else:
            equest.session['_messages'].append(cc_msg('From where to where?'))
            request.session['_unfinished']['lengthTime'] = 'needTrip'

        return HttpResponseRedirect(reverse('index'))
    elif intent == "restaurant":
        businesses = query_multiterm(["food"], geo_loc, '3')
        parsed = parseResponses(businesses)
        candidates = [name for term in parsed for name in parsed[term]]
        request.session['_messages'].append(cc_msg("Here's what I found: "+",".join(candidates)))
        return HttpResponseRedirect(reverse('index'))
    # unknown intent
    else:
        r = response["result"]["fulfillment"]["speech"]
        if r is not "" and r is not None:
            request.session['_messages'].append({'author':'charliechat','msg':r})
        else:
            request.session['_messages'].append({'author':'charliechat', \
                        'msg':random_choice(INTENT_CLARIFICATION)})
        return HttpResponseRedirect(reverse('index'))

def dialog_flow(request,query,geo_loc):
    response = apiai_request(query)
    intent = get_intent_from_response(response)
    request.session['_historyIntents'].append(intent)
    print(intent)

    # logic for first time use
    if len(request.session['_messages']) == 2 and request.session['_messages'][0] == cc_msg(FIRST_TIME_MSG):
        user = User.objects.get(username="admin") if isinstance(request.user, AnonymousUser) else request.user
        if intent == 'agree':
            user.profile.group = 1
            user.save()
            request.session['_messages'].append(cc_msg('Awesome, welcome! What do you want to know?'))
        elif intent == 'disagree':
            request.session['_messages'].append(cc_msg('Well hi! What can I do for you?'))
        else:
            request.session['_messages'].append(cc_msg('I\'ll get on that.'))
            decide_intent(intent,response,request,query,geo_loc)
        return HttpResponseRedirect(reverse('index'))
    # returning user
    else:
        if intent == "agree":
            if len(request.session['_unfinished']) == 0:
                request.session['_messages'].append(cc_msg('Yes to you too!'))
            else:
                if len(request.session['_unfinished']) == 1:
                    unfinished_request = list(request.session['_unfinished'].keys())[0]
                    if unfinished_request == 'getCost':
                        if request.session['_unfinished'][unfinished_request] == 'farBack':
                            request.session['_messages'].append(cc_msg('Got it!'))
                            price = request.session['_historyQueries'][-1]['MBTA price']
                            if price != 'Directions not found':
                                request.session['_messages'].append(cc_msg("The price was ${}.".format(price)))
                            else:
                                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
                            request.session['_historyIntents'].append('getCost')
                    elif unfinished_request == 'lengthTime':
                        if request.session['_unfinished'][unfinished_request] == 'farBack':
                            request.session['_messages'].append(cc_msg('On it!'))
                            duration = request.session['_historyQueries'][-1]['MBTA duration']
                            if duration != 'Directions not found':
                                request.session['_messages'].append(cc_msg("The trip will take {}.".format(duration)))
                            else:
                                request.session['_messages'].append(cc_msg(random_choice(TROUBLE)))
                            request.session['_historyIntents'].append('lengthTrip')

                    request.session['_unfinished'] = {}

                # somehow, a bunch of unfinished queries
                else:
                    pass

        elif intent == "disagree":
            if len(request.session['_unfinished']) == 0:
                request.session['_messages'].append(cc_msg('Boo to you too.'))
            else:
                if len(request.session['_unfinished']) == 1:
                    unfinished_request = list(request.session['_unfinished'].keys())[0]
                    if unfinished_request == 'lengthTime':
                        # not the last trip mentioned
                        if request.session['_unfinished'][unfinished_request] == 'farBack':
                            request.session['_messages'].append(cc_msg('Okay. What trip do you mean?'))
                    elif unfinished_request == 'getCost':
                        # not the last trip mentioned
                        if request.session['_unfinished'][unfinished_request] == 'farBack':
                            request.session['_messages'].append(cc_msg('Okay. The cost from where to where?'))
        else:
            decide_intent(intent,response,request,query,geo_loc)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
    Mango ORGs system logic.
'''

# This file is part of mango.

# Distributed under the terms of the last AGPL License.
# The full license is in the file LICENCE, distributed as part of this software.

__author__ = 'Team Machine'

import uuid
import logging
import ujson as json
from tornado import gen
from schematics.types import compound
from mango.messages import accounts
from mango.messages import BaseResult
from mango.structures.accounts import AccountMap
from riak.datatypes import Map
from mango.tools import clean_response, clean_structure
from mango.tools import get_search_item, get_search_list
from tornado import httpclient as _http_client

_http_client.AsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient')
http_client = _http_client.AsyncHTTPClient()

class OrgResult(BaseResult):
    '''
        List result
    '''
    results = compound.ListType(compound.ModelType(accounts.Org))

class Org(object):
    '''
        ORG
    '''
    @gen.coroutine
    def get_org(self, account, org_uuid):
        '''
            Get org
        '''
        search_index = 'mango_account_index'
        query = 'uuid_register:{0}'.format(org_uuid)
        filter_query = 'account_register:{0}'.format(account.decode('utf-8'))
        # note where the hack change ' to %27 for the url string!
        fq_watchers = "watchers_register:*'{0}'*".format(account.decode('utf8')).replace("'",'%27')
        urls = set()
        urls.add(get_search_item(self.solr, search_index, query, filter_query))
        urls.add(get_search_item(self.solr, search_index, query, fq_watchers))
        # init got response list
        got_response = []
        # init crash message
        message = {'message': 'not found'}
        # ignore riak fields
        IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
        # hopefully asynchronous handle function request
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            # and know for something completly different!
            for url in urls:
                http_client.fetch(
                    url,
                    callback=handle_request
                )
            while len(got_response) <= 1:
                # Yo, don't be careless with the time!
                yield gen.sleep(0.0010)
            # get stuff from response
            stuff = got_response[0]
            # get it from watchers list
            watchers = got_response[1]
            if stuff['response']['numFound']:
                response = stuff['response']['docs'][0]
                message = clean_response(response, IGNORE_ME)
            elif watchers['response']['numFound']:
                response = watchers['response']['docs'][0]
                message = clean_response(response, IGNORE_ME)
            else:
                logging.error('there is probably something wrong!')
        except Exception as error:
            logging.warning(error)
        return message

    @gen.coroutine
    def get_org_list(self, account, start, end, lapse, status, page_num):
        '''
            Get org list
        '''
        search_index = 'mango_account_index'
        query = 'uuid_register:*'
        filter_query = 'account_register:{0}'.format(account.decode('utf-8'))
        # note where the hack change ' to %27 for the url string!
        fq_watchers = "watchers_register:*'{0}'*".format(account.decode('utf8')).replace("'",'%27')
        # page number
        page_num = int(page_num)
        page_size = self.settings['page_size']
        start_num = page_size * (page_num - 1)
        # set of urls
        urls = set()
        urls.add(get_search_list(self.solr, search_index, query, filter_query, start_num, page_size))
        urls.add(get_search_list(self.solr, search_index, query, fq_watchers, start_num, page_size))
        # init got response list
        got_response = []
        # init crash message
        message = {
            'count': 0,
            'page': page_num,
            'results': []
        }
        # ignore riak fields
        IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
        # hopefully asynchronous handle function request
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            # and know for something completly different!
            for url in urls:
                http_client.fetch(
                    url,
                    callback=handle_request
                )
            while len(got_response) <= 1:
                # Yo, don't be careless with the time!
                yield gen.sleep(0.0010)
            # get stuff from response
            stuff = got_response[0]
            # get it from watchers list
            watchers = got_response[1]
            if stuff['response']['numFound']:
                message['count'] += stuff['response']['numFound']
                for doc in stuff['response']['docs']:
                    message['results'].append(clean_response(doc, IGNORE_ME))
            if watchers['response']['numFound']:
                message['count'] += watchers['response']['numFound']
                for doc in watchers['response']['docs']:
                    message['results'].append(clean_response(doc, IGNORE_ME))
            else:
                logging.error('there is probably something wrong!')
        except Exception as error:
            logging.warning(error)
        return message

    @gen.coroutine
    def new_org(self, struct):
        '''
            New ORG event
        '''
        search_index = 'mango_account_index'
        bucket_type = 'mango_account'
        bucket_name = 'accounts'
        try:
            event = accounts.Org(struct)
            event.validate()
            event = clean_structure(event)
        except Exception as error:
            raise error
        try:
            message = event.get('uuid')
            structure = {
                "uuid": str(event.get('uuid', str(uuid.uuid4()))),
                "account": str(event.get('account', 'pebkac')),
                "source": str(event.get('source', '')),
                "org": str(event.get('org', '')),
                "resource": str(event.get('resource', '')),
                "sentiment": str(event.get('sentiment', '')),
                "ranking": str(event.get('ranking', '')),
                "created_by": str(event.get('created_by', '')),
                "created_at": str(event.get('created_at', '')),
                "last_update_by": str(event.get('last_update_by', '')),
                "last_update_at": str(event.get('last_update_at', '')),
                "history": str(event.get('history', '')),
                "labels": str(event.get('labels', '')),
                "url":str(event.get('url', '')),
                "status": str(event.get('status', '')),
                "resource": str(event.get('resource', '')),
                "resource_uuid": str(event.get('resource_uuid', '')),
                "active": str(event.get('active', '')),
                "watchers": str(event.get('watchers', '')),
            }
            result = AccountMap(
                self.kvalue,
                bucket_name,
                bucket_type,
                search_index,
                structure
            )
            message = structure.get('uuid')
        except Exception as error:
            logging.error(error)
            message = str(error)
        return message

    @gen.coroutine
    def modify_org(self, account, account_uuid, struct):
        '''
            Modify org
        '''
        # riak search index
        search_index = 'mango_account_index'
        # riak bucket type
        bucket_type = 'mango_account'
        # riak bucket name
        bucket_name = 'accounts'
        # solr query
        query = 'uuid_register:{0}'.format(account_uuid.rstrip('/'))
        # filter query
        filter_query = 'account_register:{0}'.format(account.decode('utf-8'))
        # search query url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        # pretty please, ignore this list of fields from database.
        IGNORE_ME = ("_yz_id","_yz_rk","_yz_rt","_yz_rb","checked","keywords")
        # got callback response?
        got_response = []
        # yours truly
        message = {'update_complete':False}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                # don't be careless with the time.
                yield gen.sleep(0.0010)
            logging.warning(got_response)
            response = got_response[0].get('response')['docs'][0]
            riak_key = str(response['_yz_rk'])
            bucket = self.kvalue.bucket_type(bucket_type).bucket('{0}'.format(bucket_name))
            bucket.set_properties({'search_index': search_index})
            account = Map(bucket, riak_key)
            for key in struct:
                if key not in IGNORE_ME:
                    if type(struct.get(key)) == list:
                        account.reload()
                        old_value = account.registers['{0}'.format(key)].value
                        if old_value:
                            old_list = json.loads(old_value.replace("'",'"'))
                            for thing in struct.get(key):
                                old_list.append(thing)
                            account.registers['{0}'.format(key)].assign(str(old_list))
                        else:
                            new_list = []
                            for thing in struct.get(key):
                                new_list.append(thing)
                            account.registers['{0}'.format(key)].assign(str(new_list))
                    else:
                        account.registers['{0}'.format(key)].assign(str(struct.get(key)))
                    account.update()
            update_complete = True
            message['update_complete'] = True
        except Exception as error:
            logging.exception(error)
        return message.get('update_complete', False)

    @gen.coroutine
    def modify_remove(self, account, org_uuid, struct):
        '''
            Modify remove
        '''
        # riak search index
        search_index = 'mango_account_index'
        # riak bucket type
        bucket_type = 'mango_account'
        # riak bucket name
        bucket_name = 'acounts'
        # solr query
        query = 'uuid_register:{0}'.format(org_uuid.rstrip('/'))
        # filter query
        filter_query = 'account_register:{0}'.format(account.decode('utf-8'))
        # search query url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        # pretty please, ignore this list of fields from database.
        IGNORE_ME = ("_yz_id","_yz_rk","_yz_rt","_yz_rb","checked","keywords")
        # got callback response?
        got_response = []
        # yours truly
        message = {'update_complete':False}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                # Please, don't be careless with the time.
                yield gen.sleep(0.0010)
            response = got_response[0].get('response')['docs'][0]
            riak_key = str(response['_yz_rk'])
            bucket = self.kvalue.bucket_type(bucket_type).bucket('{0}'.format(bucket_name))
            bucket.set_properties({'search_index': search_index})
            org = Map(bucket, riak_key)
            for key in struct:
                if key not in IGNORE_ME:
                    if type(struct.get(key)) == list:
                        org.reload()
                        old_value = org.registers['{0}'.format(key)].value
                        if old_value:
                            old_list = json.loads(old_value.replace("'",'"'))
                            new_list = [x for x in old_list if x not in struct.get(key)]
                            org.registers['{0}'.format(key)].assign(str(new_list))
                            org.update()
                            message['update_complete'] = True
                    else:
                        message['update_complete'] = False
        except Exception as error:
            logging.exception(error)
        return message.get('update_complete', False)

    @gen.coroutine
    def remove_org(self, account, org_uuid):
        '''
            Remove org
        '''
        # Yo, missing history ?
        struct = {}
        struct['status'] = 'deleted'
        message = yield self.modify_org(account, org_uuid, struct)
        return message
<commit_msg>clean, clean, clean<commit_after># -*- coding: utf-8 -*-
'''
    Mango ORGs system logic.
'''

# This file is part of mango.

# Distributed under the terms of the last AGPL License.
# The full license is in the file LICENCE, distributed as part of this software.

__author__ = 'Team Machine'

import uuid
import logging
import ujson as json
from tornado import gen
from schematics.types import compound
from mango.messages import accounts
from mango.messages import BaseResult
from mango.structures.accounts import AccountMap
from riak.datatypes import Map
from mango.tools import clean_response, clean_structure
from mango.tools import get_search_item, get_search_list
from tornado import httpclient as _http_client

_http_client.AsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient')
http_client = _http_client.AsyncHTTPClient()

class OrgResult(BaseResult):
    '''
        List result
    '''
    results = compound.ListType(compound.ModelType(accounts.Org))

class Org(object):
    '''
        ORG
    '''
    @gen.coroutine
    def get_org(self, account, org_uuid):
        '''
            Get org
        '''
        search_index = 'mango_account_index'
        query = 'uuid_register:{0}'.format(org_uuid)
        filter_query = 'account_register:{0}'.format(account.decode('utf-8'))
        # note where the hack change ' to %27 for the url string!
        fq_watchers = "watchers_register:*'{0}'*".format(account.decode('utf8')).replace("'",'%27')
        urls = set()
        urls.add(get_search_item(self.solr, search_index, query, filter_query))
        urls.add(get_search_item(self.solr, search_index, query, fq_watchers))
        # init got response list
        got_response = []
        # init crash message
        message = {'message': 'not found'}
        # ignore riak fields
        IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
        # hopefully asynchronous handle function request
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            # and know for something completly different!
            for url in urls:
                http_client.fetch(
                    url,
                    callback=handle_request
                )
            while len(got_response) <= 1:
                # Yo, don't be careless with the time!
                yield gen.sleep(0.0010)
            # get stuff from response
            stuff = got_response[0]
            # get it from watchers list
            watchers = got_response[1]
            if stuff['response']['numFound']:
                response = stuff['response']['docs'][0]
                message = clean_response(response, IGNORE_ME)
            elif watchers['response']['numFound']:
                response = watchers['response']['docs'][0]
                message = clean_response(response, IGNORE_ME)
            else:
                logging.error('there is probably something wrong!')
        except Exception as error:
            logging.warning(error)
        return message

    @gen.coroutine
    def get_org_list(self, account, start, end, lapse, status, page_num):
        '''
            Get org list
        '''
        search_index = 'mango_account_index'
        query = 'uuid_register:*'
        filter_query = 'account_register:{0}'.format(account.decode('utf-8'))
        # note where the hack change ' to %27 for the url string!
        fq_watchers = "watchers_register:*'{0}'*".format(account.decode('utf8')).replace("'",'%27')
        # page number
        page_num = int(page_num)
        page_size = self.settings['page_size']
        start_num = page_size * (page_num - 1)
        # set of urls
        urls = set()
        urls.add(get_search_list(self.solr, search_index, query, filter_query, start_num, page_size))
        urls.add(get_search_list(self.solr, search_index, query, fq_watchers, start_num, page_size))
        # init got response list
        got_response = []
        # init crash message
        message = {
            'count': 0,
            'page': page_num,
            'results': []
        }
        # ignore riak fields
        IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
        # hopefully asynchronous handle function request
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            # and know for something completly different!
            for url in urls:
                http_client.fetch(
                    url,
                    callback=handle_request
                )
            while len(got_response) <= 1:
                # Yo, don't be careless with the time!
                yield gen.sleep(0.0010)
            # get stuff from response
            stuff = got_response[0]
            # get it from watchers list
            watchers = got_response[1]
            if stuff['response']['numFound']:
                message['count'] += stuff['response']['numFound']
                for doc in stuff['response']['docs']:
                    message['results'].append(clean_response(doc, IGNORE_ME))
            if watchers['response']['numFound']:
                message['count'] += watchers['response']['numFound']
                for doc in watchers['response']['docs']:
                    message['results'].append(clean_response(doc, IGNORE_ME))
            else:
                logging.error('there is probably something wrong!')
        except Exception as error:
            logging.warning(error)
        return message

    @gen.coroutine
    def new_org(self, struct):
        '''
            New ORG event
        '''
        search_index = 'mango_account_index'
        bucket_type = 'mango_account'
        bucket_name = 'accounts'
        try:
            event = accounts.Org(struct)
            event.validate()
            event = clean_structure(event)
        except Exception as error:
            raise error
        try:
            message = event.get('uuid')
            structure = {
                "uuid": str(event.get('uuid', str(uuid.uuid4()))),
                "account": str(event.get('account', 'pebkac')),
                "source": str(event.get('source', '')),
                "org": str(event.get('org', '')),
                "resource": str(event.get('resource', '')),
                "sentiment": str(event.get('sentiment', '')),
                "ranking": str(event.get('ranking', '')),
                "created_by": str(event.get('created_by', '')),
                "created_at": str(event.get('created_at', '')),
                "last_update_by": str(event.get('last_update_by', '')),
                "last_update_at": str(event.get('last_update_at', '')),
                "history": str(event.get('history', '')),
                "labels": str(event.get('labels', '')),
                "url":str(event.get('url', '')),
                "status": str(event.get('status', '')),
                "resource": str(event.get('resource', '')),
                "resource_uuid": str(event.get('resource_uuid', '')),
                "active": str(event.get('active', '')),
                "watchers": str(event.get('watchers', '')),
            }
            result = AccountMap(
                self.kvalue,
                bucket_name,
                bucket_type,
                search_index,
                structure
            )
            message = structure.get('uuid')
        except Exception as error:
            logging.error(error)
            message = str(error)
        return message

    @gen.coroutine
    def modify_org(self, account, account_uuid, struct):
        '''
            Modify org
        '''
        # riak search index
        search_index = 'mango_account_index'
        # riak bucket type
        bucket_type = 'mango_account'
        # riak bucket name
        bucket_name = 'accounts'
        # solr query
        query = 'uuid_register:{0}'.format(account_uuid.rstrip('/'))
        # filter query
        filter_query = 'account_register:{0}'.format(account.decode('utf-8'))
        # search query url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        # pretty please, ignore this list of fields from database.
        IGNORE_ME = ("_yz_id","_yz_rk","_yz_rt","_yz_rb","checked","keywords")
        # got callback response?
        got_response = []
        # yours truly
        logging.warning(url)
        message = {'update_complete':False}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                # don't be careless with the time.
                yield gen.sleep(0.0010)
            logging.warning(got_response)
            response = got_response[0].get('response')['docs'][0]
            riak_key = str(response['_yz_rk'])
            bucket = self.kvalue.bucket_type(bucket_type).bucket('{0}'.format(bucket_name))
            bucket.set_properties({'search_index': search_index})
            account = Map(bucket, riak_key)
            for key in struct:
                if key not in IGNORE_ME:
                    if type(struct.get(key)) == list:
                        account.reload()
                        old_value = account.registers['{0}'.format(key)].value
                        if old_value:
                            old_list = json.loads(old_value.replace("'",'"'))
                            for thing in struct.get(key):
                                old_list.append(thing)
                            account.registers['{0}'.format(key)].assign(str(old_list))
                        else:
                            new_list = []
                            for thing in struct.get(key):
                                new_list.append(thing)
                            account.registers['{0}'.format(key)].assign(str(new_list))
                    else:
                        account.registers['{0}'.format(key)].assign(str(struct.get(key)))
                    account.update()
            update_complete = True
            message['update_complete'] = True
        except Exception as error:
            logging.exception(error)
        return message.get('update_complete', False)

    @gen.coroutine
    def modify_remove(self, account, org_uuid, struct):
        '''
            Modify remove
        '''
        # riak search index
        search_index = 'mango_account_index'
        # riak bucket type
        bucket_type = 'mango_account'
        # riak bucket name
        bucket_name = 'acounts'
        # solr query
        query = 'uuid_register:{0}'.format(org_uuid.rstrip('/'))
        # filter query
        filter_query = 'account_register:{0}'.format(account.decode('utf-8'))
        # search query url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        # pretty please, ignore this list of fields from database.
        IGNORE_ME = ("_yz_id","_yz_rk","_yz_rt","_yz_rb","checked","keywords")
        # got callback response?
        got_response = []
        # yours truly
        message = {'update_complete':False}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                # Please, don't be careless with the time.
                yield gen.sleep(0.0010)
            response = got_response[0].get('response')['docs'][0]
            riak_key = str(response['_yz_rk'])
            bucket = self.kvalue.bucket_type(bucket_type).bucket('{0}'.format(bucket_name))
            bucket.set_properties({'search_index': search_index})
            org = Map(bucket, riak_key)
            for key in struct:
                if key not in IGNORE_ME:
                    if type(struct.get(key)) == list:
                        org.reload()
                        old_value = org.registers['{0}'.format(key)].value
                        if old_value:
                            old_list = json.loads(old_value.replace("'",'"'))
                            new_list = [x for x in old_list if x not in struct.get(key)]
                            org.registers['{0}'.format(key)].assign(str(new_list))
                            org.update()
                            message['update_complete'] = True
                    else:
                        message['update_complete'] = False
        except Exception as error:
            logging.exception(error)
        return message.get('update_complete', False)

    @gen.coroutine
    def remove_org(self, account, org_uuid):
        '''
            Remove org
        '''
        # Yo, missing history ?
        struct = {}
        struct['status'] = 'deleted'
        message = yield self.modify_org(account, org_uuid, struct)
        return message
<|endoftext|>