<commit_before>import platform
import traceback
import array
import enum
import x64dbgpy.pluginsdk.x64dbg as x64dbg
import x64dbgpy.pluginsdk._scriptapi as script

version = "0.2.0.29"

def notImplemented():
    try:
        raise NotImplementedError()
    except:
        traceback.print_stack()
        raise

def archValue(x32, x64):
    if is64bitSystem():
        return x64
    return x32

def is64bitSystem():
    return platform.architecture()[0] == "64bit"

def dbgCommand(command, suppressOutput = False):
    if command == "!teb":
        return "TEB at %x" % getImplicitThread()
    else:
        print "command: %s" % command
        notImplemented()

def dprintln(str, dml = False):
    print str

def findMemoryRegion(va):
    base = script.GetBase(va)
    size = script.GetSize(va)
    if base == 0 or size == 0:
        raise MemoryException("No such address 0x%x" % va)
    return base, size

def findSymbol(va):
    notImplemented()

def getCurrentProcess():
    return x64dbg.DbgGetPebAddress(x64dbg.DbgGetProcessId())

def getCurrentProcessId():
    return x64dbg.DbgGetProcessId()

def getImplicitThread():
    return x64dbg.DbgGetTebAddress(x64dbg.DbgGetThreadId())

def getProcessThreads():
    l = x64dbg.THREADLIST()
    x64dbg.DbgGetThreadList(l)
    result = []
    if l.count > 0:
        return [t.BasicInfo.ThreadLocalBase for t in x64dbg.GetThreadInfoList(l)]
    return result

def getVaProtect(va):
    return memoryProtect(script.GetProtect(va))

def isValid(va):
    return x64dbg.DbgMemIsValidReadPtr(va)

def loadBytes(va, count):
    return list(script.Read(va, count))

def loadChars(va, count):
    return script.Read(va, count)

def loadCStr(va):
    chars = loadChars(va, 256)
    index = chars.find('\0')
    if index != -1:
        return chars[:index]
    return chars

def loadDwords(va, count):
    A = array.array("I")
    A.fromstring(script.Read(va, count * 4))
    return A.tolist()

def loadUnicodeString(va):
    va = int(va)
    # https://msdn.microsoft.com/en-us/library/windows/desktop/aa380518(v=vs.85).aspx
    Length = script.ReadWord(va)
    va += 2
    MaximumLength = script.ReadWord(va)
    va += 2
    if is64bitSystem():
        va += 4
    Buffer = script.ReadPtr(va)
    if Length > MaximumLength or not script.IsValidPtr(Buffer):
        raise DbgException("Corrupted UNICODE_STRING structure")
    A = array.array("u")
    A.fromstring(script.Read(Buffer, Length))
    return A.tounicode().rstrip(u'\0')

def loadWChars(va, count):
    A = array.array("u")
    A.fromstring(script.Read(va, count * 2))
    return A.tounicode()

def loadWStr(va):
    wchars = loadWChars(va, 256)
    index = wchars.find(u'\0')
    if index != -1:
        return wchars[:index]
    return wchars

def ptrDWord(va):
    return script.ReadDword(va)

def writeBytes(va, data):
    A = array.array("B")
    A.fromlist(data)
    script.Write(va, A.tostring())

def reg(name):
    return x64dbg.DbgValFromString(name)

def ptrPtr(va):
    return script.ReadPtr(va)

def typedVar(typename, va):
    if typename == "ntdll!_PEB":
        # TODO: fill in x64 offsets
        peb = typeStruct("_PEB", va)
        peb.Ldr = typePtr("_PEB_LDR_DATA*", va + archValue(0x000c, 0x0000))
        peb.ProcessParameters = typePtr("_RTL_USER_PROCESS_PARAMETERS*", va + archValue(0x0010, 0x0000))
        peb.NumberOfHeaps = typeInt32(va + archValue(0x008c, 0x0000))
        peb.ProcessHeaps = typePtr("void**", va + archValue(0x0090, 0x0000))
        peb.OSMajorVersion = typeInt16(va + archValue(0x00a4, 0x0000))
        peb.OSMinorVersion = typeInt16(va + archValue(0x00a8, 0x0000))
        peb.OSBuildNumber = typeInt8(va + archValue(0x00ac, 0x0000))
        return peb
    elif typename == "_PEB_LDR_DATA":
        # TODO: fill in x64 offsets
        ldr = typeStruct("_PEB_LDR_DATA", va)
        ldr.InLoadOrderModuleList = typeStruct("_LDR_DATA_TABLE_ENTRY", va + archValue(0x0014, 0x0000))
        return ldr
    elif typename == "_TEB":
        teb = typeStruct("_TEB", va)
        teb.Self = va
        return teb
    elif typename in ("_IMAGE_NT_HEADERS", "_IMAGE_NT_HEADERS64"):
        ntheaders = typeStruct(typename, va)
        ntheaders.FileHeader = typedVar("_IMAGE_FILE_HEADER", va + 0x0004)
        ntheaders.OptionalHeader = typedVar(archValue("_IMAGE_OPTIONAL_HEADER", "_IMAGE_OPTIONAL_HEADER64"), va + 0x0018)
        return ntheaders
    elif typename == "_IMAGE_FILE_HEADER":
        fileheader = typeStruct("_IMAGE_FILE_HEADER", va)
        fileheader.NumberOfSections = typeInt16(va + 0x0002)
        fileheader.SizeOfOptionalHeader = typeInt16(va + 0x0010)
        return fileheader
    elif typename in ("_IMAGE_OPTIONAL_HEADER", "_IMAGE_OPTIONAL_HEADER64"):
        optheader = typeStruct(typename, va)
        optheader.SizeOfCode = typeInt32(va + 0x0004)
        optheader.AddressOfEntryPoint = typeInt32(va + 0x0010)
        optheader.BaseOfCode = typeInt32(va + 0x0014)
        if not is64bitSystem():
            optheader.BaseOfData = typeInt32(va + 0x0018)
        optheader.ImageBase = typeInt32(va + archValue(0x001c, 0x0018))
        optionalheadersize = int(typeInt16(va - 0x0004))
        optheader.DataDirectory = [typedVar("_IMAGE_DATA_DIRECTORY", i) for i in range(va + archValue(0x0060, 0x0070), va + optionalheadersize, 8)]
        return optheader
    elif typename == "_IMAGE_DATA_DIRECTORY":
        imgdatadir = typeStruct("_IMAGE_DATA_DIRECTORY", va)
        imgdatadir.VirtualAddress = typeInt32(va)
        imgdatadir.Size = typeInt32(va + 0x0004)
        return imgdatadir
    else:
        print "typename: %s, va: %x" % (typename, va)
        notImplemented()

def typedVarList(va, typename, flink):
    if typename == "ntdll!_LDR_DATA_TABLE_ENTRY" and flink == "InMemoryOrderLinks.Flink":
        # TODO: fill in x64 offsets
        va = script.ReadPtr(int(va) + archValue(0x0008, 0x0000))
        result = []

        while va != 0 and len(result) < 10:
            entry = typeStruct("_LDR_DATA_TABLE_ENTRY", va)
            entry.BaseDllName = typeStruct("UNICODE_STRING", va + archValue(0x001c, 0x0000))
            result.append(entry)
            va = script.ReadPtr(va)
            pass
        return result
    else:
        notImplemented()

class typeBase(object):
    def __init__(self, name, size, addr = 0):
        self.name = name
        self.size = size
        self.addr = addr

    def getAddress(self):
        return self.addr

    def __int__(self):
        return self.getAddress()

    def __add__(self, other):
        return int(self) + other

class typePrimitive(typeBase):
    def __init__(self, name, size, addr = 0):
        super(typePrimitive, self).__init__(name, size, addr)

    def __int__(self):
        if self.size == 1:
            return script.ReadByte(self.addr)
        elif self.size == 2:
            return script.ReadWord(self.addr)
        elif self.size == 4:
            return script.ReadDword(self.addr)
        elif self.size == 8:
            return script.ReadQword(self.addr)
        else:
            notImplemented()

class typeInt8(typePrimitive):
    def __init__(self, addr = 0):
        super(typeInt8, self).__init__("uint8_t", 1, addr)

class typeInt16(typePrimitive):
    def __init__(self, addr = 0):
        super(typeInt16, self).__init__("uint16_t", 2, addr)

class typeInt32(typePrimitive):
    def __init__(self, addr = 0):
        super(typeInt32, self).__init__("uint32_t", 4, addr)

class typeInt64(typePrimitive):
    def __init__(self, addr = 0):
        super(typeInt64, self).__init__("uint64_t", 8, addr)

class typePtr(typePrimitive):
    def __init__(self, type = "void*", addr = 0):
        super(typePtr, self).__init__(type, archValue(4, 8), addr)

    def deref(self):
        return typedVar(self.name[:-1], int(self))

class typeStruct(typeBase):
    def __init__(self, name, addr):
        super(typeStruct, self).__init__(name, 0, addr)

class memoryProtect(enum.Enum):
    PageExecute = 16
    PageExecuteWriteCopy = 128
    PageReadOnly = 2
    PageReadWrite = 4
    PageExecuteRead = 32
    PageExecuteReadWrite = 64
    PageNoAccess = 1
    PageWriteCopy = 2

class disasm:
    def __init__(self):
        notImplemented()

    def __init__(self, offset):
        notImplemented()

    def __str__(self):
        notImplemented()

    def asm(self, code):
        notImplemented()

    def begin(self):
        notImplemented()

    def current(self):
        notImplemented()

    def disasm(self):
        notImplemented()

    def disasm(self, offset):
        notImplemented()

    def ea(self):
        notImplemented()

    def findOffset(self, arg2):
        notImplemented()

    def instruction(self):
        notImplemented()

    def jump(self, arg2):
        notImplemented()

    def jumprel(self, arg2):
        notImplemented()

    def length(self):
        notImplemented()

    def opcode(self):
        notImplemented()

    def opmnemo(self):
        notImplemented()

    def reset(self):
        notImplemented()

class DbgException(Exception):
    pass

class MemoryException(DbgException):
    pass

