Skip to content

Commit

Permalink
improve formatting and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromepin committed Dec 20, 2024
1 parent fd9c6a2 commit 9728037
Show file tree
Hide file tree
Showing 27 changed files with 274 additions and 238 deletions.
2 changes: 1 addition & 1 deletion esctl/cmd/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def take_action(self, parsed_args):
aliases = self.es.cat.aliases(name=parsed_args.alias, format="json")

return JSONToCliffFormatter(aliases).format_for_lister(
columns=[("index",), ("alias",)]
columns=[("index",), ("alias",)],
)

def get_parser(self, prog_name):
Expand Down
52 changes: 26 additions & 26 deletions esctl/cmd/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ def take_action(self, parsed_args):
("host"),
("ip", "IP"),
("node"),
]
],
)

def transform(self, allocation):
nodes = []

for node in allocation:
if self.uses_table_formatter():
if node.get("disk.percent") is not None:
if int(node.get("disk.percent")) > 85:
node["disk.percent"] = Color.colorize(
node.get("disk.percent"), Color.RED
)

elif int(node.get("disk.percent")) > 70:
node["disk.percent"] = Color.colorize(
node.get("disk.percent"), Color.YELLOW
)
if self.uses_table_formatter() and node.get("disk.percent") is not None:
if int(node.get("disk.percent")) > 85:
node["disk.percent"] = Color.colorize(
node.get("disk.percent"),
Color.RED,
)

elif int(node.get("disk.percent")) > 70:
node["disk.percent"] = Color.colorize(
node.get("disk.percent"),
Color.YELLOW,
)

nodes.append(node)

Expand All @@ -55,7 +56,7 @@ def take_action(self, parsed_args):
plugins = self.transform(self.es.cat.plugins(format="json"))

return JSONToCliffFormatter(plugins).format_for_lister(
columns=[("name", "node"), ("component", "plugin"), ("version")]
columns=[("name", "node"), ("component", "plugin"), ("version")],
)

def transform(self, plugins):
Expand All @@ -69,7 +70,9 @@ def take_action(self, parsed_args):
nodes = [n.get("name") for n in self.es.cat.nodes(format="json", h="name")]
shards = self.transform(
self.es.cat.shards(
format="json", index=parsed_args.index, h="index,node,shard,prirep"
format="json",
index=parsed_args.index,
h="index,node,shard,prirep",
),
nodes,
)
Expand All @@ -96,7 +99,7 @@ def transform(self, shards_list: list[dict[str, str]], nodes: list[str]):

if shard_index not in indices:
indices[shard_index] = {
**{"index": shard_index},
"index": shard_index,
**{n: "" for n in nodes},
}

Expand All @@ -105,9 +108,7 @@ def transform(self, shards_list: list[dict[str, str]], nodes: list[str]):
if shard_node == "UNASSIGNED":
self.has_unassigned_shards = True

indices[shard_index][shard_node] = (
f"{shard.get('shard')}{shard.get('prirep')}"
)
indices[shard_index][shard_node] = f"{shard.get('shard')}{shard.get('prirep')}"

return list(indices.values())

Expand Down Expand Up @@ -137,22 +138,19 @@ def take_action(self, parsed_args):
format="json",
h=headers,
thread_pool_patterns=parsed_args.thread_pool_patterns,
)
),
)

return JSONToCliffFormatter(thread_pools).format_for_lister(
columns=[(h,) for h in headers.split(",")]
columns=[(h,) for h in headers.split(",")],
)

def get_parser(self, prog_name):
parser = super().get_parser(prog_name)

