From 5fc6e77920844de4123b429d0dc5c30ff0f924ca Mon Sep 17 00:00:00 2001 From: xin liang Date: Tue, 9 Jul 2024 19:56:33 +0800 Subject: [PATCH] Dev: Remove crmsh/ordereddict.py It's sufficient to use built-in dict under python 3.7+ as it maintains insertion order. --- crmsh/cibconfig.py | 9 ++- crmsh/constants.py | 12 ++-- crmsh/crm_gv.py | 11 ++-- crmsh/help.py | 11 ++-- crmsh/ordereddict.py | 133 ------------------------------------------- crmsh/ui_script.py | 5 +- 6 files changed, 20 insertions(+), 161 deletions(-) delete mode 100644 crmsh/ordereddict.py diff --git a/crmsh/cibconfig.py b/crmsh/cibconfig.py index ac822aa262..efc8233878 100644 --- a/crmsh/cibconfig.py +++ b/crmsh/cibconfig.py @@ -19,7 +19,6 @@ from . import utils from . import cibverify from . import parse -from . import ordereddict from . import orderedset from . import cibstatus from . import crm_gv @@ -1352,7 +1351,7 @@ class Op(object): def __init__(self, op_name, prim, node=None): self.prim = prim self.node = node - self.attr_d = ordereddict.odict() + self.attr_d = {} self.attr_d["name"] = op_name if self.node is not None: self.xml2dict() @@ -2080,7 +2079,7 @@ def can_be_renamed(self): def _repr_cli_head(self, format_mode): s = clidisplay.keyword(self.obj_type) - d = ordereddict.odict() + d = {} for c in self.node.iterchildren("fencing-level"): if "target-pattern" in c.attrib: target = (None, c.get("target-pattern")) @@ -2091,7 +2090,7 @@ def _repr_cli_head(self, format_mode): if target not in d: d[target] = {} d[target][c.get("index")] = c.get("devices") - dd = ordereddict.odict() + dd = {} for target in list(d.keys()): sorted_keys = sorted([int(i) for i in list(d[target].keys())]) dd[target] = [d[target][str(x)] for x in sorted_keys] @@ -2284,7 +2283,7 @@ def get_default_timeout(): # generate a translation cli -> tag -backtrans = ordereddict.odict((item[0], key) for key, item in cib_object_map.items()) +backtrans = dict((item[0], key) for key, item in cib_object_map.items()) def default_id_for_tag(tag): diff --git a/crmsh/constants.py b/crmsh/constants.py index a8ed3f4e13..c49a69b2e3 100644 --- a/crmsh/constants.py +++ b/crmsh/constants.py @@ -1,9 +1,5 @@ # Copyright (C) 2008-2011 Dejan Muhamedagic # See COPYING for license information. - -from .ordereddict import odict - - cib_cli_map = { "node": "node", "primitive": "primitive", @@ -64,15 +60,15 @@ "deny": "deny", } acl_rule_names = ("read", "write", "deny") -acl_spec_map = odict({ +acl_spec_map = { "xpath": "xpath", "ref": "ref", "tag": "tag", "attribute": "attribute", -}) +} # ACLs were rewritten in pacemaker 1.1.12 # this is the new acl syntax -acl_spec_map_2 = odict({ +acl_spec_map_2 = { "xpath": "xpath", "ref": "reference", "reference": "reference", @@ -80,7 +76,7 @@ "type": "object-type", "attr": "attribute", "attribute": "attribute" -}) +} acl_spec_map_2_rev = (('xpath', 'xpath'), ('reference', 'ref'), diff --git a/crmsh/crm_gv.py b/crmsh/crm_gv.py index c1ed3b1fe1..248d687e55 100644 --- a/crmsh/crm_gv.py +++ b/crmsh/crm_gv.py @@ -5,7 +5,6 @@ from . import config from . import tmpfiles from . import utils -from .ordereddict import odict from . import log @@ -38,9 +37,9 @@ def __init__(self, ident=None): self.nodes = {} self.edges = [] self.subgraphs = [] - self.node_attrs = odict() - self.attrs = odict() - self.graph_attrs = odict() + self.node_attrs = {} + self.attrs = {} + self.graph_attrs = {} self.edge_attrs = [] self.top_nodes = [] self.norank_nodes = [] @@ -54,7 +53,7 @@ def new_graph_attr(self, attr, v): def new_attr(self, n, attr_n, attr_v): ident = self.gv_id(n) if ident not in self.attrs: - self.attrs[ident] = odict() + self.attrs[ident] = {} self.attrs[ident][attr_n] = attr_v def new_node(self, n, top_node=False, norank=False): @@ -81,7 +80,7 @@ def new_edge(self, e): continue self.nodes[node] = i self.edges.append(ne) - self.edge_attrs.append(odict()) + self.edge_attrs.append({}) return len(self.edges)-1 def new_edge_attr(self, e_id, attr_n, attr_v): diff --git a/crmsh/help.py b/crmsh/help.py index 5ac08fd7d3..a9852dc2c4 100644 --- a/crmsh/help.py +++ b/crmsh/help.py @@ -31,7 +31,6 @@ from .utils import page_string from . import config from . import clidisplay -from .ordereddict import odict from . import log @@ -139,9 +138,9 @@ def __repr__(self): # _LOADED is set to True when an attempt # has been made (so it won't be tried again) _LOADED = False -_TOPICS = odict() -_LEVELS = odict() -_COMMANDS = odict() +_TOPICS = {} +_LEVELS = {} +_COMMANDS = {} _TOPICS["Overview"] = HelpEntry("Available help topics and commands", generated=True) _TOPICS["Topics"] = HelpEntry("Available help topics", generated=True) @@ -300,7 +299,7 @@ def add_help(entry, topic=None, level=None, command=None): if level not in _LEVELS: _LEVELS[level] = HelpEntry("No description available", generated=True) if level not in _COMMANDS: - _COMMANDS[level] = odict() + _COMMANDS[level] = {} lvl = _COMMANDS[level] if command not in lvl or lvl[command] is _DEFAULT: lvl[command] = entry @@ -360,7 +359,7 @@ def process(entry): elif entry['type'] == 'command': lvl = entry['level'] if lvl not in _COMMANDS: - _COMMANDS[lvl] = odict() + _COMMANDS[lvl] = {} helpobj.set_long_lazy_load_source(entry['level'], entry['name'], entry['from_cli']) _COMMANDS[lvl][name] = helpobj diff --git a/crmsh/ordereddict.py b/crmsh/ordereddict.py deleted file mode 100644 index 90ef8a1a78..0000000000 --- a/crmsh/ordereddict.py +++ /dev/null @@ -1,133 +0,0 @@ -# Copyright (c) 2009 Raymond Hettinger -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation files -# (the "Software"), to deal in the Software without restriction, -# including without limitation the rights to use, copy, modify, merge, -# publish, distribute, sublicense, and/or sell copies of the Software, -# and to permit persons to whom the Software is furnished to do so, -# subject to the following conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. - -try: - from collections import OrderedDict -except ImportError: - from UserDict import DictMixin - - class OrderedDict(dict, DictMixin): - - def __init__(self, *args, **kwds): - if len(args) > 1: - raise TypeError('expected at most 1 arguments, got %d' % len(args)) - try: - self.__end - except AttributeError: - self.clear() - self.update(*args, **kwds) - - def clear(self): - self.__end = end = [] - end += [None, end, end] # sentinel node for doubly linked list - self.__map = {} # key --> [key, prev, next] - dict.clear(self) - - def __setitem__(self, key, value): - if key not in self: - end = self.__end - curr = end[1] - curr[2] = end[1] = self.__map[key] = [key, curr, end] - dict.__setitem__(self, key, value) - - def __delitem__(self, key): - dict.__delitem__(self, key) - key, prev, next_ = self.__map.pop(key) - prev[2] = next_ - next_[1] = prev - - def __iter__(self): - end = self.__end - curr = end[2] - while curr is not end: - yield curr[0] - curr = curr[2] - - def __reversed__(self): - end = self.__end - curr = end[1] - while curr is not end: - yield curr[0] - curr = curr[1] - - def popitem(self, last=True): - if not self: - raise KeyError('dictionary is empty') - if last: - key = next(reversed(self)) - else: - key = next(iter(self)) - value = self.pop(key) - return key, value - - def __reduce__(self): - items = [[k, self[k]] for k in self] - tmp = self.__map, self.__end - del self.__map, self.__end - inst_dict = vars(self).copy() - self.__map, self.__end = tmp - if inst_dict: - return (self.__class__, (items,), inst_dict) - return self.__class__, (items,) - - def keys(self): - return list(self) - - setdefault = DictMixin.setdefault - update = DictMixin.update - pop = DictMixin.pop - values = DictMixin.values - items = DictMixin.items - iterkeys = DictMixin.iterkeys - itervalues = DictMixin.itervalues - iteritems = DictMixin.iteritems - - def __repr__(self): - if not self: - return '%s()' % (self.__class__.__name__,) - return '%s(%r)' % (self.__class__.__name__, list(self.items())) - - def copy(self): - return self.__class__(self) - - @classmethod - def fromkeys(cls, iterable, value=None): - d = cls() - for key in iterable: - d[key] = value - return d - - def __eq__(self, other): - if isinstance(other, OrderedDict): - if len(self) != len(other): - return False - for p, q in zip(list(self.items()), list(other.items())): - if p != q: - return False - return True - return dict.__eq__(self, other) - - def __ne__(self, other): - return not self == other - - -odict = OrderedDict diff --git a/crmsh/ui_script.py b/crmsh/ui_script.py index abf6f0bfe2..d0b8178590 100644 --- a/crmsh/ui_script.py +++ b/crmsh/ui_script.py @@ -348,7 +348,6 @@ def do_convert(self, context, workflow, outdir=".", category="basic"): """ import yaml import os - from .ordereddict import OrderedDict def flatten(script): if not isinstance(script, dict): @@ -373,7 +372,7 @@ def scriptsorter(item): order = ["version", "name", "category", "shortdesc", "longdesc", "include", "parameters", "steps", "actions"] return order.index(item[0]) - yaml.add_representer(OrderedDict, order_rep) + yaml.add_representer(dict, order_rep) fromscript = os.path.abspath(workflow) tgtdir = outdir @@ -386,7 +385,7 @@ def scriptsorter(item): del script["dir"] script["actions"] = [{"cib": "\n\n".join([action["cib"] for action in script["actions"]])}] - script = OrderedDict(sorted(list(script.items()), key=scriptsorter)) + script = dict(sorted(list(script.items()), key=scriptsorter)) if script is not None: try: os.mkdir(os.path.join(tgtdir, name))