class module:
    def __init__(self, arg):
        if isinstance(arg, basestring):
            self._base = script.BaseFromName(arg)
        else:
            self._base = arg

        self._image = script.NameFromAddr(self._base)
        self._size = script.SizeFromAddr(self._base)

        index = self._image.rfind(".")
        if index != -1:
            self._name = self._image[:index]
        else:
            self._name = self._image

        if self._base == 0 or self._size == 0:
            raise DbgException("Failed to get module for %s" % arg)

    def begin(self):
        return self._base

    def size(self):
        return self._size

    def image(self):
        return self._image

    def name(self):
        return self._name

    def end(self):
        return self._base + self._size

    def typedVar(self, typename, va):
        return typedVar(typename, va)<commit_msg>added x64 offsets + fixed typedVarList + remove enum dependency<commit_after>import platform
import traceback
import array
import x64dbgpy.pluginsdk.x64dbg as x64dbg
import x64dbgpy.pluginsdk._scriptapi as script

version = "0.2.0.29"

def notImplemented():
    try:
        raise NotImplementedError()
    except:
        traceback.print_stack()
        raise

def archValue(x32, x64):
    if is64bitSystem():
        return x64
    return x32

def is64bitSystem():
    return platform.architecture()[0] == "64bit"

def dbgCommand(command, suppressOutput = False):
    args = command.split(" ")
    if args[0] == "!teb":
        return "TEB at %x" % getImplicitThread()
    else:
        print "command: %s" % command
        notImplemented()

def dprintln(str, dml = False):
    print str

def findMemoryRegion(va):
    base = script.GetBase(va)
    size = script.GetSize(va)
    if base == 0 or size == 0:
        raise MemoryException("No such address 0x%x" % va)
    return base, size

def findSymbol(va):
    notImplemented()

def getCurrentProcess():
    return x64dbg.DbgGetPebAddress(x64dbg.DbgGetProcessId())

def getCurrentProcessId():
    return x64dbg.DbgGetProcessId()

def getImplicitThread():
    return x64dbg.DbgGetTebAddress(x64dbg.DbgGetThreadId())

def getProcessThreads():
    l = x64dbg.THREADLIST()
    x64dbg.DbgGetThreadList(l)
    result = []
    if l.count > 0:
        return [t.BasicInfo.ThreadLocalBase for t in x64dbg.GetThreadInfoList(l)]
    return result

def getVaProtect(va):
    return memoryProtect(script.GetProtect(va))

def isValid(va):
    return x64dbg.DbgMemIsValidReadPtr(va)

def loadBytes(va, count):
    return list(script.Read(va, count))

def loadChars(va, count):
    return script.Read(va, count)

def loadCStr(va):
    chars = loadChars(va, 256)
    index = chars.find('\0')
    if index != -1:
        return chars[:index]
    return chars

def loadDwords(va, count):
    A = array.array("I")
    A.fromstring(script.Read(va, count * 4))
    return A.tolist()

def loadUnicodeString(va):
    va = int(va)
    # https://msdn.microsoft.com/en-us/library/windows/desktop/aa380518(v=vs.85).aspx
    Length = script.ReadWord(va)
    va += 2
    MaximumLength = script.ReadWord(va)
    va += 2
    if is64bitSystem():
        va += 4
    Buffer = script.ReadPtr(va)
    if Length > MaximumLength or not script.IsValidPtr(Buffer):
        raise DbgException("Corrupted UNICODE_STRING structure")
    A = array.array("u")
    A.fromstring(script.Read(Buffer, Length))
    return A.tounicode().rstrip(u'\0')

def loadWChars(va, count):
    A = array.array("u")
    A.fromstring(script.Read(va, count * 2))
    return A.tounicode()

def loadWStr(va):
    wchars = loadWChars(va, 256)
    index = wchars.find(u'\0')
    if index != -1:
        return wchars[:index]
    return wchars

def ptrDWord(va):
    return script.ReadDword(va)

def writeBytes(va, data):
    A = array.array("B")
    A.fromlist(data)
    script.Write(va, A.tostring())

def reg(name):
    return x64dbg.DbgValFromString(name)

def ptrPtr(va):
    return script.ReadPtr(va)

def typedVar(typename, va):
    if typename == "ntdll!_PEB":
        peb = typeStruct("_PEB", va)
        peb.Ldr = typePtr("_PEB_LDR_DATA*", va + archValue(0x000c, 0x0018))
        peb.ProcessParameters = typePtr("_RTL_USER_PROCESS_PARAMETERS*", va + archValue(0x0010, 0x0020))
        peb.NumberOfHeaps = typeInt32(va + archValue(0x0088, 0x00e8))
        peb.ProcessHeaps = typePtr("void**", va + archValue(0x0090, 0x00f0))
        peb.OSMajorVersion = typeInt16(va + archValue(0x00a4, 0x0118))
        peb.OSMinorVersion = typeInt16(va + archValue(0x00a8, 0x011c))
        peb.OSBuildNumber = typeInt8(va + archValue(0x00ac, 0x0120))
        return peb
    elif typename == "_PEB_LDR_DATA":
        ldr = typeStruct("_PEB_LDR_DATA", va)
        ldr.InLoadOrderModuleList = typeStruct("_LDR_DATA_TABLE_ENTRY", va + archValue(0x000c, 0x0010))
        return ldr
    elif typename == "_TEB":
        teb = typeStruct("_TEB", va)
        teb.Self = va
        return teb
    elif typename in ("_IMAGE_NT_HEADERS", "_IMAGE_NT_HEADERS64"):
        ntheaders = typeStruct(typename, va)
        ntheaders.FileHeader = typedVar("_IMAGE_FILE_HEADER", va + 0x0004)
        ntheaders.OptionalHeader = typedVar(archValue("_IMAGE_OPTIONAL_HEADER", "_IMAGE_OPTIONAL_HEADER64"), va + 0x0018)
        return ntheaders
    elif typename == "_IMAGE_FILE_HEADER":
        fileheader = typeStruct("_IMAGE_FILE_HEADER", va)
        fileheader.NumberOfSections = typeInt16(va + 0x0002)
        fileheader.SizeOfOptionalHeader = typeInt16(va + 0x0010)
        return fileheader
    elif typename in ("_IMAGE_OPTIONAL_HEADER", "_IMAGE_OPTIONAL_HEADER64"):
        optheader = typeStruct(typename, va)
        optheader.SizeOfCode = typeInt32(va + 0x0004)
        optheader.AddressOfEntryPoint = typeInt32(va + 0x0010)
        optheader.BaseOfCode = typeInt32(va + 0x0014)
        if not is64bitSystem():
            optheader.BaseOfData = typeInt32(va + 0x0018)
        optheader.ImageBase = typeInt32(va + archValue(0x001c, 0x0018))
        optionalheadersize = int(typeInt16(va - 0x0004))
        optheader.DataDirectory = [typedVar("_IMAGE_DATA_DIRECTORY", i) for i in range(va + archValue(0x0060, 0x0070), va + optionalheadersize, 8)]
        return optheader
    elif typename == "_IMAGE_DATA_DIRECTORY":
        imgdatadir = typeStruct("_IMAGE_DATA_DIRECTORY", va)
        imgdatadir.VirtualAddress = typeInt32(va)
        imgdatadir.Size = typeInt32(va + 0x0004)
        return imgdatadir
    else:
        print "typename: %s, va: %x" % (typename, va)
        notImplemented()

def typedVarList(va, typename, flink):
    if typename == "ntdll!_LDR_DATA_TABLE_ENTRY" and flink == "InMemoryOrderLinks.Flink":
        start = va + archValue(0x0008, 0x0010)
        va = script.ReadPtr(start)
        result = []

        while va != start and len(result) < 50:
            entry = typeStruct("_LDR_DATA_TABLE_ENTRY", va)
            # This is actually _LDR_DATA_TABLE_ENTRY.FullDllName
            entry.BaseDllName = typeStruct("UNICODE_STRING", va + archValue(0x001c, 0x0038))
            result.append(entry)
            va = script.ReadPtr(va)

        return result
    else:
        notImplemented()

class typeBase(object):
    def __init__(self, name, size, addr = 0):
        self.name = name
        self.size = size
        self.addr = addr

    def getAddress(self):
        return self.addr

    def __int__(self):
        return self.getAddress()

    def __add__(self, other):
        return int(self) + other

class typePrimitive(typeBase):
    def __init__(self, name, size, addr = 0):
        super(typePrimitive, self).__init__(name, size, addr)

    def __int__(self):
        if self.size == 1:
            return script.ReadByte(self.addr)
        elif self.size == 2:
            return script.ReadWord(self.addr)
        elif self.size == 4:
            return script.ReadDword(self.addr)
        elif self.size == 8:
            return script.ReadQword(self.addr)
        else:
            notImplemented()

class typeInt8(typePrimitive):
    def __init__(self, addr = 0):
        super(typeInt8, self).__init__("uint8_t", 1, addr)

class typeInt16(typePrimitive):
    def __init__(self, addr = 0):
        super(typeInt16, self).__init__("uint16_t", 2, addr)

class typeInt32(typePrimitive):
    def __init__(self, addr = 0):
        super(typeInt32, self).__init__("uint32_t", 4, addr)

class typeInt64(typePrimitive):
    def __init__(self, addr = 0):
        super(typeInt64, self).__init__("uint64_t", 8, addr)

class typePtr(typePrimitive):
    def __init__(self, type = "void*", addr = 0):
        super(typePtr, self).__init__(type, archValue(4, 8), addr)

    def deref(self):
        return typedVar(self.name[:-1], int(self))

class typeStruct(typeBase):
    def __init__(self, name, addr):
        super(typeStruct, self).__init__(name, 0, addr)

class memoryProtect(int):
    PageExecute = 16
    PageExecuteWriteCopy = 128
    PageReadOnly = 2
    PageReadWrite = 4
    PageExecuteRead = 32
    PageExecuteReadWrite = 64
    PageNoAccess = 1
    PageWriteCopy = 2

class disasm:
    def __init__(self):
        notImplemented()

    def __init__(self, offset):
        notImplemented()

    def __str__(self):
        notImplemented()

    def asm(self, code):
        notImplemented()

    def begin(self):
        notImplemented()

    def current(self):
        notImplemented()

    def disasm(self):
        notImplemented()

    def disasm(self, offset):
        notImplemented()

    def ea(self):
        notImplemented()

    def findOffset(self, arg2):
        notImplemented()

    def instruction(self):
        notImplemented()

    def jump(self, arg2):
        notImplemented()

    def jumprel(self, arg2):
        notImplemented()

    def length(self):
        notImplemented()

    def opcode(self):
        notImplemented()

    def opmnemo(self):
        notImplemented()

    def reset(self):
        notImplemented()

