Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
versions += "\n\n" + ngettext("Slave channel:", "Slave channels:", len(conf['slave_channels']))
for i in conf['slave_channels']:
instance_id = i.split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or _("Default instance")
slave_channel: EFBChannel = utils.locate_module(i, 'slave')
versions += "\n " + _("{name} ({id}) {version} # {instance_id}") \
.format(name=slave_channel.channel_name,
id=slave_channel.channel_id,
version=slave_channel.__version__,
instance_id=instance_id)
versions += "\n\n" + ngettext("Middleware:", "Middlewares:", len(conf['middlewares']))
if conf['middlewares']:
for i in conf['middlewares']:
instance_id = i.split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or _("Default instance")
middleware: EFBMiddleware = utils.locate_module(i, 'middleware')
versions += "\n " + _("{name} ({id}) {version} # {instance_id}") \
.format(name=middleware.middleware_name,
id=middleware.middleware_id,
version=middleware.__version__,
instance_id=instance_id)
else:
versions += "\n " + _("No middleware is enabled.")
finally:
print(versions)
else:
if args.profile:
coordinator.profile = str(args.profile)
conf = config.load_config()
setup_logging(args, conf)
conf = config.load_config()
# Master channel
master_channel: EFBChannel = utils.locate_module(conf['master_channel'], 'master')
instance_id = conf['master_channel'].split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or _("Default instance")
versions += "\n\n" + _("Master channel:") + "\n " + _("{name} ({id}) {version} # {instance_id}") \
.format(name=master_channel.channel_name,
id=master_channel.channel_id,
version=master_channel.__version__,
instance_id=instance_id)
versions += "\n\n" + ngettext("Slave channel:", "Slave channels:", len(conf['slave_channels']))
for i in conf['slave_channels']:
instance_id = i.split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or _("Default instance")
slave_channel: EFBChannel = utils.locate_module(i, 'slave')
versions += "\n " + _("{name} ({id}) {version} # {instance_id}") \
.format(name=slave_channel.channel_name,
id=slave_channel.channel_id,
version=slave_channel.__version__,
instance_id=instance_id)
versions += "\n\n" + ngettext("Middleware:", "Middlewares:", len(conf['middlewares']))
if conf['middlewares']:
for i in conf['middlewares']:
instance_id = i.split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or _("Default instance")
middleware: EFBMiddleware = utils.locate_module(i, 'middleware')
versions += "\n " + _("{name} ({id}) {version} # {instance_id}") \
.format(name=middleware.middleware_name,
id=middleware.middleware_id,
version=middleware.__version__,
instance_id=instance_id)
args = parser.parse_args()
if getattr(args, "version", None):
versions = _("EH Forwarder Bot\n"
"Version: {version}\n"
"Python version:\n"
"{py_version}\n"
"Running on profile \"{profile}\"."
).format(version=__version__, py_version=sys.version, profile=args.profile)
try:
if args.profile:
coordinator.profile = str(args.profile)
conf = config.load_config()
# Master channel
master_channel: EFBChannel = utils.locate_module(conf['master_channel'], 'master')
instance_id = conf['master_channel'].split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or _("Default instance")
versions += "\n\n" + _("Master channel:") + "\n " + _("{name} ({id}) {version} # {instance_id}") \
.format(name=master_channel.channel_name,
id=master_channel.channel_id,
version=master_channel.__version__,
instance_id=instance_id)
versions += "\n\n" + ngettext("Slave channel:", "Slave channels:", len(conf['slave_channels']))
for i in conf['slave_channels']:
instance_id = i.split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or _("Default instance")
slave_channel: EFBChannel = utils.locate_module(i, 'slave')
versions += "\n " + _("{name} ({id}) {version} # {instance_id}") \
.format(name=slave_channel.channel_name,
id=slave_channel.channel_id,
version=slave_channel.__version__,
"""
Initialize all channels.
"""
logger = logging.getLogger(__name__)
# Initialize mimetypes library
mimetypes.init([pkg_resources.resource_filename('ehforwarderbot', 'mimetypes')])
# Initialize all channels
# (Load libraries and modules and init them)
for i in conf['slave_channels']:
logger.log(99, "\x1b[0;37;46m %s \x1b[0m", _("Initializing slave {}...").format(i))
cls = utils.locate_module(i, 'slave')
telemetry_set_metadata({i: cls.__version__})
instance_id = i.split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or None
coordinator.add_channel(cls(instance_id=instance_id))
logger.log(99, "\x1b[0;37;42m %s \x1b[0m",
_("Slave channel {name} ({id}) # {instance_id} is initialized.")
.format(name=cls.channel_name, id=cls.channel_id,
instance_id=instance_id or _("Default profile")))
logger.log(99, "\x1b[0;37;46m %s \x1b[0m",
_("Initializing master {}...").format(conf['master_channel']))
instance_id = conf['master_channel'].split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or None
module = utils.locate_module(conf['master_channel'], 'master')
coordinator.add_channel(module(instance_id=instance_id))
cls = utils.locate_module(i, 'slave')
telemetry_set_metadata({i: cls.__version__})
instance_id = i.split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or None
coordinator.add_channel(cls(instance_id=instance_id))
logger.log(99, "\x1b[0;37;42m %s \x1b[0m",
_("Slave channel {name} ({id}) # {instance_id} is initialized.")
.format(name=cls.channel_name, id=cls.channel_id,
instance_id=instance_id or _("Default profile")))
logger.log(99, "\x1b[0;37;46m %s \x1b[0m",
_("Initializing master {}...").format(conf['master_channel']))
instance_id = conf['master_channel'].split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or None
module = utils.locate_module(conf['master_channel'], 'master')
coordinator.add_channel(module(instance_id=instance_id))
telemetry_set_metadata({conf['master_channel']: module.__version__})
logger.log(99, "\x1b[0;37;42m %s \x1b[0m",
_("Master channel {name} ({id}) # {instance_id} is initialized.")
.format(name=coordinator.master.channel_name,
id=coordinator.master.channel_id,
instance_id=instance_id or _("Default profile")))
logger.log(99, "\x1b[1;37;42m %s \x1b[0m", _("All channels initialized."))
for i in conf['middlewares']:
logger.log(99, "\x1b[0;37;46m %s \x1b[0m", _("Initializing middleware {}...").format(i))
cls = utils.locate_module(i, 'middleware')
telemetry_set_metadata({i: cls.__version__})
instance_id = i.split('#', 1)[1:]
instance_id = (instance_id and instance_id[0]) or None
if not conf_path.exists():
raise FileNotFoundError(_("Config File does not exist. ({})").format(conf_path))
with conf_path.open() as f:
data: Dict[str, Any] = OPTIONAL_DEFAULTS.copy()
data.update(YAML().load(f))
# Verify configuration
# - Master channel
master_channel_id = data.get("master_channel", None)
if not master_channel_id:
raise ValueError(_("Master Channel is not specified in the profile config."))
elif not isinstance(master_channel_id, str):
raise ValueError(_("Master Channel ID is expected to be a string, but "
"\"{0}\" is of type {1}.").format(master_channel_id, type(master_channel_id)))
channel = utils.locate_module(data['master_channel'], 'master')
if not channel:
raise ValueError(_("\"{}\" is not found.").format(master_channel_id))
if not issubclass(channel, EFBChannel):
raise ValueError(_("\"{0}\" is not a channel, but a {1}.").format(master_channel_id, channel))
if not channel.channel_type == ChannelType.Master:
raise ValueError(_("\"{0}\" is not a master channel, but a {1}.")
.format(master_channel_id, channel.channel_type))
# - Slave channels
slave_channels_list = data.get("slave_channels", None)
if not slave_channels_list:
if not master_channel_id:
raise ValueError(_("Slave Channels are not specified in the profile config."))
elif not isinstance(slave_channels_list, list):
raise ValueError(_("Slave Channel IDs are expected to be a list, but {} is found.")
.format(slave_channels_list))