Skip to content
This repository has been archived by the owner on Aug 14, 2021. It is now read-only.

Commit

Permalink
Support to generate a range of variables (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
posidron committed Dec 15, 2018
1 parent f2ef80d commit 4253e07
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
32 changes: 21 additions & 11 deletions dharma/core/dharma.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from collections import OrderedDict

if sys.version_info[0] == 2:
from extensions import * # pylint: disable=E0401,W0401
from extensions import * # pylint: disable=E0401,W0401
else:
from dharma.core.extensions import * # pylint: disable=W0401,W0614
from dharma.core.extensions import * # pylint: disable=W0401,W0614


class GenState:
Expand Down Expand Up @@ -96,7 +96,7 @@ def __init__(self, ident, machine):
self.namespace = machine.namespace
self.lineno = machine.lineno

def id(self): # pylint: disable=invalid-name
def id(self): # pylint: disable=invalid-name
return "Line %d [%s]" % (self.lineno, self.namespace)

def __hash__(self):
Expand Down Expand Up @@ -136,7 +136,7 @@ def append(self, value):
return
self.leaf.append(value)

def generate(self, state): # pylint: disable=too-many-branches
def generate(self, state): # pylint: disable=too-many-branches
if not state.leaf_mode:
state.leaf_trigger += 1
if state.leaf_trigger > DharmaConst.LEAF_TRIGGER:
Expand Down Expand Up @@ -186,12 +186,14 @@ def clear(self):

def generate(self, state):
"""Return a random variable if any, otherwise create a new default variable."""
if self.count:
if self.count >= random.randint(DharmaConst.VARIABLE_MIN, DharmaConst.VARIABLE_MAX):
return "%s%d" % (self.var, random.randint(1, self.count))
self.count += 1
var = random.choice(self)
prefix = self.eval(var[0], state)
suffix = self.eval(var[1], state)
self.count += 1
element_name = "%s%d" % (self.var, self.count)
self.default = "%s%s%s" % (self.eval(var[0], state), element_name, self.eval(var[1], state))
self.default += "%s%s%s\n" % (prefix, element_name, suffix)
return element_name


Expand All @@ -202,7 +204,7 @@ def generate(self, state):
return self.eval(random.choice(self), state)


class DharmaMachine: # pylint: disable=too-many-instance-attributes
class DharmaMachine: # pylint: disable=too-many-instance-attributes
def __init__(self, prefix="", suffix="", template=""):
self.section = None
self.level = "top"
Expand Down Expand Up @@ -237,13 +239,13 @@ def __init__(self, prefix="", suffix="", template=""):
def process_settings(self, settings):
"""A lazy way of feeding Dharma with configuration settings."""
logging.debug("Using configuration from: %s", settings.name)
exec(compile(settings.read(), settings.name, 'exec'), globals(), locals()) # pylint: disable=exec-used
exec(compile(settings.read(), settings.name, 'exec'), globals(), locals()) # pylint: disable=exec-used

def set_namespace(self, name):
self.namespace = name
self.lineno = 0

def id(self): # pylint: disable=invalid-name
def id(self): # pylint: disable=invalid-name
return "Line %d [%s]" % (self.lineno, self.namespace)

def parse_line(self, line):
Expand Down Expand Up @@ -372,6 +374,14 @@ def parse_assign_value(self, tokens):
self.current_obj.append(tokens)

def parse_assign_variable(self, tokens):
"""
Example:
tokens
dharma.String: 'let ',
dharma.ElementXRef: 'GrammarNS:<VarName>',
dharma.String: '= new ',
dharma.ValueXRef: 'GrammarNS:<ValueName>'
"""
for i, token in enumerate(tokens):
if isinstance(token, ElementXRef):
variable = token.value
Expand All @@ -385,7 +395,7 @@ def parse_assign_variable(self, tokens):
if not isinstance(self.current_obj, DharmaVariable):
logging.error("%s: Inconsistent object for variable assignment", self.id())
sys.exit(-1)
prefix, suffix = tokens[:i], tokens[i + 1:] # pylint: disable=undefined-loop-variable
prefix, suffix = tokens[:i], tokens[i + 1:] # pylint: disable=undefined-loop-variable
self.current_obj.append((prefix, suffix))

def parse_assign_variance(self, tokens):
Expand Down
2 changes: 2 additions & 0 deletions dharma/core/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class DharmaConst:
VARIANCE_TEMPLATE = "%s"
VARIANCE_MIN = 1
VARIANCE_MAX = 8
VARIABLE_MIN = 1
VARIABLE_MAX = 4


class MetaBlock:
Expand Down
4 changes: 3 additions & 1 deletion dharma/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# pylint: disable = undefined-variable
DharmaConst.VARIANCE_MIN = 1
DharmaConst.VARIANCE_MAX = 1
DharmaConst.VARIANCE_MAX = 8
DharmaConst.VARIABLE_MIN = 1
DharmaConst.VARIABLE_MAX = 4
DharmaConst.VARIANCE_TEMPLATE = "%s"
DharmaConst.MAX_REPEAT_POWER = 12
DharmaConst.LEAF_TRIGGER = 256
Expand Down

0 comments on commit 4253e07

Please sign in to comment.