class DbgException(Exception):
    pass

class MemoryException(DbgException):
    pass

class module:
    def __init__(self, arg):
        if isinstance(arg, basestring):
            self._base = script.BaseFromName(arg)
        else:
            self._base = arg

        self._image = script.NameFromAddr(self._base)
        self._size = script.SizeFromAddr(self._base)

        index = self._image.rfind(".")
        if index != -1:
            self._name = self._image[:index]
        else:
            self._name = self._image

        if self._base == 0 or self._size == 0:
            raise DbgException("Failed to get module for %s" % arg)

    def begin(self):
        return self._base

    def size(self):
        return self._size

    def image(self):
        return self._image

    def name(self):
        return self._name

    def end(self):
        return self._base + self._size

    def typedVar(self, typename, va):
        return typedVar(typename, va)<|endoftext|>


<commit_before>#!/usr/bin/env python

from __future__ import print_function

import getopt
import os
import sys

import numpy
import pylab

import compare
import mesh.patch as patch
from util import msg, profile, runparams

usage = """
       usage:

      ./pyro [options] <solver> <problem> <input file> [runtime parameters]

      <solver> is one of:
          advection
          compressible
          diffusion
          incompressible

      <problem> is one of the problems defined in the solver's problems/
      sub-directory

      <input file> is the inputs file to use for that problem.  You can
      refer to a file in the solver's problem directory directly, without
      giving the path

      [options] include:
         --make_benchmark : store the output of this run as the new
                            reference solution in the solver's tests/
                            sub-directory

         --compare_benchmark : compare the final result of this run
                               to the stored benchmark for this problem
                               (looking in the solver's tests/ sub-
                               directory).

      [runtime parameters] override any of the runtime defaults of
      parameters specified in the inputs file.  For instance, to turn
      off runtime visualization, add:

         vis.dovis=0

      to the end of the commandline.

      """

msg.bold('pyro ...')

tc = profile.TimerCollection()

tm_main = tc.timer("main")
tm_main.begin()

#-----------------------------------------------------------------------------
# command line arguments / solver setup
#-----------------------------------------------------------------------------

# parse the runtime arguments.  We specify a solver (which we import
# locally under the namespace 'solver', the problem name, and the
# input file name

if len(sys.argv) == 1:
    print(usage)
    sys.exit(2)

# commandline argument defaults
make_bench = 0
comp_bench = 0

try: opts, next = getopt.getopt(sys.argv[1:], "",
                                ["make_benchmark", "compare_benchmark"])

except getopt.GetoptError:
    msg.fail("invalid calling sequence")
    sys.exit(2)

for o, a in opts:

    if o == "--make_benchmark":
        make_bench = 1

    if o == "--compare_benchmark":
        comp_bench = 1

if make_bench and comp_bench:
    msg.fail("ERROR: cannot have both --make_benchmark and --compare_benchmark")

try: solver_name = next[0]
except IndexError:
    print(usage)
    msg.fail("ERROR: solver name not specified on command line")

try: problem_name = next[1]
except IndexError:
    print(usage)
    msg.fail("ERROR: problem name not specified on command line")

try: param_file = next[2]
except IndexError:
    print(usage)
    msg.fail("ERROR: parameter file not specified on command line")

other_commands = []
if len(next) > 3:
    other_commands = next[3:]

# actually import the solver-specific stuff under the 'solver' namespace
exec('import ' + solver_name + ' as solver')

#-----------------------------------------------------------------------------
# runtime parameters
#-----------------------------------------------------------------------------

# parameter defaults
rp = runparams.RuntimeParameters()
rp.load_params("_defaults")
rp.load_params(solver_name + "/_defaults")

# problem-specific runtime parameters
rp.load_params(solver_name + "/problems/_" + problem_name + ".defaults")

# now read in the inputs file
if not os.path.isfile(param_file):
    # check if the param file lives in the solver's problems directory
    param_file = solver_name + "/problems/" + param_file
    if not os.path.isfile(param_file):
        msg.fail("ERROR: inputs file does not exist")

rp.load_params(param_file, no_new=1)

# and any commandline overrides
rp.command_line_params(other_commands)

# write out the inputs.auto
rp.print_paramfile()

#-----------------------------------------------------------------------------
# initialization
#-----------------------------------------------------------------------------

# initialize the Simulation object -- this will hold the grid and data and
# know about the runtime parameters and which problem we are running
sim = solver.Simulation(problem_name, rp, timers=tc)

sim.initialize()
sim.preevolve()

#-----------------------------------------------------------------------------
# evolve
#-----------------------------------------------------------------------------
tmax = rp.get_param("driver.tmax")
max_steps = rp.get_param("driver.max_steps")

init_tstep_factor = rp.get_param("driver.init_tstep_factor")
max_dt_change = rp.get_param("driver.max_dt_change")
fix_dt = rp.get_param("driver.fix_dt")

pylab.ion()

n = 0
sim.cc_data.t = 0.0

# output the 0th data
basename = rp.get_param("io.basename")
sim.cc_data.write(basename + "%4.4d" % (n))

dovis = rp.get_param("vis.dovis")
if dovis:
    pylab.figure(num=1, figsize=(8,6), dpi=100, facecolor='w')
    sim.dovis()

nout = 0

while sim.cc_data.t < tmax and n < max_steps:

    # fill boundary conditions
    tm_bc = tc.timer("fill_bc")
    tm_bc.begin()
    sim.cc_data.fill_BC_all()
    tm_bc.end()

    # get the timestep
    dt = sim.timestep()
    if fix_dt > 0.0:
        dt = fix_dt
    else:
        if n == 0:
            dt = init_tstep_factor*dt
            dt_old = dt
        else:
            dt = min(max_dt_change*dt_old, dt)
            dt_old = dt

    if sim.cc_data.t + dt > tmax:
        dt = tmax - sim.cc_data.t

    # evolve for a single timestep
    sim.evolve(dt)

    # increment the time
    sim.cc_data.t += dt
    n += 1
    print("%5d %10.5f %10.5f" % (n, sim.cc_data.t, dt))

    # output
    dt_out = rp.get_param("io.dt_out")
    n_out = rp.get_param("io.n_out")

    if sim.cc_data.t >= (nout + 1)*dt_out or n%n_out == 0:

        tm_io = tc.timer("output")
        tm_io.begin()

        msg.warning("outputting...")
        basename = rp.get_param("io.basename")
        sim.cc_data.write(basename + "%4.4d" % (n))
        nout += 1

        tm_io.end()

    # visualization
    if dovis:
        tm_vis = tc.timer("vis")
        tm_vis.begin()

        sim.dovis()
        store = rp.get_param("vis.store_images")

        if store == 1:
            basename = rp.get_param("io.basename")
            pylab.savefig(basename + "%4.4d" % (n) + ".png")

        tm_vis.end()

tm_main.end()

#-----------------------------------------------------------------------------
# benchmarks (for regression testing)
#-----------------------------------------------------------------------------
# are we comparing to a benchmark?
if comp_bench:
    compare_file = solver_name + "/tests/" + basename + "%4.4d" % (n)
    msg.warning("comparing to: %s " % (compare_file) )
    bench_grid, bench_data = patch.read(compare_file)

    result = compare.compare(sim.cc_data.grid, sim.cc_data, bench_grid, bench_data)

    if result == 0:
        msg.success("results match benchmark\n")
    else:
        msg.fail("ERROR: " + compare.errors[result] + "\n")

# are we storing a benchmark?
if make_bench:
    bench_file = solver_name + "/tests/" + basename + "%4.4d" % (n)
    msg.warning("storing new benchmark: %s\n " % (bench_file) )
    sim.cc_data.write(bench_file)

#-----------------------------------------------------------------------------
# final reports
#-----------------------------------------------------------------------------
rp.print_unused_params()
tc.report()

sim.finalize()

<commit_msg>numpy not needed<commit_after>#!/usr/bin/env python

from __future__ import print_function

import getopt
import os
import sys

import pylab

import compare
import mesh.patch as patch
from util import msg, profile, runparams

usage = """
       usage:

      ./pyro [options] <solver> <problem> <input file> [runtime parameters]

      <solver> is one of:
          advection
          compressible
          diffusion
          incompressible

      <problem> is one of the problems defined in the solver's problems/
      sub-directory

      <input file> is the inputs file to use for that problem.  You can
      refer to a file in the solver's problem directory directly, without
      giving the path

      [options] include:
         --make_benchmark : store the output of this run as the new
                            reference solution in the solver's tests/
                            sub-directory

         --compare_benchmark : compare the final result of this run
                               to the stored benchmark for this problem
                               (looking in the solver's tests/ sub-
                               directory).

      [runtime parameters] override any of the runtime defaults of
      parameters specified in the inputs file.  For instance, to turn
      off runtime visualization, add:

         vis.dovis=0

      to the end of the commandline.

      """

msg.bold('pyro ...')

tc = profile.TimerCollection()

tm_main = tc.timer("main")
tm_main.begin()

#-----------------------------------------------------------------------------
# command line arguments / solver setup
#-----------------------------------------------------------------------------

# parse the runtime arguments.  We specify a solver (which we import
# locally under the namespace 'solver', the problem name, and the
# input file name

if len(sys.argv) == 1:
    print(usage)
    sys.exit(2)

# commandline argument defaults
make_bench = 0
comp_bench = 0

try: opts, next = getopt.getopt(sys.argv[1:], "",
                                ["make_benchmark", "compare_benchmark"])

except getopt.GetoptError:
    msg.fail("invalid calling sequence")
    sys.exit(2)

for o, a in opts:

    if o == "--make_benchmark":
        make_bench = 1

    if o == "--compare_benchmark":
        comp_bench = 1

if make_bench and comp_bench:
    msg.fail("ERROR: cannot have both --make_benchmark and --compare_benchmark")

try: solver_name = next[0]
except IndexError:
    print(usage)
    msg.fail("ERROR: solver name not specified on command line")

try: problem_name = next[1]
except IndexError:
    print(usage)
    msg.fail("ERROR: problem name not specified on command line")

try: param_file = next[2]
except IndexError:
    print(usage)
    msg.fail("ERROR: parameter file not specified on command line")

