Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterjm committed Sep 3, 2020
1 parent 9ec49cd commit de97d23
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions homeassistant/components/media_source/local_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ def async_setup(hass: HomeAssistant):
hass.http.register_view(LocalMediaView(hass))


@callback
def async_parse_identifier(item: MediaSourceItem) -> Tuple[str, str]:
"""Parse identifier."""
if not item.identifier:
source_dir_id = "media"
location = ""

else:
source_dir_id, location = item.identifier.lstrip("/").split("/", 1)

if source_dir_id != "media":
raise Unresolvable("Unknown source directory.")

if location != sanitize_path(location):
raise Unresolvable("Invalid path.")

return source_dir_id, location


class LocalSource(MediaSource):
"""Provide local directories as media sources."""

Expand All @@ -31,34 +50,14 @@ def __init__(self, hass: HomeAssistant):
super().__init__(DOMAIN)
self.hass = hass

@callback
def async_parse_identifier(self, item: MediaSourceItem):
"""Parse identifier."""
if not item.identifier:
source_dir_id = "media"
location = ""

else:
source_dir_id, location = item.identifier[
item.identifier.startswith("/") and 1 :
].split("/", 1)

if source_dir_id != "media":
raise Unresolvable("Unknown source directory.")

if location != sanitize_path(location):
raise Unresolvable("Invalid path.")

return source_dir_id, location

@callback
def async_full_path(self, source_dir_id, location) -> Path:
"""Return full path."""
return self.hass.config.path("media", location)

async def async_resolve_media(self, item: MediaSourceItem) -> str:
"""Resolve media to a url."""
source_dir_id, location = self.async_parse_identifier(item)
source_dir_id, location = async_parse_identifier(item)
mime_type, _ = await self.hass.async_add_executor_job(
mimetypes.guess_type, self.async_full_path(source_dir_id, location)
)
Expand All @@ -69,7 +68,7 @@ async def async_browse_media(
) -> BrowseMedia:
"""Return media."""
try:
source_dir_id, location = self.async_parse_identifier(item)
source_dir_id, location = async_parse_identifier(item)
except Unresolvable as err:
raise BrowseError(str(err)) from err

Expand Down

0 comments on commit de97d23

Please sign in to comment.