Skip to content

Commit a7aec99

Browse files
authored
Merge pull request #15 from mbdevpl/feature/improve-swap-support
improve swap support
2 parents e18c258 + f442df5 commit a7aec99

File tree

7 files changed

+44
-32
lines changed

7 files changed

+44
-32
lines changed

README.rst

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ Using
7373
Installing *system-query* doesn't enable all the features by default. Some of the query functions
7474
will work only on **some** systems. To attempt installation with all features enables,
7575
run :bash:`pip3 install system-query[all]`. If something brakes, you can narrow down the features
76-
by typing a feature scope instead of :bash:`all`.
77-
You can choose from :bash:`cpu`, :bash:`gpu`, :bash:`hdd`, :bash:`ram` and :bash:`swap`.
76+
by typing a feature scope instead of ``all``.
77+
You can choose from ``cpu``, ``gpu``, ``hdd``, ``ram`` and ``swap``.
7878
E.g. :bash:`pip3 install system-query[gpu]`. You can also select more than one feature
7979
at the same time, e.g. :bash:`pip3 install system-query[cpu,hdd,ram]`.
8080

@@ -140,13 +140,18 @@ assuming their executables are in system path.
140140
system_query.query_swap()
141141
~~~~~~~~~~~~~~~~~~~~~~~~~
142142

143-
To be able to see amount of swap space, install Python package :bash:`psutil`.
143+
To be able to see amount of swap space, install Python package ``psutil``.
144+
145+
.. code:: python
146+
147+
In [9]: system_query.query_swap()
148+
Out[9]: {'total': 0}
144149
145150
146151
As command-line tool
147152
--------------------
148153

149-
For example:
154+
Below will run :python:`system_query.query_all()` and output results to stdout:
150155

151156
.. code:: bash
152157
@@ -161,23 +166,23 @@ For example:
161166
'host': 'TestMachine',
162167
'os': 'Linux-4.4.0-109-generic-x86_64-with-debian-stretch-sid',
163168
'ram': {'total': 33701269504},
164-
'swap': 0}
169+
'swap': {'total': 0}}
165170
166-
Usage information:
171+
Please use ``-h`` to see usage information:
167172

168173
.. code::
169174
170175
$ python3 -m system_query -h
171-
usage: system_query [-h] [-s {all,cpu,gpu,ram}] [-f {raw,json}] [-t TARGET]
172-
[--version]
176+
usage: system_query [-h] [-s {all,cpu,gpu,ram,swap}] [-f {raw,json}]
177+
[-t TARGET] [--version]
173178
174179
Comprehensive and concise system information tool. Query a given hardware
175180
and/or software scope of your system and get results in human- and machine-
176181
readable formats.
177182
178183
optional arguments:
179184
-h, --help show this help message and exit
180-
-s {all,cpu,gpu,ram}, --scope {all,cpu,gpu,ram}
185+
-s {all,cpu,gpu,ram,swap}, --scope {all,cpu,gpu,ram,swap}
181186
Scope of the query (default: all)
182187
-f {raw,json}, --format {raw,json}
183188
Format of the results of the query. (default: raw)
@@ -187,6 +192,8 @@ Usage information:
187192
stdout and stderr, respectively. (default: stdout)
188193
--version show program's version number and exit
189194
195+
Copyright 2017-2023 by the contributors, Apache License 2.0,
196+
https://github.com/mbdevpl/system-query
190197
191198
Requirements
192199
============

system_query/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
__all__ = [
44
'query_all', 'query_cpu', 'query_gpus', 'query_hdds', 'query_ram', 'query_software',
5-
'query_and_export']
5+
'query_swap', 'query_and_export']
66

77
from .all_info import query_all
88
from .cpu_info import query_cpu
@@ -12,5 +12,5 @@
1212
# from .os_info import query_os
1313
from .ram_info import query_ram
1414
from .software_info import query_software
15-
# from .swap_info import query_swap
15+
from .swap_info import query_swap
1616
from .query import query_and_export

system_query/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66

77
from ._version import VERSION
8-
from .query import query_and_export
8+
from .query import QUERY_FUNCTIONS, query_and_export
99

1010

