<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from ctypes import *
import time

from IC_GrabberDLL import IC_GrabberDLL
from IC_Exception import IC_Exception
from IC_Structures import GrabberHandle
from IC_Property import IC_Property

# "typedefs"
IMG_FILETYPE = ['FILETYPE_BMP',
                'FILETYPE_JPG']
COLOR_FORMAT = ['Y800',
                'RGB24',
                'RGB32',
                'UYVY',
                'Y16',
                'NONE']

# c function type for frame callback
# outside of class so it can be called by unbound function
C_FRAME_READY_CALLBACK = CFUNCTYPE(None, GrabberHandle, POINTER(c_ubyte), c_ulong, c_void_p)

class IC_Camera(object):
    
    @property
    def callback_registered(self):
        return self._callback_registered
  
    def __init__(self, unique_device_name):
        
        self._unique_device_name = unique_device_name
        
        self._handle = IC_GrabberDLL.create_grabber()
        if not self._handle:
            raise IC_Exception(todo)
        
        self._callback_registered = False
        self._frame = {'num'    :   -1,
                       'ready'  :   False}
        
    def __getattr__(self, attr):
    
        if attr in IC_Property.get_all_property_names():
            return IC_Property(self._handle, attr)
        else:
            raise IC_Exception(-101)
        
    #def __setattr__(self, attr, val):
    #    
    #    if attr.startswith('_'):
    #        super(IC_Camera, self).__setattr__(attr, val)
    #
    #    # if it's an actual device property
    #    elif attr in self.get_all_property_names():
    #        IC_Property(self._handle, attr).value = val
    #
    #    # otherwise just set the attribute value as normal
    #    else:
    #        super(IC_Camera, self).__setattr__(attr, val)
        
    def open(self):
        """
        Open the camera device, required for most functions.
        """
        err = IC_GrabberDLL.open_device_by_unique_name(self._handle,
                                                       self._unique_device_name)
        if err != 1:
            raise IC_Exception(err)
    
    def is_open(self):
        """
        Check if the camera device is currently open.
        
        :returns: boolean -- True if camera is open.
        """
        return bool(IC_GrabberDLL.is_dev_valid(self._handle))
    
    def show_property_dialog(self):
        """
        Show property dialog for device.
        """
        err = IC_GrabberDLL.show_property_dialog(self._handle)
        if err != 1:
            raise IC_Exception(err)
    
    def list_property_names(self):
        return IC_Property.get_all_property_names()
    
    # use props instead, e.g. cam.gain.range
    #def get_property_range(self, property_name):
    #    return IC_Property(self._handle, property_name).range
    #
    #def is_property_available(self, property_name):
    #    return IC_Property(self._handle, property_name).is_available
    #
    #def is_property_auto_available(self, property_name):
    #    return IC_Property(self._handle, property_name).is_auto_available
    #
    #def get_property_type(self, property_name):
    #    return IC_Property(self._handle, property_name).type
    
    def list_video_formats(self):
        """
        :returns: list -- available video formats.
        """
        vf_list = ((c_char * 80) * 40)()
        num_vfs = IC_GrabberDLL.list_video_formats(self._handle, byref(vf_list), c_int(80))
        if num_vfs < 0:
            raise IC_Exception(num_vfs)
        return_list = []
        for vf in vf_list:
            if vf.value:
                return_list.append(vf.value)
        return return_list    
    
    def get_video_format_count(self):
        """
        :returns: int -- number of available video formats.
        """
        vf_count = IC_GrabberDLL.get_video_format_count(self._handle)
        if vf_count < 0:
            raise IC_Exception(vf_count)
        return vf_count
    
    def get_video_format(self, format_index):
        """
        Get video format of the device.
        """
        # DLL says need to call this first for it to work
        num_vfs = self.get_video_format_count()
        if format_index >= num_vfs:
            raise IC_Exception(todo)
        vf = IC_GrabberDLL.get_video_format(self._handle, c_int(format_index))
        if vf is None:
            raise IC_Exception(err)
        return vf
    
    def set_video_format(self, video_format):
        """
        Set a video format for the device. Must be supported.
        
        :param video_format: string -- video format to use.
        """
        err = IC_GrabberDLL.set_video_format(self._handle, c_char_p(video_format))
        if err != 1:
            raise IC_Exception(err)
    
    def get_video_format_width(self):
        """
        """
        return IC_GrabberDLL.get_video_format_width(self._handle)
        
    def get_video_format_height(self):
        """
        """
        return IC_GrabberDLL.get_video_format_height(self._handle)
        
    def get_format(self):
        """
        """
        return IC_GrabberDLL.get_format(self._handle)
    
    def set_format(self, format):
        """
        """
        err = IC_GrabberDLL.set_format(self._handle, c_int(value))
        if err != 1:
            raise IC_Exception(err)
            
    def is_triggerable(self):
        """
        """
        return bool(IC_GrabberDLL.is_trigger_available(self._handle))
        
    def get_frame_rate(self):
        """
        """
        return IC_GrabberDLL.get_frame_rate(self._handle)
    
    def set_frame_rate(self, frame_rate):
        """<commit_msg>Added get_video_norm_count, get_video_norm, set_video_norm methods.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from ctypes import *
import time

from IC_GrabberDLL import IC_GrabberDLL
from IC_Exception import IC_Exception
from IC_Structures import GrabberHandle
from IC_Property import IC_Property

# "typedefs"
IMG_FILETYPE = ['FILETYPE_BMP',
                'FILETYPE_JPG']
COLOR_FORMAT = ['Y800',
                'RGB24',
                'RGB32',
                'UYVY',
                'Y16',
                'NONE']

# c function type for frame callback
# outside of class so it can be called by unbound function
C_FRAME_READY_CALLBACK = CFUNCTYPE(None, GrabberHandle, POINTER(c_ubyte), c_ulong, c_void_p)

class IC_Camera(object):
    
    @property
    def callback_registered(self):
        return self._callback_registered
  
    def __init__(self, unique_device_name):
        
        self._unique_device_name = unique_device_name
        
        self._handle = IC_GrabberDLL.create_grabber()
        if not self._handle:
            raise IC_Exception(todo)
        
        self._callback_registered = False
        self._frame = {'num'    :   -1,
                       'ready'  :   False}
        
    def __getattr__(self, attr):
    
        if attr in IC_Property.get_all_property_names():
            return IC_Property(self._handle, attr)
        else:
            raise IC_Exception(-101)
        
    #def __setattr__(self, attr, val):
    #    
    #    if attr.startswith('_'):
    #        super(IC_Camera, self).__setattr__(attr, val)
    #
    #    # if it's an actual device property
    #    elif attr in self.get_all_property_names():
    #        IC_Property(self._handle, attr).value = val
    #
    #    # otherwise just set the attribute value as normal
    #    else:
    #        super(IC_Camera, self).__setattr__(attr, val)
        
    def open(self):
        """
        Open the camera device, required for most functions.
        """
        err = IC_GrabberDLL.open_device_by_unique_name(self._handle,
                                                       self._unique_device_name)
        if err != 1:
            raise IC_Exception(err)
    
    def is_open(self):
        """
        Check if the camera device is currently open.
        
        :returns: boolean -- True if camera is open.
        """
        return bool(IC_GrabberDLL.is_dev_valid(self._handle))
    
    def show_property_dialog(self):
        """
        Show property dialog for device.
        """
        err = IC_GrabberDLL.show_property_dialog(self._handle)
        if err != 1:
            raise IC_Exception(err)
    
    def list_property_names(self):
        return IC_Property.get_all_property_names()
    
    # use props instead, e.g. cam.gain.range
    #def get_property_range(self, property_name):
    #    return IC_Property(self._handle, property_name).range
    #
    #def is_property_available(self, property_name):
    #    return IC_Property(self._handle, property_name).is_available
    #
    #def is_property_auto_available(self, property_name):
    #    return IC_Property(self._handle, property_name).is_auto_available
    #
    #def get_property_type(self, property_name):
    #    return IC_Property(self._handle, property_name).type
    
    def list_video_formats(self):
        """
        :returns: list -- available video formats.
        """
        vf_list = ((c_char * 80) * 40)()
        num_vfs = IC_GrabberDLL.list_video_formats(self._handle, byref(vf_list), c_int(80))
        if num_vfs < 0:
            raise IC_Exception(num_vfs)
        return_list = []
        for vf in vf_list:
            if vf.value:
                return_list.append(vf.value)
        return return_list
        
    def get_video_norm_count(self):
        """
        Get the number of the available video norm formats for the current device. 
        A video capture device must have been opened before this call.
        
        :returns: int -- number of available video norms.
        """
        vn_count = IC_GrabberDLL.get_video_norm_count(self._handle)
        if vn_count < 0:
            raise IC_Exception(vn_count)
        return vn_count
    
    def get_video_norm(self, norm_index):
        """
        Get a string representation of the video norm specified by norm_index. 
        norm_index must be between 0 and get_video_norm_count().
        
        :returns: string -- name of video norm of specified index.
        """
        # DLL says need to call this first for it to work
        num_vns = self.get_video_norm_count()
        if norm_index >= num_vns:
            raise IC_Exception(-102)
        vn = IC_GrabberDLL.get_video_norm(self._handle, c_int(norm_index))
        if vn is None:
            raise IC_Exception(-104)
        return vn
    
    def get_video_format_count(self):
        """
        Get the number of the available video formats for the current device. 
        A video capture device must have been opened before this call.

        :returns: int -- number of available video formats.
        """
        vf_count = IC_GrabberDLL.get_video_format_count(self._handle)
        if vf_count < 0:
            raise IC_Exception(vf_count)
        return vf_count
    
    def get_video_format(self, format_index):
        """
        Get a string representation of the video format specified by format_index. 
        format_index must be between 0 and get_video_format_count().
        """
        # DLL says need to call this first for it to work
        num_vfs = self.get_video_format_count()
        if format_index >= num_vfs:
            raise IC_Exception(-103)
        vf = IC_GrabberDLL.get_video_format(self._handle, c_int(format_index))
        if vf is None:
            raise IC_Exception(-105)
        return vf
        
    def get_video_format_width(self):
        """
        """
        return IC_GrabberDLL.get_video_format_width(self._handle)
        
    def get_video_format_height(self):
        """
        """
        return IC_GrabberDLL.get_video_format_height(self._handle)
    
    def set_video_format(self, video_format):
        """
        Set a video format for the device. Must be supported.
        
        :param video_format: string -- video format to use.
        """
        err = IC_GrabberDLL.set_video_format(self._handle, c_char_p(video_format))
        if err != 1:
            raise IC_Exception(err)
            
    def set_video_norm(self, video_norm):
        """
        Sets video norm format, whatver that means.
        
        :param video_norm: string -- video norm to use.
        """
        err = IC_GrabberDLL.set_video_norm(self._handle, c_char_p(video_norm))
        if err != 1:
            raise IC_Exception(err)

    def get_format(self):
        """
        """
        return IC_GrabberDLL.get_format(self._handle)
    
    def set_format(self, format):
        """
        """
        err = IC_GrabberDLL.set_format(self._handle, c_int(value))
        if err != 1:
            raise IC_Exception(err)
            
    def is_triggerable(self):
        """
        """
        return bool(IC_GrabberDLL.is_trigger_available(self._handle))
        
    def get_frame_rate(self):
        """
        """
        return IC_GrabberDLL.get_frame_rate(self._handle)
    
    def set_frame_rate(self, frame_rate):
        """
{'fraction_non_alphanumeric': 0.0872991583779648, 'fraction_numerical': 0.005202754399387911, 'mean_word_length': 2.024994214302245, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4641627', 'n_tokens_mistral': 4034, 'n_tokens_neox': 3899, 'n_words': 893}