Skip to content
Snippets Groups Projects
Commit 7fdf8a30 authored by ptangu01's avatar ptangu01
Browse files

clean code, and pep8 format style code, fix syntax error XAALDeviceError

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/trunk@1358 b32b6428-25c9-4566-ad07-03861ab6144f
parent f77556e3
No related branches found
No related tags found
No related merge requests found
# -*- coding: utf-8 -*-
#
# Copyright 2014, Jérôme Colin, Jérôme Kerdreux, Philippe Tanguy, Telecom Bretagne.
# Copyright 2014, Jérôme Colin, Jérôme Kerdreux, Philippe Tanguy,
# Telecom Bretagne.
#
# This file is part of xAAL.
#
......@@ -20,7 +21,6 @@
#
from __future__ import print_function
import time
from . import config
......@@ -84,18 +84,27 @@ class DeviceError(Exception):
self.code = code
self.description = desc
class Device(object):
def __init__(self,devType=None,addr=None,parent=None,childrens=None,engine=None):
def __init__(
self,
devType=None,
addr=None,
parent=None,
childrens=None,
engine=None):
self.setType(devType)
self.setAddress(addr)
if parent:
self.setParent(parent)
else: self.__parent = None
else:
self.__parent = None
if childrens:
# childrens is a list of xaal devices instances
self.setChildrens(childrens)
else: self.__childrens = list()
else:
self.__childrens = list()
self.setEngine(engine)
self.__vendorID = None
self.__productID = None
......@@ -123,7 +132,7 @@ class Device(object):
# Address
def setAddress(self, value):
if value == config.XAAL_BCAST_ADDR:
raise XAALDeviceError("This address is reserved")
raise DeviceError("This address is reserved")
self.__address = value
def getAddress(self):
......@@ -135,7 +144,7 @@ class Device(object):
# Parent
def setParent(self, value):
if isinstance(self, Gateway):
raise XAALDeviceError("Unable to set parent for Gateway")
raise DeviceError("Unable to set parent for Gateway")
self.__parent = value
def getParent(self):
......@@ -283,18 +292,25 @@ class Device(object):
#####################################################
def getDescription(self):
result = {}
if self.getVendorID(): result['vendorID']=self.getVendorID()
if self.getProductID(): result['productID']=self.getProductID()
if self.getVersion(): result['version']=self.getVersion()
if self.getURL(): result['url']=self.getURL()
if self.getInfo(): result['info']=self.getInfo()
if self.getHwId(): result['hwId']=self.getHwId()
if self.getVendorID():
result['vendorID'] = self.getVendorID()
if self.getProductID():
result['productID'] = self.getProductID()
if self.getVersion():
result['version'] = self.getVersion()
if self.getURL():
result['url'] = self.getURL()
if self.getInfo():
result['info'] = self.getInfo()
if self.getHwId():
result['hwId'] = self.getHwId()
if self.getParent():
result['parent'] = self.getParent().getAddress()
else:
result['parent'] = ""
if self.getChildrens():
result['childrens']=[dev.getAddress() for dev in self.getChildrens()]
result['childrens'] = [dev.getAddress()
for dev in self.getChildrens()]
else:
result['childrens'] = list()
result['unsupportedMethods'] = self.getUnsupportedMethods()
......@@ -312,8 +328,8 @@ class Device(object):
list
TODO: (Waiting for spec. decision) add test on attribute devices
- case physical sensor not responding or value not ready
add error with specific error code and with value= suspicious/stale/cached ...
- case physical sensor not responding or value not ready add error
with specific error code and with value = suspicious/stale/cached
"""
result = {}
devAttr = self._getAttributes()
......@@ -324,12 +340,15 @@ class Device(object):
# Warning: just to test because error msg not yet specified
# in spec.
if devAttr[attr].getStatus() == "update":
result.update({devAttr[attr].getName():devAttr[attr].getValue()})
result.update(
{devAttr[attr].getName(): devAttr[attr].getValue()}
)
else:
logger.debug('Not Yet Implemented')
else:
# Todo: raise error???
# Problem??? because raise error and do not process other attributes if others are good
# Problem??? because raise error and do not process other
# attributes if others are good
# raise XAALInternalError("Attribute %s not found" % attr)
logger.debug("Attribute %s not found" % attr)
else:
......@@ -372,7 +391,8 @@ class _ParentComposite(Device):
raise GatewayError("unable to attach a Gateway to anything")
if isinstance(self, Composite):
if isinstance(dev, Composite):
raise CompositeError("unable to attach a composite to another one")
raise CompositeError(
"unable to attach a composite to another one")
self.__devices.append(dev)
......@@ -408,10 +428,13 @@ class _ParentComposite(Device):
return {'embedded': embedded}
class CompositeError(DeviceError): pass
class CompositeError(DeviceError):
pass
class Composite(_ParentComposite):
""" Composite is a device with some embedded (childrens) & a visibility flag """
""" Composite is a device with some embedded
(childrens) & a visibility flag """
def __init__(self, devType=None, address=None, parent=None):
self.__visibility = True
......@@ -420,7 +443,7 @@ class Composite(_ParentComposite):
# visibility attribute
def setVisibility(self, value):
if value==True:
if value:
self.__visibility = True
else:
self.__visibility = False
......@@ -436,14 +459,14 @@ class Composite(_ParentComposite):
- embedded
"""
result = {}
devAttr=self._getAttributes()
if attributes:
"""Process attributes filter"""
for attr in attributes:
if attr == 'embedded':
result.update(self._getEmbedded())
else:
# Problem??? because raise error and do not process other attributes if others are good
# Problem??? because raise error and do not process other
# attributes if others are good
# raise XAALInternalError("Attribute %s not found" % attr)
logger.debug("Attribute %s not found" % attr)
else:
......@@ -453,7 +476,9 @@ class Composite(_ParentComposite):
getAttributes.exposed = True
class GatewayError(DeviceError): pass
class GatewayError(DeviceError):
pass
class Gateway(_ParentComposite):
......@@ -476,7 +501,6 @@ class Gateway(_ParentComposite):
- embedded
"""
result = {}
devAttr=self._getAttributes()
if attributes:
"""Process attributes filter"""
for attr in attributes:
......@@ -485,7 +509,8 @@ class Gateway(_ParentComposite):
elif attr == 'embedded':
result.update(self._getEmbedded())
else:
# Problem??? because raise error and do not process other attributes if others are good
# Problem??? because raise error and do not process other
# attributes if others are good
# raise XAALInternalError("Attribute %s not found" % attr)
logger.info("Attribute %s not found" % attr)
else:
......@@ -518,4 +543,3 @@ def findDeviceMethods(dev):
method = getattr(dev, m)
result[method.__name__] = method
return result
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment