New API to check if a table class exists before defining it.

- Check if it's part of the metadata through a function call rather than
  checking `Base.metadata` in every single module.

- Make it possible to override them (mostly for doc generation logic
  that needs to be able to import those classes).

- Make it possible to extend them.
This commit is contained in:
Fabio Manganiello 2023-10-04 09:50:10 +02:00
parent 608844ca0c
commit d872835093
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
32 changed files with 170 additions and 82 deletions

View file

@ -1,3 +1,6 @@
from contextlib import contextmanager
from dataclasses import dataclass
from sqlalchemy import __version__
sa_version = tuple(map(int, __version__.split('.')))
@ -8,3 +11,38 @@ else:
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
@dataclass
class DbContext:
"""
Context flags for the database session.
"""
override_definitions: bool = False
_ctx = DbContext()
@contextmanager
def override_definitions():
"""
Temporarily override the definitions of the entities in the entities
registry.
This is useful when the entities are being imported off-context, like
e.g. in the `inspect` or `alembic` modules.
"""
_ctx.override_definitions = True
yield
_ctx.override_definitions = False
def is_defined(table_name: str) -> bool:
"""
Check if the given entity class is defined in the entities registry.
:param table_name: Name of the table associated to the entity class.
"""
return not _ctx.override_definitions and table_name in Base.metadata

View file

