Skip to content

Commit

Permalink
giza: additional testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Kleinman committed May 21, 2014
1 parent 710b5af commit 40e4ce3
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 25 deletions.
Empty file added giza/__init__.py
Empty file.
80 changes: 56 additions & 24 deletions giza/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,79 @@
logger = logging.getLogger(os.path.basename(__file__))

from utils.structures import BuildConfiguration
from utils.serialization import ingest_yaml_doc

class ConfigurationBase(object):
def __init__(self, fn=None):
def __init__(self, input_obj=None):
self._state = {}

if fn is not None and os.path.exists(fn):
conf = BuildConfiguration(fn)
for k in conf:
self._state[k] = conf[k]
if isinstance(input_obj, dict):
pass
elif os.path.exists(input_obj):
input_obj = ingest_yaml_doc(input_obj)
else:
msg = 'cannot instantiate Configuration obj with type {0}'.format(type(input_obj))
logger.critical(msg)
raise TypeError(msg)

for k, v in input_obj.items():
if '-' in k:
k = k.replace('-', '_')

if k in dir(self):
object.__setattr__(self, k, v)
elif isinstance(v, dict):
logger.warning('conf object lacks "{0}" attr (dict value)'.format(k))
v = ConfigurationBase(v)
object.__setattr__(self, k, v)
self.state[k] = v
else:
logger.warning('conf object lacks "{0}" attr'.format(k))
self.state[k] = v

@property
def state(self):
return self._state

@state.setter
def state(self, value):
logger.warning('cannot set state record directly')

def __getitem__(self, key):
return self._state[key]
def __getattr__(self, key):
return self.state[key]

def __contains__(self, key):
return key in self._state
return key in self.state

def __setattr__(self, key, value):
if key.startswith('_') or hasattr(self, key):
object.__setattr__(self, key, value)
elif isinstance(value, ConfigurationBase):
self.state[key] = value
else:
self._state[key] = value

__getattr__ = __getitem__
msg = 'configuration object lacks support for {0} value'.format(key)
logger.critical(msg)
raise TypeError(msg)

def __repr__(self):
return str(self._state)
return str(self.state)

def dict(self):
return self._state
return self.state

class ExampleConfig(ConfigurationBase):
class Configuration(ConfigurationBase):
@property
def a(self):
return self._state['a'] + 1
def project(self):
return self.state['project']

@a.setter
def a(self, key):
if isinstance(key, list):
print "refusing to add list"
else:
self._state['a'] = key
@project.setter
def project(self, value):
self.state['project'] = value

class Configuration(ConfigurationBase):
pass
@property
def git(self):
return self.state['git']

@git.setter
def git(self, value):
self.state['git'] = value
10 changes: 9 additions & 1 deletion giza/giza.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
import os.path

logger = logging.getLogger(os.path.basename(__file__))
logging.basicConfig(level=logging.INFO) # set basic default log level

import argh

from app import BuildApp
from configuration import Configuration

@argh.arg('--confp')
def test(arg):
print arg
c = Configuration(arg.confp)

print(dir(Configuration))
print(dir(c))
print('---')
print(c)


def main():
parser = argh.ArghParser()
Expand Down
4 changes: 4 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: test

test:
@nosetests2 -w test/ giza
Empty file added test/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions test/test_giza.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import giza.configuration

from unittest import TestCase

class TestGiza(TestCase):
@classmethod
def setUp(self):
self.conf = giza.configuration.Configuration()

self.result = [ 1, 1, 2, 3, 5, 8 ]

def test_baseline(self):
self.conf.baseline = [ 1, 1, 2, 3, 5, 8 ]

self.assertEqual(self.result, self.conf.baseline)

def test_subdoc_type(self):
self.conf.base.foo = 1
print(type(self.conf))
self.assertEqual(self.conf.base, True)
Empty file modified utils/rstcloth/releases.py
100755 → 100644
Empty file.

0 comments on commit 40e4ce3

Please sign in to comment.