Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Object validation in separate class
Moved validation logic into eeml.validator.Validator so one can turn off these checks using eeml.invalidator.Invalidator. Fixes #24
- Loading branch information
Showing
4 changed files
with
115 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
Don't validate any input | ||
""" | ||
|
||
|
||
class Invalidator(object): | ||
""" | ||
Doesn't do much | ||
""" | ||
|
||
def environment(self, env): | ||
pass | ||
|
||
def location(self, loc): | ||
pass | ||
|
||
def data(self, data): | ||
pass | ||
|
||
def datapoints(self, datapoints): | ||
pass | ||
|
||
Validator = Invalidator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Here are the validators | ||
""" | ||
|
||
from datetime import datetime | ||
|
||
from eeml.unit import Unit | ||
from eeml.util import _assertPosInt | ||
|
||
import logging | ||
|
||
class Version051(object): | ||
""" | ||
Validate constructors by version 0.5.1 specification | ||
""" | ||
|
||
def environment(self, env): | ||
status = env._status | ||
id_ = env._id | ||
private = env._private | ||
|
||
if status is not None and status not in ['frozen', 'live']: | ||
raise ValueError("status must be either 'frozen' or 'live', " | ||
"got {}".format(status)) | ||
_assertPosInt(id_, 'id', False) | ||
if private is not None and not isinstance(private, bool): | ||
raise ValueError("private is expected to be bool, got {}" | ||
.format(type(private))) | ||
|
||
def location(self, loc): | ||
exposure = loc._exposure | ||
domain = loc._domain | ||
disposition = loc._disposition | ||
# TODO validate lat and lon | ||
|
||
if exposure is not None and exposure not in ['indoor', 'outdoor']: | ||
raise ValueError("exposure must be 'indoor' or 'outdoor', got '{}'" | ||
.format(exposure)) | ||
|
||
if domain not in ['physical', 'virtual']: | ||
raise ValueError("domain is required, must be 'physical' or 'virtual', got '{}'" | ||
.format(domain)) | ||
|
||
if disposition is not None and disposition not in ['fixed', 'mobile']: | ||
raise ValueError("disposition must be 'fixed' or 'mobile', got '{}'" | ||
.format(disposition)) | ||
|
||
def data(self, data): | ||
unit = data._unit | ||
at = data._at | ||
id_ = data._id | ||
|
||
_assertPosInt(id_, 'id', True) | ||
if unit is not None and not isinstance(unit, Unit): | ||
raise ValueError("unit must be an instance of Unit, got {}" | ||
.format(type(unit))) | ||
if at is not None and not isinstance(at, datetime): | ||
raise ValueError("at must be an instance of datetime.datetime, " | ||
"got {}".format(type(at))) | ||
|
||
def datapoints(self, datapoints): | ||
id_ = datapoints._id | ||
|
||
_assertPosInt(id_, 'id', True) | ||
|
||
Validator = Version051 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters