| Conditions | 9 |
| Total Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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:
| 1 | """Module for getting Rainmeter-specific paths.""" |
||
| 80 | def get_current_config(filepath): |
||
| 81 | """Get the value of the #CURRENTCONFIG# variable for the specified path. |
||
| 82 | |||
| 83 | Returns None if the path is not in the skins folder |
||
| 84 | """ |
||
| 85 | filepath = os.path.normpath(filepath) |
||
| 86 | |||
| 87 | skinspath = get_cached_skin_path() |
||
| 88 | if not skinspath: |
||
| 89 | logger.info("current config could not be found" + |
||
| 90 | " because the skins path could not be found.") |
||
| 91 | return |
||
| 92 | if not filepath.startswith(skinspath): |
||
| 93 | logger.info("current config could not be found" + |
||
| 94 | " because the current file is not located in the skins path.") |
||
| 95 | |||
| 96 | """workaround for symlinks: |
||
| 97 | It could exists a symlink in the rainmeter skins folder. |
||
| 98 | Rainmeter detects it correctly but uses the name of the symlink |
||
| 99 | for the config name.""" |
||
| 100 | |||
| 101 | logger.info("trying symlink detection to find the skin config.") |
||
| 102 | skins = os.listdir(skinspath) |
||
| 103 | |||
| 104 | for skin in skins: |
||
| 105 | check = os.path.join(skinspath, skin) |
||
| 106 | # 0x0400 is the attribute for reparse point https://msdn.microsoft.com/en-us/library/ee332330(VS.85).aspx |
||
| 107 | symbolic = os.path.isdir(check) and (ctypes.windll.kernel32.GetFileAttributesW(str(check)) & 0x0400) > 0 |
||
| 108 | if symbolic: |
||
| 109 | logger.info("detected a symbolic link '" + check + "' through reparse pointer in file attributes.") |
||
| 110 | walked = os.walk(check) |
||
| 111 | # we walk deeper here because there can also be sub configs hidden in folders like <config/subconfig/config.ini> |
||
| 112 | # which results the config being <config/subconfig> |
||
| 113 | # at this point <skin> is probably the <config/ |
||
| 114 | for root, dirs, files in walked: |
||
| 115 | for file in files: |
||
| 116 | probe = os.path.join(root, file) |
||
| 117 | if filecmp.cmp(probe, filepath): |
||
| 118 | config = os.path.relpath(root, skinspath) |
||
| 119 | logger.info("found same file '" + file + "'. Recreate interpreted config '" + config + "'.") |
||
| 120 | |||
| 121 | return config |
||
| 122 | |||
| 123 | # when we found the file, we need to reverse the path to detect the sub configs |
||
| 124 | # _, probe_path_and_file = os.path.splitdrive(probe) |
||
| 125 | # _, actual_path_and_file = os.path.splitdrive(filepath) |
||
| 126 | |||
| 127 | # prime the algorithm to remove the files |
||
| 128 | # probe_path, _ = os.path.split(probe_path_and_file) |
||
| 129 | # actual_path, _ = os.path.split(actual_path_and_file) |
||
| 130 | |||
| 131 | # while 1: |
||
| 132 | # probe_path, probe_folder = os.path.split(probe_path) |
||
| 133 | # actual_path, actual_folder = os.path.split(actual_path) |
||
| 134 | # if probe_folder != actual_folder: |
||
| 135 | # logger.info("Found last not matching folder '" + probe_folder + "' with '" + actual_folder + "'") |
||
| 136 | |||
| 137 | # directory = os.path.dirname(probe) |
||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | return |
||
| 142 | |||
| 143 | # before you get the whole path called from the skin D:\Documents\Rainmeter\Skins\test\test.ini |
||
| 144 | if os.path.isfile(filepath): |
||
| 145 | filepath = os.path.dirname(filepath) |
||
| 146 | # after the file name is trimmed D:\Documents\Rainmeter\Skins\test |
||
| 147 | |||
| 148 | # the result is the directory path minus the skin path |
||
| 149 | # D:\Documents\Rainmeter\Skins\test minus D:\Documents\Rainmeter\Skins\test equals test |
||
| 150 | # thus the config is called <test> |
||
| 151 | return os.path.relpath(filepath, skinspath) |
||
| 152 | |||
| 248 |