Friday, March 27, 2020

[389-commits] [389-ds-base] branch 389-ds-base-1.4.2 updated: Issue 50337 - Replace exec() with setattr()

This is an automated email from the git hooks/post-receive script.

spichugi pushed a commit to branch 389-ds-base-1.4.2
in repository 389-ds-base.

The following commit(s) were added to refs/heads/389-ds-base-1.4.2 by this push:
new 9820e1c Issue 50337 - Replace exec() with setattr()
9820e1c is described below

commit 9820e1c37ab145f7aaff04b2a877ee41212a5158
Author: Simon Pichugin <spichugi@redhat.com>
AuthorDate: Fri Mar 27 14:00:06 2020 +0100

Issue 50337 - Replace exec() with setattr()

Description: _constants.py uses exec() a lot to define module
global variables. That's rather slow and not very elegant.

Get the current module object with sys.modules[__name__] and
then use setattr() instead.

https://pagure.io/389-ds-base/issue/50337

Reviewed by: mreynolds (Thanks!)
---
src/lib389/lib389/_constants.py | 42 ++++++++++++++++++++++-------------------
1 file changed, 23 insertions(+), 19 deletions(-)

diff --git a/src/lib389/lib389/_constants.py b/src/lib389/lib389/_constants.py
index 5c9b90a..0fcc86f 100644
--- a/src/lib389/lib389/_constants.py
+++ b/src/lib389/lib389/_constants.py
@@ -6,10 +6,14 @@
# See LICENSE for details.
# --- END COPYRIGHT BLOCK ---

+import sys
import os
from enum import Enum, IntEnum
from lib389.properties import *

+# current module object
+mod = sys.modules[__name__]
+
(
MASTER_TYPE,
HUB_TYPE,
@@ -286,11 +290,11 @@ number_of_instances = 99
# Standalone topology
for i in range(port_start, port_start + number_of_instances):
N+=1
- exec("HOST_STANDALONE{0} = {1}".format(N, "LOCALHOST"))
- exec("PORT_STANDALONE{0} = {1}".format(N, i))
- exec("SECUREPORT_STANDALONE{0} = {1}".format(N, i + 24700))
- exec("SERVERID_STANDALONE{0} = {1}".format(N, "\"standalone{0}\"".format(N)))
- exec("REPLICAID_STANDALONE_{0} = {1}".format(N, 65535))
+ setattr(mod, "HOST_STANDALONE{0}".format(N), "LOCALHOST")
+ setattr(mod, "PORT_STANDALONE{0}".format(N), i)
+ setattr(mod, "SECUREPORT_STANDALONE{0}".format(N), i + 24700)
+ setattr(mod, "SERVERID_STANDALONE{0}".format(N), "\"standalone{0}\"".format(N))
+ setattr(mod, "REPLICAID_STANDALONE_{0}".format(N), 65535)

# For compatibility
HOST_STANDALONE = HOST_STANDALONE1
@@ -303,32 +307,32 @@ N=0
port_start+=100
for i in range(port_start, port_start + number_of_instances):
N+=1
- exec("HOST_MASTER_{0} = {1}".format(N, "LOCALHOST"))
- exec("PORT_MASTER_{0} = {1}".format(N, i))
- exec("SECUREPORT_MASTER_{0} = {1}".format(N, i + 24700))
- exec("SERVERID_MASTER_{0} = {1}".format(N, "\"master{0}\"".format(N)))
- exec("REPLICAID_MASTER_{0} = {1}".format(N, N))
+ setattr(mod, "HOST_MASTER_{0}".format(N), "LOCALHOST")
+ setattr(mod, "PORT_MASTER_{0}".format(N), i)
+ setattr(mod, "SECUREPORT_MASTER_{0}".format(N), i + 24700)
+ setattr(mod, "SERVERID_MASTER_{0}".format(N), "\"master{0}\"".format(N))
+ setattr(mod, "REPLICAID_MASTER_{0}".format(N), N)

# Replication topology - hubs
N=0
port_start+=100
for i in range(port_start, port_start + number_of_instances):
N+=1
- exec("HOST_HUB_{0} = {1}".format(N, "LOCALHOST"))
- exec("PORT_HUB_{0} = {1}".format(N, i))
- exec("SECUREPORT_HUB_{0} = {1}".format(N, i + 24700))
- exec("SERVERID_HUB_{0} = {1}".format(N, "\"hub{0}\"".format(N)))
- exec("REPLICAID_HUB_{0} = {1}".format(N, 65535))
+ setattr(mod, "HOST_HUB_{0}".format(N), "LOCALHOST")
+ setattr(mod, "PORT_HUB_{0}".format(N), i)
+ setattr(mod, "SECUREPORT_HUB_{0}".format(N), i + 24700)
+ setattr(mod, "SERVERID_HUB_{0}".format(N), "\"hub{0}\"".format(N))
+ setattr(mod, "REPLICAID_HUB_{0}".format(N), 65535)

# Replication topology - consumers
N=0
port_start+=100
for i in range(port_start, port_start + number_of_instances):
N+=1
- exec("HOST_CONSUMER_{0} = {1}".format(N, "LOCALHOST"))
- exec("PORT_CONSUMER_{0} = {1}".format(N, i))
- exec("SECUREPORT_CONSUMER_{0} = {1}".format(N, i + 24700))
- exec("SERVERID_CONSUMER_{0} = {1}".format(N, "\"consumer{0}\"".format(N)))
+ setattr(mod, "HOST_CONSUMER_{0}".format(N), "LOCALHOST")
+ setattr(mod, "PORT_CONSUMER_{0}".format(N), i)
+ setattr(mod, "SECUREPORT_CONSUMER_{0}".format(N), i + 24700)
+ setattr(mod, "SERVERID_CONSUMER_{0}".format(N), "\"consumer{0}\"".format(N))

# Cleanup, we don't need to export that
del N, port_start, number_of_instances

--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
_______________________________________________
389-commits mailing list -- 389-commits@lists.fedoraproject.org
To unsubscribe send an email to 389-commits-leave@lists.fedoraproject.org
Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/389-commits@lists.fedoraproject.org

No comments:

Post a Comment