# Path: ripyl/util/enum.py
# class Enum(object):
#     '''Base class for enumeration classes
#     
#     This provides a name() class method that will return the string
#     attribute name for an encoded enumeration value.
#     
#     e.g.
#     
#     >>> class Colors(Enum):
#     ...     blue = 1
#     ...     red = 2
#     ...     green = 3
#         
#     >>> Colors.name(Colors.red)
#     'red'
#     
#     >>> Colors.name(1, full_name=True)
#     'Colors.blue'
# 
#     "Instantiating" the Enum sub-class calls the name() method as well:
#     
#     >>> Colors(3, full_name=False)
#     'green'
#     
#     If necesssary you can use non-integers as the values as long as they are hashable:
#     
#     >>> class MathConst(Enum):
#     ...     pi = 3.2     # in certain US states :)
#     ...     e = 2.71828
#         
#     >>> MathConst.pi
#     3.2
#     
#     '''
# 
#     @classmethod
#     def name(cls, value, full_name=False):
#         '''Lookup the enumeration name with the provided value
# 
#         value (hashable)
#             A hashable Enum value (typically int) to find a name for.
# 
#         full_name (bool)
#             Include full name of Enum object in result
# 
#         Returns a string for the Enum attribute associated with value.
#         '''
#         
#         try:
#             enum_lookup = cls._enum_lookup
#         except AttributeError:
#             # Build inverse dict of class attributes with their values as key
#             enum_lookup = dict((v, k) for k, v in cls.__dict__.items() if k[0] != '_')
#             cls._enum_lookup = enum_lookup
#         
#         try:
#             aname = enum_lookup[value]
#             prefix = cls.__name__ + '.' if full_name else ''
#             return prefix + aname
# 
#         except KeyError:
#             return 'unknown'
# 
#     def __new__(cls, value, full_name=False):
#         '''Override class instantitation to perform name lookup instead'''
#         return cls.name(value, full_name)


import itertools
import ripyl.streaming as stream
import ripyl.sigproc as sigp
from ripyl.decode import *
from ripyl.util.enum import Enum
from ripyl.util.bitops import *
from copy import copy

    if variant not in can_variant:
        raise ValueError('Invalid CAN variant "{}"'.format(variant))

    field_bounds = dict(can_variant[variant])
    field_names = [f[0] for f in can_variant[variant]]
    field_vals = dict(zip(field_names, [0]*len(field_names)))

    for k, v in kwargs.iteritems():
        if k not in field_vals:
            raise ValueError('Invalid field name "{}"'.format(k))

        field_vals[k] = v

    id = 0
    for fn in field_names:
        v = field_vals[fn]
        bounds = field_bounds[fn]
        flen = bounds[0] - bounds[1] + 1

        mask = 2**flen - 1
        if v > mask:
            raise ValueError('Value too large for field "{}", {}'.format(fn, v))

        id <<= flen
        id += v & mask

    return id
        



# Path: ripyl/util/enum.py
# class Enum(object):
#     '''Base class for enumeration classes
#     
#     This provides a name() class method that will return the string
#     attribute name for an encoded enumeration value.
#     
#     e.g.
#     
#     >>> class Colors(Enum):
#     ...     blue = 1
#     ...     red = 2
#     ...     green = 3
#         
#     >>> Colors.name(Colors.red)
#     'red'
#     
#     >>> Colors.name(1, full_name=True)
#     'Colors.blue'
# 
#     "Instantiating" the Enum sub-class calls the name() method as well:
#     
#     >>> Colors(3, full_name=False)
#     'green'
#     
#     If necesssary you can use non-integers as the values as long as they are hashable:
#     
#     >>> class MathConst(Enum):
#     ...     pi = 3.2     # in certain US states :)
#     ...     e = 2.71828
#         
#     >>> MathConst.pi
#     3.2
#     
#     '''
# 
#     @classmethod
#     def name(cls, value, full_name=False):
#         '''Lookup the enumeration name with the provided value
# 
#         value (hashable)
#             A hashable Enum value (typically int) to find a name for.
# 
#         full_name (bool)
#             Include full name of Enum object in result
# 
#         Returns a string for the Enum attribute associated with value.
#         '''
#         
#         try:
#             enum_lookup = cls._enum_lookup
#         except AttributeError:
#             # Build inverse dict of class attributes with their values as key
#             enum_lookup = dict((v, k) for k, v in cls.__dict__.items() if k[0] != '_')
#             cls._enum_lookup = enum_lookup
#         
#         try:
#             aname = enum_lookup[value]
#             prefix = cls.__name__ + '.' if full_name else ''
#             return prefix + aname
# 
#         except KeyError:
#             return 'unknown'
# 
#     def __new__(cls, value, full_name=False):
#         '''Override class instantitation to perform name lookup instead'''
#         return cls.name(value, full_name)


# Path: ripyl/protocol/can.py
import itertools
import ripyl.streaming as stream
import ripyl.sigproc as sigp
from ripyl.decode import *
from ripyl.util.enum import Enum
from ripyl.util.bitops import *
from copy import copy

    if variant not in can_variant:
        raise ValueError('Invalid CAN variant "{}"'.format(variant))

    field_bounds = dict(can_variant[variant])
    field_names = [f[0] for f in can_variant[variant]]
    field_vals = dict(zip(field_names, [0]*len(field_names)))

    for k, v in kwargs.iteritems():
        if k not in field_vals:
            raise ValueError('Invalid field name "{}"'.format(k))

        field_vals[k] = v

    id = 0
    for fn in field_names:
        v = field_vals[fn]
        bounds = field_bounds[fn]
        flen = bounds[0] - bounds[1] + 1

        mask = 2**flen - 1
        if v > mask:
            raise ValueError('Value too large for field "{}", {}'.format(fn, v))

        id <<= flen
        id += v & mask

    return id
        



class CANStreamStatus(Enum):
