From 8d260908a58df094e787bd9487eb6f940c94bdfd Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sun, 22 Sep 2024 17:16:33 +0200 Subject: [PATCH] meta: Python3 fixes --- cybertools/meta/README.txt | 4 +--- cybertools/meta/config.py | 31 +++++-------------------------- cybertools/meta/element.py | 31 ++++++------------------------- cybertools/meta/namespace.py | 32 ++++++++------------------------ cybertools/meta/namespace.txt | 27 +++++++++++++-------------- 5 files changed, 33 insertions(+), 92 deletions(-) diff --git a/cybertools/meta/README.txt b/cybertools/meta/README.txt index 22d4aa2..4f1ba9c 100644 --- a/cybertools/meta/README.txt +++ b/cybertools/meta/README.txt @@ -2,8 +2,6 @@ Meta Information Management =========================== - ($Id$) - Configuration Options, Settings, Preferences ============================================ @@ -51,7 +49,7 @@ Loading options as Python code >>> config.controller.telnet.port 5001 - >>> print config + >>> print(config) controller.telnet(port=5001) controller(names=('cmdline', 'telnet')) scheduler(name='core') diff --git a/cybertools/meta/config.py b/cybertools/meta/config.py index 8a4a9ed..20ebf34 100644 --- a/cybertools/meta/config.py +++ b/cybertools/meta/config.py @@ -1,39 +1,19 @@ -# -# Copyright (c) 2009 Helmut Merz helmutm@cy55.de -# -# 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 2 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, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# +# cybertools.meta.config -""" -Basic implementations for configuration options - -$Id$ +""" Basic implementations for configuration options """ import os -from zope.interface import implements +from zope.interface import implementer from cybertools.meta.element import Element from cybertools.meta.interfaces import IOptions, IConfigurator from cybertools.meta.namespace import AutoNamespace, Executor, ExecutionError +@implementer(IOptions) class Options(AutoNamespace): - implements(IOptions) - def __call__(self, key, default=None): value = self for part in key.split('.'): @@ -59,10 +39,9 @@ class GlobalOptions(Options): return super(GlobalOptions, self).__call__(key, default) +@implementer(IConfigurator) class Configurator(object): - implements(IConfigurator) - def __init__(self, context): self.context = context diff --git a/cybertools/meta/element.py b/cybertools/meta/element.py index 22ed1dd..635abd6 100644 --- a/cybertools/meta/element.py +++ b/cybertools/meta/element.py @@ -1,29 +1,10 @@ -# -# Copyright (c) 2008 Helmut Merz helmutm@cy55.de -# -# 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 2 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, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# +# cybertools.meta.element -""" -Building flexible hierarchical object structures with XML-like +""" Building flexible hierarchical object structures with XML-like elements. - -$Id$ """ -from cStringIO import StringIO +from io import StringIO from cybertools.util.jeep import Jeep @@ -59,7 +40,7 @@ class Element(dict): elif isinstance(key, Element): self.children.append(key) return key - elif isinstance(key, (int, long, basestring)): + elif isinstance(key, (int, str)): return self.children[key] else: raise KeyError(key) @@ -78,8 +59,8 @@ class Element(dict): else: self[key] = value - def __iter__(self): - return iter(self.children) + #def __iter__(self): + # return iter(self.children) def __str__(self): return self.__name__ diff --git a/cybertools/meta/namespace.py b/cybertools/meta/namespace.py index 70951eb..f7eb7d1 100644 --- a/cybertools/meta/namespace.py +++ b/cybertools/meta/namespace.py @@ -1,27 +1,8 @@ -# -# Copyright (c) 2008 Helmut Merz helmutm@cy55.de -# -# 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 2 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, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# +# cybertools.meta.namespace -""" -Special namespaces for execution of Python code, e.g. for implementing +""" Special namespaces for execution of Python code, e.g. for implementing configuration or export/import formats, or providing a restricted Python for scripts or an interactive interpreter. - -$Id$ """ import traceback @@ -34,7 +15,7 @@ _not_found = object() class BaseNamespace(dict): - builtins = '__builtins__', 'dir', 'output' + builtins = '__builtins__', 'dir', 'output', 'print' def __init__(self, *args, **kw): self.__builtins__ = {} @@ -43,7 +24,10 @@ class BaseNamespace(dict): self[key] = getattr(self, key) def output(self, value): - print value + print(value) + + def print(self, *vals): + print(*vals) def dir(self, obj=None): if obj is None: @@ -100,7 +84,7 @@ class Executor(object): def execute(self, text): error = '' try: - exec text in self.namespace + exec(text, self.namespace) except: error = traceback.format_exc() return error diff --git a/cybertools/meta/namespace.txt b/cybertools/meta/namespace.txt index 63e219a..9d1872b 100644 --- a/cybertools/meta/namespace.txt +++ b/cybertools/meta/namespace.txt @@ -2,8 +2,6 @@ Python Execution in Special Dynamic Namespaces ============================================== -$Id$ - >>> from cybertools.meta import namespace @@ -15,16 +13,16 @@ A Very Restricted Basic Namespace We >>> code = """ - ... print 'Hello' + ... print('Hello') ... # a comment - ... print dir('') - ... print '__builtins__:', __builtins__ + ... print(dir('')) + ... print('__builtins__:', __builtins__) ... output('something') ... a = 'a is a variable' ... if a: - ... print a + ... print(a) ... """ - >>> exec code in minimal + >>> exec(code, minimal) Hello ['__add__', ..., 'zfill'] __builtins__: {} @@ -40,12 +38,12 @@ of the namespace later. By setting the ``__builtins__`` mapping to an empty dictionary the minimal namespace provides a secure restricted execution environment. - >>> exec "import os" in minimal + >>> exec("import os", minimal) Traceback (most recent call last): ... ImportError: __import__ not found - >>> exec "f = open('dummy.txt', 'w')" in minimal + >>> exec("f = open('dummy.txt', 'w')", minimal) Traceback (most recent call last): ... NameError: open @@ -68,7 +66,7 @@ Elements and sub-elements (children) >>> symbols['annotation'] = ElementFactory(symbols, 'annotation') >>> symbols['child'] = ElementFactory(symbols, 'child') - >>> exec code in symbols + >>> exec(code, symbols) >>> symbols.topic.instances [, ] @@ -84,7 +82,7 @@ A Namespace Automatically Generating Elements >>> auto = namespace.AutoNamespace() - >>> exec "print something" in auto + >>> exec("print(something)", auto) something >>> auto.something @@ -112,15 +110,16 @@ value and an - hopefully empty - error string. (None, 'Traceback...ZeroDivisionError...') Trying to execute a statement leads to a syntax error. +... in Python3 print is a function ... - >>> ev.evaluate('print something_else') - (None, 'Traceback...SyntaxError...') +ev.evaluate('print(something_else)') +(None, 'Traceback...SyntaxError...') But we can explicitly allow the execution of statements. The result of executing a statement is None. >>> ev = namespace.Evaluator(auto, allowExec=True) - >>> ev.evaluate('print something_else') + >>> ev.evaluate('print(something_else)') something_else (None, '') >>> ev.evaluate('25 * 25')