other_commands = []
if len(next) > 3:
    other_commands = next[3:]

# actually import the solver-specific stuff under the 'solver' namespace
exec('import ' + solver_name + ' as solver')

#-----------------------------------------------------------------------------
# runtime parameters
#-----------------------------------------------------------------------------

# parameter defaults
rp = runparams.RuntimeParameters()
rp.load_params("_defaults")
rp.load_params(solver_name + "/_defaults")

# problem-specific runtime parameters
rp.load_params(solver_name + "/problems/_" + problem_name + ".defaults")

# now read in the inputs file
if not os.path.isfile(param_file):
    # check if the param file lives in the solver's problems directory
    param_file = solver_name + "/problems/" + param_file
    if not os.path.isfile(param_file):
        msg.fail("ERROR: inputs file does not exist")

rp.load_params(param_file, no_new=1)

# and any commandline overrides
rp.command_line_params(other_commands)

# write out the inputs.auto
rp.print_paramfile()

#-----------------------------------------------------------------------------
# initialization
#-----------------------------------------------------------------------------

# initialize the Simulation object -- this will hold the grid and data and
# know about the runtime parameters and which problem we are running
sim = solver.Simulation(problem_name, rp, timers=tc)

sim.initialize()
sim.preevolve()

#-----------------------------------------------------------------------------
# evolve
#-----------------------------------------------------------------------------
tmax = rp.get_param("driver.tmax")
max_steps = rp.get_param("driver.max_steps")

init_tstep_factor = rp.get_param("driver.init_tstep_factor")
max_dt_change = rp.get_param("driver.max_dt_change")
fix_dt = rp.get_param("driver.fix_dt")

pylab.ion()

n = 0
sim.cc_data.t = 0.0

# output the 0th data
basename = rp.get_param("io.basename")
sim.cc_data.write(basename + "%4.4d" % (n))

dovis = rp.get_param("vis.dovis")
if dovis:
    pylab.figure(num=1, figsize=(8,6), dpi=100, facecolor='w')
    sim.dovis()

nout = 0

while sim.cc_data.t < tmax and n < max_steps:

    # fill boundary conditions
    tm_bc = tc.timer("fill_bc")
    tm_bc.begin()
    sim.cc_data.fill_BC_all()
    tm_bc.end()

    # get the timestep
    dt = sim.timestep()
    if fix_dt > 0.0:
        dt = fix_dt
    else:
        if n == 0:
            dt = init_tstep_factor*dt
            dt_old = dt
        else:
            dt = min(max_dt_change*dt_old, dt)
            dt_old = dt

    if sim.cc_data.t + dt > tmax:
        dt = tmax - sim.cc_data.t

    # evolve for a single timestep
    sim.evolve(dt)

    # increment the time
    sim.cc_data.t += dt
    n += 1
    print("%5d %10.5f %10.5f" % (n, sim.cc_data.t, dt))

    # output
    dt_out = rp.get_param("io.dt_out")
    n_out = rp.get_param("io.n_out")

    if sim.cc_data.t >= (nout + 1)*dt_out or n%n_out == 0:

        tm_io = tc.timer("output")
        tm_io.begin()

        msg.warning("outputting...")
        basename = rp.get_param("io.basename")
        sim.cc_data.write(basename + "%4.4d" % (n))
        nout += 1

        tm_io.end()

    # visualization
    if dovis:
        tm_vis = tc.timer("vis")
        tm_vis.begin()

        sim.dovis()
        store = rp.get_param("vis.store_images")

        if store == 1:
            basename = rp.get_param("io.basename")
            pylab.savefig(basename + "%4.4d" % (n) + ".png")

        tm_vis.end()

tm_main.end()

#-----------------------------------------------------------------------------
# benchmarks (for regression testing)
#-----------------------------------------------------------------------------
# are we comparing to a benchmark?
if comp_bench:
    compare_file = solver_name + "/tests/" + basename + "%4.4d" % (n)
    msg.warning("comparing to: %s " % (compare_file) )
    bench_grid, bench_data = patch.read(compare_file)

    result = compare.compare(sim.cc_data.grid, sim.cc_data, bench_grid, bench_data)

    if result == 0:
        msg.success("results match benchmark\n")
    else:
        msg.fail("ERROR: " + compare.errors[result] + "\n")

# are we storing a benchmark?
if make_bench:
    bench_file = solver_name + "/tests/" + basename + "%4.4d" % (n)
    msg.warning("storing new benchmark: %s\n " % (bench_file) )
    sim.cc_data.write(bench_file)

#-----------------------------------------------------------------------------
# final reports
#-----------------------------------------------------------------------------
rp.print_unused_params()
tc.report()

sim.finalize()

<|endoftext|>


<commit_before>from django.conf import settings
from django.contrib.auth.models import User, Group
from rest_framework import serializers
from rest_auth.registration.serializers import SocialLoginSerializer
from .models import *

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'id', 'username', 'email', 'first_name', 'last_name', 'posts')

class PostSerializer(serializers.ModelSerializer):

    class Meta:
        model = Post
        fields = ('id', 'url', 'date', 'content', 'owner', 'phase')

class PhaseSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Phase
        fields = ('id', 'url', 'name', 'description', 'isactive', 'valid_to')

class CampaignSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Campaign
        fields = ('url', 'id', 'name', 'description', 'isactive', 'valid_to')

class SocialAccountSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = SocialAccount
        fields = ('url', 'id', 'username', 'email', 'access_token', 'token_expiration', 'isactive', 'valid_to')

class SocialGroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = SocialGroup
        fields = ('url', 'id', 'name', 'description', 'isactive', 'valid_to')

class SocialAccountGroupSerializer(serializers.HyperlinkedModelSerializer):
    #socialaccounts = SocialAccountSerializer(many=True, read_only=True)
    class Meta:
        model = SocialAccountGroup
        fields = ('url', 'id', 'socialaccount', 'socialgroup', 'isactive', 'valid_to')

class BucketSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Bucket
        fields = ('url', 'name')

class StaticPageSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = StaticPage
        fields = ('__all__')
<commit_msg>UPDATE User serializer with ModelSerializer<commit_after>from django.conf import settings
from django.contrib.auth.models import User, Group
from rest_framework import serializers
from rest_auth.registration.serializers import SocialLoginSerializer
from .models import *

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'first_name', 'last_name', 'posts')

class PostSerializer(serializers.ModelSerializer):

    class Meta:
        model = Post
        fields = ('id', 'url', 'date', 'content', 'owner', 'phase')

class PhaseSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Phase
        fields = ('id', 'url', 'name', 'description', 'isactive', 'valid_to')

class CampaignSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Campaign
        fields = ('url', 'id', 'name', 'description', 'isactive', 'valid_to')

class SocialAccountSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = SocialAccount
        fields = ('url', 'id', 'username', 'email', 'access_token', 'token_expiration', 'isactive', 'valid_to')

class SocialGroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = SocialGroup
        fields = ('url', 'id', 'name', 'description', 'isactive', 'valid_to')

class SocialAccountGroupSerializer(serializers.HyperlinkedModelSerializer):
    #socialaccounts = SocialAccountSerializer(many=True, read_only=True)
    class Meta:
        model = SocialAccountGroup
        fields = ('url', 'id', 'socialaccount', 'socialgroup', 'isactive', 'valid_to')

class BucketSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Bucket
        fields = ('url', 'name')

class StaticPageSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = StaticPage
        fields = ('__all__')
<|endoftext|>


<commit_before>from types import StringTypes
import time
import re

from PLC.Faults import *
from PLC.Parameter import Parameter
from PLC.Filter import Filter
from PLC.Debug import profile
from PLC.Table import Row, Table
from PLC.SliceInstantiations import SliceInstantiation, SliceInstantiations
from PLC.Nodes import Node, Nodes
from PLC.Persons import Person, Persons

class Slice(Row):
    """
    Representation of a row in the slices table. To use, optionally
    instantiate with a dict of values. Update as you would a
    dict. Commit to the database with sync().To use, instantiate
    with a dict of values.
    """

    table_name = 'slices'
    primary_key = 'slice_id'
    join_tables = ['slice_node', 'slice_person', 'slice_attribute', 'peer_slice']
    fields = {
        'slice_id': Parameter(int, "Slice identifier"),
        'site_id': Parameter(int, "Identifier of the site to which this slice belongs"),
        'name': Parameter(str, "Slice name", max = 32),
        'instantiation': Parameter(str, "Slice instantiation state"),
        'url': Parameter(str, "URL further describing this slice", max = 254, nullok = True),
        'description': Parameter(str, "Slice description", max = 2048, nullok = True),
        'max_nodes': Parameter(int, "Maximum number of nodes that can be assigned to this slice"),
        'creator_person_id': Parameter(int, "Identifier of the account that created this slice"),
        'created': Parameter(int, "Date and time when slice was created, in seconds since UNIX epoch", ro = True),
        'expires': Parameter(int, "Date and time when slice expires, in seconds since UNIX epoch"),
        'node_ids': Parameter([int], "List of nodes in this slice", ro = True),
        'person_ids': Parameter([int], "List of accounts that can use this slice", ro = True),
        'slice_attribute_ids': Parameter([int], "List of slice attributes", ro = True),
        'peer_id': Parameter(int, "Peer to which this slice belongs", nullok = True),
        'peer_slice_id': Parameter(int, "Foreign slice identifier at peer", nullok = True),
        }
    # for Cache
    class_key = 'name'
    foreign_fields = ['instantiation', 'url', 'description', 'max_nodes', 'expires']
    foreign_xrefs = [
        {'field': 'node_ids' ,         'class': 'Node',   'table': 'slice_node' },
	{'field': 'person_ids',        'class': 'Person', 'table': 'slice_person'},
	{'field': 'creator_person_id', 'class': 'Person', 'table': 'unused-on-direct-refs'},
        {'field': 'site_id',           'class': 'Site',   'table': 'unused-on-direct-refs'},
    ]
    # forget about this one, it is read-only anyway
    # handling it causes Cache to re-sync all over again
    # 'created'

    def validate_name(self, name):
        # N.B.: Responsibility of the caller to ensure that login_base
        # portion of the slice name corresponds to a valid site, if
        # desired.

        # 1. Lowercase.
        # 2. Begins with login_base (letters or numbers).
        # 3. Then single underscore after login_base.
        # 4. Then letters, numbers, or underscores.
        good_name = r'^[a-z0-9]+_[a-zA-Z0-9_]+$'
        if not name or \
           not re.match(good_name, name):
            raise PLCInvalidArgument, "Invalid slice name"

        conflicts = Slices(self.api, [name])
        for slice in conflicts:
            if 'slice_id' not in self or self['slice_id'] != slice['slice_id']:
                raise PLCInvalidArgument, "Slice name already in use, %s"%name

        return name

    def validate_instantiation(self, instantiation):
        instantiations = [row['instantiation'] for row in SliceInstantiations(self.api)]
        if instantiation not in instantiations:
            raise PLCInvalidArgument, "No such instantiation state"

        return instantiation

    validate_created = Row.validate_timestamp

    def validate_expires(self, expires):
        # N.B.: Responsibility of the caller to ensure that expires is
        # not too far into the future.
        check_future = not ('is_deleted' in self and self['is_deleted'])
        return Row.validate_timestamp(self, expires, check_future = check_future)

    add_person = Row.add_object(Person, 'slice_person')
    remove_person = Row.remove_object(Person, 'slice_person')

    add_node = Row.add_object(Node, 'slice_node')
    remove_node = Row.remove_object(Node, 'slice_node')

    def sync(self, commit = True):
        """
        Add or update a slice.
        """

        # Before a new slice is added, delete expired slices
        if 'slice_id' not in self:
            expired = Slices(self.api, expires = -int(time.time()))
            for slice in expired:
                slice.delete(commit)

        Row.sync(self, commit)

    def delete(self, commit = True):
        """
        Delete existing slice.
        """

        assert 'slice_id' in self

        # Clean up miscellaneous join tables
        for table in self.join_tables:
            self.api.db.do("DELETE FROM %s WHERE slice_id = %d" % \
                           (table, self['slice_id']))

        # Mark as deleted
        self['is_deleted'] = True
        self.sync(commit)

