Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for optional keys in templates #30

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ba97133
adding support for optional keys in templates
Apr 3, 2016
6f18214
should not react on empty brackets
Apr 4, 2016
adfe8cb
inital commit of key objects
nebukadhezer Apr 22, 2016
d709cd1
adding support for optional keys and key objects
nebukadhezer Apr 22, 2016
6cebe75
Merge github.com:nebukadhezer/lucidity
nebukadhezer Apr 22, 2016
42c35c4
clean up of keys
nebukadhezer May 18, 2016
aa8e650
key resolver defaults to dict so our "is in checks" wont fail
nebukadhezer May 18, 2016
6a655b5
changed default unit tests to match macke base conf
nebukadhezer May 18, 2016
14dedae
added unit test for key objects
nebukadhezer May 18, 2016
b6975d6
removed a whee bit ambigous locals from unit test.... slipped through
nebukadhezer May 18, 2016
d3a9009
Merge branch 'tests' into 'master'
instinct-vfx May 18, 2016
0734246
initial commit
nebukadhezer Oct 10, 2016
b1e7fe8
added db placeholders to key objcts
nebukadhezer Oct 13, 2016
841eadd
only sort template.keys() as this is much more efficient to iterate t…
nebukadhezer Oct 13, 2016
970a605
change unittest to expect a list from keys instead of a set
nebukadhezer Oct 13, 2016
1df5658
updates
nebukadhezer Nov 16, 2016
4af927b
Add .gitlab-ci.yml
instinct-vfx Jan 13, 2017
99397f3
Update .gitlab-ci.yml
instinct-vfx Jan 13, 2017
4f1a4a8
Update .gitlab-ci.yml
instinct-vfx Jan 13, 2017
47d4581
Update .gitlab-ci.yml
instinct-vfx Jan 13, 2017
d5a5940
Update .gitlab-ci.yml
instinct-vfx Jan 13, 2017
a4ac215
store regex on the template objects
nebukadhezer Mar 20, 2017
d64c63e
added support for functional keys
nebukadhezer Mar 20, 2017
6c2df09
Merge branch 'master' of sv-str-git002.mvision.lan:pipeline/lucidity
nebukadhezer Mar 20, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test:
script:
- set PYTHONPATH=.\source
- py.test test --html=report.html
# - coverage html -d coverage_html
1 change: 1 addition & 0 deletions source/lucidity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ._version import __version__
from .template import Template, Resolver
from .key import Key
from .error import ParseError, FormatError, NotFound


Expand Down
138 changes: 138 additions & 0 deletions source/lucidity/key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# :coding: utf-8
import re
import logging

class Key(object):
'''Key baseClass
used to store and validate values for templates
a dict needs to be provided
{'name': 'shot',
'regex': r'^([0-9]){3}$',
'type': int,
'padding': '%04d'
}
name and type are the main keys that need to be provided
without them the object cannot initialize
types are "str" and "int"

'''
def __init__(self, name, type,**kwargs):
super(Key, self).__init__()

self.__name = name
self.__type = type
self.__value = None
self.__regex = None
self.__padding = None
self.__function = None
self.__abstract = ''
self.__dbEntity = self.__name
self.__dbField = ''

for key, value in kwargs.items():
if key == 'regex':
self.__regex = re.compile(value)
if key == 'abstract':
self.__abstract = value
if key == 'function':
self.__function = value
self.__value = value() #call the function once at init to set a value
if key == 'padding':
if re.match(r'(\%)([0-9]{0,2})(d)', value):
self.__padding = value
else:
raise Exception('provided padding {0} is not a valid padding pattern must be like "%04d"'.format(value))
if key == 'dbEntity':
self.__dbEntity = value
if key == 'dbField':
self.__dbField = value

@property
def name(self):
return self.__name

@property
def type(self):
return self.__type

@property
def abstract(self):
return self.__abstract

@property
def function(self):
return self.__function

@property
def padding(self):
return self.__padding

@property
def regex(self):
return self.__regex

@property
def dbEntity(self):
return self.__dbEntity

@property
def dbField(self):
return self.__dbField

@property
def value(self):
return self.__value

def setValue(self, value):
if value:
if self.type == int and isinstance(value,int) and self.padding:
## we can skip the regex check if the incoming value is an int and we do have a padding
self.__value = self.type(value)
return

if self.regex:
if re.match(self.regex, value):
if self.abstract:
if value == self.abstract:
return
self.__value = self.type(value)
return
elif str(value) == str(self.name):
self.__value = value
return
else:
raise Exception('provided value {0} does not match regex {1} for {2}'.format(value, self.regex.pattern,self.__repr__()))
else:
self.__value = self.type(value)
return

def __repr__(self):
if self.value:
return '<lucidity.Key "{0}" value "{1}">'.format(self.name,str(self))
else:
return '<lucidity.Key "{0}">'.format(self.name)

def __str__(self):
'''
used in the format method to fill the keys
'''
if not self.value and not self.value == 0:
if self.abstract:
return str(self.abstract)
return str(self.name)
if self.type == str:
return str(self.value)
elif self.type == int and self.padding:
return self.padding % self.value
elif self.type == int:
return str(self.value)
elif self.function:
self.__value = self.function()
return str(self.value)

def __cmp__(self,other):
'''
compare against name
'''
return cmp(self.name,other)