Skip to content

Commit

Permalink
Refactor device type into device interface for future device type
Browse files Browse the repository at this point in the history
Closes-Bug: #2
  • Loading branch information
Dantali0n committed Jun 11, 2020
1 parent ad81f1d commit 296e7dc
Show file tree
Hide file tree
Showing 29 changed files with 133 additions and 130 deletions.
2 changes: 1 addition & 1 deletion radloggerpy/cli/argument_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ArgumentHelper(object):
The expected structure of :py:attr:`arguments` could look as follows:
`{ 'name' : Argument(), '--type' : Argument('-t', required=True) }`
`{ 'name' : Argument(), '--interface' : Argument('-i', required=True) }`
When the extend of certain Argument parameters is not known at compile time
these parameters can be added using
Expand Down
12 changes: 6 additions & 6 deletions radloggerpy/cli/v1/device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from radloggerpy.cli.argument import Argument
from radloggerpy.cli.v1.device.device_helper import DeviceHelper
from radloggerpy.types.device_types import TYPE_CHOICES
from radloggerpy.types.device_interfaces import INTERFACE_CHOICES


@six.add_metaclass(abc.ABCMeta)
Expand All @@ -39,8 +39,8 @@ def arguments(self):
type=int),
'--name': Argument(
'-n', help="Unique name to help identify this device."),
'--type': Argument(
'-t', help="Type of interface to communicate with the "
'--interface': Argument(
'-f', help="Type of interface to communicate with the "
"radiation monitoring device."),
'--implementation': Argument(
'-m', help="The specific implementation of radiation "
Expand All @@ -49,8 +49,8 @@ def arguments(self):
})
return self._arguments

def _add_types(self):
self.arguments['--type'].add_kwarg(
def _add_interfaces(self):
self.arguments['--interface'].add_kwarg(
'choices',
TYPE_CHOICES.values()
INTERFACE_CHOICES.values()
)
6 changes: 3 additions & 3 deletions radloggerpy/cli/v1/device/device_add_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from radloggerpy.cli.argument import Argument
from radloggerpy.cli.v1.device.device_add import DeviceAddCommand
from radloggerpy.database.objects.serial_device import SerialDeviceObject
from radloggerpy.types.device_types import DeviceTypes
from radloggerpy.types.device_interfaces import DeviceInterfaces
from radloggerpy.types.serial_bytesize import BYTESIZE_CHOICES
from radloggerpy.types.serial_parity import PARITY_CHOICES
from radloggerpy.types.serial_stopbit import STOPBIT_CHOICES
Expand Down Expand Up @@ -56,11 +56,11 @@ def arguments(self):

def get_parser(self, program_name):
parser = super(DeviceAddSerial, self).get_parser(program_name)
self._add_implementations(DeviceTypes.SERIAL)
self._add_implementations(DeviceInterfaces.SERIAL)
self.register_arguments(parser)
return parser

def take_action(self, parsed_args):
serial_obj = SerialDeviceObject(**dict(parsed_args._get_kwargs()))
serial_obj.type = DeviceTypes.SERIAL
serial_obj.interface = DeviceInterfaces.SERIAL
return SerialDeviceObject.add(self.app.database_session, serial_obj)
6 changes: 3 additions & 3 deletions radloggerpy/cli/v1/device/device_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class DeviceHelper(ArgumentHelper):
# Should be overridden by child classes
_implementation_key = ''

def _add_implementations(self, device_type=None):
def _add_implementations(self, device_interface=None):

if device_type is None:
if device_interface is None:
choices = [dev.NAME for dev in
DeviceManager.get_device_implementations()]
else:
choices = [dev.NAME for dev in
DeviceManager.get_device_implementations()
if dev.TYPE == device_type]
if dev.INTERFACE == device_interface]

