<commit_before>import caffe
import numpy as np
import argparse, pprint
from multiprocessing import Pool
import scipy.misc as scm
from os import path as osp
import my_pycaffe_io as mpio
import my_pycaffe as mp
from easydict import EasyDict as edict

class PythonWindowDataLayer(caffe.Layer):
	@classmethod
	def parse_args(cls, argsStr):
		parser = argparse.ArgumentParser(description='Python Window Data Layer')
		parser.add_argument('--source', default='', type=str)
		parser.add_argument('--root_folder', default='', type=str)
		parser.add_argument('--mean_file', default='', type=str)
		parser.add_argument('--batch_size', default=128, type=int)
		parser.add_argument('--crop_size', default=192, type=int)
		parser.add_argument('--is_gray', dest='is_gray', action='store_true')
		parser.add_argument('--no-is_gray', dest='is_gray', action='store_false')
		args   = parser.parse_args(argsStr.split())
		print('Using Config:')
		pprint.pprint(args)
		return args

	def setup(self, bottom, top):
		self.param_ = PythonWindowDataLayer.parse_args(self.param_str)
		self.wfid_   = mpio.GenericWindowReader(self.param_.source)
		self.numIm_  = self.wfid_.numIm_
		self.lblSz_  = self.wfid_.lblSz_
		if self.param_.is_gray:
			self.ch_ = 1
		else:
			self.ch_ = 3
		top[0].reshape(self.param_.batch_size, self.numIm_ * self.ch_,
										self.param_.crop_size, self.param_.crop_size)
		top[1].reshape(self.param_.batch_size, self.lblSz_, 1, 1)

		self.mu_ = None
		if len(self.param_.mean_file) > 0:
			#Mean is assumbed to be in BGR format
			self.mu_ = mp.read_mean(self.param_.mean_file)
			self.mu_ = self.mu_.astype(np.float32)
			ch, h, w = self.mu_.shape
			assert (h >= self.param_.crop_size and w >= self.param_.crop_size)
			y1 = int(h/2 - (self.param_.crop_size/2))
			x1 = int(w/2 - (self.param_.crop_size/2))
			y2 = int(y1 + self.param_.crop_size)
			x2 = int(x1 + self.param_.crop_size)
			self.mu_ = self.mu_[:,y1:y2,x1:x2]

	def forward(self, bottom, top):
		for b in range(self.param_.batch_size):
			imNames, lbls = self.wfid_.read_next()
			#Read images
			for n in range(self.numIm_):
				#Load images
				imName, ch, h, w, x1, y1, x2, y2 = imNames[n].strip().split()
				imName = osp.join(self.param_.root_folder, imName)
				im     = scm.imread(imName)
				#Process the image
				x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
				#Feed the image
				cSt = n * self.ch_
				cEn = cSt + self.ch_
				im = scm.imresize(im[y1:y2, x1:x2, :],
									(self.param_.crop_size, self.param_.crop_size))
				im = im[:,:,[2,1,0]].transpose((2,0,1))
				if self.mu_ is not None:
					im = im - self.mu_
				top[0].data[b,cSt:cEn, :, :] = im.astype(np.float32)
			#Read the labels
			top[1].data[b,:,:,:] = lbls.reshape(self.lblSz_,1,1).astype(np.float32)

	def backward(self, top, propagate_down, bottom):
		""" This layer has no backward """
		pass

	def reshape(self, bottom, top):
		""" This layer has no reshape """
		pass

##
#Parallel version
class PythonWindowDataParallelLayer(caffe.Layer):
	@classmethod
	def parse_args(cls, argsStr):
		parser = argparse.ArgumentParser(description='Python Window Data Parallel Layer')
		parser.add_argument('--source', default='', type=str)
		parser.add_argument('--root_folder', default='', type=str)
		parser.add_argument('--batch_size', default=128, type=int)
		parser.add_argument('--crop_size', default=192, type=int)
		parser.add_argument('--is_gray', default=False, type=bool)
		args   = parser.parse_args(argsStr.split())
		print('Using Config:')
		pprint.pprint(args)
		return args

	def __del__(self):
		self.wfid_.close()
		self.pool_.terminate()

	def setup(self, bottom, top):
		self.param_ = PythonWindowDataLayer.parse_args(self.param_str)
		self.wfid_   = mpio.GenericWindowReader(self.param_.source)
		self.numIm_  = self.wfid_.numIm_
		self.lblSz_  = self.wfid_.lblSz_
		if self.param_.is_gray:
			self.ch_ = 1
		else:
			self.ch_ = 3
		top[0].reshape(self.param_.batch_size, self.numIm_ * self.ch_,
										self.param_.crop_size, self.param_.crop_size)
		top[1].reshape(self.param_.batch_size, self.lblSz_, 1, 1)
		#Create the pool
		self.pool_ = Pool(processes=self.numIm_)

	def load_images(self, imNames, jobid):
		imData = np.zeros((self.param_.batch_size, self.ch_,
							self.param_.crop_size, self.param_.crop_size), np.float32)
		for b in range(self.param_.batch_size):
			#Load images
			imName, ch, h, w, x1, y1, x2, y2 = imNames[b].strip().split()
			imName = osp.join(self.param_.root_folder, imName)
			im     = scm.imread(imName)
			im     = im.astype(np.float32)
			#Process the image
			x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
			#Feed the image
			cSt = n * self.ch_
			cEn = cSt + self.ch_
			imData[b,cSt:cEn, :, :] = im[y1:y2, x1:x2,[2,1,0]].transpose((2,0,1))
		return jobid, imData

	def forward(self, bottom, top):
		keyStr = 'btch%d'
		inArgs = edict()
		for n in range(self.numIm_):
			inArgs[keyStr % n] = []

		for b in range(self.param_.batch_size):
			imNames, lbls = self.wfid_.read_next()
			#Read images
			for n in range(self.numIm_):
				pass
			#Read the labels
			top[1].data[b,:,:,:] = lbls.reshape(self.lblSz_,1,1).astype(np.float32)

	def backward(self, top, propagate_down, bottom):
		""" This layer has no backward """
		pass

	def reshape(self, bottom, top):
		""" This layer has no reshape """
		pass
<commit_msg>Completed Parallel version, need to check it now<commit_after>import caffe
import numpy as np
import argparse, pprint
from multiprocessing import Pool
import scipy.misc as scm
from os import path as osp
import my_pycaffe_io as mpio
import my_pycaffe as mp
from easydict import EasyDict as edict

class PythonWindowDataLayer(caffe.Layer):
	@classmethod
	def parse_args(cls, argsStr):
		parser = argparse.ArgumentParser(description='Python Window Data Layer')
		parser.add_argument('--source', default='', type=str)
		parser.add_argument('--root_folder', default='', type=str)
		parser.add_argument('--mean_file', default='', type=str)
		parser.add_argument('--batch_size', default=128, type=int)
		parser.add_argument('--crop_size', default=192, type=int)
		parser.add_argument('--is_gray', dest='is_gray', action='store_true')
		parser.add_argument('--no-is_gray', dest='is_gray', action='store_false')
		args   = parser.parse_args(argsStr.split())
		print('Using Config:')
		pprint.pprint(args)
		return args

	def setup(self, bottom, top):
		self.param_ = PythonWindowDataLayer.parse_args(self.param_str)
		self.wfid_   = mpio.GenericWindowReader(self.param_.source)
		self.numIm_  = self.wfid_.numIm_
		self.lblSz_  = self.wfid_.lblSz_
		if self.param_.is_gray:
			self.ch_ = 1
		else:
			self.ch_ = 3
		top[0].reshape(self.param_.batch_size, self.numIm_ * self.ch_,
										self.param_.crop_size, self.param_.crop_size)
		top[1].reshape(self.param_.batch_size, self.lblSz_, 1, 1)

		self.mu_ = None
		if len(self.param_.mean_file) > 0:
			#Mean is assumbed to be in BGR format
			self.mu_ = mp.read_mean(self.param_.mean_file)
			self.mu_ = self.mu_.astype(np.float32)
			ch, h, w = self.mu_.shape
			assert (h >= self.param_.crop_size and w >= self.param_.crop_size)
			y1 = int(h/2 - (self.param_.crop_size/2))
			x1 = int(w/2 - (self.param_.crop_size/2))
			y2 = int(y1 + self.param_.crop_size)
			x2 = int(x1 + self.param_.crop_size)
			self.mu_ = self.mu_[:,y1:y2,x1:x2]

	def forward(self, bottom, top):
		for b in range(self.param_.batch_size):
			imNames, lbls = self.wfid_.read_next()
			#Read images
			for n in range(self.numIm_):
				#Load images
				imName, ch, h, w, x1, y1, x2, y2 = imNames[n].strip().split()
				imName = osp.join(self.param_.root_folder, imName)
				im     = scm.imread(imName)
				#Process the image
				x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
				#Feed the image
				cSt = n * self.ch_
				cEn = cSt + self.ch_
				im = scm.imresize(im[y1:y2, x1:x2, :],
									(self.param_.crop_size, self.param_.crop_size))
				im = im[:,:,[2,1,0]].transpose((2,0,1))
				if self.mu_ is not None:
					im = im - self.mu_
				top[0].data[b,cSt:cEn, :, :] = im.astype(np.float32)
			#Read the labels
			top[1].data[b,:,:,:] = lbls.reshape(self.lblSz_,1,1).astype(np.float32)

	def backward(self, top, propagate_down, bottom):
		""" This layer has no backward """
		pass

	def reshape(self, bottom, top):
		""" This layer has no reshape """
		pass