parser.add_argument(
"--thread-pool-patterns",
help=(
"A comma-separated list of regular-expressions or strings "
"to filter the thread pools in the output"
),
help=("A comma-separated list of regular-expressions or strings to filter the thread pools in the output"),
)
parser.add_argument(
"--headers",
Expand Down Expand Up @@ -184,12 +182,14 @@ def take_action(self, parsed_args):
templates = self.es.cat.templates(name=parsed_args.name, format="json")

return JSONToCliffFormatter(templates).format_for_lister(
columns=[("name",), ("index_patterns",), ("order",), ("version")]
columns=[("name",), ("index_patterns",), ("order",), ("version")],
)

def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
"name", help="A pattern that returned template names must match", nargs="?"
"name",
help="A pattern that returned template names must match",
nargs="?",
)
return parser
19 changes: 11 additions & 8 deletions esctl/cmd/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def take_action(self, parsed_args):
response = self.es.cluster.allocation_explain()
except elasticsearch.TransportError as transport_error:
if transport_error.args[0] == 400:
self.log.warn(
self.log.warning(
"Unable to find any unassigned shards to explain."
" This may indicate that all shards are allocated."
" This may indicate that all shards are allocated.",
)

return (("Attribute", "Value"), tuple())
Expand All @@ -25,14 +25,14 @@ def take_action(self, parsed_args):
"can_allocate": response.get("can_allocate"),
"explanation": response.get("allocate_explanation"),
"last_allocation_status": response.get("unassigned_info").get(
"last_allocation_status"
"last_allocation_status",
),
"reason": response.get("unassigned_info").get("reason"),
}

for node in response.get("node_allocation_decisions"):
output[node.get("node_name")] = node.get("deciders")[0].get(
"explanation"
"explanation",
)

return (("Attribute", "Value"), tuple(output.items()))
Expand All @@ -46,7 +46,8 @@ def take_action(self, parsed_args):

if self.uses_table_formatter():
health["status"] = Color.colorize(
health.get("status"), getattr(Color, health.get("status").upper())
health.get("status"),
getattr(Color, health.get("status").upper()),
)

return (tuple(health.keys()), tuple(health.values()))
Expand All @@ -69,7 +70,7 @@ class ClusterStats(EsctlShowOne):

def take_action(self, parsed_args):
cluster_stats = self._sort_and_order_dict(
self.transform(flatten_dict(self.es.cluster.stats()))
self.transform(flatten_dict(self.es.cluster.stats())),
)

return (tuple(cluster_stats.keys()), tuple(cluster_stats.values()))
Expand All @@ -85,7 +86,7 @@ def transform(self, stats):
]:
if attribute in stats:
for key, value in self.objects_list_to_flat_dict(
stats.get(attribute)
stats.get(attribute),
).items():
stats.update({f"{attribute}{key}": value})

Expand Down Expand Up @@ -117,7 +118,9 @@ def get_parser(self, prog_name):
help=("Set setting as transient (default)"),
)
persistency_group.add_argument(
"--persistent", action="store_true", help=("Set setting as persistent")
"--persistent",
action="store_true",
help=("Set setting as persistent"),
)

parser.add_argument(
Expand Down
20 changes: 12 additions & 8 deletions esctl/cmd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def take_action(self, parsed_args):
"servers": "\n".join(cluster_definition.get("servers")),
}
for cluster_name, cluster_definition in Esctl._config.get(
"clusters"
"clusters",
).items()
]

return JSONToCliffFormatter(clusters).format_for_lister(
columns=[("name"), ("servers")]
columns=[("name"), ("servers")],
)


Expand All @@ -36,12 +36,12 @@ def take_action(self, parsed_args):
"cluster": context_definition.get("cluster"),
}
for context_name, context_definition in Esctl._config.get(
"contexts"
"contexts",
).items()
]

return JSONToCliffFormatter(self.transform(contexts)).format_for_lister(
columns=[("name"), ("user"), ("cluster")]
columns=[("name"), ("user"), ("cluster")],
)

def transform(self, raw_contexts):
Expand All @@ -51,7 +51,8 @@ def transform(self, raw_contexts):
if context.get("name") == Esctl._config.get("default-context"):
for context_attribute_name, context_attribute_value in context.items():
context[context_attribute_name] = Color.colorize(
context_attribute_value, Color.UNDERLINE
context_attribute_value,
Color.UNDERLINE,
)

modified_contexts.append(context)
Expand Down Expand Up @@ -84,8 +85,11 @@ def take_action(self, parsed_args):
setup_yaml()
print(
yaml.safe_dump(
dict(Esctl._config), None, default_flow_style=False, width=500
)
dict(Esctl._config),
None,
default_flow_style=False,
width=500,
),
)


