Skip to content

Commit

Permalink
Fix support for pyproject.toml (code copied over from locust, so it s…
Browse files Browse the repository at this point in the history
…hould behave the same way as it does)
  • Loading branch information
cyberw committed Mar 19, 2024
1 parent 064234f commit 7694c7b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions locust_swarm/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import subprocess
import sys
import time
from collections import OrderedDict
from datetime import datetime, timezone

import configargparse
Expand All @@ -20,6 +21,37 @@

from locust_swarm._version import version

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib


class LocustTomlConfigParser(configargparse.TomlConfigParser):
def parse(self, stream):
try:
config = tomllib.loads(stream.read())
except Exception as e:
raise configargparse.ConfigFileParserException(f"Couldn't parse TOML file: {e}")

# convert to dict and filter based on section names
result = OrderedDict()

for section in self.sections:
data = configargparse.get_toml_section(config, section)
if data:
for key, value in data.items():
if isinstance(value, list):
result[key] = value
elif value is None:
pass
else:
result[key] = str(value)
break

return result


logging.basicConfig(
format="%(asctime)s,%(msecs)d %(levelname)-4s [%(filename)s:%(lineno)d] %(message)s",
datefmt="%Y-%m-%d:%H:%M:%S",
Expand All @@ -38,6 +70,10 @@
],
auto_env_var_prefix="LOCUST_",
formatter_class=configargparse.RawDescriptionHelpFormatter,
config_file_parser_class=configargparse.CompositeConfigParser([
LocustTomlConfigParser(["tool.locust"]),
configargparse.DefaultConfigFileParser,
]),
description="""A tool for automating distributed locust runs using ssh.
Example: swarm -f test.py --loadgen-list loadgen1.domain.com,loadgen2.domain.com --users 50""",
Expand Down

0 comments on commit 7694c7b

Please sign in to comment.