self.arguments[self._implementation_key].add_kwarg(
'choices', choices
Expand Down
6 changes: 3 additions & 3 deletions radloggerpy/cli/v1/device/device_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def arguments(self):

def get_parser(self, program_name):
parser = super(DeviceList, self).get_parser(program_name)
self._add_types()
self._add_interfaces()
self._add_implementations()
self.register_arguments(parser)
return parser
Expand All @@ -48,10 +48,10 @@ def take_action(self, parsed_args):
if len(data) == 0:
raise RuntimeWarning(_("No devices found"))

fields = ('id', 'name', 'type', 'implementation')
fields = ('id', 'name', 'interface', 'implementation')
values = []
for result in data:
value = (result.id, result.name, result.type,
value = (result.id, result.name, result.interface,
result.implementation)
values.append(value)

Expand Down
14 changes: 7 additions & 7 deletions radloggerpy/cli/v1/device/device_list_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from radloggerpy.cli.argument import Argument
from radloggerpy.cli.v1.device.device import DeviceCommand
from radloggerpy.database.objects.serial_device import SerialDeviceObject
from radloggerpy.types.device_types import DeviceTypes
from radloggerpy.types.device_interfaces import DeviceInterfaces
from radloggerpy.types.serial_bytesize import BYTESIZE_CHOICES
from radloggerpy.types.serial_parity import PARITY_CHOICES
from radloggerpy.types.serial_stopbit import STOPBIT_CHOICES
Expand Down Expand Up @@ -56,13 +56,13 @@ def arguments(self):
choices=STOPBIT_CHOICES.values()),
'--timeout': Argument('-t', default=None),
})
if '--type' in self._arguments:
del self._arguments['--type']
if '--interface' in self._arguments:
del self._arguments['--interface']
return self._arguments

def get_parser(self, program_name):
parser = super(DeviceListSerial, self).get_parser(program_name)
self._add_implementations(DeviceTypes.SERIAL)
self._add_implementations(DeviceInterfaces.SERIAL)
self.register_arguments(parser)
return parser

Expand All @@ -76,11 +76,11 @@ def take_action(self, parsed_args):
if len(data) == 0:
raise RuntimeWarning(_("No devices found"))

