build.rsudp.entry_points.ep_cat_log()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 18
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 18
rs 10
c 0
b 0
f 0
cc 2
nop 0
1
import platform
2
import subprocess
3
import os
4
from rsudp import COLOR, log_loc, settings_loc
5
6
7
def ep_edit_settings():
8
	'''
9
	This function calls the system's default text editor to open the settings
10
	file for editing. It is provided for convenience only.
11
12
	This function is accessible via the console command ``rs-settings``.
13
14
	Advanced users may prefer to add an alias in place of this function.
15
	The alias **should** override the entrypoint command set in rsudp's
16
	``setup.py``.
17
18
	On Linux and MacOS, adding an alias may look like this:
19
20
	.. code-block:: bash
21
22
		# add the alias definition to the aliases file
23
		echo "alias rsudp-settings='nano ~/.config/rsudp/rsudp_settings.json'" >> .bash_aliases
24
		# then reload the console
25
		bash
26
27
	To add an alias on Windows via the command prompt is much more difficult,
28
	so the method is not provided here.
29
30
	.. note::
31
32
		This function has been tested on multiple operating systems, but
33
		because each system's functionality and defaults may be different,
34
		proper operation cannot be not guaranteed.
35
36
	'''
37
	if not os.path.exists(settings_loc):
38
		raise(FileNotFoundError('Settings file not found at %s' % settings_loc))
39
40
	if platform.system() == 'Darwin':		# MacOS
41
		subprocess.call(('open', settings_loc))
42
	elif platform.system() == 'Windows':	# Windows
43
		os.startfile(settings_loc)
44
	else:									# linux variants
45
		subprocess.call(('xdg-open', settings_loc))
46
47
def ep_cat_log():
48
	'''
49
	This function uses a posix system's ``cat`` command to print messages in
50
	the rsudp log file. It is provided for convenience only.
51
52
	It is accessible via the console command ``rs-log`` on posix (Linux, MacOS)
53
	style operating systems.
54
55
	.. note::
56
57
		This function is the equivalent of ``cat /tmp/rsudp/rsudp.log`` on
58
		Linux/MacOS and ``type "C:/tmp/rsudp/rsudp.log"`` on
59
		Windows.
60
	'''
61
	if os.name == 'posix':
62
		subprocess.call(('cat', log_loc))
63
	else:
64
		subprocess.call(('type', '"' + log_loc + '"'))
65
66
def ep_tailf_log():
67
	'''
68
	This function uses a the system's follow command to follow new
69
	messages added to the log file. It is provided for convenience only.
70
71
	This function is accessible via the console command ``rs-tailf``.
72
73
	The function will run until it receives a keyboard interrupt
74
	(:kbd:`Ctrl`\ +\ :kbd:`C`).
75
76
	.. note::
77
78
		This function is the equivalent of ``tail -f /tmp/rsudp/rsudp.log`` on
79
		Linux/MacOS and ``Get-Content -Path "C:/tmp/rsudp/rsudp.log" -Wait`` on
80
		Windows.
81
82
	'''
83
	if os.name == 'posix':
84
		try:
85
			print(COLOR['blue'] + 'Entering log follow (tail -f) mode.')
86
			print('New log messages will be printed until the console receives an interrupt (CTRL+C to end).' + COLOR['white'])
87
			subprocess.call(('tail','-f', log_loc))
88
		except KeyboardInterrupt:
89
			print()
90
			print(COLOR['blue'] + 'Quitting tail -f mode.' + COLOR['white'])
91
	if os.name == 'nt':
92
		try:
93
			print('Entering log follow mode.')
94
			print('New log messages will be printed until the console receives an interrupt (CTRL+C to end).')
95
			subprocess.call(('Get-Content', '-Path', '"' + log_loc + '"', '-Wait'))
96
		except Exception as e:
97
			print('This function is not available on Windows. Error: %s' % (e))
98