Skip to content

Commit

Permalink
yaml changes
Browse files Browse the repository at this point in the history
  • Loading branch information
habanoz committed Feb 10, 2019
1 parent 9fe377a commit 04a9eb9
Show file tree
Hide file tree
Showing 11 changed files with 528 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
requests==2.20.0
base58==1.0.2
PyYAML==3.13
Empty file added src/config/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions src/config/addr_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from enum import Enum


class AddrType(Enum):
KT = 1
TZ = 2
KTALS = 3
TZALS = 4
31 changes: 31 additions & 0 deletions src/config/config_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from abc import ABC, abstractmethod


class ConfigParser(ABC):
def __init__(self, text, verbose=None):
super(ConfigParser, self).__init__()
self.conf_text = text
self.verbose = verbose
self.conf_obj = None

@abstractmethod
def parse(self):
pass

@abstractmethod
def validate(self):
pass

@staticmethod
def load_file(conf_file_path):
with open(conf_file_path, 'r') as file:
return file.readlines()

def set_conf_obj(self, conf_obj):
self.conf_obj = conf_obj

def get_conf_obj(self):
return self.conf_obj

def get_conf_obj_attr(self, attr):
return self.conf_obj[attr]
153 changes: 153 additions & 0 deletions src/config/test_AppYamlConfParser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
from unittest import TestCase

from config.addr_type import AddrType
from config.yaml_app_conf_parser import AppYamlConfParser


class TestYamlAppConfParser(TestCase):
def test_validate(self):
data_fine = """
version : 1.0
baking_address : tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj
payment_address : tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj
founders_map : {'KT2Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5,'KT3Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5}
owners_map : {'KT2Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5,'KT3Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5}
service_fee : 4.53
"""