@ -30,7 +30,7 @@ from sqlalchemy.orm.exc import ObjectDeletedError
import platypush
from platypush.config import Config
from platypush.common.db import Base
from platypush.common.db import Base, is_defined
from platypush.message import JSONAble, Message
EntityRegistryType = Dict[str, Type['Entity']]
@ -52,7 +52,7 @@ fail.
logger = logging.getLogger(__name__)
if 'entity' not in Base.metadata:
if not is_defined('entity'):
class Entity(Base):
"""

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .three_axis import ThreeAxisSensor
if 'accelerometer' not in Base.metadata:
if not is_defined('accelerometer'):
class Accelerometer(ThreeAxisSensor):
"""
@ -20,6 +20,7 @@ if 'accelerometer' not in Base.metadata:
primary_key=True,
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,12 +1,12 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .dimmers import Dimmer
from .switches import Switch
if 'volume' not in Base.metadata:
if not is_defined('volume'):
class Volume(Dimmer):
__tablename__ = 'volume'
@ -15,12 +15,13 @@ if 'volume' not in Base.metadata:
Integer, ForeignKey(Dimmer.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'muted' not in Base.metadata:
if not is_defined('muted'):
class Muted(Switch):
__tablename__ = 'muted'
@ -29,6 +30,7 @@ if 'muted' not in Base.metadata:
Integer, ForeignKey(Switch.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'battery' not in Base.metadata:
if not is_defined('battery'):
class Battery(NumericSensor):
__tablename__ = 'battery'
@ -19,6 +19,7 @@ if 'battery' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -9,13 +9,13 @@ from sqlalchemy import (
String,
)
from platypush.common.db import Base
from platypush.common.db import is_defined
from ..devices import Device
from ._service import BluetoothService
if 'bluetooth_device' not in Base.metadata:
if not is_defined('bluetooth_device'):
class BluetoothDevice(Device):
"""
@ -68,6 +68,7 @@ if 'bluetooth_device' not in Base.metadata:
model_id = Column(String, default=None)
""" Device model ID. """
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -8,10 +8,10 @@ from sqlalchemy import (
String,
)
from platypush.common.db import Base
from platypush.common.db import is_defined
from platypush.entities import Entity
if 'bluetooth_service' not in Base.metadata:
if not is_defined('bluetooth_service'):
class BluetoothService(Entity):
"""
@ -44,6 +44,7 @@ if 'bluetooth_service' not in Base.metadata:
connected = Column(Boolean, default=False)
""" Whether an active connection exists to this service. """
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -6,14 +6,14 @@ from sqlalchemy import (
Integer,
)
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import EnumSensor
logger = logging.getLogger(__name__)
if 'button' not in Base.metadata:
if not is_defined('button'):
class Button(EnumSensor):
__tablename__ = 'button'
@ -22,6 +22,7 @@ if 'button' not in Base.metadata:
Integer, ForeignKey(EnumSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -6,12 +6,12 @@ from sqlalchemy import (
String,
)
from platypush.common.db import Base
from platypush.common.db import is_defined
from .devices import Device
if 'cloud_instance' not in Base.metadata:
if not is_defined('cloud_instance'):
class CloudInstance(Device):
"""
@ -38,6 +38,7 @@ if 'cloud_instance' not in Base.metadata:
alerts = Column(JSON)
backups = Column(JSON)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import BinarySensor
if 'contact_sensor' not in Base.metadata:
if not is_defined('contact_sensor'):
class ContactSensor(BinarySensor):
"""
@ -18,6 +18,7 @@ if 'contact_sensor' not in Base.metadata:
Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, Boolean, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from ._base import Entity
if 'device' not in Base.metadata:
if not is_defined('device'):
class Device(Entity):
"""
@ -19,6 +19,7 @@ if 'device' not in Base.metadata:
)
reachable = Column(Boolean, default=True)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey, Float, String
from platypush.common.db import Base
from platypush.common.db import is_defined
from .devices import Device
if 'dimmer' not in Base.metadata:
if not is_defined('dimmer'):
class Dimmer(Device):
"""
@ -24,6 +24,7 @@ if 'dimmer' not in Base.metadata:
value = Column(Float)
unit = Column(String)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'distance_sensor' not in Base.metadata:
if not is_defined('distance_sensor'):
class DistanceSensor(NumericSensor):
"""
@ -18,6 +18,7 @@ if 'distance_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'power_sensor' not in Base.metadata:
if not is_defined('power_sensor'):
class PowerSensor(NumericSensor):
__tablename__ = 'power_sensor'
@ -14,12 +14,13 @@ if 'power_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'current_sensor' not in Base.metadata:
if not is_defined('current_sensor'):
class CurrentSensor(NumericSensor):
__tablename__ = 'current_sensor'
@ -28,12 +29,13 @@ if 'current_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'voltage_sensor' not in Base.metadata:
if not is_defined('voltage_sensor'):
class VoltageSensor(NumericSensor):
__tablename__ = 'voltage_sensor'
@ -42,12 +44,13 @@ if 'voltage_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'energy_sensor' not in Base.metadata:
if not is_defined('energy_sensor'):
class EnergySensor(NumericSensor):
__tablename__ = 'energy_sensor'
@ -56,6 +59,7 @@ if 'energy_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'heart_rate_sensor' not in Base.metadata:
if not is_defined('heart_rate_sensor'):
class HeartRateSensor(NumericSensor):
"""
@ -18,6 +18,7 @@ if 'heart_rate_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'humidity_sensor' not in Base.metadata:
if not is_defined('humidity_sensor'):
class HumiditySensor(NumericSensor):
"""
@ -18,12 +18,13 @@ if 'humidity_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'dew_point_sensor' not in Base.metadata:
if not is_defined('dew_point_sensor'):
class DewPointSensor(NumericSensor):
"""
@ -36,6 +37,7 @@ if 'dew_point_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'illuminance_sensor' not in Base.metadata:
if not is_defined('illuminance_sensor'):
class IlluminanceSensor(NumericSensor):
__tablename__ = 'illuminance_sensor'
@ -14,6 +14,7 @@ if 'illuminance_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, Float
from platypush.common.db import Base
from platypush.common.db import is_defined
from .devices import Device
if 'light' not in Base.metadata:
if not is_defined('light'):
class Light(Device):
__tablename__ = 'light'
@ -34,6 +34,7 @@ if 'light' not in Base.metadata:
temperature_min = Column(Float)
temperature_max = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'link_quality' not in Base.metadata:
if not is_defined('link_quality'):
class LinkQuality(NumericSensor):
__tablename__ = 'link_quality'
@ -19,6 +19,7 @@ if 'link_quality' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .three_axis import ThreeAxisSensor
if 'magnetometer' not in Base.metadata:
if not is_defined('magnetometer'):
class Magnetometer(ThreeAxisSensor):
"""
@ -20,6 +20,7 @@ if 'magnetometer' not in Base.metadata:
primary_key=True,
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import BinarySensor
if 'motion_sensor' not in Base.metadata:
if not is_defined('motion_sensor'):
class MotionSensor(BinarySensor):
__tablename__ = 'motion_sensor'
@ -14,6 +14,7 @@ if 'motion_sensor' not in Base.metadata:
Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import BinarySensor
if 'presence_sensor' not in Base.metadata:
if not is_defined('presence_sensor'):
class PresenceSensor(BinarySensor):
"""
@ -18,6 +18,7 @@ if 'presence_sensor' not in Base.metadata:
Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'pressure_sensor' not in Base.metadata:
if not is_defined('pressure_sensor'):
class PressureSensor(NumericSensor):
"""
@ -18,6 +18,7 @@ if 'pressure_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -12,7 +12,7 @@ from sqlalchemy import (
String,
)
from platypush.common.db import Base
from platypush.common.db import is_defined
from .devices import Device
@ -32,7 +32,7 @@ class Sensor(Device):
super().__init__(*args, **kwargs)
if 'raw_sensor' not in Base.metadata:
if not is_defined('raw_sensor'):
class RawSensor(Sensor):
"""
@ -86,12 +86,13 @@ if 'raw_sensor' not in Base.metadata:
self.is_binary = False
self.is_json = False
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'numeric_sensor' not in Base.metadata and 'percent_sensor' not in Base.metadata:
if not is_defined('numeric_sensor') and not is_defined('percent_sensor'):
class NumericSensor(Sensor):
"""
@ -109,6 +110,7 @@ if 'numeric_sensor' not in Base.metadata and 'percent_sensor' not in Base.metada
max = Column(Float)
unit = Column(String)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
@ -124,6 +126,7 @@ if 'numeric_sensor' not in Base.metadata and 'percent_sensor' not in Base.metada
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
@ -135,7 +138,7 @@ if 'numeric_sensor' not in Base.metadata and 'percent_sensor' not in Base.metada
super().__init__(*args, **kwargs)
if 'binary_sensor' not in Base.metadata:
if not is_defined('binary_sensor'):
class BinarySensor(Sensor):
"""
@ -163,12 +166,13 @@ if 'binary_sensor' not in Base.metadata:
)
value = Column(Boolean)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'enum_sensor' not in Base.metadata:
if not is_defined('enum_sensor'):
class EnumSensor(Sensor):
"""
@ -184,12 +188,13 @@ if 'enum_sensor' not in Base.metadata:
values = Column(JSON)
""" Possible values for the sensor, as a JSON array. """
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'composite_sensor' not in Base.metadata:
if not is_defined('composite_sensor'):
class CompositeSensor(Sensor):
"""
@ -204,6 +209,7 @@ if 'composite_sensor' not in Base.metadata:
)
value = Column(JSON)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'steps_sensor' not in Base.metadata:
if not is_defined('steps_sensor'):
class StepsSensor(NumericSensor):
"""
@ -18,6 +18,7 @@ if 'steps_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey, Boolean, String, JSON
from platypush.common.db import Base
from platypush.common.db import is_defined
from .devices import Device
if 'switch' not in Base.metadata:
if not is_defined('switch'):
class Switch(Device):
__tablename__ = 'switch'
@ -15,12 +15,13 @@ if 'switch' not in Base.metadata:
)
state = Column(Boolean)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'enum_switch' not in Base.metadata:
if not is_defined('enum_switch'):
class EnumSwitch(Device):
__tablename__ = 'enum_switch'
@ -31,6 +32,7 @@ if 'enum_switch' not in Base.metadata:
value = Column(String)
values = Column(JSON)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,6 +1,6 @@
from sqlalchemy import Boolean, Column, Float, ForeignKey, Integer, JSON, String
from platypush.common.db import Base
from platypush.common.db import is_defined
from . import Entity
from .devices import Device
@ -8,7 +8,7 @@ from .sensors import NumericSensor, PercentSensor
from .temperature import TemperatureSensor
if 'cpu' not in Base.metadata:
if not is_defined('cpu'):
class Cpu(Entity):
"""
@ -23,12 +23,13 @@ if 'cpu' not in Base.metadata:
percent = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'cpu_info' not in Base.metadata:
if not is_defined('cpu_info'):
class CpuInfo(Entity):
"""
@ -54,12 +55,13 @@ if 'cpu_info' not in Base.metadata:
l2_cache_size = Column(Integer)
l3_cache_size = Column(Integer)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'cpu_times' not in Base.metadata:
if not is_defined('cpu_times'):
class CpuTimes(Entity):
"""
@ -72,12 +74,13 @@ if 'cpu_times' not in Base.metadata:
Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'cpu_stats' not in Base.metadata:
if not is_defined('cpu_stats'):
class CpuStats(Entity):
"""
@ -90,12 +93,13 @@ if 'cpu_stats' not in Base.metadata:
Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'memory_stats' not in Base.metadata:
if not is_defined('memory_stats'):
class MemoryStats(Entity):
"""
@ -119,12 +123,13 @@ if 'memory_stats' not in Base.metadata:
shared = Column(Integer)
percent = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'swap_stats' not in Base.metadata:
if not is_defined('swap_stats'):
class SwapStats(Entity):
"""
@ -142,12 +147,13 @@ if 'swap_stats' not in Base.metadata:
free = Column(Integer)
percent = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'disk' not in Base.metadata:
if not is_defined('disk'):
class Disk(Entity):
"""
@ -175,12 +181,13 @@ if 'disk' not in Base.metadata:
write_time = Column(Float)
busy_time = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'network_interface' not in Base.metadata:
if not is_defined('network_interface'):
class NetworkInterface(Device):
"""
@ -207,12 +214,13 @@ if 'network_interface' not in Base.metadata:
duplex = Column(String)
flags = Column(JSON)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'system_temperature' not in Base.metadata:
if not is_defined('system_temperature'):
class SystemTemperature(TemperatureSensor):
"""
@ -230,12 +238,13 @@ if 'system_temperature' not in Base.metadata:
high = Column(Float)
critical = Column(Float)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'system_fan' not in Base.metadata:
if not is_defined('system_fan'):
class SystemFan(NumericSensor):
"""
@ -250,12 +259,13 @@ if 'system_fan' not in Base.metadata:
primary_key=True,
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'system_battery' not in Base.metadata:
if not is_defined('system_battery'):
class SystemBattery(PercentSensor):
"""
@ -273,6 +283,7 @@ if 'system_battery' not in Base.metadata:
seconds_left = Column(Float)
power_plugged = Column(Boolean)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'temperature_sensor' not in Base.metadata:
if not is_defined('temperature_sensor'):
class TemperatureSensor(NumericSensor):
__tablename__ = 'temperature_sensor'
@ -14,6 +14,7 @@ if 'temperature_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,13 +1,13 @@
from typing import Iterable, Mapping, Optional, Union
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from platypush.common.sensors import Numeric
from .sensors import RawSensor
if 'three_axis_sensor' not in Base.metadata:
if not is_defined('three_axis_sensor'):
class ThreeAxisSensor(RawSensor):
"""
@ -20,6 +20,7 @@ if 'three_axis_sensor' not in Base.metadata:
Integer, ForeignKey(RawSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'time_duration' not in Base.metadata:
if not is_defined('time_duration'):
class TimeDuration(NumericSensor):
"""
@ -18,6 +18,7 @@ if 'time_duration' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -2,14 +2,14 @@ import logging
from sqlalchemy import Column, ForeignKey, Integer, String
from platypush.common.db import Base
from platypush.common.db import is_defined
from . import Entity
logger = logging.getLogger(__name__)
if 'variable' not in Base.metadata:
if not is_defined('variable'):
class Variable(Entity):
"""

View file

@ -1,11 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey
from platypush.common.db import Base
from platypush.common.db import is_defined
from .sensors import NumericSensor
if 'weight_sensor' not in Base.metadata:
if not is_defined('weight_sensor'):
class WeightSensor(NumericSensor):
"""
@ -18,6 +18,7 @@ if 'weight_sensor' not in Base.metadata:
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__table_args__ = {'extend_existing': True}
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}