Skip to content

Commit

Permalink
Cleanup program path provider
Browse files Browse the repository at this point in the history
  • Loading branch information
thatsIch committed Dec 14, 2016
1 parent 8b7fe12 commit f5fa7f7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
68 changes: 48 additions & 20 deletions path/program_path_provider.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""This module is about path resolving for the program path.
This is required to execute Rainmeter.exe for example to refresh the current skin.
"""

import os
import winreg

Expand All @@ -8,10 +13,11 @@
from .. import logger


def _get_rainmeter_path_from_default_path():
"""
Default location is "C:\Program Files\Rainmeter" in windows
we can get "C:\Program Files" through the environmental variables
def _get_rm_path_from_default_path():
r"""
Default location is "C:\Program Files\Rainmeter" in windows.
We can get "C:\Program Files" through the environmental variables
%PROGRAMFILES%
"""
programfiles = os.getenv("PROGRAMFILES")
Expand All @@ -21,25 +27,45 @@ def _get_rainmeter_path_from_default_path():


def _get_rainmeter_registry_key():
"""
throws FileNotFoundException if Software\WOW6432Node\Rainmeter does not exist
r"""
Throw FileNotFoundException if Software\WOW6432Node\Rainmeter does not exist.
not much we can do to handle that
"""
return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\WOW6432Node\\Rainmeter")


def _get_rainmeter_path_from_registry():
"""
Registry
"""
def _get_rm_path_from_registry():
"""Registry."""
rainmeter_key = _get_rainmeter_registry_key()
rainmeter_path = winreg.QueryValue(rainmeter_key, None)

return rainmeter_path


def __executable_exists(rm_path):
# normalize path
rainmeter_exe = os.path.join(rm_path, "Rainmeter.exe")
if not os.path.exists(rainmeter_exe):
message = """Rainmeter path was found, but no Rainmeter.exe found.
Check if you have installed Rainmeter correctly."""
logger.error(__file__, "get_cached_program_path()", message)
sublime.error_message(message)
return False

return True

@lru_cache(maxsize=None)
def get_cached_program_path():
"""Try to retrieve the program path of RM through magic.
Possible options are:
* It is given through settings
* It is in the default installation path
* It is registered in the registry
* Ask the user for the path
"""
# Load setting
settings = sublime.load_settings("Rainmeter.sublime-settings")
rm_path = settings.get("rainmeter_path", None)
Expand All @@ -51,25 +77,27 @@ def get_cached_program_path():
"get_cached_program_path()",
"rainmeter_path not found in settings. Trying default location."
)
rm_path = _get_rainmeter_path_from_default_path()
rm_path = _get_rm_path_from_default_path()

# if it is not even specified by default, try using the registry to retrieve the installation path
# if it is not even specified by default,
# try using the registry to retrieve the installation path
if not os.path.isdir(rm_path):
rm_path = _get_rainmeter_path_from_registry()
rm_path = _get_rm_path_from_registry()

# Check if path exists and contains Rainmeter.exe
if not os.path.isdir(rm_path):
message = "Path to Rainmeter.exe could neither be found in the standard directory nor via registry. Check your \"rainmeter_path\" setting."
message = """Path to Rainmeter.exe could neither be found:
* in the settings,
* in the standard directory,
* nor via registry.
Check your \"rainmeter_path\" setting."""
logger.info(__file__, "get_cached_program_path()", message)
sublime.error_message(message)
return

# normalize path
rainmeter_exe = os.path.join(rm_path, "Rainmeter.exe")
if not os.path.exists(rainmeter_exe):
message = "Rainmeter path was found, but no Rainmeter.exe found. Check if you have correctly installed Rainmeter."
logger.error(__file__, "get_cached_program_path()", message)
sublime.error_message(message)
if not __executable_exists(rm_path):
return

logger.info(__file__, "get_cached_program_path()", "Rainmeter found in " + rm_path)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_path_program_path_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_default_path(self):
If the user installed the application into the default directory
it is in "C:/Program Files/Rainmeter"
"""
default_program_path = program_path_provider._get_rainmeter_path_from_default_path()
default_program_path = program_path_provider._get_rm_path_from_default_path()

self.assertEqual(default_program_path, "C:\\Program Files\\Rainmeter")

Expand All @@ -22,7 +22,7 @@ def test_from_registry(self):
We can use this to find the actual Rainmeter.
Since we use the default installation path, there should no difference
"""
registry_program_path = program_path_provider._get_rainmeter_path_from_registry()
registry_program_path = program_path_provider._get_rm_path_from_registry()

self.assertEqual(registry_program_path, "C:\\Program Files\\Rainmeter")

Expand Down

0 comments on commit f5fa7f7

Please sign in to comment.