Expand All @@ -103,5 +107,5 @@ def take_action(self, parsed_args):
]

return JSONToCliffFormatter(users).format_for_lister(
columns=[("name"), ("username"), ("password")]
columns=[("name"), ("username"), ("password")],
)
2 changes: 1 addition & 1 deletion esctl/cmd/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DocumentGet(EsctlShowOne):

def take_action(self, parsed_args):
document = flatten_dict(
self.es.get(format="json", index=parsed_args.index, id=parsed_args.id)
self.es.get(format="json", index=parsed_args.index, id=parsed_args.id),
)

return JSONToCliffFormatter(document).to_show_one(lines=list(document.keys()))
Expand Down
13 changes: 8 additions & 5 deletions esctl/cmd/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class IndexList(EsctlLister):

def take_action(self, parsed_args):
indices = self.transform(
self.es.cat.indices(format="json", index=parsed_args.index)
self.es.cat.indices(format="json", index=parsed_args.index),
)
return JSONToCliffFormatter(indices).format_for_lister(
columns=[
Expand All @@ -47,7 +47,7 @@ def take_action(self, parsed_args):
("docs.deleted"),
("store.size"),
("pri.store.size", "Primary Store Size"),
]
],
)

def transform(self, indices):
Expand All @@ -61,7 +61,8 @@ def transform(self, indices):

if indice.get("status") == "close":
indices[idx]["status"] = Color.colorize(
indice.get("status"), Color.ITALIC
indice.get("status"),
Color.ITALIC,
)

return indices
Expand Down Expand Up @@ -122,10 +123,12 @@ def take_action(self, parsed_args):
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
"source_index", help="Name of the index to index document from"
"source_index",
help="Name of the index to index document from",
)
parser.add_argument(
"destination_index", help="Name of the index to index document to"
"destination_index",
help="Name of the index to index document to",
)
parser.add_argument(
"--conflicts",
Expand Down
14 changes: 7 additions & 7 deletions esctl/cmd/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ def take_action(self, parsed_args):
if not parsed_args.logger.startswith("logger"):
parsed_args.logger = "logger." + parsed_args.logger

level = (
self.cluster_settings.get(parsed_args.logger, persistency=persistency) or ""
)
level = self.cluster_settings.get(parsed_args.logger, persistency=persistency) or ""

print(f"{str(parsed_args.logger)} : {str(level)}")
print(f"{parsed_args.logger!s} : {level!s}")


class LoggingReset(EsctlCommandLogging):
Expand All @@ -30,7 +28,7 @@ def take_action(self, parsed_args):

print(f"Resetting logger {parsed_args.logger}")
print(
self.cluster_settings.set(parsed_args.logger, None, persistency=persistency)
self.cluster_settings.set(parsed_args.logger, None, persistency=persistency),
)


Expand All @@ -48,8 +46,10 @@ def take_action(self, parsed_args):

print(
self.cluster_settings.set(
parsed_args.logger, parsed_args.level, persistency=persistency
)
parsed_args.logger,
parsed_args.level,
persistency=persistency,
),
)

def get_parser(self, prog_name):
Expand Down
9 changes: 5 additions & 4 deletions esctl/cmd/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

class MigrationDeprecations(EsctlLister):
"""Retrieve information about different cluster, node, and index level settings that use
deprecated features that will be removed or changed in a future version."""
deprecated features that will be removed or changed in a future version.
"""

def take_action(self, parsed_args):
deprecations = self.transform(
self.request("GET", "/_migration/deprecations", None)
self.request("GET", "/_migration/deprecations", None),
)

return JSONToCliffFormatter(deprecations).format_for_lister(
Expand All @@ -17,7 +18,7 @@ def take_action(self, parsed_args):
("level",),
("message"),
("url", "Doc"),
]
],
)

def transform(
Expand Down Expand Up @@ -45,7 +46,7 @@ def transform(
"level": deprecation["level"],
"message": deprecation["message"],
"url": deprecation["url"],
}
},
)

return foo
Loading

0 comments on commit 9728037

Please sign in to comment.