class Slices(Table):
    """
    Representation of row(s) from the slices table in the
    database.
    """

    def __init__(self, api, slice_filter = None, columns = None, expires = int(time.time())):
        Table.__init__(self, api, Slice, columns)

        sql = "SELECT %s FROM view_slices WHERE is_deleted IS False" % \
              ", ".join(self.columns)

        if expires is not None:
            if expires >= 0:
                sql += " AND expires > %d" % expires
            else:
                expires = -expires
                sql += " AND expires < %d" % expires

        if slice_filter is not None:
            if isinstance(slice_filter, (list, tuple, set)):
                # Separate the list into integers and strings
                ints = filter(lambda x: isinstance(x, (int, long)), slice_filter)
                strs = filter(lambda x: isinstance(x, StringTypes), slice_filter)
                slice_filter = Filter(Slice.fields, {'slice_id': ints, 'name': strs})
                sql += " AND (%s)" % slice_filter.sql(api, "OR")
            elif isinstance(slice_filter, dict):
                slice_filter = Filter(Slice.fields, slice_filter)
                sql += " AND (%s)" % slice_filter.sql(api, "AND")

        self.selectall(sql)
<commit_msg>- added add_to_node_whitelist() function - added delete_from_node_whitelist() function<commit_after>from types import StringTypes
import time
import re

from PLC.Faults import *
from PLC.Parameter import Parameter
from PLC.Filter import Filter
from PLC.Debug import profile
from PLC.Table import Row, Table
from PLC.SliceInstantiations import SliceInstantiation, SliceInstantiations
from PLC.Nodes import Node
from PLC.Persons import Person, Persons

class Slice(Row):
    """
    Representation of a row in the slices table. To use, optionally
    instantiate with a dict of values. Update as you would a
    dict. Commit to the database with sync().To use, instantiate
    with a dict of values.
    """

    table_name = 'slices'
    primary_key = 'slice_id'
    join_tables = ['slice_node', 'slice_person', 'slice_attribute', 'peer_slice', 'node_slice_whitelist']
    fields = {
        'slice_id': Parameter(int, "Slice identifier"),
        'site_id': Parameter(int, "Identifier of the site to which this slice belongs"),
        'name': Parameter(str, "Slice name", max = 32),
        'instantiation': Parameter(str, "Slice instantiation state"),
        'url': Parameter(str, "URL further describing this slice", max = 254, nullok = True),
        'description': Parameter(str, "Slice description", max = 2048, nullok = True),
        'max_nodes': Parameter(int, "Maximum number of nodes that can be assigned to this slice"),
        'creator_person_id': Parameter(int, "Identifier of the account that created this slice"),
        'created': Parameter(int, "Date and time when slice was created, in seconds since UNIX epoch", ro = True),
        'expires': Parameter(int, "Date and time when slice expires, in seconds since UNIX epoch"),
        'node_ids': Parameter([int], "List of nodes in this slice", ro = True),
        'person_ids': Parameter([int], "List of accounts that can use this slice", ro = True),
        'slice_attribute_ids': Parameter([int], "List of slice attributes", ro = True),
        'peer_id': Parameter(int, "Peer to which this slice belongs", nullok = True),
        'peer_slice_id': Parameter(int, "Foreign slice identifier at peer", nullok = True),
        }
    # for Cache
    class_key = 'name'
    foreign_fields = ['instantiation', 'url', 'description', 'max_nodes', 'expires']
    foreign_xrefs = [
        {'field': 'node_ids' ,         'class': 'Node',   'table': 'slice_node' },
	{'field': 'person_ids',        'class': 'Person', 'table': 'slice_person'},
	{'field': 'creator_person_id', 'class': 'Person', 'table': 'unused-on-direct-refs'},
        {'field': 'site_id',           'class': 'Site',   'table': 'unused-on-direct-refs'},
    ]
    # forget about this one, it is read-only anyway
    # handling it causes Cache to re-sync all over again
    # 'created'

    def validate_name(self, name):
        # N.B.: Responsibility of the caller to ensure that login_base
        # portion of the slice name corresponds to a valid site, if
        # desired.

        # 1. Lowercase.
        # 2. Begins with login_base (letters or numbers).
        # 3. Then single underscore after login_base.
        # 4. Then letters, numbers, or underscores.
        good_name = r'^[a-z0-9]+_[a-zA-Z0-9_]+$'
        if not name or \
           not re.match(good_name, name):
            raise PLCInvalidArgument, "Invalid slice name"

        conflicts = Slices(self.api, [name])
        for slice in conflicts:
            if 'slice_id' not in self or self['slice_id'] != slice['slice_id']:
                raise PLCInvalidArgument, "Slice name already in use, %s"%name

        return name

    def validate_instantiation(self, instantiation):
        instantiations = [row['instantiation'] for row in SliceInstantiations(self.api)]
        if instantiation not in instantiations:
            raise PLCInvalidArgument, "No such instantiation state"

        return instantiation

    validate_created = Row.validate_timestamp

    def validate_expires(self, expires):
        # N.B.: Responsibility of the caller to ensure that expires is
        # not too far into the future.
        check_future = not ('is_deleted' in self and self['is_deleted'])
        return Row.validate_timestamp(self, expires, check_future = check_future)

    add_person = Row.add_object(Person, 'slice_person')
    remove_person = Row.remove_object(Person, 'slice_person')

    add_node = Row.add_object(Node, 'slice_node')
    remove_node = Row.remove_object(Node, 'slice_node')

    add_to_node_whitelist = Row.add_object(Node, 'node_slice_whitelist')
    delete_from_node_whitelist = Row.remove_object(Node, 'node_slice_whitelist')

    def sync(self, commit = True):
        """
        Add or update a slice.
        """

        # Before a new slice is added, delete expired slices
        if 'slice_id' not in self:
            expired = Slices(self.api, expires = -int(time.time()))
            for slice in expired:
                slice.delete(commit)

        Row.sync(self, commit)

    def delete(self, commit = True):
        """
        Delete existing slice.
        """

        assert 'slice_id' in self

        # Clean up miscellaneous join tables
        for table in self.join_tables:
            self.api.db.do("DELETE FROM %s WHERE slice_id = %d" % \
                           (table, self['slice_id']))

        # Mark as deleted
        self['is_deleted'] = True
        self.sync(commit)

class Slices(Table):
    """
    Representation of row(s) from the slices table in the
    database.
    """

    def __init__(self, api, slice_filter = None, columns = None, expires = int(time.time())):
        Table.__init__(self, api, Slice, columns)

        sql = "SELECT %s FROM view_slices WHERE is_deleted IS False" % \
              ", ".join(self.columns)

        if expires is not None:
            if expires >= 0:
                sql += " AND expires > %d" % expires
            else:
                expires = -expires
                sql += " AND expires < %d" % expires

        if slice_filter is not None:
            if isinstance(slice_filter, (list, tuple, set)):
                # Separate the list into integers and strings
                ints = filter(lambda x: isinstance(x, (int, long)), slice_filter)
                strs = filter(lambda x: isinstance(x, StringTypes), slice_filter)
                slice_filter = Filter(Slice.fields, {'slice_id': ints, 'name': strs})
                sql += " AND (%s)" % slice_filter.sql(api, "OR")
            elif isinstance(slice_filter, dict):
                slice_filter = Filter(Slice.fields, slice_filter)
                sql += " AND (%s)" % slice_filter.sql(api, "AND")

        self.selectall(sql)
<|endoftext|>


<commit_before># This code is licensed under the 3-Clause BSD License.

"""
qlqw dumps your queue into ${XDG_RUNTIME_DIR} on every song change.
"""

import os
import threading
import urllib.parse

from gi.repository import Gtk, GLib

from quodlibet import _
from quodlibet import app
from quodlibet.plugins.events import EventPlugin
from quodlibet.qltk import Icons
from quodlibet.util.dprint import print_d
from quodlibet.errorreport import errorhook
from quodlibet import commands

LOGGING_IS_ENABLED = False

class QlqwError(RuntimeError):
    """Do-nothing exception specific to qlqw."""

class QlqwBackend:
    """Provides threaded I/O for queue writing."""

    FILE_PREFIX = "file://"
    TARGET_PATH = os.path.join(
        os.getenv("XDG_RUNTIME_DIR"),
        "qlqw.txt"
    )

    def __init__(self):
        self.called_event = threading.Event()
        self.last_queue_hash = None

    def parse_queue(self, dumped_queue_string):
        parsed = list()

        for line in urllib.parse.unquote(dumped_queue_string).splitlines():
            if not line.startswith(self.FILE_PREFIX):
                raise QlqwError("unexpected queue entry: ``{}''".format(line))

            path = line[len(self.FILE_PREFIX):]
            if not os.path.exists(path):
                raise QlqwError("nonexistent queue entry: ``{}''".format(path))

            parsed.append(path)

        return parsed

    def get_queue(self):
        dumped_string = commands.registry.run(app, "dump-queue")
        queue_hash = hash(dumped_string)
        if not dumped_string or queue_hash == self.last_queue_hash:
            return None

        self.last_queue_hash = queue_hash
        return self.parse_queue(dumped_string)

    def commit_queue(self, queue_list):
        with open(self.TARGET_PATH, "w") as qfp:
            for line in queue_list:
                qfp.write(line)
                qfp.write("\n")

    def get_and_commit_queue(self):
        queue_list = self.get_queue()
        if queue_list is not None:
            self.commit_queue(queue_list)

    def fire(self):
        """
        Arms this backend to write the queue. Called from the main
        plugin context.
        """
        self.called_event.set()

    def _run_loop(self):
        self.called_event.wait()
        self.called_event.clear()
        self.get_and_commit_queue()

    def run_loop(self):
        """
        Main entry point of this class. Waits for the main plugin
        context to request a queue write. Operates from the I/O context.
        """
        while True:
            try:
                self._run_loop()
            except Exception:
                errorhook()

class Qlqw(EventPlugin):
    """
    This class does nothing but deliver queue write requests to the
    backend (residing in a separate I/O thread). It does not fail
    meaningfully, deferring error handling to said backend.
    """

    PLUGIN_ID = "qlqw"
    PLUGIN_NAME = _("Write Queue Aggressively")
    PLUGIN_DESC = _("Commits queue contents to disk on every song "
                    "change.")
    PLUGIN_ICON = Icons.DIALOG_ERROR

    def __init__(self):
        self.__enabled = False
        self.backend = QlqwBackend()

        backend_thread = threading.Thread(None, self.backend.run_loop)
        backend_thread.setDaemon(True)
        backend_thread.start()

    def plugin_on_song_started(self, song):
        if not self.__enabled:
            return

        # TODO(j39m): rate-limit the present method.
        self.backend.fire()

    def enabled(self):
        self.__enabled = True

    def disabled(self):
        self.__enabled = False
<commit_msg>qlqw: Add newlines<commit_after># This code is licensed under the 3-Clause BSD License.

"""
qlqw dumps your queue into ${XDG_RUNTIME_DIR} on every song change.
"""

import os
import threading
import urllib.parse

from gi.repository import Gtk, GLib

from quodlibet import _
from quodlibet import app
from quodlibet.plugins.events import EventPlugin
from quodlibet.qltk import Icons
from quodlibet.util.dprint import print_d
from quodlibet.errorreport import errorhook
from quodlibet import commands

LOGGING_IS_ENABLED = False

class QlqwError(RuntimeError):
    """Do-nothing exception specific to qlqw."""

class QlqwBackend:
    """Provides threaded I/O for queue writing."""

    FILE_PREFIX = "file://"
    TARGET_PATH = os.path.join(
        os.getenv("XDG_RUNTIME_DIR"),
        "qlqw.txt"
    )

    def __init__(self):
        self.called_event = threading.Event()
        self.last_queue_hash = None

    def parse_queue(self, dumped_queue_string):
        parsed = list()

        for line in urllib.parse.unquote(dumped_queue_string).splitlines():
            if not line.startswith(self.FILE_PREFIX):
                raise QlqwError("unexpected queue entry: ``{}''".format(line))

            path = line[len(self.FILE_PREFIX):]
            if not os.path.exists(path):
                raise QlqwError("nonexistent queue entry: ``{}''".format(path))

            parsed.append(path)

        return parsed

    def get_queue(self):
        dumped_string = commands.registry.run(app, "dump-queue")
        queue_hash = hash(dumped_string)
        if not dumped_string or queue_hash == self.last_queue_hash:
            return None

        self.last_queue_hash = queue_hash
        return self.parse_queue(dumped_string)

    def commit_queue(self, queue_list):
        with open(self.TARGET_PATH, "w") as qfp:
            for line in queue_list:
                qfp.write(line)
                qfp.write("\n")

    def get_and_commit_queue(self):
        queue_list = self.get_queue()
        if queue_list is not None:
            self.commit_queue(queue_list)

    def fire(self):
        """
        Arms this backend to write the queue. Called from the main
        plugin context.
        """
        self.called_event.set()

    def _run_loop(self):
        self.called_event.wait()
        self.called_event.clear()
        self.get_and_commit_queue()

    def run_loop(self):
        """
        Main entry point of this class. Waits for the main plugin
        context to request a queue write. Operates from the I/O context.
        """
        while True:
            try:
                self._run_loop()
            except Exception:
                errorhook()

class Qlqw(EventPlugin):
    """
    This class does nothing but deliver queue write requests to the
    backend (residing in a separate I/O thread). It does not fail
    meaningfully, deferring error handling to said backend.
    """

    PLUGIN_ID = "qlqw"
    PLUGIN_NAME = _("Write Queue Aggressively")
    PLUGIN_DESC = _("Commits queue contents to disk on every song "
                    "change.")
    PLUGIN_ICON = Icons.DIALOG_ERROR

    def __init__(self):
        self.__enabled = False
        self.backend = QlqwBackend()

        backend_thread = threading.Thread(None, self.backend.run_loop)
        backend_thread.setDaemon(True)
        backend_thread.start()

    def plugin_on_song_started(self, song):
        if not self.__enabled:
            return

        # TODO(j39m): rate-limit the present method.
        self.backend.fire()

    def enabled(self):
        self.__enabled = True

    def disabled(self):
        self.__enabled = False
<|endoftext|>


<commit_before>import copy
import functools
import inspect

def identity(value):
    return value

def raise_exception(exception):
    raise exception

def ignore(value):
    pass

class Error(Exception):
    pass

class UnmatchedValueError(Error):
    def __init__(self, value):
        self.value = value
        super(Error, self).__init__(str(value))

def match(*args):
    def get_map_function(value):
        for is_match, map_function in args:
            if is_match(value):
                return map_function
        raise UnmatchedValueError(value)
    return lambda error: get_map_function(error)(error)

def match_type(*args):
    return match(*[
        (lambda value, types=types: isinstance(value, types), map_function)
        for types, map_function in args
    ])

class PositionalArg(object):
    NO_VALUE = object()
    NO_DEFAULT = object()

    def __init__(self, name, default, value):
        self.name = name
        self.default = default
        self.value = value

    @classmethod
    def from_name(cls, name, default=NO_DEFAULT):
        return cls(name, default, value=PositionalArg.NO_VALUE)

    def has_value(self):
        return self.value != PositionalArg.NO_VALUE

    def has_value_or_default(self):
        return self.has_value() or self.default != PositionalArg.NO_DEFAULT

    def value_or_default(self):
        return self.value if self.has_value() else self.default

    def with_value(self, value):
        return PositionalArg(self.name, self.default, value)

class Args(object):
    def __init__(self, positional_args, args, kwargs):
        self.positional_args = positional_args
        self.args = args
        self.kwargs = kwargs

    @classmethod
    def from_argspec(cls, argspec):
        defaults = argspec.defaults if argspec.defaults is not None else ()
        non_default_arg_count = len(argspec.args) - len(defaults)
        arg_defaults = (
            (PositionalArg.NO_DEFAULT,) * non_default_arg_count + defaults
        )
        return cls(
            positional_args=[
                PositionalArg.from_name(name, default)
                for name, default in zip(argspec.args, arg_defaults)
            ],
            args=(),
            kwargs={}
        )

    def apply_args(self, *args, **kwargs):
        new_positional_args = copy.copy(self.positional_args)
        new_args = copy.copy(self.args)
        new_kwargs = copy.copy(self.kwargs)
        for value in args:
            update_index = next(
                (
                    index for index, arg in enumerate(new_positional_args)
                    if not arg.has_value()
                ),
                None
            )
            if update_index is not None:
                arg = new_positional_args[update_index]
                new_positional_args[update_index] = arg.with_value(value)
            else:
                new_args += (value,)
        for name, value in kwargs.items():
            update_index = next(
                (
                    index for index, arg in enumerate(new_positional_args)
                    if arg.name == name
                ),
                None
            )
            if update_index is not None:
                arg = new_positional_args[update_index]
                new_positional_args[update_index] = arg.with_value(value)
            else:
                new_kwargs[name] = value
        return Args(new_positional_args, new_args, new_kwargs)

    def all_present(self):
        return all(arg.has_value_or_default() for arg in self.positional_args)

    def positional_arg_values(self):
        return tuple(arg.value_or_default() for arg in self.positional_args)

class Partial(object):
    def __init__(self, function, args):
        self.function = function
        self.args = args

    @classmethod
    def from_function(cls, function):
        argspec = inspect.getargspec(function)
        return cls(function, Args.from_argspec(argspec))

    def __call__(self, *args, **kwargs):
        partial = Partial(self.function, self.args.apply_args(*args, **kwargs))
        return partial.execute() if partial.args.all_present() else partial

    def execute(self):
        return self.function(
            *(self.args.positional_arg_values() + self.args.args),
            **self.args.kwargs
        )

def partial(function):
    return Partial.from_function(function)

def new():
    return Track.new()

def compose(*functions):
    return Track.new().compose(*functions)

class Track(object):
    def __init__(self, function):
        self.function = function

    def __call__(self, arg):
        return self.function(arg)

    @classmethod
    def new(cls, function=identity):
        return Track(function)

    def compose(self, *functions):
        def compose2(function1, function2):
            return lambda arg: function2(function1(arg))
        return Track.new(functools.reduce(compose2, functions, self.function))

    def tee(self, *functions):
        def tee_function(arg):
            Track.new().compose(*functions)(arg)
            return arg
        return self.compose(tee_function)

    def fold(self, fold_value, fold_error):
        def fold_function(arg, function=self.function):
            try:
                return fold_value(function(arg))
            except Error as error:
                return fold_error(error)
        return Track.new(fold_function)
