Conditions | 13 |
Total Lines | 93 |
Code Lines | 73 |
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.SystemTools.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 | """ |
||
70 | @classmethod |
||
71 | def get_env_info(cls, *, include_insecure: bool = False) -> Mapping[str, str]: |
||
72 | """ |
||
73 | Get a dictionary of some system and environment information. |
||
74 | Includes os_release, hostname, username, mem + disk, shell, etc. |
||
75 | |||
76 | Args: |
||
77 | include_insecure: Include data like hostname and username |
||
78 | |||
79 | .. caution :: |
||
80 | Even with ``include_insecure=False``, avoid exposing this data to untrusted |
||
81 | sources. For example, this includes the specific OS release, which could |
||
82 | be used in attack. |
||
83 | """ |
||
84 | try: |
||
85 | import psutil |
||
86 | except ImportError: |
||
87 | psutil = None |
||
88 | logger.warning("psutil is not installed, so cannot get extended env info") |
||
89 | |||
90 | now = datetime.now(timezone.utc).astimezone().isoformat() |
||
91 | uname = platform.uname() |
||
92 | language_code, encoding = locale.getlocale() |
||
93 | # build up this dict: |
||
94 | data = {} |
||
95 | |||
96 | def _try(os_fn, k: str, *args): |
||
97 | if any((a is None for a in args)): |
||
98 | return None |
||
99 | try: |
||
100 | v = os_fn(*args) |
||
101 | data[k] = v |
||
102 | return v |
||
103 | except (OSError, ImportError): |
||
104 | return None |
||
105 | |||
106 | data.update( |
||
107 | dict( |
||
108 | platform=platform.platform(), |
||
109 | python=".".join(str(i) for i in sys.version_info), |
||
110 | os=uname.system, |
||
111 | os_release=uname.release, |
||
112 | os_version=uname.version, |
||
113 | machine=uname.machine, |
||
114 | byte_order=sys.byteorder, |
||
115 | processor=uname.processor, |
||
116 | build=sys.version, |
||
117 | python_bits=8 * struct.calcsize("P"), |
||
118 | environment_info_capture_datetime=now, |
||
119 | encoding=encoding, |
||
120 | lang_code=language_code, |
||
121 | recursion_limit=sys.getrecursionlimit(), |
||
122 | float_info=sys.float_info, |
||
123 | int_info=sys.int_info, |
||
124 | flags=sys.flags, |
||
125 | hash_info=sys.hash_info, |
||
126 | implementation=sys.implementation, |
||
127 | switch_interval=sys.getswitchinterval(), |
||
128 | filesystem_encoding=sys.getfilesystemencoding(), |
||
129 | ) |
||
130 | ) |
||
131 | if "LANG" in os.environ: |
||
132 | data["lang"] = os.environ["LANG"] |
||
133 | if "SHELL" in os.environ: |
||
134 | data["shell"] = os.environ["SHELL"] |
||
135 | if "LC_ALL" in os.environ: |
||
136 | data["lc_all"] = os.environ["LC_ALL"] |
||
137 | if hasattr(sys, "winver"): |
||
138 | data["win_ver"] = sys.getwindowsversion() |
||
139 | if hasattr(sys, "mac_ver"): |
||
140 | data["mac_ver"] = sys.mac_ver() |
||
141 | if hasattr(sys, "linux_distribution"): |
||
142 | data["linux_distribution"] = sys.linux_distribution() |
||
143 | if include_insecure: |
||
144 | _try(getuser, "username") |
||
145 | _try(os.getlogin, "login") |
||
146 | _try(socket.gethostname, "hostname") |
||
147 | _try(os.getcwd, "cwd") |
||
148 | pid = _try(os.getpid, "pid") |
||
149 | ppid = _try(os.getppid, "parent_pid") |
||
150 | if hasattr(os, "getpriority"): |
||
151 | _try(os.getpriority, "priority", os.PRIO_PROCESS, pid) |
||
152 | _try(os.getpriority, "parent_priority", os.PRIO_PROCESS, ppid) |
||
153 | if psutil is not None: |
||
154 | data.update( |
||
155 | dict( |
||
156 | disk_used=psutil.disk_usage(".").used, |
||
157 | disk_free=psutil.disk_usage(".").free, |
||
158 | memory_used=psutil.virtual_memory().used, |
||
159 | memory_available=psutil.virtual_memory().available, |
||
160 | ) |
||
161 | ) |
||
162 | return {k: str(v) for k, v in dict(data).items()} |
||
163 | |||
238 |