This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrun_module.py
113 lines (77 loc) · 2.68 KB
/
run_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
# coding: utf-8
import importlib
import inspect
from pathlib import Path
from datetime import datetime
def run_module(module_args=[]):
def print_usage():
print('Usage:')
print('--run_plugin_func plugin.function [ARG1] [ARG2]')
print('\nor for more information:\n--plugin_info plugin')
if not module_args:
print_usage()
return
my_module = module_args[0].split('.')
my_args = module_args[1:]
if len(my_args) < 1:
my_args.append(None)
if len(my_module) < 2:
print_usage()
return
try:
i = importlib.import_module(f'plugins.{my_module[0]}.{my_module[0]}')
except Exception as e:
print(type(e))
print(f'error running plugin function: {my_module[0]}')
return
try:
my_function = getattr(i, my_module[1])
except AttributeError as e:
print(f'error: module {my_module[0]} has no function "{my_module[1]}"')
return
try:
my_function(my_args[0])
except Exception as e:
print(f'error: {e}')
return
def add_config(module=None, config_file=None):
def print_usage():
print('Adds basic configuration to config file')
print('Usage:')
print('--add_config plugin user|daemon')
print('\nExample: --add_config moon_phase daemon')
print('\nfor list of plugins:\n--list_plugins')
def time_stamp():
return datetime.now().strftime('# {} added >>>>>> %Y.%m.%d %H:%M:%S\n')
if not module:
print_usage()
return
my_module = module
my_config = Path(config_file)
try:
i = importlib.import_module(f'plugins.{module}.constants')
except Exception as e:
print(f'plugin "{module}" not found\n')
print_usage()
return
try:
config = i.sample_config
except AttributeError:
print('this plugin does not appear to have a sample configuration')
print('aborting')
return
try:
with open(config_file, 'a') as f:
f.write(time_stamp().format('start'))
f.write(config)
f.write(time_stamp().format('end'))
except PermissionError:
print('It appears you are trying to append to the daemon config file;')
print('you may need to run this with "sudo."')
return
except OSError as e:
print(f'An error occured while writing file "{config}": {e}')
return
print(f'Finished writing configuration for {module}')
print(f'It is a very good idea to open {config_file}\nand check the configuration you have just added!')