managers = {'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj': 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj'}
cnf_prsr = AppYamlConfParser(data_fine, dict(), managers)
cnf_prsr.parse()
cnf_prsr.validate()

self.assertEqual(cnf_prsr.get_conf_obj_attr('baking_address'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_pkh'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_manager'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_type'), AddrType.TZ)
self.assertEqual(0, cnf_prsr.get_conf_obj_attr('min_delegation_amt'))
self.assertEqual(cnf_prsr.get_conf_obj_attr('excluded_delegators_set'), set())
self.assertEqual(cnf_prsr.get_conf_obj_attr('prcnt_scale'), None)
self.assertEqual(cnf_prsr.get_conf_obj_attr('pymnt_scale'), None)

def test_validate_no_founders_map(self):
data_no_founders = """
version : 1.0
baking_address : tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj
payment_address : tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj
owners_map : {'KT2Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5,'KT3Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5}
service_fee : 4.5
"""

managers_map = {'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj': 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj'}
cnf_prsr = AppYamlConfParser(data_no_founders, dict(), managers_map)
cnf_prsr.parse()
cnf_prsr.validate()

self.assertEqual(cnf_prsr.get_conf_obj_attr('baking_address'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_pkh'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_manager'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_type'), AddrType.TZ)
self.assertEqual(cnf_prsr.get_conf_obj_attr('founders_map'), dict())
self.assertEqual(cnf_prsr.get_conf_obj_attr('specials_map'), dict())
self.assertEqual(cnf_prsr.get_conf_obj_attr('supporters_set'), set())
self.assertEqual(cnf_prsr.get_conf_obj_attr('excluded_delegators_set'), set())
self.assertEqual(cnf_prsr.get_conf_obj_attr('prcnt_scale'), None)
self.assertEqual(cnf_prsr.get_conf_obj_attr('pymnt_scale'), None)
self.assertEqual(0, cnf_prsr.get_conf_obj_attr('min_delegation_amt'))

def test_validate_pymnt_alias(self):
data_no_founders = """
version : 1.0
baking_address : tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj
payment_address : ktPay
owners_map : {'KT2Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5,'KT3Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5}
service_fee : 4.5
min_delegation_amt : 100
"""

managers_map = {'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj': 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj',
'KT1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj': 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj'}

known_contracts = {'ktPay': 'KT1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj'}

cnf_prsr = AppYamlConfParser(data_no_founders, known_contracts, managers_map)
cnf_prsr.parse()
cnf_prsr.validate()

self.assertEqual(cnf_prsr.get_conf_obj_attr('baking_address'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address'), 'ktPay')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_pkh'), 'KT1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_manager'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_type'), AddrType.KTALS)

self.assertEqual(cnf_prsr.get_conf_obj_attr('founders_map'), dict())
self.assertEqual(cnf_prsr.get_conf_obj_attr('specials_map'), dict())
self.assertEqual(cnf_prsr.get_conf_obj_attr('supporters_set'), set())
self.assertEqual(cnf_prsr.get_conf_obj_attr('excluded_delegators_set'), set())

self.assertEqual(100, cnf_prsr.get_conf_obj_attr('min_delegation_amt'))
self.assertEqual(cnf_prsr.get_conf_obj_attr('prcnt_scale'), None)
self.assertEqual(cnf_prsr.get_conf_obj_attr('pymnt_scale'), None)

def test_validate_scales(self):
data_fine = """
version : 1.0
baking_address : tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj
payment_address : tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj
founders_map : {'KT2Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5,'KT3Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5}
owners_map : {'KT2Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5,'KT3Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj':0.5}
service_fee : 4.5
pymnt_scale : 3
prcnt_scale : 5
"""

managers = {'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj': 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj',
'KT1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj': 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj'}
cnf_prsr = AppYamlConfParser(data_fine, dict(), managers)
cnf_prsr.parse()
cnf_prsr.validate()

self.assertEqual(cnf_prsr.get_conf_obj_attr('baking_address'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_pkh'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_manager'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_type'), AddrType.TZ)
self.assertEqual(0, cnf_prsr.get_conf_obj_attr('min_delegation_amt'))
self.assertEqual(cnf_prsr.get_conf_obj_attr('excluded_delegators_set'), set())
self.assertEqual(cnf_prsr.get_conf_obj_attr('prcnt_scale'), 5)
self.assertEqual(cnf_prsr.get_conf_obj_attr('pymnt_scale'), 3)

def test_validate_empty(self):
data_fine = """
version : 1.0
baking_address : tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj
payment_address : KT1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj
service_fee : 4.5
founders_map : {}
owners_map : {}
prcnt_scale : None
pymnt_scale : None
specials_map : {}
supporters_set : {}
min_delegation_amt : 0
excluded_delegators_set : {}
"""

managers = {'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj': 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj',
'KT1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj': 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj'}
cnf_prsr = AppYamlConfParser(data_fine, dict(), managers)
cnf_prsr.parse()
cnf_prsr.validate()

self.assertEqual(cnf_prsr.get_conf_obj_attr('baking_address'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address'), 'KT1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_pkh'), 'KT1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_manager'), 'tz1Z1tMai15JWUWeN2PKL9faXXVPMuWamzJj')
self.assertEqual(cnf_prsr.get_conf_obj_attr('payment_address_type'), AddrType.KT)
self.assertEqual(0, cnf_prsr.get_conf_obj_attr('min_delegation_amt'))
self.assertEqual(cnf_prsr.get_conf_obj_attr('excluded_delegators_set'), set())
self.assertEqual(cnf_prsr.get_conf_obj_attr('supporters_set'), set())
self.assertEqual(cnf_prsr.get_conf_obj_attr('prcnt_scale'), None)
self.assertEqual(cnf_prsr.get_conf_obj_attr('pymnt_scale'), None)
26 changes: 26 additions & 0 deletions src/config/test_yamlConfParser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from unittest import TestCase

from config.yaml_conf_parser import YamlConfParser

document = """
none: [~, null]
bool: [true, false, on, off]
int: 42
float: 3.14159
list: [LITE, RES_ACID, SUS_DEXT]
dict: {hp: 13, sp: 5}
"""


class TestYamlConfParser(TestCase):
def test_parse(self):
appConfParser = YamlConfParser(document)
conf_obj = appConfParser.parse()

self.assertEqual(conf_obj['none'], [None, None])
self.assertEqual(conf_obj['bool'], [True, False, True, False])
self.assertEqual(conf_obj['list'], ['LITE', 'RES_ACID', 'SUS_DEXT'])
self.assertEqual(conf_obj['dict']['hp'], 13)
self.assertEqual(conf_obj['int'], 42)

appConfParser.validate()
Loading

0 comments on commit 04a9eb9

Please sign in to comment.