fields = ('id', 'name', 'type', 'implementation', 'port', 'baudrate',
'bytesize', 'parity', 'stopbits', 'timeout')
fields = ('id', 'name', 'interface', 'implementation', 'port',
'baudrate', 'bytesize', 'parity', 'stopbits', 'timeout')
values = []
for result in data:
value = (result.id, result.name, result.type,
value = (result.id, result.name, result.interface,
result.implementation, result.port, result.baudrate,
result.bytesize, result.parity, result.stopbits,
result.timeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
from radloggerpy.device.device_manager import DeviceManager


class DeviceTypes(Lister):
"""Command to list available device types and implementations"""
class DeviceModels(Lister):
"""Command to list available device interfaces and implementations"""

def take_action(self, parsed_args):
columns = ("type", 'implementation')
columns = ("interface", 'implementation')

# Convert data from device_map
map = DeviceManager.get_device_map()
Expand Down
8 changes: 4 additions & 4 deletions radloggerpy/cli/v1/device/device_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class DeviceRemove(ShowOne, DeviceCommand):
def arguments(self):
if self._arguments is None:
self._arguments = super().arguments
if '--type' in self._arguments:
del self._arguments['--type']
if '--interface' in self._arguments:
del self._arguments['--interface']
if '--implementation' in self._arguments:
del self._arguments['--implementation']
return self._arguments
Expand Down Expand Up @@ -59,8 +59,8 @@ def take_action(self, parsed_args):
if data is None:
raise RuntimeWarning(_("Device could not be found"))

fields = ('id', 'name', 'type', 'implementation')
values = (data.id, data.name, data.type, data.implementation)
fields = ('id', 'name', 'interface', 'implementation')
values = (data.id, data.name, data.interface, data.implementation)

self.app.LOG.info(_("Device removed successfully"))
return (fields, values)
19 changes: 11 additions & 8 deletions radloggerpy/cli/v1/device/device_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from radloggerpy.cli.v1.device.device import DeviceCommand
from radloggerpy.database.objects.device import DeviceObject
from radloggerpy.database.objects.serial_device import SerialDeviceObject
from radloggerpy.types.device_types import DeviceTypes
from radloggerpy.types.device_types import TYPE_CHOICES
from radloggerpy.types.device_interfaces import DeviceInterfaces
from radloggerpy.types.device_interfaces import INTERFACE_CHOICES


class DeviceShow(ShowOne, DeviceCommand):
Expand All @@ -43,7 +43,7 @@ def arguments(self):

def get_parser(self, program_name):
parser = super(DeviceShow, self).get_parser(program_name)
self._add_types()
self._add_interfaces()
self._add_implementations()
self.register_arguments(parser)
return parser
Expand All @@ -63,19 +63,22 @@ def take_action(self, parsed_args):
if data is None:
raise RuntimeWarning(_("Device could not be found"))

fields = ('id', 'name', 'type', 'implementation')
values = (data.id, data.name, data.type, data.implementation)
fields = ('id', 'name', 'interface', 'implementation')
values = (data.id, data.name, data.interface, data.implementation)

if details and data.type == TYPE_CHOICES[DeviceTypes.SERIAL]:
if details and data.interface == \
INTERFACE_CHOICES[DeviceInterfaces.SERIAL]:
data = SerialDeviceObject.find(
self.app.database_session, device_obj, False)
fields += ('port', 'baudrate', 'bytesize', 'parity',
'stopbits', 'timeout')
values += (data.port, data.baudrate, data.bytesize, data.parity,
data.stopbits, data.timeout)
elif details and data.type == TYPE_CHOICES[DeviceTypes.ETHERNET]:
elif details and data.interface == \
INTERFACE_CHOICES[DeviceInterfaces.ETHERNET]:
pass
elif details and data.type == TYPE_CHOICES[DeviceTypes.USB]:
elif details and data.interface == \
INTERFACE_CHOICES[DeviceInterfaces.USB]:
pass

return (fields, values)
14 changes: 7 additions & 7 deletions radloggerpy/cli/v1/device/device_show_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from radloggerpy.cli.argument import Argument
from radloggerpy.cli.v1.device.device_show import DeviceShow
from radloggerpy.database.objects.serial_device import SerialDeviceObject
from radloggerpy.types.device_types import DeviceTypes
from radloggerpy.types.device_interfaces import DeviceInterfaces
from radloggerpy.types.serial_bytesize import BYTESIZE_CHOICES
from radloggerpy.types.serial_parity import PARITY_CHOICES
from radloggerpy.types.serial_stopbit import STOPBIT_CHOICES
Expand Down Expand Up @@ -56,13 +56,13 @@ def arguments(self):
choices=STOPBIT_CHOICES.values()),
'--timeout': Argument('-t', default=None),
})
if '--type' in self._arguments:
del self._arguments['--type']
if '--interface' in self._arguments:
del self._arguments['--interface']
return self._arguments

def get_parser(self, program_name):
parser = super(DeviceShow, self).get_parser(program_name)
self._add_implementations(DeviceTypes.SERIAL)
self._add_implementations(DeviceInterfaces.SERIAL)
self.register_arguments(parser)
return parser

Expand All @@ -79,9 +79,9 @@ def take_action(self, parsed_args):
if data is None:
raise RuntimeWarning(_("Device could not be found"))

