| Conditions | 10 |
| Total Lines | 120 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like get_cached_skin_path() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import getpass |
||
| 17 | @lru_cache(maxsize=None) |
||
| 18 | def get_cached_skin_path(): |
||
| 19 | """Get the value of the #SKINSPATH# variable""" |
||
| 20 | |||
| 21 | # First try to load the value from the "rainmeter_skins_path" setting |
||
| 22 | loaded_settings = sublime.load_settings("Rainmeter.sublime-settings") |
||
| 23 | skinspath = loaded_settings.get("rainmeter_skins_path", None) |
||
| 24 | |||
| 25 | # if it's found, return it |
||
| 26 | # We trust the user to enter something meaningful here |
||
| 27 | # and don't check anything. |
||
| 28 | if skinspath: |
||
| 29 | logger.info(__file__, "get_skins_path", "Skins path found in sublime-settings file.") |
||
| 30 | return os.path.normpath(skinspath) + "\\" |
||
| 31 | |||
| 32 | # If it's not set, try to detect it automagically |
||
| 33 | |||
| 34 | rainmeterpath = get_cached_program_path() |
||
| 35 | if not rainmeterpath: |
||
| 36 | return |
||
| 37 | |||
| 38 | settingspath = get_cached_setting_path() |
||
| 39 | if not settingspath: |
||
| 40 | return |
||
| 41 | |||
| 42 | # First, try to read the SkinPath setting from Rainmeter.ini |
||
| 43 | fhnd = io.open(os.path.join(settingspath, "Rainmeter.ini")) |
||
| 44 | lines = fhnd.read() |
||
| 45 | fhnd.close() |
||
| 46 | |||
| 47 | # Find the skinspath setting in the file |
||
| 48 | match = re.search(r"""(?imsx) |
||
| 49 | |||
| 50 | # Find the first [Rainmeter] section |
||
| 51 | (^\s*\[\s*Rainmeter\s*\]\s*$) |
||
| 52 | (.*? |
||
| 53 | |||
| 54 | # Find the "SkinPath" and "=" |
||
| 55 | (^\s*SkinPath\s*=\s* |
||
| 56 | |||
| 57 | # Read until the next line ending and store |
||
| 58 | # in named group |
||
| 59 | (?P<skinpath>[^$]+?)\s*?$ |
||
| 60 | ) |
||
| 61 | ).*? |
||
| 62 | |||
| 63 | # All of this needs to happen before the next section |
||
| 64 | (?:^\s*\[\s*[^\[\]\s]+\s*\]\s*$) |
||
| 65 | """, lines) |
||
| 66 | |||
| 67 | # if skinspath setting was found, return it |
||
| 68 | if match: |
||
| 69 | logger.info(__file__, "get_skins_path", "Skins path found in Rainmeter.ini.") |
||
| 70 | return match.group("skinpath").strip().replace("/", "\\") |
||
| 71 | |||
| 72 | # if it's not found in the settings file, try to guess it |
||
| 73 | |||
| 74 | # If program path and setting path are equal, we have a portable |
||
| 75 | # installation. In this case, the Skins folder is inside the rainmeter |
||
| 76 | # path |
||
| 77 | if os.path.samefile(rainmeterpath, settingspath): |
||
| 78 | logger.info(__file__, "get_skins_path", "Skin path found in #PROGRAMPATH#" + |
||
| 79 | " because portable installation") |
||
| 80 | return os.path.join(rainmeterpath, "Skins") + "\\" |
||
| 81 | |||
| 82 | # If it's not a portable installation, we try looking into the "My |
||
| 83 | # Documents" folder Since it could be relocated by the user, we have to |
||
| 84 | # query its value from the registry |
||
| 85 | try: |
||
| 86 | regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, |
||
| 87 | r"Software\Microsoft\Windows" + |
||
| 88 | r"\CurrentVersion\Explorer" + |
||
| 89 | r"\User Shell Folders") |
||
| 90 | keyval = winreg.QueryValueEx(regkey, "Personal") |
||
| 91 | |||
| 92 | pathrep = keyval[0] |
||
| 93 | |||
| 94 | # The path could (and most likely, will) contain environment |
||
| 95 | # variables that have to be expanded first |
||
| 96 | pathrep = os.path.expandvars(pathrep) |
||
| 97 | |||
| 98 | logger.info(__file__, "get_skins_path", "Guessed Skin path from My Documents" + |
||
| 99 | " location in registry") |
||
| 100 | return os.path.join(pathrep, "Rainmeter\\Skins") + "\\" |
||
| 101 | |||
| 102 | except OSError: |
||
| 103 | pass |
||
| 104 | |||
| 105 | # If the value could not be retrieved from the registry, |
||
| 106 | # we try some educated guesses about default locations |
||
| 107 | try: |
||
| 108 | username = getpass.getuser() |
||
| 109 | except Exception: |
||
| 110 | logger.info(__file__, "get_skins_path", "Skins path could not be located." + |
||
| 111 | " Please set the \"skins_path\" setting in your Rainmeter" + |
||
| 112 | " settings file.") |
||
| 113 | return |
||
| 114 | else: |
||
| 115 | # check if windows version is XP |
||
| 116 | winversion = platform.version() |
||
| 117 | if int(winversion[0]) < 6: |
||
| 118 | mydocuments = os.path.join("C:\\Documents and Settings", |
||
| 119 | username, |
||
| 120 | "My Documents") + "\\" |
||
| 121 | |||
| 122 | logger.info(__file__, "get_skins_path", "Found Windows XP or lower." + |
||
| 123 | " Skins path assumed to be " + mydocuments + |
||
| 124 | "Rainmeter\\Skins\\") |
||
| 125 | else: |
||
| 126 | mydocuments = os.path.join("C:\\Users", |
||
| 127 | username, |
||
| 128 | "Documents") + "\\" |
||
| 129 | |||
| 130 | logger.info(__file__, "get_skins_path", "Found Windows Vista or higher." + |
||
| 131 | " Skins path assumed to be " + mydocuments + |
||
| 132 | "Rainmeter\\Skins\\") |
||
| 133 | |||
| 134 | logger.info(__file__, "get_skins_path", "Skin path guessed from user name" + |
||
| 135 | " and Windows version") |
||
| 136 | return os.path.join(mydocuments, "Rainmeter\\Skins") + "\\" |
||
| 137 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.