Source code for tagit.token
"""
Part of the tagit module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2016
"""
# IMPORTS
import json
# INNER-MODULE IMPORTS
from model.attributes import IMAGE_ATTRIBUTES
# EXPORTS
__all__ = ('Token_Tag', 'Token_Include', 'Token_Exclude', 'Token_Attribute', 'Token_Width', 'Token_Height', 'token_factory')
## CODE ##
[docs]class Token(object):
def type(self):
raise NotImplementedError()
def display(self):
raise NotImplementedError()
def edit(self):
raise NotImplementedError()
def update(self, string):
raise NotImplementedError()
def get(self):
raise NotImplementedError()
@staticmethod
def from_string(string):
raise NotImplementedError()
@staticmethod
def match(obj):
raise NotImplementedError()
def __eq__(self, other):
return self.type() == other.type() and self.get() == other.get()
[docs]class Token_Tag(Token):
def __init__(self, tag):
self.tag = tag
def type(self):
return 'tag'
def display(self):
return self.tag
def edit(self):
return self.tag
def update(self, string):
self.tag = string
def get(self):
return self.tag
@staticmethod
def from_string(string):
return Token_Tag(string)
@staticmethod
def match(obj):
return obj.type() == 'tag'
[docs]class Token_Image(Token):
def __init__(self, images):
self.images = images
def edit(self):
return json.dumps(self.images)
def update(self, string):
self.images = json.loads(string)
def get(self):
return self.images
[docs]class Token_Include(Token_Image):
def type(self):
return 'include'
def display(self):
return 'R'
@staticmethod
def from_string(string):
return Token_Include(json.loads(string))
@staticmethod
def match(obj): return obj.type() == 'include'
[docs]class Token_Exclude(Token_Image):
def type(self):
return 'exclude'
def display(self):
return 'E'
@staticmethod
def from_string(string):
return Token_Exclude(json.loads(string))
@staticmethod
def match(obj): return obj.type() == 'exclude'
[docs]class Token_Attribute(Token):
def __init__(self, attribute, range_):
if attribute not in IMAGE_ATTRIBUTES: raise Exception('Invalid attribute')
self.attribute = attribute
self.range_ = range_
def type(self):
return self.attribute
def display(self):
return self.attribute
def edit(self):
return json.dumps(self.range_)
def update(self, string):
self.range_= json.loads(string)
def get(self):
return (self.attribute, self.range_)
@staticmethod
def match(obj): return isinstance(obj, Token_Attribute)
[docs]class Token_Height(Token_Attribute):
def __init__(self, range_): super(Token_Height, self).__init__('height', range_)
@staticmethod
def from_string(string): return Token_Height(json.loads(string))
@staticmethod
def match(obj): return obj.type() == 'height'
[docs]class Token_Width(Token_Attribute):
def __init__(self, range_): super(Token_Width, self).__init__('width', range_)
@staticmethod
def from_string(string): return Token_Width(json.loads(string))
@staticmethod
def match(obj): return obj.type() == 'width'
[docs]def token_factory(type_, *args, **kwargs):
if type_ == 'width':
return Token_Width.from_string(*args, **kwargs)
elif type_ == 'height':
return Token_Height.from_string(*args, **kwargs)
elif type_ == 'tag':
return Token_Tag.from_string(*args, **kwargs)
elif type_ == 'include':
return Token_Include.from_string(*args, **kwargs)
elif type_ == 'exclude':
return Token_Exclude.from_string(*args, **kwargs)
raise Exception('token type unknown')
## EOF ##