<commit_msg>Rename argument attributes<commit_after>import copy
import functools
import inspect

def identity(value):
    return value

def raise_exception(exception):
    raise exception

def ignore(value):
    pass

class Error(Exception):
    pass

class UnmatchedValueError(Error):
    def __init__(self, value):
        self.value = value
        super(Error, self).__init__(str(value))

def match(*args):
    def get_map_function(value):
        for is_match, map_function in args:
            if is_match(value):
                return map_function
        raise UnmatchedValueError(value)
    return lambda error: get_map_function(error)(error)

def match_type(*args):
    return match(*[
        (lambda value, types=types: isinstance(value, types), map_function)
        for types, map_function in args
    ])

class PositionalArg(object):
    NO_VALUE = object()
    NO_DEFAULT = object()

    def __init__(self, name, default, value):
        self.name = name
        self.default = default
        self.value = value

    @classmethod
    def from_name(cls, name, default=NO_DEFAULT):
        return cls(name, default, value=PositionalArg.NO_VALUE)

    def has_value(self):
        return self.value != PositionalArg.NO_VALUE

    def has_value_or_default(self):
        return self.has_value() or self.default != PositionalArg.NO_DEFAULT

    def value_or_default(self):
        return self.value if self.has_value() else self.default

    def with_value(self, value):
        return PositionalArg(self.name, self.default, value)

class Args(object):
    def __init__(self, named_args, list_args, keyword_args):
        self.named_args = named_args
        self.list_args = list_args
        self.keyword_args = keyword_args

    @classmethod
    def from_argspec(cls, argspec):
        defaults = argspec.defaults if argspec.defaults is not None else ()
        non_default_arg_count = len(argspec.args) - len(defaults)
        arg_defaults = (
            (PositionalArg.NO_DEFAULT,) * non_default_arg_count + defaults
        )
        return cls(
            named_args=[
                PositionalArg.from_name(name, default)
                for name, default in zip(argspec.args, arg_defaults)
            ],
            list_args=(),
            keyword_args={}
        )

    def apply_args(self, *args, **kwargs):
        named_args = copy.copy(self.named_args)
        list_args = copy.copy(self.list_args)
        keyword_args = copy.copy(self.keyword_args)
        for value in args:
            update_index = next(
                (
                    index for index, arg in enumerate(named_args)
                    if not arg.has_value()
                ),
                None
            )
            if update_index is not None:
                arg = named_args[update_index]
                named_args[update_index] = arg.with_value(value)
            else:
                list_args += (value,)
        for name, value in kwargs.items():
            update_index = next(
                (
                    index for index, arg in enumerate(named_args)
                    if arg.name == name
                ),
                None
            )
            if update_index is not None:
                arg = named_args[update_index]
                named_args[update_index] = arg.with_value(value)
            else:
                keyword_args[name] = value
        return Args(named_args, list_args, keyword_args)

    def all_present(self):
        return all(arg.has_value_or_default() for arg in self.named_args)

    def named_arg_values(self):
        return tuple(arg.value_or_default() for arg in self.named_args)

class Partial(object):
    def __init__(self, function, args):
        self.function = function
        self.args = args

    @classmethod
    def from_function(cls, function):
        argspec = inspect.getargspec(function)
        return cls(function, Args.from_argspec(argspec))

    def __call__(self, *args, **kwargs):
        partial = Partial(self.function, self.args.apply_args(*args, **kwargs))
        return partial.execute() if partial.args.all_present() else partial

    def execute(self):
        return self.function(
            *(self.args.named_arg_values() + self.args.list_args),
            **self.args.keyword_args
        )

def partial(function):
    return Partial.from_function(function)

def new():
    return Track.new()

def compose(*functions):
    return Track.new().compose(*functions)

class Track(object):
    def __init__(self, function):
        self.function = function

    def __call__(self, arg):
        return self.function(arg)

    @classmethod
    def new(cls, function=identity):
        return Track(function)

    def compose(self, *functions):
        def compose2(function1, function2):
            return lambda arg: function2(function1(arg))
        return Track.new(functools.reduce(compose2, functions, self.function))

    def tee(self, *functions):
        def tee_function(arg):
            Track.new().compose(*functions)(arg)
            return arg
        return self.compose(tee_function)

    def fold(self, fold_value, fold_error):
        def fold_function(arg, function=self.function):
            try:
                return fold_value(function(arg))
            except Error as error:
                return fold_error(error)
        return Track.new(fold_function)
<|endoftext|>


<commit_before>from __future__ import unicode_literals
from httplib import NO_CONTENT, BAD_REQUEST
from django.conf import settings
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse_lazy
from django.db.models import Count
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import View, TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView
from django.http import HttpResponse, JsonResponse, Http404, QueryDict
from django.http import HttpResponseForbidden
from .models import Board, Quote, Comment

try:
    QUOTE_LIST_PAGINATION = settings.QUOTE_LIST_PAGINATION
except AttributeError:
    QUOTE_LIST_PAGINATION = 25

class BoardViewMixin(object):
    @property
    def board(self):
        return Board.objects.get(slug__iexact = self.kwargs.get('board', ''))

    def _get_quote_or_404(self, quote_pk):
        return get_object_or_404(
            self.board.quotes.all(),
            pk = quote_pk)

    def _get_comment_or_404(self, quote_pk, comment_pk):
        return get_object_or_404(
            self._get_quote_or_404(quote_pk).comments.all(),
            pk = comment_pk)

class QuoteList(ListView, BoardViewMixin):
    model = Quote
    template_name = 'quotes/list.html'
    context_object_name = 'quotes'
    paginate_by = QUOTE_LIST_PAGINATION

    def get_queryset(self):
        queryset = super(QuoteList, self).get_queryset() \
            .filter(board__slug__iexact = self.board.slug)

        author = self.request.GET.get('by', None)
        mentions = self.request.GET.get('mentioned', None)

        # Filter by author.
        if author is not None:
            queryset = queryset.filter(author__username__exact = author)

        # Filter by mentioned user.
        if mentions is not None:
            # Split mentioned list and remove any empty items.
            mentions = set(mentions.split(',')) - set([''])
            queryset = queryset.filter(mentions__username__in = mentions).distinct()

        return queryset

@method_decorator(login_required, name = 'dispatch')
class QuoteCreate(CreateView, BoardViewMixin):
    model = Quote
    template_name = 'quotes/create.html'
    fields = ('title', 'text', )

    def form_valid(self, form):
        form.instance.author = self.request.user
        form.instance.board = self.board
        return super(QuoteCreate, self).form_valid(form)

class QuoteDetail(DetailView, BoardViewMixin):
    model = Quote
    template_name = 'quotes/detail.html'
    context_object_name = 'quote'

@method_decorator(login_required, name = 'dispatch')
class QuoteEdit(UpdateView, BoardViewMixin):
    model = Quote
    template_name = 'quotes/edit.html'
    fields = ('title', 'text', )

@method_decorator(login_required, name = 'dispatch')
class QuoteDelete(DeleteView, BoardViewMixin):
    model = Quote
    success_url = reverse_lazy('index')

class TopQuotes(QuoteList, BoardViewMixin):
    def get_queryset(self):
        queryset = super(TopQuotes, self).get_queryset().annotate(likers_count = Count('likers'))
        return queryset.filter(likers_count__gte = 1).order_by('-likers_count', '-created')

    def get_context_data(self, **kwargs):
        try:
            return super(TopQuotes, self).get_context_data(**kwargs)
        except Http404:
            # If the user un-liked a quote, the pagination might get screwed up
            # trying to move to the next page. In this case, we reset the page
            # number to 1.
            self.kwargs['page'] = 1
            return super(TopQuotes, self).get_context_data(**kwargs)

class LikeView(View, BoardViewMixin):
    def get(self, request, board, pk):
        quote = self._get_quote_or_404(pk)
        return JsonResponse({'likers': map(str, quote.likers.all()), })

    def put(self, request, board, pk):
        if self.request.user.pk is None:
            return HttpResponseForbidden()

        quote = self._get_quote_or_404(pk)
        quote.likers.add(self.request.user)
        return JsonResponse({'likers': map(str, quote.likers.all()), })

    def delete(self, request, board, pk):
        if self.request.user.pk is None:
            return HttpResponseForbidden()

        quote = self._get_quote_or_404(pk)
        quote.likers.remove(self.request.user)
        return JsonResponse({'likers': map(str, quote.likers.all()), })

class LikersList(TemplateView, BoardViewMixin):
    template_name = 'quotes/likers.html'

    def get_context_data(self, **kwargs):
        context = super(LikersList, self).get_context_data(**kwargs)
        likers = self.request.GET.getlist('likers[]')
        likers = get_user_model().objects.filter(username__in = likers)

        if self.request.user in likers:
            context['liked_by_user'] = True
            context['likers'] = likers.exclude(id = self.request.user.id)
        else:
            context['likers'] = likers

        return context

@method_decorator(login_required, name = 'dispatch')
class Comments(View, BoardViewMixin):
    def post(self, request, board, pk):
        comment_text = self.request.POST['text']

        # It's worth noting we check the stripped version, but save the text as
        # it was given.
        if not comment_text.strip():
            return HttpResponse(status = BAD_REQUEST)

        quote = self._get_quote_or_404(pk)
        comment = quote.comments.create(
            author = self.request.user,
            text = comment_text)

        return JsonResponse({'comment_pk': comment.pk, })

    def put(self, request, board, pk, comment_pk):
        comment = self._get_comment_or_404(pk, comment_pk)
        comment_text = QueryDict(request.body)['text']

        # It's worth noting we check the stripped version, but save the text as
        # it was given.
        if not comment_text.strip():
            return HttpResponse(status = BAD_REQUEST)

        comment.text = comment_text
        comment.save()

        return HttpResponse(status = NO_CONTENT)

    def delete(self, request, board, pk, comment_pk):
        comment = self._get_comment_or_404(pk, comment_pk)
        comment.delete()

        return HttpResponse(status = NO_CONTENT)
<commit_msg>Redirect to quotes list after deletion<commit_after>from __future__ import unicode_literals
from httplib import NO_CONTENT, BAD_REQUEST
from django.conf import settings
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse
from django.db.models import Count
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import View, TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView
from django.http import HttpResponse, JsonResponse, Http404, QueryDict
from django.http import HttpResponseForbidden
from .models import Board, Quote, Comment

