summary refs log tree commit diff stats
path: root/tests/test_nimscript.nims
Commit message (Expand)AuthorAgeFilesLines
* fixes #19863; move sha1, md5 to nimble packages for 2.0 (#21702)ringabout2023-05-021-1/+1
* build the documentation of official packages (#20986)ringabout2022-12-061-1/+1
* move widestrs out of system (#20462)metagn2022-10-011-1/+3
* correct typos (#19485)flywind2022-02-031-1/+1
* move io out of system (#19442)flywind2022-02-021-0/+3
* Use openarray of bytes in md5 (#19307)hlaaftana2022-01-151-2/+1
* Update JS and nimscript import tests (#19306)hlaaftana2022-01-011-21/+29
* envPairs works in vm, nims (#18615)Timothee Cour2021-09-291-0/+4
* Fix initrand to avoid random number sequences overlapping (#18744)Tomohiro2021-09-021-0/+2
* Make parseopt available on all backends (#17009)hlaaftana2021-02-131-2/+6
* stdlib/os: handle symlinks in copy/move functions (#16709)Roman Inflianskas2021-02-041-0/+27
* doAssertRaises improvements; nimscript supports `except Exception as e` (#15765)Timothee Cour2020-11-121-0/+6
* $(uint|uint64) now works with nimscript (#15644)Timothee Cour2020-10-201-0/+3
* deprecate existsDir; use dirExists instead (#14884)Timothee Cour2020-07-031-1/+0
* {.deprecated: [existsFile: fileExists].} (#14735)Timothee Cour2020-07-021-1/+0
* cleanup tests/test_nimscript.nims (#14686)Timothee Cour2020-06-161-9/+1
* fix #14179, fix #14142, make CI 1.4x faster (2x faster locally) (#14658)Timothee Cour2020-06-161-0/+7
* don't close #14142Araq2020-05-021-1/+1
* closes #14142Araq2020-05-021-2/+9
* StringStream & more stdlib modules support for JS/NimScript (#14095)hlaaftana2020-04-281-20/+68
* relativePath("foo", "foo") is now ".", not "" (#13452)Timothee Cour2020-02-221-0/+3
* remove `subexes`narimiran2019-01-101-1/+0
* deprecated ospaths (#9665)Andreas Rumpf2018-11-091-1/+1
* bugfix: make 'macros' module available for nimscriptAndreas Rumpf2017-11-141-0/+1
* Added deques module, deprecating queuesRuslan Mustakov2016-11-241-1/+1
* added file back with proper access rights (thanks for that, only took me an h...Araq2016-06-071-0/+25
* remove file with broken access bitsAraq2016-06-071-25/+0
* update the version number in more placesAraq2016-06-071-0/+0
* Disable pegsFederico Ceratto2016-04-111-1/+1
* Add nimscript test and run it in Travis CIFederico Ceratto2016-04-031-0/+25
-01-22 00:11:46 +0100 committer hut <hut@lavabit.com> 2010-01-22 00:11:46 +0100 help: added documentation for running fi
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

class Tree(object):
	def __init__(self, dictionary=None, parent=None, key=None):
		if dictionary is None:
			self._tree = dict()
		else:
			self._tree = dictionary
		self.key = key
		self.parent = parent

	def copy(self):
		"""Create a deep copy"""
		def deep_copy_dict(dct):
			dct = dct.copy()
			for key, val in dct.items():
				if isinstance(val, dict):
					dct[key] = deep_copy_dict(val)
			return dct
		newtree = Tree()
		if isinstance(self._tree, dict):
			newtree._tree = deep_copy_dict(self._tree)
		else:
			newtree._tree = self._tree
		return newtree

	def merge(self, other, copy=False):
		"""Merge another Tree into a copy of self"""
		def deep_merge(branch, otherbranch):
			assert isinstance(otherbranch, dict)
			if not isinstance(branch, dict):
				branch = dict()
			elif copy:
				branch = branch.copy()
			for key, val in otherbranch.items():
				if isinstance(val, dict):
					if key not in branch:
						branch[key] = None
					branch[key] = deep_merge(branch[key], val)
				else:
					branch[key] = val
			return branch

		if isinstance(self._tree, dict) and isinstance(other._tree, dict):
			content = deep_merge(self._tree, other._tree)
		elif copy and hasattr(other._tree, 'copy'):
			content = other._tree.copy()
		else:
			content = other._tree
		return type(self)(content)

	def set(self, keys, value, force=True):
		"""Sets the element at the end of the path to <value>."""
		if not isinstance(keys, (list, tuple)):
			keys = tuple(keys)
		if len(keys) == 0:
			self.replace(value)
		else:
			fnc = force and self.plow or self.traverse
			subtree = fnc(keys)
			subtree.replace(value)

	def unset(self, iterable):
		chars = list(iterable)
		first = True

		while chars:
			if first or isinstance(subtree, Tree) and subtree.empty():
				top = chars.pop()
				subtree = self.traverse(chars)
				assert top in subtree._tree, "no such key: " + chr(top)
				del subtree._tree[top]
			else:
				break
			first = False

	def empty(self):
		return len(self._tree) == 0

	def replace(self, value):
		if self.parent:
			self.parent[self.key] = value
		self._tree = value

	def plow(self, iterable):
		"""Move along a path, creating nonexistant subtrees"""
		tree = self._tree
		last_tree = None
		char = None
		for char in iterable:
			try:
				newtree = tree[char]
				if not isinstance(newtree, dict):
					raise KeyError()
			except KeyError:
				newtree = dict()
				tree[char] = newtree
			last_tree = tree
			tree = newtree
		if isinstance(tree, dict):
			return type(self)(tree, parent=last_tree, key=char)
		else:
			return tree

	def traverse(self, iterable):
		"""Move along a path, raising exceptions when failed"""
		tree = self._tree
		last_tree = tree
		char = None
		for char in iterable:
			last_tree = tree
			try:
				tree = tree[char]
			except TypeError:
				raise KeyError("trying to enter leaf")
			except KeyError:
				raise KeyError(repr(char) + " not in tree " + str(tree))
		if isinstance(tree, dict):
			return type(self)(tree, parent=last_tree, key=char)
		else:
			return tree

	__getitem__ = traverse