| Conditions | 10 |
| Total Lines | 119 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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_skins_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 | """Module for getting Rainmeter-specific paths""" |
||
| 31 | skinspath = get_cached_skin_path() |
||
| 32 | if not skinspath or not filepath.startswith(skinspath): |
||
| 33 | logger.info(__file__, "get_current_path", "current path could not be found because" + |
||
| 34 | " either the skins path could not be found or the current file" + |
||
| 35 | " is not located in the skins path.") |
||
| 36 | return |
||
| 37 | |||
| 38 | if os.path.isfile(filepath): |
||
| 39 | return os.path.dirname(filepath) + "\\" |
||
| 40 | else: |
||
| 41 | return filepath + "\\" |
||
| 42 | |||
| 43 | |||
| 44 | def get_root_config_path(filepath): |
||
| 45 | """Get the value of the #ROOTCONFIGPATH# variable for the specified path |
||
| 46 | |||
| 47 | Returns None if the path is not in the skins folder |
||
| 48 | |||
| 49 | """ |
||
| 50 | |||
| 51 | filepath = os.path.normpath(filepath) |
||
| 52 | |||
| 53 | skinspath = get_cached_skin_path() |
||
| 54 | if not skinspath or not filepath.startswith(skinspath): |
||
| 55 | logger.info(__file__, "get_root_config_path", "root config path could not be found" + |
||
| 56 | " because either the skins path could not be found or the" + |
||
| 57 | " current file is not located in the skins path.") |
||
| 58 | return |
||
| 59 | |||
| 60 | relpath = os.path.relpath(filepath, skinspath) |
||
| 61 | logger.info(__file__, "get_root_config_path", |
||
| 62 | os.path.join(skinspath, relpath.split("\\")[0]) + "\\") |
||
| 63 | |||
| 64 | return os.path.join(skinspath, relpath.split("\\")[0]) + "\\" |
||
| 65 | |||
| 66 | |||
| 67 | def get_current_file(filepath): |
||
| 68 | """Get the value of the #CURRENTFILE# variable for the specified path |
||
| 69 | |||
| 70 | Returns None if the path is not in the skins folder |
||
| 71 | |||
| 72 | """ |
||
| 73 | |||
| 74 | filepath = os.path.normpath(filepath) |
||
| 75 | |||
| 76 | skinspath = get_cached_skin_path() |
||
| 77 | if not skinspath or not filepath.startswith(skinspath): |
||
| 78 | logger.info(__file__, "get_current_file", "current file could not be found because" + |
||
| 79 | " either the skins path could not be found or the current" + |
||
| 80 | " file is not located in the skins path.") |
||
| 81 | return |
||
| 82 | |||
| 83 | if os.path.isfile(filepath): |
||
| 84 | return os.path.basename(filepath) |
||
| 85 | else: |
||
| 86 | logger.info(__file__, "get_current_file", "specified path is not a file.") |
||
| 87 | return |
||
| 88 | |||
| 89 | |||
| 90 | def get_current_config(filepath): |
||
| 91 | """Get the value of the #CURRENTCONFIG# variable for the specified path |
||
| 92 | |||
| 93 | Returns None if the path is not in the skins folder |
||
| 94 | |||
| 95 | """ |
||
| 96 | |||
| 97 | filepath = os.path.normpath(filepath) |
||
| 98 | |||
| 99 | skinspath = get_cached_skin_path() |
||
| 100 | if not skinspath or not filepath.startswith(skinspath): |
||
| 101 | logger.info(__file__, "get_current_config", "current config could not be found" + |
||
| 102 | " because \either the skins path could not be found or the" + |
||
| 103 | " current file is not located in the skins path.") |
||
| 104 | return |
||
| 105 | |||
| 106 | if os.path.isfile(filepath): |
||
| 107 | filepath = os.path.dirname(filepath) |
||
| 108 | |||
| 109 | return os.path.relpath(filepath, skinspath) |
||
| 110 | |||
| 111 | |||
| 112 | def get_resources_path(filepath): |
||
| 113 | """Get the value of the #@# variable for the specified path |
||
| 114 | |||
| 115 | Returns None if the path is not in the skins folder |
||
| 116 | |||
| 117 | """ |
||
| 118 | |||
| 119 | rfp = get_root_config_path(filepath) |
||
| 120 | |||
| 121 | if not rfp: |
||
| 122 | return |
||
| 123 | logger.info(__file__, "get_resources_path", os.path.join(rfp, "@Resources") + "\\") |
||
| 124 | return os.path.join(rfp, "@Resources") + "\\" |
||
| 125 | |||
| 126 | |||
| 127 | def replace_variables(string, filepath): |
||
| 128 | """Replace Rainmeter built-in variables and Windows environment variables |
||
| 129 | in string. |
||
| 130 | |||
| 131 | Replaces occurrences of the following variables in the string: |
||
| 132 | #CURRENTFILE# |
||
| 133 | #CURRENTPATH# |
||
| 134 | #ROOTCONFIGPATH# |
||
| 135 | #CURRENTCONFIG# |
||
| 136 | #@# |
||
| 137 | #SKINSPATH# |
||
| 138 | #SETTINGSPATH# |
||
| 139 | #PROGRAMPATH# |
||
| 140 | #PROGRAMDRIVE# |
||
| 141 | #ADDONSPATH# |
||
| 142 | #PLUGINSPATH# |
||
| 143 | Any Windows environment variables (like %APPDATA%) |
||
| 144 | filepath must be a skin file located in a subdirectory of the skins folder |
||
| 145 | |||
| 146 | """ |
||
| 147 | |||
| 148 | # lambdas for lazy evaluation |
||
| 149 | variables = {"#CURRENTFILE#": lambda: get_current_file(filepath), |
||
| 150 | "#CURRENTPATH#": lambda: get_current_path(filepath), |
||
| 369 |