##
#Parallel version
class PythonWindowDataParallelLayer(caffe.Layer):
	@classmethod
	def parse_args(cls, argsStr):
		parser = argparse.ArgumentParser(description='Python Window Data Parallel Layer')
		parser.add_argument('--source', default='', type=str)
		parser.add_argument('--root_folder', default='', type=str)
		parser.add_argument('--batch_size', default=128, type=int)
		parser.add_argument('--crop_size', default=192, type=int)
		parser.add_argument('--is_gray', default=False, type=bool)
		args   = parser.parse_args(argsStr.split())
		print('Using Config:')
		pprint.pprint(args)
		return args

	def __del__(self):
		self.wfid_.close()
		self.pool_.terminate()

	def setup(self, bottom, top):
		self.param_ = PythonWindowDataLayer.parse_args(self.param_str)
		self.wfid_   = mpio.GenericWindowReader(self.param_.source)
		self.numIm_  = self.wfid_.numIm_
		self.lblSz_  = self.wfid_.lblSz_
		if self.param_.is_gray:
			self.ch_ = 1
		else:
			self.ch_ = 3
		top[0].reshape(self.param_.batch_size, self.numIm_ * self.ch_,
										self.param_.crop_size, self.param_.crop_size)
		top[1].reshape(self.param_.batch_size, self.lblSz_, 1, 1)
		#Create the pool
		self.pool_ = Pool(processes=self.numIm_)
		self.launch_jobs()

	def load_images(self, imNames, jobid):
		imData = np.zeros((self.param_.batch_size, self.ch_,
							self.param_.crop_size, self.param_.crop_size), np.float32)
		for b in range(self.param_.batch_size):
			#Load images
			imName, ch, h, w, x1, y1, x2, y2 = imNames[b].strip().split()
			imName = osp.join(self.param_.root_folder, imName)
			im     = scm.imread(imName)
			im     = im.astype(np.float32)
			#Process the image
			x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
			#Feed the image
			cSt = n * self.ch_
			cEn = cSt + self.ch_
			imData[b,cSt:cEn, :, :] = im[y1:y2, x1:x2,[2,1,0]].transpose((2,0,1))
		return [jobid, imData]

	def _load_images(self, args):
		return self.load_images(*args)

	def launch_jobs(self):
		inArgs = []
		self.labels_ = np.zeros((self.param_.batch_size, self.lblSz_,1,1),np.float32)
		for n in range(self.numIm_):
			inArgs.append([])
		for b in range(self.param_.batch_size):
			imNames, lbls = self.wfid_.read_next()
			self.labels_[b,:,:,] = lbls.reshape(self.lblSz_,1,1).astype(np.float32)
			#Read images
			for n in range(self.numIm_):
				inArgs[n].append([imNames[n], n])
		self.jobs_ = self.pool_.map_async(self._load_images, inArgs)

	def forward(self, bottom, top):
		#Load the images
		imRes      = self.jobs_.get()
		for res in imRes:
			jobId, imData = res
			cSt = jobId * self.ch_
			cEn = cSt + self.ch_
			top[0][:,cSt:cEn,:,:] = imData
		#Read the labels
		top[1].data[...] = self.labels_
		self.launch_jobs()

	def backward(self, top, propagate_down, bottom):
		""" This layer has no backward """
		pass

	def reshape(self, bottom, top):
		""" This layer has no reshape """
		pass
<|endoftext|>


<commit_before>"""
TODO:
* ViennaCL context object
  + interface with PyOpenCL context
* Construct data types with associated context
* Get associated context from data types
"""
import pyviennacl as p
from . import _viennacl as _v
import logging
log = logging.getLogger(__name__)

WITH_OPENCL = True
try:
    import pyviennacl.opencl as vcl
    import pyopencl as cl
except ImportError as e:
    #log.warning("OpenCL not available: %s", e)
    WITH_OPENCL = False

class MemoryDomain(object):
    def __init__(*args, **kwargs):
        raise TypeError("This class is not supposed to be instantiated")

class UninitializedMemory(MemoryDomain):
    vcl_memory_type = _v.memory_types.MEMORY_NOT_INITIALIZED

class MainMemory(MemoryDomain):
    vcl_memory_type = _v.memory_types.MAIN_MEMORY

class OpenCLMemory(MemoryDomain):
    vcl_memory_type = _v.memory_types.OPENCL_MEMORY

class CUDAMemory(MemoryDomain):
    vcl_memory_type = _v.memory_types.CUDA_MEMORY

vcl_memory_types = {
    _v.memory_types.MEMORY_NOT_INITIALIZED: UninitializedMemory,
    _v.memory_types.MAIN_MEMORY: MainMemory,
    _v.memory_types.OPENCL_MEMORY: OpenCLMemory,
    _v.memory_types.CUDA_MEMORY: CUDAMemory
}

DefaultMemory = vcl_memory_types[_v.default_memory_type]

