-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg-listener.py
151 lines (113 loc) · 5.02 KB
/
pg-listener.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) Petr Vavrin (peterbay) 2022
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
import sys
import psycopg2
import configparser
import yaml
import json
import argparse
import select
import time
class pgListener:
config = None
dsn = None
listen_list = []
def __init__(self, argv):
parser = argparse.ArgumentParser()
parser.error = self.argsError
parser.add_argument('--config', '-c', help='Config file')
parser.add_argument('--json', '-j', action='store_true', help='Parse JSON message')
parser.add_argument('--raw', '-r', action='store_true', help='Print raw message to stdout')
self.args = parser.parse_args()
if not self.args.config:
self.exit(1, 'ERROR: Missing config file')
self.loadConfig()
self.listen()
def exit(self, state = None, message = None):
if message:
sys.stdout.write(f'{message}\n')
if state:
sys.exit(state)
def argsError(self, error):
pass
def loadConfig(self):
config_extension = os.path.splitext(self.args.config)[1]
if not config_extension:
self.exit(1, 'ERROR: Missing config file extension')
if config_extension == '.ini' or config_extension == '.conf':
try:
self.config = configparser.ConfigParser()
self.config.read(self.args.config)
self.dsn = self.config.get('database', 'dsn')
listen = self.config.items('listen')
if listen:
self.listen_list = [lis[0] for lis in listen if lis[1] == 'true']
except Exception as e:
self.exit(1, f'ERROR: Config parser error: {e}')
elif config_extension == '.yaml':
try:
with open(self.args.config, 'r') as stream:
self.config = yaml.safe_load(stream)
database = self.config.get('database', None)
if database:
self.dsn = database.get('dsn', None)
listen = self.config.get('listen')
if listen:
self.listen_list = [key for key, value in listen.items() if value]
except Exception as e:
self.exit(1, f'ERROR: Yaml parser error: {e}')
else:
self.exit(1, 'ERROR: Wrong config file format. YAML and INI is supported')
def listen(self):
if not self.dsn:
self.exit(1, 'ERROR: Missing database/dsn')
try:
connection = psycopg2.connect(self.dsn)
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = connection.cursor()
for lis in self.listen_list:
if not self.args.raw:
sys.stdout.write(f'LISTEN: {lis}\n')
cur.execute(f'LISTEN {lis};')
while True:
select.select([connection],[],[])
connection.poll()
while connection.notifies:
notification_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
notification = connection.notifies.pop()
if self.args.raw:
sys.stdout.write(f'{notification.payload}\n')
continue
payload = notification.payload
payload_delimiter = ''
if self.args.json:
try:
json_content = json.loads(payload)
payload = json.dumps(json_content, indent=4, sort_keys=True)
payload_delimiter = '\n'
except:
pass
sys.stdout.write(f'{notification_time} [{notification.pid}] {notification.channel}: {payload_delimiter}{payload}\n')
except KeyboardInterrupt:
try:
connection.close()
except:
pass
self.exit(0, '\nExit')
except Exception as e:
self.exit(1, f'Error: {e}')
if __name__ == '__main__':
listener = pgListener(sys.argv)