1111
def main(args=None, namespace=None):
@@ -21,7 +21,7 @@ def main(args=None, namespace=None):
2121
https://github.com/mbdevpl/system-query''',
2222
formatter_class=argparse.ArgumentDefaultsHelpFormatter, allow_abbrev=True)
2323
parser.add_argument(
24-
'-s', '--scope', type=str, default='all', choices=['all', 'cpu', 'gpu', 'ram'],
24+
'-s', '--scope', type=str, default='all', choices=list(QUERY_FUNCTIONS.keys()),
2525
help='''Scope of the query''')
2626
parser.add_argument(
2727
'-f', '--format', type=str, default='raw', choices=['raw', 'json'],

system_query/query.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .cpu_info import query_cpu
1111
from .gpu_info import query_gpus
1212
from .ram_info import query_ram
13+
from .swap_info import query_swap
1314

1415
JSON_INDENT = 2
1516

@@ -21,28 +22,30 @@ def query_and_export(query_scope: str, export_format: str, export_target: t.Any,
2122
2223
Currently implemented values are:
2324
24-
- query_scope: all, cpu, gpu, ram.
25+
- query_scope: all, cpu, gpu, ram, swap.
2526
- export_format: json, raw.
2627
- export_target: sys.stdout, sys.stderr, path.
2728
"""
2829
info = query(query_scope, **kwargs)
2930
export(info, export_format, export_target)
3031

3132

33+
QUERY_FUNCTIONS = {
34+
'all': query_all,
35+
'cpu': query_cpu,
36+
'gpu': query_gpus,
37+
'ram': query_ram,
38+
'swap': query_swap,
39+
}
40+
41+
3242
def query(query_scope: str, **kwargs) -> t.Any:
3343
"""Wrap around selected system query functions."""
34-
info: t.Any
35-
if query_scope == 'all':
36-
info = query_all(**kwargs)
37-
elif query_scope == 'cpu':
38-
info = query_cpu(**kwargs)
39-
elif query_scope == 'gpu':
40-
info = query_gpus(**kwargs)
41-
elif query_scope == 'ram':
42-
info = query_ram(**kwargs)
43-
else:
44-
raise NotImplementedError(f'scope={query_scope}')
45-
return info
44+
try:
45+
query_function = QUERY_FUNCTIONS[query_scope]
46+
except KeyError as err:
47+
raise NotImplementedError(f'scope={query_scope}') from err
48+
return query_function(**kwargs) # type: ignore
4649

4750

4851
def export(info, export_format: str, export_target: t.Any):

system_query/swap_info.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from .available_features import psutil, SWAP
66

77

8-
def query_swap() -> t.Optional[int]:
8+
def query_swap(**_) -> t.Optional[t.Dict[str, t.Any]]:
99
"""Get information about swap."""
1010
if not SWAP:
1111
return None
1212
total_swap = psutil.swap_memory().total
13-
return total_swap
13+
return {'total': total_swap}

test/test_available_features.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import unittest
44

5-
from system_query.available_features import CPU, GPU, HDD
6-
from system_query import query_cpu, query_gpus, query_hdds
5+
from system_query.available_features import CPU, GPU, HDD, SWAP
6+
from system_query import query_cpu, query_gpus, query_hdds, query_swap
77

88

99
class Tests(unittest.TestCase):
@@ -19,3 +19,7 @@ def test_gpus(self):
1919
@unittest.skipUnless(HDD, 'querying HDDs is not supported')
2020
def test_hdds(self):
2121
self.assertGreaterEqual(len(query_hdds()), 1)
22+
23+
@unittest.skipUnless(SWAP, 'querying swap is not supported')
24+
def test_swap(self):
25+
self.assertGreaterEqual(len(query_swap()), 1)

test/test_query.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ def test_unsupported_scope(self):
1616
query_and_export('host', 'raw', sys.stdout)
1717
with self.assertRaises(NotImplementedError):
1818
query_and_export('os', 'raw', sys.stdout)
19-
with self.assertRaises(NotImplementedError):
20-
query_and_export('swap', 'raw', sys.stdout)
2119

2220
def test_unsupported_format(self):
2321
with self.assertRaises(NotImplementedError):

0 commit comments

Comments
 (0)