class MemoryHandle(object):
    vcl_handle = None
    domain = UninitializedMemory
    int_ptr = None

    def __init__(self, vcl_handle):
        self.vcl_handle = vcl_handle
        self.domain = vcl_memory_types[vcl_handle.active_handle_id]
        if self.domain is UninitializedMemory:
            self.int_ptr = None
        elif self.domain is MainMemory:
            self.int_ptr = vcl_handle.ram_handle
        elif self.domain is OpenCLMemory:
            self.int_ptr = vcl_handle.opencl_handle
        elif self.domain is CUDAMemory:
            self.int_ptr = vcl_handle.cuda_handle

    def __eq__(self, other):
        return self.vcl_handle == other.vcl_handle

    def __ne__(self, other):
        return self.vcl_handle != other.vcl_handle

    def __lt__(self, other):
        return self.vcl_handle < other.vcl_handle

    def __gt__(self, other):
        return other.vcl_handle < self.vcl_handle

    def swap(self, other):
        self.vcl_handle.swap(other.vcl_handle)
        tmp_vcl_handle = self.vcl_handle
        self.__init__(other.vcl_handle)
        other.__init__(tmp_vcl_handle)

    @property
    def buffer(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("You can currently only get a buffer with the OpenCL backend")
        return cl.MemoryObject.from_int_ptr(self.int_ptr)

class Context(object):
    domain = UninitializedMemory
    vcl_context = None
    sub_context = None
    _programs = None

    def __init__(self, domain_or_context = DefaultMemory):
        if isinstance(domain_or_context, Context):
            self.domain = domain_or_context.domain
            self.vcl_context = domain_or_context.vcl_context
            self.sub_context = domain_or_context.sub_context
            #if self.domain is OpenCLMemory:
            #    vcl.set_active_context(self)
            return

        if WITH_OPENCL:
            if isinstance(domain_or_context, cl.Context):
                self.domain = OpenCLMemory
                self.sub_context = domain_or_context
                create_vcl_context_from = vcl.get_viennacl_object(self.sub_context)

        try:
            if issubclass(domain_or_context, MemoryDomain): # cf default arg
                self.domain = domain_or_context
                if domain_or_context is OpenCLMemory:
                    self.sub_context = vcl.default_context
                    create_vcl_context_from = vcl.get_viennacl_object(self.sub_context)
                else:
                    create_vcl_context_from = self.domain.vcl_memory_type
        except TypeError: pass

        self.vcl_context = _v.context(create_vcl_context_from)
        for device in self.devices:
            if not self.queues[device]:
                self.add_queue(device)
        #if self.domain is OpenCLMemory:
        #    vcl.set_active_context(self)

    def __eq__(self, other):
        if not isinstance(other, type(self)):
            return False
        if self.domain != other.domain:
            return False
        if self.vcl_sub_context.int_ptr != other.vcl_sub_context.int_ptr:
            return False
        return True

    def __ne__(self, other):
        return not self == other

    @property
    def vcl_sub_context(self):
        if self.domain is OpenCLMemory:
            return vcl.get_viennacl_object(self.sub_context, self)
        else:
            raise TypeError("Only OpenCL sub-context supported currently")

    @property
    def opencl_context(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("You can only get the OpenCL context with the OpenCL backend")
        return self.sub_context

    @property
    def devices(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports multiple devices")
        return self.sub_context.devices

    @property
    def current_device(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports multiple devices")
        return vcl.get_pyopencl_object(self.vcl_sub_context.current_device)

    @property
    def queues(self):
        """
        TODO docstring
        """
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports multiple devices")
        queues = {}
        for d in self.devices:
            device_queues = []
            idx = 0
            while True:
                try:
                    vcl_queue = self.vcl_sub_context.get_queue(d.int_ptr, idx)
                except RuntimeError:
                    break
                queue = vcl.get_pyopencl_object(vcl_queue)
                device_queues.append(queue)
                idx += 1
            queues[d] = device_queues
        return queues

    def add_queue(self, device, queue = None):
        """
        TODO docstring
        """
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports queues")
        if queue is None:
            queue = cl.CommandQueue(self.sub_context, device)
        if queue in self.queues[device]:
            return
        self.vcl_sub_context.add_existing_queue(device.int_ptr, queue.int_ptr)

    def switch_queue(self, queue):
        """
        TODO docstring
        """
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports queues")
        if queue not in self.queues[queue.device]:
            self.add_queue(queue.device, queue)
        vcl_queue = vcl.get_viennacl_object(queue, self.sub_context)
        self.vcl_sub_context.switch_queue(vcl_queue)

    @property
    def current_queue(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports multiple devices")
        return vcl.get_pyopencl_object(self.vcl_sub_context.current_queue)

    def finish_all_queues(self):
        """
        TODO docstring
        """
        if self.domain is not OpenCLMemory:
            raise TypeError("This only makes sense on OpenCL")
        for device in self.queues.keys():
            for queue in self.queues[device]:
                queue.finish()

    @property
    def programs(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("This only makes sense on OpenCL")
        if not isinstance(self._programs, vcl.ContextPrograms):
            self._programs = vcl.ContextPrograms(self)
        return self._programs

    @property
    def default_dtype(self):
        if self.domain is not OpenCLMemory:
            return p.dtype(p.float64)
        if self.current_device.double_fp_config:
            return p.dtype(p.float64)
        else:
            return p.dtype(p.float32)

def backend_finish():
    """
    Block until any computation active on the compute backend is finished.
    """
    return _v.backend_finish()

<commit_msg>Don't do OpenCL stuff without OpenCL<commit_after>"""
TODO:
* ViennaCL context object
  + interface with PyOpenCL context
* Construct data types with associated context
* Get associated context from data types
"""
import pyviennacl as p
from . import _viennacl as _v
import logging
log = logging.getLogger(__name__)

WITH_OPENCL = True
try:
    import pyviennacl.opencl as vcl
    import pyopencl as cl
except ImportError as e:
    #log.warning("OpenCL not available: %s", e)
    WITH_OPENCL = False

class MemoryDomain(object):
    def __init__(*args, **kwargs):
        raise TypeError("This class is not supposed to be instantiated")

class UninitializedMemory(MemoryDomain):
    vcl_memory_type = _v.memory_types.MEMORY_NOT_INITIALIZED

class MainMemory(MemoryDomain):
    vcl_memory_type = _v.memory_types.MAIN_MEMORY

class OpenCLMemory(MemoryDomain):
    vcl_memory_type = _v.memory_types.OPENCL_MEMORY

class CUDAMemory(MemoryDomain):
    vcl_memory_type = _v.memory_types.CUDA_MEMORY

vcl_memory_types = {
    _v.memory_types.MEMORY_NOT_INITIALIZED: UninitializedMemory,
    _v.memory_types.MAIN_MEMORY: MainMemory,
    _v.memory_types.OPENCL_MEMORY: OpenCLMemory,
    _v.memory_types.CUDA_MEMORY: CUDAMemory
}

DefaultMemory = vcl_memory_types[_v.default_memory_type]

class MemoryHandle(object):
    vcl_handle = None
    domain = UninitializedMemory
    int_ptr = None

    def __init__(self, vcl_handle):
        self.vcl_handle = vcl_handle
        self.domain = vcl_memory_types[vcl_handle.active_handle_id]
        if self.domain is UninitializedMemory:
            self.int_ptr = None
        elif self.domain is MainMemory:
            self.int_ptr = vcl_handle.ram_handle
        elif self.domain is OpenCLMemory:
            self.int_ptr = vcl_handle.opencl_handle
        elif self.domain is CUDAMemory:
            self.int_ptr = vcl_handle.cuda_handle

    def __eq__(self, other):
        return self.vcl_handle == other.vcl_handle

    def __ne__(self, other):
        return self.vcl_handle != other.vcl_handle

    def __lt__(self, other):
        return self.vcl_handle < other.vcl_handle

    def __gt__(self, other):
        return other.vcl_handle < self.vcl_handle

    def swap(self, other):
        self.vcl_handle.swap(other.vcl_handle)
        tmp_vcl_handle = self.vcl_handle
        self.__init__(other.vcl_handle)
        other.__init__(tmp_vcl_handle)

    @property
    def buffer(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("You can currently only get a buffer with the OpenCL backend")
        return cl.MemoryObject.from_int_ptr(self.int_ptr)

class Context(object):
    domain = UninitializedMemory
    vcl_context = None
    sub_context = None
    _programs = None

    def __init__(self, domain_or_context = DefaultMemory):
        if isinstance(domain_or_context, Context):
            self.domain = domain_or_context.domain
            self.vcl_context = domain_or_context.vcl_context
            self.sub_context = domain_or_context.sub_context
            #if self.domain is OpenCLMemory:
            #    vcl.set_active_context(self)
            return

        if WITH_OPENCL:
            if isinstance(domain_or_context, cl.Context):
                self.domain = OpenCLMemory
                self.sub_context = domain_or_context
                create_vcl_context_from = vcl.get_viennacl_object(self.sub_context)

        try:
            if issubclass(domain_or_context, MemoryDomain): # cf default arg
                self.domain = domain_or_context
                if domain_or_context is OpenCLMemory:
                    self.sub_context = vcl.default_context
                    create_vcl_context_from = vcl.get_viennacl_object(self.sub_context)
                else:
                    create_vcl_context_from = self.domain.vcl_memory_type
        except TypeError: pass

        self.vcl_context = _v.context(create_vcl_context_from)
        if self.domain is OpenCLMemory:
            for device in self.devices:
                if not self.queues[device]:
                    self.add_queue(device)
        #if self.domain is OpenCLMemory:
        #    vcl.set_active_context(self)

    def __eq__(self, other):
        if not isinstance(other, type(self)):
            return False
        if self.domain != other.domain:
            return False
        if self.domain is OpenCLMemory:
            if self.vcl_sub_context.int_ptr != other.vcl_sub_context.int_ptr:
                return False
        return True

    def __ne__(self, other):
        return not self == other

    @property
    def vcl_sub_context(self):
        if self.domain is OpenCLMemory:
            return vcl.get_viennacl_object(self.sub_context, self)
        else:
            raise TypeError("Only OpenCL sub-context supported currently")

    @property
    def opencl_context(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("You can only get the OpenCL context with the OpenCL backend")
        return self.sub_context

    @property
    def devices(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports multiple devices")
        return self.sub_context.devices

    @property
    def current_device(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports multiple devices")
        return vcl.get_pyopencl_object(self.vcl_sub_context.current_device)

    @property
    def queues(self):
        """
        TODO docstring
        """
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports multiple devices")
        queues = {}
        for d in self.devices:
            device_queues = []
            idx = 0
            while True:
                try:
                    vcl_queue = self.vcl_sub_context.get_queue(d.int_ptr, idx)
                except RuntimeError:
                    break
                queue = vcl.get_pyopencl_object(vcl_queue)
                device_queues.append(queue)
                idx += 1
            queues[d] = device_queues
        return queues

    def add_queue(self, device, queue = None):
        """
        TODO docstring
        """
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports queues")
        if queue is None:
            queue = cl.CommandQueue(self.sub_context, device)
        if queue in self.queues[device]:
            return
        self.vcl_sub_context.add_existing_queue(device.int_ptr, queue.int_ptr)

    def switch_queue(self, queue):
        """
        TODO docstring
        """
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports queues")
        if queue not in self.queues[queue.device]:
            self.add_queue(queue.device, queue)
        vcl_queue = vcl.get_viennacl_object(queue, self.sub_context)
        self.vcl_sub_context.switch_queue(vcl_queue)

    @property
    def current_queue(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("Only the OpenCL backend currently supports multiple devices")
        return vcl.get_pyopencl_object(self.vcl_sub_context.current_queue)

    def finish_all_queues(self):
        """
        TODO docstring
        """
        if self.domain is not OpenCLMemory:
            raise TypeError("This only makes sense on OpenCL")
        for device in self.queues.keys():
            for queue in self.queues[device]:
                queue.finish()

    @property
    def programs(self):
        if self.domain is not OpenCLMemory:
            raise TypeError("This only makes sense on OpenCL")
        if not isinstance(self._programs, vcl.ContextPrograms):
            self._programs = vcl.ContextPrograms(self)
        return self._programs

    @property
    def default_dtype(self):
        if self.domain is not OpenCLMemory:
            return p.dtype(p.float64)
        if self.current_device.double_fp_config:
            return p.dtype(p.float64)
        else:
            return p.dtype(p.float32)

def backend_finish():
    """
    Block until any computation active on the compute backend is finished.
    """
    return _v.backend_finish()

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
    pyvisa-sim.devices
    ~~~~~~~~~~~~~~~~~~

    Classes to simulate devices.

    :copyright: 2014 by PyVISA-sim Authors, see AUTHORS for more details.
    :license: MIT, see LICENSE for more details.
"""

try:
    import Queue as queue
except ImportError:
    import queue

import stringparser

from pyvisa import logger, constants, errors

from . import common

def to_bytes(val):
    """Takes a text message and return a tuple

    """
    val = val.replace('\\r', '\r').replace('\\n', '\n')
    return val.encode()

class NoResponse(object):
    """Sentinel used for when there should not be a response to a query
    """

    def replace(self, *args):
        """Needed to emulate calls for to_bytes method
        """
        return self

    def encode(self):
        """Needed to emulate calls for to_bytes method
        """
        return self

    def strip(self, *args):
        """Needed to emulate calls for to_bytes method
        """
        return self

class Property(object):
    """A device property
    """

    _value = None

    def __init__(self, name, value, specs):
        """
        :param name: name of the property
        :param value: default value
        :param specs: specification dictionary
        :return:
        """

        t = specs.get('type', None)
        if t:
            for key, val in (('float', float), ('int', int), ('str', str)):
                if t == key:
                    t = specs['type'] = val
                    break

        for key in ('min', 'max'):
            if key in specs:
                specs[key] = t(specs[key])

        if 'valid' in specs:
            specs['valid'] = set([t(val) for val in specs['valid']])

        self.name = name
        self.specs = specs
        self.set_value(value)

    @property
    def value(self):
        return self._value

    def set_value(self, string_value):
        """Set the value
        """
        specs = self.specs
        if 'type' in specs:
            value = specs['type'](string_value)
        else:
            value = string_value
        if 'min' in specs and value < specs['min']:
            raise ValueError
        if 'max' in specs and value > specs['max']:
            raise ValueError
        if 'valid' in specs and value not in specs['valid']:
            raise ValueError
        self._value = value

class Device(object):
    """A representation of a responsive device

    :param name: The identification name of the device
    :type name: str
    :param name: fullpath of the device where it is defined.
    :type name: str
    """

    # To be bound when adding the Device to Devices
    _resource_name = None

    # Default end of message used in query operations
    # :type: bytes
    _query_eom = b''

    # Default end of message used in response operations
    # :type: bytes
    _response_eom = None

    def __init__(self, name, error_response):

        # Name of the device.
        self.name = name

        # :type: bytes
        self.error_response = to_bytes(error_response)

        #: Stores the specific end of messages for device.
        #: TYPE CLASS -> (query termination, response termination)
        #: :type: dict[(pyvisa.constants.InterfaceType, str), (str, str)]
        self._eoms = {}

        #: Stores the queries accepted by the device.
        #: query: (response, error response)
        #: :type: dict[bytes, bytes]
        self._dialogues = {}

        #: Maps property names to value, type, validator
        #: :type: dict[str, Property]
        self._properties = {}

        #: Stores the getter queries accepted by the device.
        #: query: (property_name, response)
        #: :type: dict[bytes, (str, str)]
        self._getters = {}

        #: Stores the setters queries accepted by the device.
        #: (property_name, string parser query, response, error response)
        #: :type: list[(str, stringparser.Parser, bytes, bytes)]
        self._setters = []

        #: Buffer in which the user can read
        #: :type: bytearray
        self._output_buffer = bytearray()

        #: Buffer in which the user can write
        #: :type: bytearray
        self._input_buffer = bytearray()

    @property
    def resource_name(self):
        """Assigned resource name
        """
        return self._resource_name

    @resource_name.setter
    def resource_name(self, value):
        p = common.parse_resource_name(value)
        self._resource_name = p['canonical_resource_name']
        self._query_eom, self._response_eom = self._eoms[(p['interface_type'],
                                                          p['resource_class'])]

    def add_dialogue(self, query, response):
        """Add dialogue to device.

        :param query: query string
        :param response: response string
        """
        self._dialogues[to_bytes(query)] = to_bytes(response)

    def add_property(self, name, default_value, getter_pair, setter_triplet, specs):
        """Add property to device

        :param name: property name
        :param default_value: default value as string
        :param getter_pair: (query, response)
        :param setter_triplet: (query, response, error)
        :param specs: specification of the Property
        """
        self._properties[name] = Property(name, default_value, specs)

        query, response = getter_pair
        self._getters[to_bytes(query)] = name, response

        query, response, error = setter_triplet
        self._setters.append((name,
                              stringparser.Parser(query),
                              to_bytes(response),
                              to_bytes(error)))

    def add_eom(self, type_class, query_termination, response_termination):
        """Add default end of message for a given interface type and resource class.

        :param type_class: interface type and resource class as strings joined by space
        :param query_termination: end of message used in queries.
        :param response_termination: end of message used in responses.
        """
        interface_type, resource_class = type_class.split(' ')
        interface_type = getattr(constants.InterfaceType, interface_type.lower())
        self._eoms[(interface_type, resource_class)] = (to_bytes(query_termination),
                                                        to_bytes(response_termination))

    def write(self, data):
        """Write data into the device input buffer.

        :param data: single element byte
        :type data: bytes
        """
        logger.debug('Writing into device input buffer: %r' % data)
        if not isinstance(data, bytes):
            raise TypeError('data must be an instance of bytes')

        if len(data) != 1:
            raise ValueError('data must have a length of 1, not %d' % len(data))

        self._input_buffer.extend(data)

        l = len(self._query_eom)
        if not self._input_buffer.endswith(self._query_eom):
            return

        query = bytes(self._input_buffer[:-l])
        response = self._match(query)
        eom = self._response_eom

        if response is None:
            response = self.error_response

        if isinstance(response, NoResponse):
            self._output_buffer = bytearray()
        else:
            self._output_buffer.extend(response)
            self._output_buffer.extend(eom)

        self._input_buffer = bytearray()

    def _match(self, query):
        """Tries to match in dialogues, getters and setters

        :param query: message tuple
        :type query: Tuple[bytes]
        :return: response if found or None
        :rtype: Tuple[bytes] | None
        """

        # Try to match in the queries
        try:
            response = self._dialogues[query]
            logger.debug('Found response in queries: %s' % repr(response))

            return response

        except KeyError:
            pass

        # Now in the getters
        try:
            name, response = self._getters[query]
            logger.debug('Found response in getter of %s' % name)

            return response.format(self._properties[name].value).encode('utf-8')

        except KeyError:
            pass

        q = query.decode('utf-8')

        # Finally in the setters, this will be slow.
        for name, parser, response, err in self._setters:
            try:
                value = parser(q)
                logger.debug('Found response in setter of %s' % name)
            except ValueError:
                continue

            try:
                self._properties[name].set_value(value)
                return response

            except ValueError:
                return err

        return None

    def read(self):
        """Return a single byte from the output buffer
        """
        if isinstance(self._output_buffer, NoResponse):
            self._output_buffer = bytearray()
            raise errors.VisaIOError(VI_ERROR_TMO)
        if self._output_buffer:
            b, self._output_buffer = self._output_buffer[0:1], self._output_buffer[1:]
            return b

        return b''

class Devices(object):
    """The group of connected devices.
    """

    def __init__(self):

        #: Devices
        #: dict[str, Device]
        self._internal = {}

    def add_device(self, resource_name, device):
        """Bind device to resource name
        """

        if device.resource_name is not None:
            raise ValueError('The device %r is already assigned to %s' % (device, device.resource_name))

        device.resource_name = resource_name

        self._internal[device.resource_name] = device

    def __getitem__(self, item):
        return self._internal[item]

    def list_resources(self):
        """List resource names.

        :rtype: tuple[str]
        """
        return tuple(self._internal.keys())

<commit_msg>Changed read to dump the error message if reading an empty buffer<commit_after># -*- coding: utf-8 -*-
"""
    pyvisa-sim.devices
    ~~~~~~~~~~~~~~~~~~

    Classes to simulate devices.

    :copyright: 2014 by PyVISA-sim Authors, see AUTHORS for more details.
    :license: MIT, see LICENSE for more details.
"""

try:
    import Queue as queue
except ImportError:
    import queue

import stringparser

from pyvisa import logger, constants, errors

from . import common

def to_bytes(val):
    """Takes a text message and return a tuple

    """
    val = val.replace('\\r', '\r').replace('\\n', '\n')
    return val.encode()

class NoResponse(object):
    """Sentinel used for when there should not be a response to a query
    """

    def replace(self, *args):
        """Needed to emulate calls for to_bytes method
        """
        return self

    def encode(self):
        """Needed to emulate calls for to_bytes method
        """
        return self

    def strip(self, *args):
        """Needed to emulate calls for to_bytes method
        """
        return self

class Property(object):
    """A device property
    """

    _value = None

    def __init__(self, name, value, specs):
        """
        :param name: name of the property
        :param value: default value
        :param specs: specification dictionary
        :return:
        """

        t = specs.get('type', None)
        if t:
            for key, val in (('float', float), ('int', int), ('str', str)):
                if t == key:
                    t = specs['type'] = val
                    break

        for key in ('min', 'max'):
            if key in specs:
                specs[key] = t(specs[key])

        if 'valid' in specs:
            specs['valid'] = set([t(val) for val in specs['valid']])

        self.name = name
        self.specs = specs
        self.set_value(value)

    @property
    def value(self):
        return self._value

    def set_value(self, string_value):
        """Set the value
        """
        specs = self.specs
        if 'type' in specs:
            value = specs['type'](string_value)
        else:
            value = string_value
        if 'min' in specs and value < specs['min']:
            raise ValueError
        if 'max' in specs and value > specs['max']:
            raise ValueError
        if 'valid' in specs and value not in specs['valid']:
            raise ValueError
        self._value = value

class Device(object):
    """A representation of a responsive device

    :param name: The identification name of the device
    :type name: str
    :param name: fullpath of the device where it is defined.
    :type name: str
    """

    # To be bound when adding the Device to Devices
    _resource_name = None

    # Default end of message used in query operations
    # :type: bytes
    _query_eom = b''

    # Default end of message used in response operations
    # :type: bytes
    _response_eom = None

    def __init__(self, name, error_response):

        # Name of the device.
        self.name = name

        # :type: bytes
        self.error_response = to_bytes(error_response)

        #: Stores the specific end of messages for device.
        #: TYPE CLASS -> (query termination, response termination)
        #: :type: dict[(pyvisa.constants.InterfaceType, str), (str, str)]
        self._eoms = {}

        #: Stores the queries accepted by the device.
        #: query: (response, error response)
        #: :type: dict[bytes, bytes]
        self._dialogues = {}

        #: Maps property names to value, type, validator
        #: :type: dict[str, Property]
        self._properties = {}

        #: Stores the getter queries accepted by the device.
        #: query: (property_name, response)
        #: :type: dict[bytes, (str, str)]
        self._getters = {}

        #: Stores the setters queries accepted by the device.
        #: (property_name, string parser query, response, error response)
        #: :type: list[(str, stringparser.Parser, bytes, bytes)]
        self._setters = []

        #: Buffer in which the user can read
        #: :type: bytearray
        self._output_buffer = bytearray()

        #: Buffer in which the user can write
        #: :type: bytearray
        self._input_buffer = bytearray()

    @property
    def resource_name(self):
        """Assigned resource name
        """
        return self._resource_name

    @resource_name.setter
    def resource_name(self, value):
        p = common.parse_resource_name(value)
        self._resource_name = p['canonical_resource_name']
        self._query_eom, self._response_eom = self._eoms[(p['interface_type'],
                                                          p['resource_class'])]

    def add_dialogue(self, query, response):
        """Add dialogue to device.

        :param query: query string
        :param response: response string
        """
        self._dialogues[to_bytes(query)] = to_bytes(response)

    def add_property(self, name, default_value, getter_pair, setter_triplet, specs):
        """Add property to device

        :param name: property name
        :param default_value: default value as string
        :param getter_pair: (query, response)
        :param setter_triplet: (query, response, error)
        :param specs: specification of the Property
        """
        self._properties[name] = Property(name, default_value, specs)

        query, response = getter_pair
        self._getters[to_bytes(query)] = name, response

        query, response, error = setter_triplet
        self._setters.append((name,
                              stringparser.Parser(query),
                              to_bytes(response),
                              to_bytes(error)))

    def add_eom(self, type_class, query_termination, response_termination):
        """Add default end of message for a given interface type and resource class.

        :param type_class: interface type and resource class as strings joined by space
        :param query_termination: end of message used in queries.
        :param response_termination: end of message used in responses.
        """
        interface_type, resource_class = type_class.split(' ')
        interface_type = getattr(constants.InterfaceType, interface_type.lower())
        self._eoms[(interface_type, resource_class)] = (to_bytes(query_termination),
                                                        to_bytes(response_termination))

    def write(self, data):
        """Write data into the device input buffer.

        :param data: single element byte
        :type data: bytes
        """
        logger.debug('Writing into device input buffer: %r' % data)
        if not isinstance(data, bytes):
            raise TypeError('data must be an instance of bytes')

        if len(data) != 1:
            raise ValueError('data must have a length of 1, not %d' % len(data))

        self._input_buffer.extend(data)

        l = len(self._query_eom)
        if not self._input_buffer.endswith(self._query_eom):
            return

        query = bytes(self._input_buffer[:-l])
        response = self._match(query)
        eom = self._response_eom

        if response is None:
            response = self.error_response

        if isinstance(response, NoResponse):
            self._output_buffer = bytearray()
        else:
            self._output_buffer.extend(response)
            self._output_buffer.extend(eom)

        self._input_buffer = bytearray()

    def _match(self, query):
        """Tries to match in dialogues, getters and setters

        :param query: message tuple
        :type query: Tuple[bytes]
        :return: response if found or None
        :rtype: Tuple[bytes] | None
        """

        # Try to match in the queries
        try:
            response = self._dialogues[query]
            logger.debug('Found response in queries: %s' % repr(response))

            return response

        except KeyError:
            pass

        # Now in the getters
        try:
            name, response = self._getters[query]
            logger.debug('Found response in getter of %s' % name)

            return response.format(self._properties[name].value).encode('utf-8')

        except KeyError:
            pass

        q = query.decode('utf-8')

        # Finally in the setters, this will be slow.
        for name, parser, response, err in self._setters:
            try:
                value = parser(q)
                logger.debug('Found response in setter of %s' % name)
            except ValueError:
                continue

            try:
                self._properties[name].set_value(value)
                return response

            except ValueError:
                return err

        return None

    def read(self):
        """Return a single byte from the output buffer
        """
        if self._output_buffer:
            b, self._output_buffer = self._output_buffer[0:1], self._output_buffer[1:]
            return b
        elif isinstance(self.error_response, bytes):
            self._output_buffer.extend(self.error_response)
            self._output_buffer.extend(self._response_eom)
            b, self._output_buffer = self._output_buffer[0:1], self._output_buffer[1:]
            return b

        return b''

class Devices(object):
    """The group of connected devices.
    """

    def __init__(self):

        #: Devices
        #: dict[str, Device]
        self._internal = {}

    def add_device(self, resource_name, device):
        """Bind device to resource name
        """

        if device.resource_name is not None:
            raise ValueError('The device %r is already assigned to %s' % (device, device.resource_name))

        device.resource_name = resource_name

        self._internal[device.resource_name] = device

    def __getitem__(self, item):
        return self._internal[item]

    def list_resources(self):
        """List resource names.

        :rtype: tuple[str]
        """
        return tuple(self._internal.keys())

<|endoftext|>


<commit_before>"""
Django settings for demo project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Add our app to path
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '!@rf6@w&uz5b#8hk_^@b2fr^t163el(@0uf!xahiuc@k13hfru'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

	'debug_toolbar',
	'ratelimit9',
	'main',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'demo.urls'

WSGI_APPLICATION = 'demo.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'
<commit_msg>Adjusted demo settings<commit_after>"""
Django settings for demo project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Add our app to path
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '!@rf6@w&uz5b#8hk_^@b2fr^t163el(@0uf!xahiuc@k13hfru'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

	'debug_toolbar',
    'captcha',
	'ratelimit9',
	'main',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'demo.urls'

WSGI_APPLICATION = 'demo.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

RECAPTCHA_PUBLIC_KEY = '@todo'
RECAPTCHA_PRIVATE_KEY = '@todo'
<|endoftext|>


<commit_before>import struct
import subprocess
import threading
from time import sleep

from scapy.layers.dot11 import Dot11, Dot11Elt, sendp, Dot11Deauth, RadioTap
from termcolor import cprint

from pywiface.models import Station, AP
from pywiface.threads import ScannerThread

class WirelessInterface:
    def __init__(self, name: str, essid: str = None, monitor_mode: bool = False, channel=None):
        self.name = name
        self.monitor_mode = monitor_mode
        if monitor_mode:
            self.set_monitor_mode()
        self.stations = {}
        self.aps = {}
        self.lock = threading.Lock()
        self.channel_lock = threading.Lock()
        self.ap_sema = threading.Semaphore(0)
        self.sta_sema = threading.Semaphore(0)
        self.essid = essid
        self.bssid = None
        self.scan_thread = None
        self._channel = channel
        self.hop = True
        if channel:
            self.set_channel(channel)
            self.hop = False

    def set_up(self):
        subprocess.run(['/bin/ip', 'link', 'set', self.name, 'up'])

    def set_down(self):
        subprocess.run(['/bin/ip', 'link', 'set', self.name, 'down'])

    def set_monitor_mode(self):
        self.set_down()
        subprocess.run(['/sbin/iw', 'dev', self.name, 'set', 'monitor', 'none'])
        self.set_up()
        self.monitor_mode = True

    def set_managed_mode(self):
        self.set_down()
        subprocess.run(['/sbin/iw', 'dev', self.name, 'set', 'type', 'managed'])
        self.set_up()
        self.monitor_mode = False

    def get_frequency(self):
        return struct.pack("<h", 2407 + (self._channel * 5))

    @property
    def channel(self):
        return self._channel

    @channel.setter
    def channel(self, value):
        self.set_channel(value)

    def set_channel(self, channel):
        # in USA reg domain
        if 1 <= channel <= 11:
            subprocess.run(['/sbin/iw', 'dev', self.name, 'set', 'channel', str(channel)])
        self._channel = channel

    def set_mac(self, mac):
        self.set_down()
        subprocess.run(['/bin/ip', 'link', 'set', 'dev', self.name, 'address', mac], stdout=subprocess.DEVNULL)
        self.set_up()

    def reset_mac(self):
        self.set_down()
        subprocess.run(['/usr/bin/macchanger', '-p', self.name], stdout=subprocess.DEVNULL)
        self.set_up()

    def spoof_mac(self):
        self.set_down()
        subprocess.run(['/usr/bin/macchanger', '-A', self.name], stdout=subprocess.DEVNULL)
        self.set_up()

class MonitorInterface(WirelessInterface):
    def __init__(self, name: str, channel=None):
        super().__init__(name, monitor_mode=True, channel=channel)

    def deauth(self, target_mac: str, source_mac: str, count=1, burst_count=200, channel=None, reason=7):
        self.channel_lock.acquire()
        if channel:
            self.set_channel(channel)
        pkt = RadioTap() / Dot11(type=0, subtype=12, addr1=target_mac, addr2=source_mac,
                                 addr3=self.bssid) / Dot11Deauth(reason=reason)
        for i in range(count):
            cprint("DEAUTH!!!", 'red')
            for j in range(burst_count):
                self.inject(pkt)
            if count > 1:
                sleep(.1)
        self.channel_lock.release()

    def get_new_station(self) -> Station:
        self.sta_sema.acquire()
        target = next((self.stations[client] for client in self.stations if self.stations[client].new), None)
        target.new = False
        return target

    def get_new_ap(self) -> AP:
        self.ap_sema.acquire()
        target = next((self.aps[ap] for ap in self.aps if self.aps[ap].new), None)
        target.new = False
        return target

    def inject(self, pkt, inter=0):
        sendp(pkt, iface=self.name, inter=inter, verbose=False)

    def scan(self):
        self.scan_thread = ScannerThread(self)
        self.scan_thread.start()

    def stop_scan(self):
        self.scan_thread.stop()
        self.scan_thread.join()

    def ap_passes_test(self, pkt):
        return (pkt.haslayer(Dot11) and pkt.type == 0 and pkt.subtype == 8
                and pkt.addr3 not in [target.bssid for target in self.aps])

    def sta_passes_test(self, pkt):
        # Just for readability/sanity
        client_mgmt_subtypes = (0, 2, 4)
        check = False
        if pkt.haslayer(Dot11):
            if (pkt.type == 0 and (not self.bssid or pkt.addr3 == self.bssid)
                    and pkt.subtype in client_mgmt_subtypes):
                check = True
            elif pkt.type == 2 and (pkt.addr1 == self.bssid or not self.bssid):
                check = True
        if check:
            return not (self.stations.get(pkt.addr2) or self.stations.get(pkt.addr1))

    @staticmethod
    def sta_mac(pkt):
        return pkt.addr1 if not pkt.addr1 == pkt.addr3 else pkt.addr2

    def scan_callback(self, pkt):
        try:
            # Management/Beacon
            if self.ap_passes_test(pkt):
                self.lock.acquire()
                self.ap_sema.release()
                # http://stackoverflow.com/a/21664038
                essid, channel, w = None, None, None
                bssid = pkt.addr3
                crypto = ""
                cap = pkt.sprintf("{Dot11Beacon:%Dot11Beacon.cap%}"
                                  "{Dot11ProbeResp:%Dot11ProbeResp.cap%}").split('+')
                p = pkt[Dot11Elt]
                while isinstance(p, Dot11Elt):
                    if p.ID == 0:
                        try:
                            essid = p.info.decode()
                        except UnicodeDecodeError as e:
                            print(p.info)
                            essid = p.info
                    elif p.ID == 3:
                        try:
                            channel = ord(p.info)
                        except TypeError as e:
                            print(p.info)
                            channel = p.info
                    elif p.ID == 48:
                        crypto = "WPA2"
                        w = p.info[18:19]
                    elif p.ID == 221 and p.info.startswith(b'\x00P\xf2\x01\x01\x00'):
                        crypto = "WPA"
                    p = p.payload
                if not crypto:
                    if 'privacy' in cap:
                        crypto = "WEP"
                    else:
                        crypto = "OPN"
                self.aps[bssid] = AP(bssid, essid, crypto, channel, w)
                self.lock.release()
            elif self.sta_passes_test(pkt):
                self.lock.acquire()
                try:
                    channel = pkt[Dot11Elt:3].info
                except IndexError:
                    channel = self.channel
                mac = self.sta_mac(pkt)
                self.stations[mac] = Station(mac, channel, pkt.addr3)
                self.sta_sema.release()
                self.lock.release()
        except Exception as e:
            print(pkt)
            raise e
<commit_msg>Triple oops<commit_after>import struct
import subprocess
import threading
from time import sleep

from scapy.layers.dot11 import Dot11, Dot11Elt, sendp, Dot11Deauth, RadioTap
from termcolor import cprint

from pywiface.models import Station, AP
from pywiface.threads import ScannerThread

class WirelessInterface:
    def __init__(self, name: str, essid: str = None, monitor_mode: bool = False, channel=None):
        self.name = name
        self.monitor_mode = monitor_mode
        if monitor_mode:
            self.set_monitor_mode()
        self.stations = {}
        self.aps = {}
        self.lock = threading.Lock()
        self.channel_lock = threading.Lock()
        self.ap_sema = threading.Semaphore(0)
        self.sta_sema = threading.Semaphore(0)
        self.essid = essid
        self.bssid = None
        self.scan_thread = None
        self._channel = channel
        self.hop = True
        if channel:
            self.set_channel(channel)
            self.hop = False

    def set_up(self):
        subprocess.run(['/bin/ip', 'link', 'set', self.name, 'up'])

    def set_down(self):
        subprocess.run(['/bin/ip', 'link', 'set', self.name, 'down'])

    def set_monitor_mode(self):
        self.set_down()
        subprocess.run(['/sbin/iw', 'dev', self.name, 'set', 'monitor', 'none'])
        self.set_up()
        self.monitor_mode = True

    def set_managed_mode(self):
        self.set_down()
        subprocess.run(['/sbin/iw', 'dev', self.name, 'set', 'type', 'managed'])
        self.set_up()
        self.monitor_mode = False

    def get_frequency(self):
        return struct.pack("<h", 2407 + (self._channel * 5))

    @property
    def channel(self):
        return self._channel

    @channel.setter
    def channel(self, value):
        self.set_channel(value)

    def set_channel(self, channel):
        # in USA reg domain
        if 1 <= channel <= 11:
            subprocess.run(['/sbin/iw', 'dev', self.name, 'set', 'channel', str(channel)])
        self._channel = channel

    def set_mac(self, mac):
        self.set_down()
        subprocess.run(['/bin/ip', 'link', 'set', 'dev', self.name, 'address', mac], stdout=subprocess.DEVNULL)
        self.set_up()

    def reset_mac(self):
        self.set_down()
        subprocess.run(['/usr/bin/macchanger', '-p', self.name], stdout=subprocess.DEVNULL)
        self.set_up()

    def spoof_mac(self):
        self.set_down()
        subprocess.run(['/usr/bin/macchanger', '-A', self.name], stdout=subprocess.DEVNULL)
        self.set_up()

class MonitorInterface(WirelessInterface):
    def __init__(self, name: str, channel=None):
        super().__init__(name, monitor_mode=True, channel=channel)

    def deauth(self, target_mac: str, source_mac: str, count=1, burst_count=200, channel=None, reason=7):
        self.channel_lock.acquire()
        if channel:
            self.set_channel(channel)
        pkt = RadioTap() / Dot11(type=0, subtype=12, addr1=target_mac, addr2=source_mac,
                                 addr3=self.bssid) / Dot11Deauth(reason=reason)
        for i in range(count):
            cprint("DEAUTH!!!", 'red')
            for j in range(burst_count):
                self.inject(pkt)
            if count > 1:
                sleep(.1)
        self.channel_lock.release()

    def get_new_station(self) -> Station:
        self.sta_sema.acquire()
        target = next((self.stations[client] for client in self.stations if self.stations[client].new), None)
        target.new = False
        return target

    def get_new_ap(self) -> AP:
        self.ap_sema.acquire()
        target = next((self.aps[ap] for ap in self.aps if self.aps[ap].new), None)
        target.new = False
        return target

    def inject(self, pkt, inter=0):
        sendp(pkt, iface=self.name, inter=inter, verbose=False)

    def scan(self):
        self.scan_thread = ScannerThread(self)
        self.scan_thread.start()

    def stop_scan(self):
        self.scan_thread.stop()
        self.scan_thread.join()

    def ap_passes_test(self, pkt):
        return (pkt.haslayer(Dot11) and pkt.type == 0 and pkt.subtype == 8
                and not self.aps.get(pkt.addr3))

    def sta_passes_test(self, pkt):
        # Just for readability/sanity
        client_mgmt_subtypes = (0, 2, 4)
        check = False
        if pkt.haslayer(Dot11):
            if (pkt.type == 0 and (not self.bssid or pkt.addr3 == self.bssid)
                    and pkt.subtype in client_mgmt_subtypes):
                check = True
            elif pkt.type == 2 and (pkt.addr1 == self.bssid or not self.bssid):
                check = True
        if check:
            return not (self.stations.get(pkt.addr2) or self.stations.get(pkt.addr1))

    @staticmethod
    def sta_mac(pkt):
        return pkt.addr1 if not pkt.addr1 == pkt.addr3 else pkt.addr2

    def scan_callback(self, pkt):
        try:
            # Management/Beacon
            if self.ap_passes_test(pkt):
                self.lock.acquire()
                self.ap_sema.release()
                # http://stackoverflow.com/a/21664038
                essid, channel, w = None, None, None
                bssid = pkt.addr3
                crypto = ""
                cap = pkt.sprintf("{Dot11Beacon:%Dot11Beacon.cap%}"
                                  "{Dot11ProbeResp:%Dot11ProbeResp.cap%}").split('+')
                p = pkt[Dot11Elt]
                while isinstance(p, Dot11Elt):
                    if p.ID == 0:
                        try:
                            essid = p.info.decode()
                        except UnicodeDecodeError as e:
                            print(p.info)
                            essid = p.info
                    elif p.ID == 3:
                        try:
                            channel = ord(p.info)
                        except TypeError as e:
                            print(p.info)
                            channel = p.info
                    elif p.ID == 48:
                        crypto = "WPA2"
                        w = p.info[18:19]
                    elif p.ID == 221 and p.info.startswith(b'\x00P\xf2\x01\x01\x00'):
                        crypto = "WPA"
                    p = p.payload
                if not crypto:
                    if 'privacy' in cap:
                        crypto = "WEP"
                    else:
                        crypto = "OPN"
                self.aps[bssid] = AP(bssid, essid, crypto, channel, w)
                self.lock.release()
            elif self.sta_passes_test(pkt):
                self.lock.acquire()
                try:
                    channel = pkt[Dot11Elt:3].info
                except IndexError:
                    channel = self.channel
                mac = self.sta_mac(pkt)
                self.stations[mac] = Station(mac, channel, pkt.addr3)
                self.sta_sema.release()
                self.lock.release()
        except Exception as e:
            print(pkt)
            raise e
<|endoftext|>


<commit_before>from demobrowser import app, oid, db
from demobrowser.helpers import get_steam_userinfo, get_my_ip
from demobrowser.models import User, Demo
from flask import render_template, session, redirect, url_for, request, g, flash, get_flashed_messages
from functools import wraps
from pyfile import write_pyfile
import re
from glob import glob
import os

_steam_id_re = re.compile('steamcommunity.com/openid/id/(.*?)$')

if app.config.get('TEST', False):
    session['user_admin'] = True
    session['user_id'] = 'TEST_ID'
## Route Helpers ##
def admin_required(function):
    @wraps(function)
    def decorated(*args, **kwargs):
        if session.get('user_admin', None) is None:
            flash('Error, please login through Steam to view this page.', category='error')
            return redirect(url_for('index'))
        return function(*args, **kwargs)
    return decorated

def login_required(function):
    @wraps(function)
    def decorated(*args, **kwargs):
        print session
        if session.get('user_id', None) is None:
            flash('Error, please login through Steam to view this page.', category='error')
            return redirect(url_for('index'))
        return function(*args, **kwargs)
    return decorated

## Routes ##

@app.route('/')
def index():
    return render_template('demos.html', demos=Demo.get_page(1, app.config['DEMO_PER_PAGE']))

@app.route('/demos/page/<page>/')
def demopage(page=1):
    try:
        page = int(page)
    except ValueError:
        return redirect(url_for('index'))
    if page == 1:
        return redirect(url_for('index'))
    return render_template('demos.html', demos=Demo.get_page(page, app.config['DEMO_PER_PAGE']))

@app.route('/login/')
@oid.loginhandler
def login():
    if g.user is not None:
        return redirect(oid.get_next_url())
    return oid.try_login('http://steamcommunity.com/openid')

@oid.after_login
def check_login(resp):
    match = _steam_id_re.search(resp.identity_url)
    g.user = User.get(match.group(1))
    if not g.user:
        flash("Error, Could not log in. You don't have an account.", category='error')
        return redirect(url_for('index'))
    steamdata = get_steam_userinfo(g.user.steam_id, app.config['STEAM_API_KEY'])
    g.user.nickname = steamdata['personaname']
    db.session.commit()
    session['user_id'] = g.user.id
    session['user_admin'] = g.user.admin
    session['user_nick'] = g.user.nickname
    session['avatar'] = steamdata['avatar']
    flash('You are logged in as %s' % g.user.nickname, category='success')
    return redirect(oid.get_next_url())

@app.before_request
def before_request():
    g.user = None
    if 'user_id' in session:
        g.user = User.query.get(session['user_id'])

@app.route('/logout')
def logout():
    session.pop('user_id', None)
    session.pop('user_nick', None)
    session.pop('user_admin', None)
    flash('You\'ve been logged out.', category='info')
    return redirect(url_for('index'))

@app.route('/users/')
@admin_required
def users():
    return render_template('users.html', users=User.get_all())

@app.route('/users/delete/', methods=['POST'])
@admin_required
def delete_user():
    try:
        if int(request.form['delete']) == session['user_id']:
            return "You cannot delete yourself!", 403
    except ValueError:
        return "Fail.", 403
    if User.delete(int(request.form['delete'])):
        return "Success."
    return "Fail.", 403

@app.route('/users/add/', methods=['GET', 'POST'])
@admin_required
def add_user():
    errors = []
    values = {}
    if request.method == 'POST':
        values['name'] = request.form['name']
        values['steamid'] = request.form['steamid']
        values['admin'] = request.form.get('admin', None) is not None
        for key, value in values.items():
            if value == '':
                errors.append(key)
        if not errors:
            success, msg = User.create(values['name'], values['steamid'], values['admin'])
            if success:
                flash(msg, category='success')
                db.session.commit()
            else:
                flash(msg, category='error')
            return redirect(url_for('add_user'))
    return render_template('add_user.html', errors=errors, values=values)

@app.route('/users/makeadmin/', methods=['POST'])
@admin_required
def make_admin():
    try:
        if int(request.form['userid']) == session['user_id']:
            return "You cannot modify your own admin privs.", 403
    except ValueError:
        return "Fail.", 403
    user = User.get_from_id(int(request.form['userid']))
    admin = request.form['admin'].lower() == 'true'
    if user:
        user.make_admin(admin)
        return "Success"
    return "Fail.", 403

@app.route('/settings/', methods=['GET', 'POST'])
@admin_required
def settings():
    values = {}
    if request.method == 'POST':
        # These 4 cannot be changed in settings
        values['STEAM_API_KEY'] = str(app.config['STEAM_API_KEY'])
        values['SECRET_KEY'] = str(app.config['SECRET_KEY'])
        values['ADDRESS'] = str(app.config['ADDRESS'])
        values['SQLALCHEMY_DATABASE_URI'] = str(app.config['SQLALCHEMY_DATABASE_URI'])
        # The rest of these can
        values['TITLE'] = str(request.form['title'])
        values['DEMO_INBOX'] = str(request.form.get('inbox', None))
        values['DEBUG'] = request.form.get('debug', None) is not None
        values['DEMO_DIR'] = str(request.form.get('upload', None))
        values['DEMO_PUBLIC'] = request.form.get('public', None) is not None
        try:
            values['DEMO_PER_PAGE'] = int(request.form.get('perpage', 12))
        except ValueError:
            flash("Warning: Defaulting Demos Per Page to 12 because you didn't put a number in that textbox, idiot.", category='warn')
        #if request.form.get('cleardb', None) is not None and values['DEBUG'] is True:
        #    db.drop_all()
        #    db.create_all()
        write_pyfile(app.config['CFG_FILE'], values)
        # Now set our config values properly
        for key, value in values.iteritems():
            app.config[key] = value
        flash('Success! Your settings were updated.', category='success')
    return render_template('settings.html')

@app.route('/demos/add/', methods=['GET', 'POST'])
@admin_required
def add_demo():
    values = {}
    if request.method == 'POST':
        demo_name = request.form.get("demo_name", None)
        if demo_name:
            success, msg = Demo.create_from_name(demo_name)
            if success:
                flash(msg, category='success')
                db.session.commit()
            else:
                flash(msg, category='error')
            return redirect(url_for('index'))
    return render_template('add_demo.html', values=values)

@app.route('/demos/import/', methods=['GET', 'POST'])
@admin_required
def import_demo():
    values = {}
    if request.method == 'POST':
        for demo_name in request.form:
            success, msg = Demo.create_from_name(demo_name)
            if success:
                flash(msg, category='success')
                db.session.commit()
            else:
                flash(msg, category='error')

    demos_raw = glob(app.config.get('DEMO_INBOX', '.') + "/*.dem")
    demos = []
    for demo in demos_raw:
        if not Demo.demo_exists(os.path.basename(demo)):
            demos.append(os.path.basename(demo))
    return render_template('import_demo.html', demos=demos)

@app.route('/demos/view_raw/<demo>', methods=['GET'])
def view_demo_raw(demo=None):
    # This gets the version that doesn't include the headers.
    if demo is None:
        return redirect(url_for('index'))
    demo = Demo.get_from_id(demo)
    return render_template('view_demo_raw.html', demo=demo)

@app.route('/demos/view/<demo>', methods=['GET'])
def view_demo(demo=None):
    # This renders the permalink
    if demo is None:
        return redirect(url_for('index'))
    demo = Demo.get_from_id(demo)
    return render_template('view_demo.html', demo=demo, pages=Demo.get_page(demo.id, 1))

@app.route('/demos/delete/', methods=['POST'])
def delete_demo():
    if request.method == 'POST':
        demoid = request.form.get("demoid", None)
        Demo.delete(int(demoid))
    return "Yay."

@app.route('/demos/field/<demo>', methods=['POST'])
@admin_required
def edit_demo_field(demo=None):
    demo = Demo.get_from_id(demo)
    if request.method == 'POST':
        name = request.form.get("name", '')
        value = request.form.get("value", '')
        if name == "delete":
            # special case,  we need to redirect
            print "Yay!"
            flash('Success! Demo %s was deleted!' % demo.title, category='success')
            return redirect(url_for('index'))
        if name == "summary":
            value = value.replace("\n", "
")
        setattr(demo, name, value)
        db.session.commit()
        return "Yep."
    return "Nope."
<commit_msg>Added a flash for undefined demos, fix security exploit with deleting<commit_after>from demobrowser import app, oid, db
from demobrowser.helpers import get_steam_userinfo, get_my_ip
from demobrowser.models import User, Demo
from flask import render_template, session, redirect, url_for, request, g, flash, get_flashed_messages
from functools import wraps
from pyfile import write_pyfile
import re
from glob import glob
import os

_steam_id_re = re.compile('steamcommunity.com/openid/id/(.*?)$')

if app.config.get('TEST', False):
    session['user_admin'] = True
    session['user_id'] = 'TEST_ID'
## Route Helpers ##
def admin_required(function):
    @wraps(function)
    def decorated(*args, **kwargs):
        if session.get('user_admin', None) is None:
            flash('Error, please login through Steam to view this page.', category='error')
            return redirect(url_for('index'))
        return function(*args, **kwargs)
    return decorated

def login_required(function):
    @wraps(function)
    def decorated(*args, **kwargs):
        print session
        if session.get('user_id', None) is None:
            flash('Error, please login through Steam to view this page.', category='error')
            return redirect(url_for('index'))
        return function(*args, **kwargs)
    return decorated

## Routes ##

@app.route('/')
def index():
    return render_template('demos.html', demos=Demo.get_page(1, app.config['DEMO_PER_PAGE']))

@app.route('/demos/page/<page>/')
def demopage(page=1):
    try:
        page = int(page)
    except ValueError:
        return redirect(url_for('index'))
    if page == 1:
        return redirect(url_for('index'))
    return render_template('demos.html', demos=Demo.get_page(page, app.config['DEMO_PER_PAGE']))

@app.route('/login/')
@oid.loginhandler
def login():
    if g.user is not None:
        return redirect(oid.get_next_url())
    return oid.try_login('http://steamcommunity.com/openid')

@oid.after_login
def check_login(resp):
    match = _steam_id_re.search(resp.identity_url)
    g.user = User.get(match.group(1))
    if not g.user:
        flash("Error, Could not log in. You don't have an account.", category='error')
        return redirect(url_for('index'))
    steamdata = get_steam_userinfo(g.user.steam_id, app.config['STEAM_API_KEY'])
    g.user.nickname = steamdata['personaname']
    db.session.commit()
    session['user_id'] = g.user.id
    session['user_admin'] = g.user.admin
    session['user_nick'] = g.user.nickname
    session['avatar'] = steamdata['avatar']
    flash('You are logged in as %s' % g.user.nickname, category='success')
    return redirect(oid.get_next_url())

@app.before_request
def before_request():
    g.user = None
    if 'user_id' in session:
        g.user = User.query.get(session['user_id'])

@app.route('/logout')
def logout():
    session.pop('user_id', None)
    session.pop('user_nick', None)
    session.pop('user_admin', None)
    flash('You\'ve been logged out.', category='info')
    return redirect(url_for('index'))

@app.route('/users/')
@admin_required
def users():
    return render_template('users.html', users=User.get_all())

@app.route('/users/delete/', methods=['POST'])
@admin_required
def delete_user():
    try:
        if int(request.form['delete']) == session['user_id']:
            return "You cannot delete yourself!", 403
    except ValueError:
        return "Fail.", 403
    if User.delete(int(request.form['delete'])):
        return "Success."
    return "Fail.", 403

@app.route('/users/add/', methods=['GET', 'POST'])
@admin_required
def add_user():
    errors = []
    values = {}
    if request.method == 'POST':
        values['name'] = request.form['name']
        values['steamid'] = request.form['steamid']
        values['admin'] = request.form.get('admin', None) is not None
        for key, value in values.items():
            if value == '':
                errors.append(key)
        if not errors:
            success, msg = User.create(values['name'], values['steamid'], values['admin'])
            if success:
                flash(msg, category='success')
                db.session.commit()
            else:
                flash(msg, category='error')
            return redirect(url_for('add_user'))
    return render_template('add_user.html', errors=errors, values=values)

@app.route('/users/makeadmin/', methods=['POST'])
@admin_required
def make_admin():
    try:
        if int(request.form['userid']) == session['user_id']:
            return "You cannot modify your own admin privs.", 403
    except ValueError:
        return "Fail.", 403
    user = User.get_from_id(int(request.form['userid']))
    admin = request.form['admin'].lower() == 'true'
    if user:
        user.make_admin(admin)
        return "Success"
    return "Fail.", 403

@app.route('/settings/', methods=['GET', 'POST'])
@admin_required
def settings():
    values = {}
    if request.method == 'POST':
        # These 4 cannot be changed in settings
        values['STEAM_API_KEY'] = str(app.config['STEAM_API_KEY'])
        values['SECRET_KEY'] = str(app.config['SECRET_KEY'])
        values['ADDRESS'] = str(app.config['ADDRESS'])
        values['SQLALCHEMY_DATABASE_URI'] = str(app.config['SQLALCHEMY_DATABASE_URI'])
        # The rest of these can
        values['TITLE'] = str(request.form['title'])
        values['DEMO_INBOX'] = str(request.form.get('inbox', None))
        values['DEBUG'] = request.form.get('debug', None) is not None
        values['DEMO_DIR'] = str(request.form.get('upload', None))
        values['DEMO_PUBLIC'] = request.form.get('public', None) is not None
        try:
            values['DEMO_PER_PAGE'] = int(request.form.get('perpage', 12))
        except ValueError:
            flash("Warning: Defaulting Demos Per Page to 12 because you didn't put a number in that textbox, idiot.", category='warn')
        #if request.form.get('cleardb', None) is not None and values['DEBUG'] is True:
        #    db.drop_all()
        #    db.create_all()
        write_pyfile(app.config['CFG_FILE'], values)
        # Now set our config values properly
        for key, value in values.iteritems():
            app.config[key] = value
        flash('Success! Your settings were updated.', category='success')
    return render_template('settings.html')

@app.route('/demos/add/', methods=['GET', 'POST'])
@admin_required
def add_demo():
    values = {}
    if request.method == 'POST':
        demo_name = request.form.get("demo_name", None)
        if demo_name:
            success, msg = Demo.create_from_name(demo_name)
            if success:
                flash(msg, category='success')
                db.session.commit()
            else:
                flash(msg, category='error')
            return redirect(url_for('index'))
    return render_template('add_demo.html', values=values)

@app.route('/demos/import/', methods=['GET', 'POST'])
@admin_required
def import_demo():
    values = {}
    if request.method == 'POST':
        for demo_name in request.form:
            success, msg = Demo.create_from_name(demo_name)
            if success:
                flash(msg, category='success')
                db.session.commit()
            else:
                flash(msg, category='error')

    demos_raw = glob(app.config.get('DEMO_INBOX', '.') + "/*.dem")
    demos = []
    for demo in demos_raw:
        if not Demo.demo_exists(os.path.basename(demo)):
            demos.append(os.path.basename(demo))
    return render_template('import_demo.html', demos=demos)

@app.route('/demos/view_raw/<demo>', methods=['GET'])
def view_demo_raw(demo=None):
    # This gets the version that doesn't include the headers.
    if demo is None:
        return redirect(url_for('index'))
    demo = Demo.get_from_id(demo)
    return render_template('view_demo_raw.html', demo=demo)

@app.route('/demos/view/<demo>', methods=['GET'])
def view_demo(demo=None):
    # This renders the permalink
    if demo is None:
        return redirect(url_for('index'))
    demo = Demo.get_from_id(demo)
    if demo is None:
        flash('Sorry I couldn\'t find that demo...', category='warning')
        return redirect(url_for('index'))
    return render_template('view_demo.html', demo=demo, pages=Demo.get_page(demo.id, 1))

@app.route('/demos/delete/', methods=['POST'])
@admin_required
def delete_demo():
    if request.method == 'POST':
        demoid = request.form.get("demoid", None)
        Demo.delete(int(demoid))
    return "Yay."

@app.route('/demos/field/<demo>', methods=['POST'])
@admin_required
def edit_demo_field(demo=None):
    demo = Demo.get_from_id(demo)
    if request.method == 'POST':
        name = request.form.get("name", '')
        value = request.form.get("value", '')
        if name == "delete":
            # special case,  we need to redirect
            flash('Success! Demo %s was deleted!' % demo.title, category='success')
            return redirect(url_for('index'))
        if name == "summary":
            value = value.replace("\n", "
")
        setattr(demo, name, value)
        db.session.commit()
        return "Yep."
    return "Nope."
<|endoftext|>


<commit_before>"""
Provides helper functionality specificially for Qt5 QML code generators
"""

class Filters(object):
    """provides a set of filters to be used with the template engine"""
    classPrefix = ''

    @staticmethod
    def className(symbol):
        classPrefix = Filters.classPrefix
        return '{0}{1}'.format(classPrefix, symbol.name)

    @staticmethod
    def defaultValue(symbol):
        module = symbol.module.module_name
        t = symbol.type
        if t.is_primitive:
            if t.name == 'int':
                return '0'
            elif t.name == 'bool':
                return 'false'
            elif t.name == 'string':
                return "''"
            elif t.name == 'var':
                return "undefined"
        elif t.is_enum:
            value = next(iter(t.reference.members))
            return '{0}Module.{1}'.format(module, value)
        if t.is_struct:
            return '{0}Module.create{1}()'.format(module, t)
        if t.is_model:
            return 'ListModel {}'
        return 'XXX'

    @staticmethod
    def propertyType(symbol):
        t = symbol.type
        if t.is_enum:
            return 'int'
        if t.is_void or t.is_primitive:
            if t.name == 'int':
                return 'int'
            if t.name == 'bool':
                return 'bool'
            if t.name == 'string':
                return 'string'
        if t.is_struct:
            return 'var'
        if t.is_model:
            return 'ListModel'
        return t

<commit_msg>added default value support for a real value<commit_after>"""
Provides helper functionality specificially for Qt5 QML code generators
"""

class Filters(object):
    """provides a set of filters to be used with the template engine"""
    classPrefix = ''

    @staticmethod
    def className(symbol):
        classPrefix = Filters.classPrefix
        return '{0}{1}'.format(classPrefix, symbol.name)

    @staticmethod
    def defaultValue(symbol):
        module = symbol.module.module_name
        t = symbol.type
        if t.is_primitive:
            if t.name == 'int':
                return '0'
            elif t.name == 'real':
                return "0.0"
            elif t.name == 'bool':
                return 'false'
            elif t.name == 'string':
                return "''"
            elif t.name == 'var':
                return "undefined"
        elif t.is_enum:
            value = next(iter(t.reference.members))
            return '{0}Module.{1}'.format(module, value)
        if t.is_struct:
            return '{0}Module.create{1}()'.format(module, t)
        if t.is_model:
            return 'ListModel {}'
        return 'XXX'

    @staticmethod
    def propertyType(symbol):
        t = symbol.type
        if t.is_enum:
            return 'int'
        if t.is_void or t.is_primitive:
            if t.name == 'int':
                return 'int'
            if t.name == 'bool':
                return 'bool'
            if t.name == 'string':
                return 'string'
        if t.is_struct:
            return 'var'
        if t.is_model:
            return 'ListModel'
        return t

<|endoftext|>


<commit_before>version_info = (0, 6, 0, 'dev0')
__version__ = '.'.join(map(str, version_info))
<commit_msg>Release 0.5.5<commit_after>version_info = (0, 5, 5)
__version__ = '.'.join(map(str, version_info))
<|endoftext|>