Conditions | 12 |
Total Lines | 90 |
Code Lines | 68 |
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:
Complex classes like pocketutils.tools.sys_tools.SystemUtils.get_env_info() 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 | # SPDX-FileCopyrightText: Copyright 2020-2023, Contributors to pocketutils |
||
86 | def get_env_info(self: Self, *, extended: bool = False, insecure: bool = False) -> Mapping[str, str]: |
||
87 | """ |
||
88 | Get a dictionary of some system and environment information. |
||
89 | Includes os_release, hostname, username, mem + disk, shell, etc. |
||
90 | |||
91 | Args: |
||
92 | extended: Get info from psutil |
||
93 | insecure: Include data like hostname and username |
||
94 | |||
95 | Warning: |
||
96 | Even with `include_insecure=False`, avoid exposing this data to untrusted |
||
97 | sources. For example, this includes the specific OS release, which could |
||
98 | be used in attack. |
||
99 | """ |
||
100 | |||
101 | now = datetime.now(UTC).astimezone().isoformat() |
||
102 | uname = platform.uname() |
||
103 | lang_code, encoding = locale.getlocale() |
||
104 | # build up this dict: |
||
105 | data = {} |
||
106 | |||
107 | def _try(os_fn, k: str, *args): |
||
108 | if any(a is None for a in args): |
||
109 | return None |
||
110 | try: |
||
111 | v = os_fn(*args) |
||
112 | data[k] = v |
||
113 | return v |
||
114 | except (OSError, ImportError): |
||
115 | return None |
||
116 | |||
117 | data.update( |
||
118 | { |
||
119 | "platform": platform.platform(), |
||
120 | "python": ".".join(str(i) for i in sys.version_info), |
||
121 | "os": uname.system, |
||
122 | "os_release": uname.release, |
||
123 | "os_version": uname.version, |
||
124 | "machine": uname.machine, |
||
125 | "byte_order": sys.byteorder, |
||
126 | "processor": uname.processor, |
||
127 | "build": sys.version, |
||
128 | "python_bits": 8 * struct.calcsize("P"), |
||
129 | "environment_info_capture_datetime": now, |
||
130 | "encoding": encoding, |
||
131 | "lang_code": lang_code, |
||
132 | "recursion_limit": sys.getrecursionlimit(), |
||
133 | "float_info": sys.float_info, |
||
134 | "int_info": sys.int_info, |
||
135 | "flags": sys.flags, |
||
136 | "hash_info": sys.hash_info, |
||
137 | "implementation": sys.implementation, |
||
138 | "switch_interval": sys.getswitchinterval(), |
||
139 | "filesystem_encoding": sys.getfilesystemencoding(), |
||
140 | }, |
||
141 | ) |
||
142 | if "LANG" in os.environ: |
||
143 | data["lang"] = os.environ["LANG"] |
||
144 | if "SHELL" in os.environ: |
||
145 | data["shell"] = os.environ["SHELL"] |
||
146 | if "LC_ALL" in os.environ: |
||
147 | data["lc_all"] = os.environ["LC_ALL"] |
||
148 | if hasattr(sys, "winver"): |
||
149 | data["win_ver"] = sys.getwindowsversion() |
||
150 | if hasattr(sys, "mac_ver"): |
||
151 | data["mac_ver"] = sys.mac_ver() |
||
152 | if hasattr(sys, "linux_distribution"): |
||
153 | data["linux_distribution"] = sys.linux_distribution() |
||
154 | if insecure: |
||
155 | _try(getuser, "username") |
||
156 | _try(os.getlogin, "login") |
||
157 | _try(socket.gethostname, "hostname") |
||
158 | _try(os.getcwd, "cwd") |
||
159 | pid = _try(os.getpid, "pid") |
||
160 | ppid = _try(os.getppid, "parent_pid") |
||
161 | if hasattr(os, "getpriority"): |
||
162 | _try(os.getpriority, "priority", os.PRIO_PROCESS, pid) |
||
163 | _try(os.getpriority, "parent_priority", os.PRIO_PROCESS, ppid) |
||
164 | if extended: |
||
165 | import psutil |
||
166 | |||
167 | data.update( |
||
168 | { |
||
169 | "disk_used": psutil.disk_usage(".").used, |
||
170 | "disk_free": psutil.disk_usage(".").free, |
||
171 | "memory_used": psutil.virtual_memory().used, |
||
172 | "memory_available": psutil.virtual_memory().available, |
||
173 | }, |
||
174 | ) |
||
175 | return {k: str(v) for k, v in dict(data).items()} |
||
176 | |||
242 |