try:
    QUOTE_LIST_PAGINATION = settings.QUOTE_LIST_PAGINATION
except AttributeError:
    QUOTE_LIST_PAGINATION = 25

class BoardViewMixin(object):
    @property
    def board(self):
        return Board.objects.get(slug__iexact = self.kwargs.get('board', ''))

    def _get_quote_or_404(self, quote_pk):
        return get_object_or_404(
            self.board.quotes.all(),
            pk = quote_pk)

    def _get_comment_or_404(self, quote_pk, comment_pk):
        return get_object_or_404(
            self._get_quote_or_404(quote_pk).comments.all(),
            pk = comment_pk)

class QuoteList(ListView, BoardViewMixin):
    model = Quote
    template_name = 'quotes/list.html'
    context_object_name = 'quotes'
    paginate_by = QUOTE_LIST_PAGINATION

    def get_queryset(self):
        queryset = super(QuoteList, self).get_queryset() \
            .filter(board__slug__iexact = self.board.slug)

        author = self.request.GET.get('by', None)
        mentions = self.request.GET.get('mentioned', None)

        # Filter by author.
        if author is not None:
            queryset = queryset.filter(author__username__exact = author)

        # Filter by mentioned user.
        if mentions is not None:
            # Split mentioned list and remove any empty items.
            mentions = set(mentions.split(',')) - set([''])
            queryset = queryset.filter(mentions__username__in = mentions).distinct()

        return queryset

@method_decorator(login_required, name = 'dispatch')
class QuoteCreate(CreateView, BoardViewMixin):
    model = Quote
    template_name = 'quotes/create.html'
    fields = ('title', 'text', )

    def form_valid(self, form):
        form.instance.author = self.request.user
        form.instance.board = self.board
        return super(QuoteCreate, self).form_valid(form)

class QuoteDetail(DetailView, BoardViewMixin):
    model = Quote
    template_name = 'quotes/detail.html'
    context_object_name = 'quote'

@method_decorator(login_required, name = 'dispatch')
class QuoteEdit(UpdateView, BoardViewMixin):
    model = Quote
    template_name = 'quotes/edit.html'
    fields = ('title', 'text', )

@method_decorator(login_required, name = 'dispatch')
class QuoteDelete(DeleteView, BoardViewMixin):
    model = Quote

    def get_success_url(self):
        return reverse('board:list', kwargs = {'board': self.board.slug, })

class TopQuotes(QuoteList, BoardViewMixin):
    def get_queryset(self):
        queryset = super(TopQuotes, self).get_queryset().annotate(likers_count = Count('likers'))
        return queryset.filter(likers_count__gte = 1).order_by('-likers_count', '-created')

    def get_context_data(self, **kwargs):
        try:
            return super(TopQuotes, self).get_context_data(**kwargs)
        except Http404:
            # If the user un-liked a quote, the pagination might get screwed up
            # trying to move to the next page. In this case, we reset the page
            # number to 1.
            self.kwargs['page'] = 1
            return super(TopQuotes, self).get_context_data(**kwargs)

class LikeView(View, BoardViewMixin):
    def get(self, request, board, pk):
        quote = self._get_quote_or_404(pk)
        return JsonResponse({'likers': map(str, quote.likers.all()), })

    def put(self, request, board, pk):
        if self.request.user.pk is None:
            return HttpResponseForbidden()

        quote = self._get_quote_or_404(pk)
        quote.likers.add(self.request.user)
        return JsonResponse({'likers': map(str, quote.likers.all()), })

    def delete(self, request, board, pk):
        if self.request.user.pk is None:
            return HttpResponseForbidden()

        quote = self._get_quote_or_404(pk)
        quote.likers.remove(self.request.user)
        return JsonResponse({'likers': map(str, quote.likers.all()), })

class LikersList(TemplateView, BoardViewMixin):
    template_name = 'quotes/likers.html'

    def get_context_data(self, **kwargs):
        context = super(LikersList, self).get_context_data(**kwargs)
        likers = self.request.GET.getlist('likers[]')
        likers = get_user_model().objects.filter(username__in = likers)

        if self.request.user in likers:
            context['liked_by_user'] = True
            context['likers'] = likers.exclude(id = self.request.user.id)
        else:
            context['likers'] = likers

        return context

@method_decorator(login_required, name = 'dispatch')
class Comments(View, BoardViewMixin):
    def post(self, request, board, pk):
        comment_text = self.request.POST['text']

        # It's worth noting we check the stripped version, but save the text as
        # it was given.
        if not comment_text.strip():
            return HttpResponse(status = BAD_REQUEST)

        quote = self._get_quote_or_404(pk)
        comment = quote.comments.create(
            author = self.request.user,
            text = comment_text)

        return JsonResponse({'comment_pk': comment.pk, })

    def put(self, request, board, pk, comment_pk):
        comment = self._get_comment_or_404(pk, comment_pk)
        comment_text = QueryDict(request.body)['text']

        # It's worth noting we check the stripped version, but save the text as
        # it was given.
        if not comment_text.strip():
            return HttpResponse(status = BAD_REQUEST)

        comment.text = comment_text
        comment.save()

        return HttpResponse(status = NO_CONTENT)

    def delete(self, request, board, pk, comment_pk):
        comment = self._get_comment_or_404(pk, comment_pk)
        comment.delete()

        return HttpResponse(status = NO_CONTENT)
<|endoftext|>


<commit_before>import yaml,json,time,pdb
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from twilio.rest import Client

#retrieve config file with secrets
config = yaml.safe_load(open("config.yml"))
#print(config)
#with open("config.yml",'r') as ymlfile:
#    cfg = yaml.load(ymlfile)
#twitdict= cfg['twitter_set']
twitdict = config['twitter_set']
#twildict= cfg['twilio_set']
twildict = config['twilio_set']
searchdict = config['search_set']
#store twitter secrets in variables
access_token = twitdict['access_token']
access_token_secret = twitdict['access_secret']
consumer_key = twitdict['consumer_key']
consumer_secret = twitdict['consumer_secret']

#store twilio secrets in variables
account = twildict['account']
token = twildict['token']

#get a twilio token

#store call details from config in variables
tophone = twildict['tophone']
smsphone = twildict['sms_notification']
fromphone = twildict['fromphone']
callurl = twildict['url']

#create client object to use for calls or texts
client = Client(account,token)

#store search parameters
ulist=searchdict['user']
tlist=searchdict['track']

#define a basic listener
class CallListener(StreamListener):

    def on_data(self, data):
        print 'matched tweet'
        jsondata=json.loads(data)
        with open('data.txt','w') as outfile:
            json.dump(jsondata,outfile)
        #pdb.set_trace()
        userid = jsondata['user']['id_str']
        if not userid in ulist:
            print 'not one of our users'
            return True
        tweettext=jsondata['text']
        print tweettext.encode('utf-8')
        #pdb.set_trace()
        trigger = 0
        for word in tlist:
            if word in tweettext.lower():
                print 'keyword '+word+' found'
                trigger = 1
        if trigger == 1:
            #print 'make call to '+tophone+' from '+fromphone
            call = client.calls.create(to=tophone, from_=fromphone, url=callurl)
        if trigger == 0:
            print 'no keywords matched; no call made'
        return True

    def on_error(self, code):
        if code == 420:
            message = client.api.account.messages.create(to=smsphone,from_=fromphone,body='420 error, we are down!')
            return False
        else:
            message = client.api.account.messages.create(to=smsphone,from_=fromphone,body='error code: '+status)
            return False
if __name__ == '__main__':

    #This handles Twitter authetification and the connection to Twitter Streaming API
    l = CallListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)
    #This line filter Twitter Streams to capture tweets from the user ids listed in ulist, which are pulled in from the config file. use the get_ids.py helper script to extract user ids from @handles
    stream.filter(follow=ulist,async=True)
<commit_msg>change from trigger == 1 to trigger > 0<commit_after>import yaml,json,time,pdb
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from twilio.rest import Client

#retrieve config file with secrets
config = yaml.safe_load(open("config.yml"))
#print(config)
#with open("config.yml",'r') as ymlfile:
#    cfg = yaml.load(ymlfile)
#twitdict= cfg['twitter_set']
twitdict = config['twitter_set']
#twildict= cfg['twilio_set']
twildict = config['twilio_set']
searchdict = config['search_set']
#store twitter secrets in variables
access_token = twitdict['access_token']
access_token_secret = twitdict['access_secret']
consumer_key = twitdict['consumer_key']
consumer_secret = twitdict['consumer_secret']

#store twilio secrets in variables
account = twildict['account']
token = twildict['token']

#get a twilio token

#store call details from config in variables
tophone = twildict['tophone']
smsphone = twildict['sms_notification']
fromphone = twildict['fromphone']
callurl = twildict['url']

#create client object to use for calls or texts
client = Client(account,token)

#store search parameters
ulist=searchdict['user']
tlist=searchdict['track']

#define a basic listener
class CallListener(StreamListener):

    def on_data(self, data):
        print 'matched tweet'
        jsondata=json.loads(data)
        with open('data.txt','w') as outfile:
            json.dump(jsondata,outfile)
        #pdb.set_trace()
        userid = jsondata['user']['id_str']
        if not userid in ulist:
            print 'not one of our users'
            return True
        tweettext=jsondata['text']
        print tweettext.encode('utf-8')
        #pdb.set_trace()
        trigger = 0
        for word in tlist:
            if word in tweettext.lower():
                print 'keyword '+word+' found'
                trigger = 1
        if trigger > 0:
            #print 'make call to '+tophone+' from '+fromphone
            call = client.calls.create(to=tophone, from_=fromphone, url=callurl)
        if trigger == 0:
            print 'no keywords matched; no call made'
        return True

    def on_error(self, code):
        if code == 420:
            message = client.api.account.messages.create(to=smsphone,from_=fromphone,body='420 error, we are down!')
            return False
        else:
            message = client.api.account.messages.create(to=smsphone,from_=fromphone,body='error code: '+status)
            return False
if __name__ == '__main__':

    #This handles Twitter authetification and the connection to Twitter Streaming API
    l = CallListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)
    #This line filter Twitter Streams to capture tweets from the user ids listed in ulist, which are pulled in from the config file. use the get_ids.py helper script to extract user ids from @handles
    stream.filter(follow=ulist,async=True)
<|endoftext|>