def get(self, ident, args=None): """Returns the value for an variable stored in the stash. The variable may be specified as a simple string, e.g. 'foo', or as an array reference representing compound variables. In the latter case, each pair of successive elements in the list represent a node in the compound variable. The first is the variable name, the second a list of arguments or 0 if undefined. So, the compound variable [% foo.bar('foo').baz %] would be represented as the list [ 'foo', 0, 'bar', ['foo'], 'baz', 0 ]. Returns the value of the identifier or an empty string if undefined. """ ident = util.unscalar(ident) root = self if isinstance(ident, str) and ident.find(".") != -1: ident = [ y for x in ident.split(".") for y in (re.sub(r"\(.*$", "", x), 0) ] if isinstance(ident, (list, tuple)): for a, b in util.chop(ident, 2): result = self.__dotop(root, a, b) if result is not None: root = result else: break else: result = self.__dotop(root, ident, args) if result is None: result = self.undefined(ident, args) return util.PerlScalar(result)
def get(self, ident, args=None): """Returns the value for an variable stored in the stash. The variable may be specified as a simple string, e.g. 'foo', or as an array reference representing compound variables. In the latter case, each pair of successive elements in the list represent a node in the compound variable. The first is the variable name, the second a list of arguments or 0 if undefined. So, the compound variable [% foo.bar('foo').baz %] would be represented as the list [ 'foo', 0, 'bar', ['foo'], 'baz', 0 ]. Returns the value of the identifier or an empty string if undefined. """ ident = util.unscalar(ident) root = self if isinstance(ident, str) and ident.find(".") != -1: ident = [y for x in ident.split(".") for y in (re.sub(r"\(.*$", "", x), 0)] if isinstance(ident, (list, tuple)): for a, b in util.chop(ident, 2): result = self.__dotop(root, a, b) if result is not None: root = result else: break else: result = self.__dotop(root, ident, args) if result is None: result = self.undefined(ident, args) return util.PerlScalar(result)
def set(self, ident, value, default=False): """Updates the value for a variable in the stash. The first parameter should be the variable name or list, as per get(). The second parameter should be the intended value for the variable. The third, optional parameter is a flag which may be set to indicate 'default' mode. When set true, the variable will only be updated if it is currently undefined or has a false value. The magical 'IMPORT' variable identifier may be used to indicate that value is a dictionary whose values should be imported. Returns the value set, or an empty string if not set (e.g. default mode). In the case of IMPORT, returns the number of items imported from the hash. """ root = self ident = util.unscalar(ident) value = util.unscalar(value) # ELEMENT: { if isinstance(ident, str) and ident.find(".") >= 0: ident = [ y for x in ident.split(".") for y in (re.sub(r"\(.*$", "", x), 0) ] if isinstance(ident, (list, tuple)): chopped = list(util.chop(ident, 2)) for i in range(len(chopped) - 1): x, y = chopped[i] result = self.__dotop(root, x, y, True) if result is None: # last ELEMENT return "" else: root = result result = self.__assign(root, chopped[-1][0], chopped[-1][1], value, default) else: result = self.__assign(root, ident, 0, value, default) if result is None: return "" else: return result
def set(self, ident, value, default=False): """Updates the value for a variable in the stash. The first parameter should be the variable name or list, as per get(). The second parameter should be the intended value for the variable. The third, optional parameter is a flag which may be set to indicate 'default' mode. When set true, the variable will only be updated if it is currently undefined or has a false value. The magical 'IMPORT' variable identifier may be used to indicate that value is a dictionary whose values should be imported. Returns the value set, or an empty string if not set (e.g. default mode). In the case of IMPORT, returns the number of items imported from the hash. """ root = self ident = util.unscalar(ident) value = util.unscalar(value) # ELEMENT: { if isinstance(ident, str) and ident.find(".") >= 0: ident = [y for x in ident.split(".") for y in (re.sub(r"\(.*$", "", x), 0)] if isinstance(ident, (list, tuple)): chopped = list(util.chop(ident, 2)) for i in range(len(chopped)-1): x, y = chopped[i] result = self.__dotop(root, x, y, True) if result is None: # last ELEMENT return "" else: root = result result = self.__assign(root, chopped[-1][0], chopped[-1][1], value, default) else: result = self.__assign(root, ident, 0, value, default) if result is None: return "" else: return result
def getref(self, ident, args=None): """Returns a "reference" to a particular item. This is represented as a function which will return the actual stash item when called. WARNING: still experimental! """ root = self if util.is_seq(ident): chopped = list(util.chop(ident, 2)) for i, (item, args) in enumerate(chopped): if i == len(chopped) - 1: break root = self.__dotop(root, item, args) if root is None: break else: item = ident if root is not None: return lambda *x: self.__dotop(root, item, tuple(args or ()) + x) else: return lambda *x: ""
def list_hash(list, n=None): if n is not None: n = int(n or 0) return dict((index + n, item) for index, item in enumerate(list)) else: return dict(util.chop(list, 2))
def add_metadata(self, setlist): setlist = [util.unscalar_lex(x) for x in setlist] if self.metadata is not None: for key, value in util.chop(setlist, 2): self.metadata[key] = value return None
def default(self, setlist): # [% DEFAULT foo = bar, baz = qux %] return "\n".join( self.assign(var, val, 1) for var, val in chop(setlist, 2))
def set(self, setlist): # [% foo = bar, baz = qux %] return "\n".join( [self.assign(var, val) for var, val in chop(setlist, 2)])
def default(self, setlist): # [% DEFAULT foo = bar, baz = qux %] return "\n".join(self.assign(var, val, 1) for var, val in chop(setlist, 2))
def set(self, setlist): # [% foo = bar, baz = qux %] return "\n".join([self.assign(var, val) for var, val in chop(setlist, 2)])