fields = ('id', 'name', 'type', 'implementation', 'port', 'baudrate',
'bytesize', 'parity', 'stopbits', 'timeout')
values = (data.id, data.name, data.type, data.implementation,
fields = ('id', 'name', 'interface', 'implementation', 'port',
'baudrate', 'bytesize', 'parity', 'stopbits', 'timeout')
values = (data.id, data.name, data.interface, data.implementation,
data.port, data.baudrate, data.bytesize, data.parity,
data.stopbits, data.timeout)

Expand Down
4 changes: 2 additions & 2 deletions radloggerpy/database/models/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

from radloggerpy.database.declarative_base import base
from radloggerpy.types.device_implementations import IMPLEMENTATION_CHOICES
from radloggerpy.types.device_types import DeviceTypes
from radloggerpy.types.device_interfaces import DeviceInterfaces


class Device(base):
id = Column(Integer, primary_key=True)

name = Column(String, unique=True)

type = Column(Enum(DeviceTypes))
interface = Column(Enum(DeviceInterfaces))

implementation = Column(ChoiceType(IMPLEMENTATION_CHOICES))

Expand Down
18 changes: 9 additions & 9 deletions radloggerpy/database/objects/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

from radloggerpy.database.models.device import Device
from radloggerpy.database.objects.base import DatabaseObject
from radloggerpy.types.device_types import TYPE_CHOICES
from radloggerpy.types.device_interfaces import INTERFACE_CHOICES


class DeviceObject(DatabaseObject):
"""device object with base model attributes"""

id = None
name = None
type = None
interface = None
implementation = None

m_device = None
Expand All @@ -36,11 +36,11 @@ def _build_object(self):
if self.name:
self.m_device.name = self.name

if self.type in TYPE_CHOICES.keys():
self.m_device.type = self.type
elif self.type in TYPE_CHOICES.values():
index = list(TYPE_CHOICES.values()).index(self.type)
self.m_device.type = list(TYPE_CHOICES.keys())[index]
if self.interface in INTERFACE_CHOICES.keys():
self.m_device.interface = self.interface
elif self.interface in INTERFACE_CHOICES.values():
index = list(INTERFACE_CHOICES.values()).index(self.interface)
self.m_device.interface = list(INTERFACE_CHOICES.keys())[index]

if self.implementation:
self.m_device.implementation = self.implementation
Expand All @@ -51,8 +51,8 @@ def _build_attributes(self):
if self.m_device.name:
self.name = self.m_device.name

if self.m_device.type:
self.type = TYPE_CHOICES[self.m_device.type]
if self.m_device.interface:
self.interface = INTERFACE_CHOICES[self.m_device.interface]

if self.m_device.implementation:
self.implementation = self.m_device.implementation.code
Expand Down
4 changes: 2 additions & 2 deletions radloggerpy/database/objects/serial_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from radloggerpy.database.models.device import Device
from radloggerpy.database.models.serial_device import SerialDevice
from radloggerpy.database.objects.device import DeviceObject
from radloggerpy.types.device_types import DeviceTypes
from radloggerpy.types.device_interfaces import DeviceInterfaces
from radloggerpy.types.serial_bytesize import BYTESIZE_CHOICES
from radloggerpy.types.serial_parity import PARITY_CHOICES
from radloggerpy.types.serial_stopbit import STOPBIT_CHOICES
Expand Down Expand Up @@ -130,7 +130,7 @@ def find(session, reference, allow_multiple=True):
reference._build_object()

"""Only look for serial devices"""
reference.m_device.type = DeviceTypes.SERIAL
reference.m_device.interface = DeviceInterfaces.SERIAL

base_filters = reference._filter(reference.m_device)

Expand Down
4 changes: 2 additions & 2 deletions radloggerpy/device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Device(object):
"Each radiation monitoring device should have a unique name"
NAME = "Device"

"Each radiation monitoring device should be of a specific type"
TYPE = None
"Each radiation monitoring device should use a specific interface"
INTERFACE = None

def __init__(self):
self.data = DeviceDataBuffer()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
import six

from radloggerpy.device import device
from radloggerpy.types.device_types import DeviceTypes
from radloggerpy.types.device_interfaces import DeviceInterfaces


@six.add_metaclass(abc.ABCMeta)
class EthernetDevice(device.Device):
"""EthernetDevice base class"""

NAME = "EthernetDevice"
TYPE = DeviceTypes.ETHERNET
INTERFACE = DeviceInterfaces.ETHERNET

def __init__(self):
super(EthernetDevice, self).__init__()
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import six

from radloggerpy.device import device
from radloggerpy.types.device_types import DeviceTypes
from radloggerpy.types.device_interfaces import DeviceInterfaces


@six.add_metaclass(abc.ABCMeta)
Expand All @@ -39,7 +39,7 @@ class SerialDevice(device.Device):
# interfacing classes(add_devices(session, [device])).

NAME = "SerialDevice"
TYPE = DeviceTypes.SERIAL
INTERFACE = DeviceInterfaces.SERIAL

def __init__(self):
super(SerialDevice, self).__init__()
Loading

0 comments on commit 296e7dc

Please sign in to comment.