Use partition instead of split where possible in core (#81806)

This commit is contained in:
Aarni Koskela 2022-11-15 22:45:48 +02:00 committed by GitHub
parent 35c1604ea7
commit 8038485ca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 15 additions and 21 deletions

View File

@ -47,7 +47,7 @@ def _lookup_domain(
perm_lookup: PermissionLookup, domains_dict: SubCategoryDict, entity_id: str perm_lookup: PermissionLookup, domains_dict: SubCategoryDict, entity_id: str
) -> ValueType | None: ) -> ValueType | None:
"""Look up entity permissions by domain.""" """Look up entity permissions by domain."""
return domains_dict.get(entity_id.split(".", 1)[0]) return domains_dict.get(entity_id.partition(".")[0])
def _lookup_area( def _lookup_area(

View File

@ -88,12 +88,12 @@ class CommandLineAuthProvider(AuthProvider):
for _line in stdout.splitlines(): for _line in stdout.splitlines():
try: try:
line = _line.decode().lstrip() line = _line.decode().lstrip()
if line.startswith("#"):
continue
key, value = line.split("=", 1)
except ValueError: except ValueError:
# malformed line # malformed line
continue continue
if line.startswith("#") or "=" not in line:
continue
key, _, value = line.partition("=")
key = key.strip() key = key.strip()
value = value.strip() value = value.strip()
if key in self.ALLOWED_META_KEYS: if key in self.ALLOWED_META_KEYS:

View File

@ -404,7 +404,7 @@ async def async_mount_local_lib_path(config_dir: str) -> str:
def _get_domains(hass: core.HomeAssistant, config: dict[str, Any]) -> set[str]: def _get_domains(hass: core.HomeAssistant, config: dict[str, Any]) -> set[str]:
"""Get domains of components to set up.""" """Get domains of components to set up."""
# Filter out the repeating and common config section [homeassistant] # Filter out the repeating and common config section [homeassistant]
domains = {key.split(" ")[0] for key in config if key != core.DOMAIN} domains = {key.partition(" ")[0] for key in config if key != core.DOMAIN}
# Add config entry domains # Add config entry domains
if not hass.config.safe_mode: if not hass.config.safe_mode:

View File

@ -734,7 +734,7 @@ async def merge_packages_config(
continue continue
# If component name is given with a trailing description, remove it # If component name is given with a trailing description, remove it
# when looking for component # when looking for component
domain = comp_name.split(" ")[0] domain = comp_name.partition(" ")[0]
try: try:
integration = await async_get_integration_with_requirements( integration = await async_get_integration_with_requirements(

View File

@ -122,7 +122,7 @@ async def async_check_ha_config_file( # noqa: C901
core_config.pop(CONF_PACKAGES, None) core_config.pop(CONF_PACKAGES, None)
# Filter out repeating config sections # Filter out repeating config sections
components = {key.split(" ")[0] for key in config.keys()} components = {key.partition(" ")[0] for key in config.keys()}
# Process and validate config # Process and validate config
for domain in components: for domain in components:

View File

@ -36,7 +36,7 @@ _EntityT = TypeVar("_EntityT", bound=entity.Entity)
@bind_hass @bind_hass
async def async_update_entity(hass: HomeAssistant, entity_id: str) -> None: async def async_update_entity(hass: HomeAssistant, entity_id: str) -> None:
"""Trigger an update for an entity.""" """Trigger an update for an entity."""
domain = entity_id.split(".", 1)[0] domain = entity_id.partition(".")[0]
entity_comp: EntityComponent[entity.Entity] | None entity_comp: EntityComponent[entity.Entity] | None
entity_comp = hass.data.get(DATA_INSTANCES, {}).get(domain) entity_comp = hass.data.get(DATA_INSTANCES, {}).get(domain)

View File

@ -202,7 +202,7 @@ def async_prepare_call_from_config(
f"Template rendered invalid service: {domain_service}" f"Template rendered invalid service: {domain_service}"
) from ex ) from ex
domain, service = domain_service.split(".", 1) domain, _, service = domain_service.partition(".")
target = {} target = {}
if CONF_TARGET in config: if CONF_TARGET in config:

View File

@ -63,7 +63,7 @@ class TraceElement:
"""Return dictionary version of this TraceElement.""" """Return dictionary version of this TraceElement."""
result: dict[str, Any] = {"path": self.path, "timestamp": self._timestamp} result: dict[str, Any] = {"path": self.path, "timestamp": self._timestamp}
if self._child_key is not None: if self._child_key is not None:
domain, item_id = self._child_key.split(".", 1) domain, _, item_id = self._child_key.partition(".")
result["child_id"] = { result["child_id"] = {
"domain": domain, "domain": domain,
"item_id": item_id, "item_id": item_id,

View File

@ -97,10 +97,7 @@ def _merge_resources(
# Build response # Build response
resources: dict[str, dict[str, Any]] = {} resources: dict[str, dict[str, Any]] = {}
for component in components: for component in components:
if "." not in component: domain = component.partition(".")[0]
domain = component
else:
domain = component.split(".", 1)[0]
domain_resources = resources.setdefault(domain, {}) domain_resources = resources.setdefault(domain, {})
@ -148,7 +145,7 @@ async def async_get_component_strings(
hass: HomeAssistant, language: str, components: set[str] hass: HomeAssistant, language: str, components: set[str]
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Load translations.""" """Load translations."""
domains = list({loaded.split(".")[-1] for loaded in components}) domains = list({loaded.rpartition(".")[-1] for loaded in components})
integrations: dict[str, Integration] = {} integrations: dict[str, Integration] = {}
ints_or_excs = await async_get_integrations(hass, domains) ints_or_excs = await async_get_integrations(hass, domains)

View File

@ -433,7 +433,7 @@ def async_get_loaded_integrations(hass: core.HomeAssistant) -> set[str]:
if "." not in component: if "." not in component:
integrations.add(component) integrations.add(component)
continue continue
domain, platform = component.split(".", 1) domain, _, platform = component.partition(".")
if domain in BASE_PLATFORMS: if domain in BASE_PLATFORMS:
integrations.add(platform) integrations.add(platform)
return integrations return integrations
@ -458,10 +458,7 @@ def async_start_setup(
time_taken = dt_util.utcnow() - started time_taken = dt_util.utcnow() - started
for unique, domain in unique_components.items(): for unique, domain in unique_components.items():
del setup_started[unique] del setup_started[unique]
if "." in domain: integration = domain.rpartition(".")[-1]
_, integration = domain.split(".", 1)
else:
integration = domain
if integration in setup_time: if integration in setup_time:
setup_time[integration] += time_taken setup_time[integration] += time_taken
else: else:

View File

@ -156,7 +156,7 @@ class Throttle:
# be prefixed by '.<locals>.' so we strip that out. # be prefixed by '.<locals>.' so we strip that out.
is_func = ( is_func = (
not hasattr(method, "__self__") not hasattr(method, "__self__")
and "." not in method.__qualname__.split(".<locals>.")[-1] and "." not in method.__qualname__.rpartition(".<locals>.")[-1]
) )
@wraps(method) @wraps(method)