Skip to content

Commit

Permalink
Optimize disk IO and cache for faster launching times
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdelorenzo committed May 28, 2021
1 parent 347bd3e commit 819c765
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
2 changes: 0 additions & 2 deletions cast_control/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
__version__ = '0.10.1'
__author__ = 'Alex DeLorenzo'

from .base import *
38 changes: 31 additions & 7 deletions cast_control/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Optional, Union, NamedTuple, \
Tuple
Tuple, List
from pathlib import Path
from uuid import UUID
from enum import auto
from os import stat_result
from functools import lru_cache
import logging

from appdirs import AppDirs
Expand Down Expand Up @@ -73,6 +75,29 @@ def get_user_dirs() -> Tuple[Path, Path, Path]:
DEFAULT_THUMB = DARK_ICON = ICON_DIR / 'cc-black.png'


@lru_cache
def get_stat(file: Path) -> stat_result:
return file.stat()


@lru_cache
def get_template() -> List[str]:
return DESKTOP_TEMPLATE \
.read_text() \
.splitlines()


@lru_cache
def is_older_than_module(other: Path) -> bool:
other_stat = get_stat(other)
src_stat = get_stat(SRC_DIR)

if src_stat.st_ctime > other_stat.st_ctime:
return True

return False


def create_desktop_file(light_icon: bool = True) -> Path:
if light_icon:
path = LIGHT_ICON
Expand All @@ -82,20 +107,19 @@ def create_desktop_file(light_icon: bool = True) -> Path:
path = DARK_ICON
name_suffix = '-dark'

icon_path = str(path.absolute())
icon_path = str(path)
file = DATA_DIR / f'{NAME}{name_suffix}{DESKTOP_SUFFIX}'

*lines, name, icon = DESKTOP_TEMPLATE \
.read_text() \
.splitlines()
if file.exists() and not is_older_than_module(file):
return file

*lines, name, icon = get_template()
name += DESKTOP_NAME
icon += icon_path
lines = (*lines, name, icon)
data = '\n'.join(lines)

if not file.exists() or data != file.read_text():
file.write_text(data)
file.write_text(data)

return file

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool]
[tool.poetry]
name = "cast_control"
version = "0.10.0"
version = "0.10.1"
description = "📺 Control Chromecasts from Linux and D-Bus"
license = "AGPL-3.0"
homepage = "https://github.com/alexdelorenzo/cast_control"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

ENTRY_POINTS: Dict[str, List[str]] = {
'console_scripts': [
f'{NAME} = {CMD_PT},
f'{NAME} = {CMD_PT}',
f'{SHORT_NAME} = {CMD_PT}',
]
}
Expand Down

0 comments on commit 819c765

Please sign in to comment.