Skip to content

Commit

Permalink
Merge pull request #7 from undertherain/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
undertherain committed Sep 12, 2018
2 parents e0ea571 + a2a7901 commit 995308d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion contextfree/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version of contextfree package."""

VERSION = "0.2.1"
VERSION = "0.2.2"
9 changes: 7 additions & 2 deletions contextfree/contextfree.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ def write_to_png(*args, **kwargs):
return image_surface.write_to_png(*args, **kwargs)


def register_rule(name, proba):
def register_rule(proba):
def real_decorator(function):
name = function.__name__

def wrapper(*args, **kwargs):
raise RuntimeError("This function had been registered as a rule and can not be called directly")
if args:
raise NotImplementedError("Parameters to rules not implemented yet")
call_rule(name)

logger.info("registering rule " + name)
if name not in _rules:
_rules[name] = []
Expand Down
35 changes: 19 additions & 16 deletions examples/Ancient Map/map.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
"""
Ancient Map demo
based on CFDG demo by the same name
https://www.contextfreeart.org/gallery2/#design/185
"""
import logging
import math
from contextfree.contextfree import *


# logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)


@register_rule("wall", 1)
def wall_1():
# pylint: disable=E0102
@register_rule(1)
def wall():
with translate(0.95, 0):
with rotate(0.01):
with scale(0.975):
ancient_map()


@register_rule("wall", 1)
def wall_2():
# pylint: disable=E0102
@register_rule(1)
def wall():
box()
with translate(0.95, 0):
with rotate(-0.0027):
Expand All @@ -24,8 +31,9 @@ def wall_2():
ancient_map()


@register_rule("wall", 0.09)
def wall_4():
# pylint: disable=E0102
@register_rule(0.09)
def wall():
box()
with scale(0.975):
with translate(0.95, 0):
Expand All @@ -35,8 +43,9 @@ def wall_4():
ancient_map()


@register_rule("wall", 0.05)
def wall_5():
# pylint: disable=E0102
@register_rule(0.05)
def wall():
with translate(0.97, 0):
with scale(1.5):
with rotate(math.pi / 2):
Expand All @@ -47,13 +56,7 @@ def wall_5():

@check_limits
def ancient_map():
call_rule("wall")
#box()
#with translate(1, 0):
# with scale(0.97):
# with rotate(0.01):
# with color(hue=0.001, saturation=0.1, lightness=0.003):
# ancient_map()
wall()


def main():
Expand Down
32 changes: 25 additions & 7 deletions tests/test_rule.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
"""Unittests for rule feature"""

import logging
import unittest
from contextfree.contextfree import init, register_rule, call_rule


@register_rule("wall", 1)
def rule():
print("I am a rule")
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)


class Tests(unittest.TestCase):
# pylint: disable=E0102
@register_rule(1)
def wall():
"""example of one instance of a rule"""
print("I am a rule 1")


def test_rnd(self):
# pylint: disable=E0102
@register_rule(1)
def wall():
"""example of another instance of a rule"""
print("I am a rule 2")


# pylint: disable=E1121
class Tests(unittest.TestCase):
"""The actual unittest class"""
def test_rule(self):
"""envoking the rule"""
init(face_color="#123456", background_color="#aaaaaa")
call_rule("wall")
with self.assertRaises(RuntimeError):
rule()
wall()
with self.assertRaises(NotImplementedError):
wall("wrong")

0 comments on commit 995308d

Please sign in to comment.