mirror of
https://github.com/paul-nameless/tg
synced 2024-11-24 21:03:06 +00:00
Refactor logger->log
This commit is contained in:
parent
27b94212d4
commit
cd9d17cf4f
5 changed files with 34 additions and 34 deletions
|
@ -4,7 +4,7 @@ import threading
|
|||
|
||||
from utils import notify
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Controller:
|
||||
|
@ -24,7 +24,7 @@ class Controller:
|
|||
try:
|
||||
self.handle_chats()
|
||||
except Exception as e:
|
||||
logger.exception('Error happened in main loop')
|
||||
log.exception('Error happened in main loop')
|
||||
|
||||
def handle_msgs(self):
|
||||
# set width to 0.25, move window to left
|
||||
|
@ -36,7 +36,7 @@ class Controller:
|
|||
while True:
|
||||
|
||||
key = self.view.get_key(self.view.chats.h, self.view.chats.w)
|
||||
logger.info('Pressed key: %s', key)
|
||||
log.info('Pressed key: %s', key)
|
||||
if key == 'q':
|
||||
return 'QUIT'
|
||||
elif key == ']':
|
||||
|
@ -100,7 +100,7 @@ class Controller:
|
|||
while True:
|
||||
|
||||
key = self.view.get_key(self.view.chats.h, self.view.chats.w)
|
||||
logger.info('Pressed key: %s', key)
|
||||
log.info('Pressed key: %s', key)
|
||||
if key == 'q':
|
||||
return
|
||||
elif key in ('l', '^E'):
|
||||
|
@ -135,10 +135,10 @@ class Controller:
|
|||
self.view.draw_msgs(self.model.get_current_msg(), msgs)
|
||||
|
||||
def update_handler(self, update):
|
||||
logger.debug('===============Received: %s', update)
|
||||
log.debug('===============Received: %s', update)
|
||||
_type = update['@type']
|
||||
if _type == 'updateNewMessage':
|
||||
logger.debug('Updating... new message')
|
||||
log.debug('Updating... new message')
|
||||
# with self.lock:
|
||||
chat_id = update['message']['chat_id']
|
||||
self.model.msgs.msgs[chat_id].append(update['message'])
|
||||
|
@ -148,7 +148,7 @@ class Controller:
|
|||
try:
|
||||
notify(update['message']['content']['text']['text'])
|
||||
except Exception:
|
||||
logger.exception('Error happened on notify: %s', update)
|
||||
log.exception('Error happened on notify: %s', update)
|
||||
# message_content = update['message']['content'].get('text', {})
|
||||
# we need this because of different message types: photos, files, etc.
|
||||
# message_text = message_content.get('text', '').lower()
|
||||
|
|
|
@ -23,7 +23,7 @@ logging.basicConfig(
|
|||
]
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
log = logging.getLogger(__name__)
|
||||
API_ID = os.getenv('API_ID')
|
||||
API_HASH = os.getenv('API_HASH')
|
||||
PHONE = os.getenv('PHONE')
|
||||
|
@ -47,7 +47,7 @@ def run(tg, stdscr):
|
|||
|
||||
|
||||
def main():
|
||||
logger.debug('#' * 64)
|
||||
log.debug('#' * 64)
|
||||
tg = Telegram(
|
||||
api_id=API_ID,
|
||||
api_hash=API_HASH,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import logging
|
||||
from collections import defaultdict
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Model:
|
||||
|
@ -73,9 +73,9 @@ class Model:
|
|||
|
||||
result.wait()
|
||||
if result.error:
|
||||
logger.info(f'send message error: {result.error_info}')
|
||||
log.info(f'send message error: {result.error_info}')
|
||||
else:
|
||||
logger.info(f'message has been sent: {result.update}')
|
||||
log.info(f'message has been sent: {result.update}')
|
||||
|
||||
|
||||
class ChatModel:
|
||||
|
@ -98,7 +98,7 @@ class ChatModel:
|
|||
for chat_id in self.chat_ids:
|
||||
chat = self.get_chat(chat_id)
|
||||
self.chats.append(chat)
|
||||
logger.debug(
|
||||
log.debug(
|
||||
'#### %s: %s, %s', chat_id, chat, i)
|
||||
if len(self.chats) >= offset + limit:
|
||||
break
|
||||
|
@ -121,7 +121,7 @@ class ChatModel:
|
|||
|
||||
result.wait()
|
||||
if result.error:
|
||||
logger.error(f'get chat ids error: {result.error_info}')
|
||||
log.error(f'get chat ids error: {result.error_info}')
|
||||
return {}
|
||||
|
||||
for chat_id in result.update['chat_ids']:
|
||||
|
@ -137,7 +137,7 @@ class ChatModel:
|
|||
result.wait()
|
||||
|
||||
if result.error:
|
||||
logger.error(f'get chat error: {result.error_info}')
|
||||
log.error(f'get chat error: {result.error_info}')
|
||||
return {}
|
||||
return result.update
|
||||
|
||||
|
@ -220,7 +220,7 @@ class UserModel:
|
|||
result = self.tg.get_me()
|
||||
result.wait()
|
||||
if result.error:
|
||||
logger.error(f'get chat ids error: {result.error_info}')
|
||||
log.error(f'get chat ids error: {result.error_info}')
|
||||
return {}
|
||||
self.me = result.update
|
||||
return self.me
|
||||
|
@ -231,7 +231,7 @@ class UserModel:
|
|||
result = self.tg.call_method('getUser', {'user_id': user_id})
|
||||
result.wait()
|
||||
if result.error:
|
||||
logger.error(f'get chat ids error: {result.error_info}')
|
||||
log.error(f'get chat ids error: {result.error_info}')
|
||||
return {}
|
||||
self.users[user_id] = result.update
|
||||
return result.update
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import logging
|
||||
import os
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def notify(msg, subtitle='New message', title='Telegram'):
|
||||
|
@ -13,7 +13,7 @@ def notify(msg, subtitle='New message', title='Telegram'):
|
|||
icon = f'-appIcon {icon_path}'
|
||||
cmd = '/usr/local/bin/terminal-notifier'
|
||||
|
||||
logger.debug('####: %s', f'{cmd} {icon} {sound} {title} {subtitle} {msg}')
|
||||
log.debug('####: %s', f'{cmd} {icon} {sound} {title} {subtitle} {msg}')
|
||||
os.system(
|
||||
f'{cmd} {icon} {sound} {title} {subtitle} {msg}'
|
||||
)
|
||||
|
|
|
@ -4,7 +4,7 @@ import math
|
|||
import re
|
||||
from datetime import datetime
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class View:
|
||||
|
@ -52,16 +52,16 @@ class View:
|
|||
# return self.stdscr.getkey()
|
||||
|
||||
ch = self.stdscr.getch(y, x)
|
||||
logger.info('raw ch without unctrl: %s', ch)
|
||||
log.info('raw ch without unctrl: %s', ch)
|
||||
try:
|
||||
return curses.unctrl(ch).decode()
|
||||
except UnicodeDecodeError:
|
||||
logger.warning('cant uncrtl: %s', ch)
|
||||
log.warning('cant uncrtl: %s', ch)
|
||||
return 'UNKNOWN'
|
||||
|
||||
def get_key_input(self, y, x):
|
||||
ch = self.msgs.win.getch(y, x)
|
||||
logger.info('raw ch without unctrl in msgs: %s', ch)
|
||||
log.info('raw ch without unctrl in msgs: %s', ch)
|
||||
return ch
|
||||
# return curses.unctrl(ch).decode()
|
||||
|
||||
|
@ -100,19 +100,19 @@ class StatusView:
|
|||
while True:
|
||||
key = self.win.get_wch(0, min(len(buff), self.w-1))
|
||||
key = ord(key)
|
||||
logger.info('Pressed in send msg: "%s"', key)
|
||||
log.info('Pressed in send msg: "%s"', key)
|
||||
# try:
|
||||
logger.info('Trying to chr: %s', chr(key))
|
||||
log.info('Trying to chr: %s', chr(key))
|
||||
# except ValueError:
|
||||
# logger.exception()
|
||||
# log.exception()
|
||||
if key == 10: # return
|
||||
logger.info('Sending msg: %s', buff)
|
||||
log.info('Sending msg: %s', buff)
|
||||
break
|
||||
elif key == 127: # del
|
||||
if buff:
|
||||
buff = buff[:-1]
|
||||
elif key == 7: # ^G cancel
|
||||
logger.info('Not Sending msg: %s', buff)
|
||||
log.info('Not Sending msg: %s', buff)
|
||||
buff = None
|
||||
break
|
||||
elif chr(key).isprintable():
|
||||
|
@ -230,7 +230,7 @@ class MsgView:
|
|||
# self.win.mvwin(0, self.x)
|
||||
|
||||
def draw(self, current, msgs):
|
||||
# logger.info('Dwaring msgs')
|
||||
# log.info('Dwaring msgs')
|
||||
self.win.clear()
|
||||
count = self.h
|
||||
|
||||
|
@ -246,7 +246,7 @@ class MsgView:
|
|||
offset = math.ceil((len(s) - 1) / self.w)
|
||||
count -= offset
|
||||
if count <= 0:
|
||||
# logger.warning('Reched end of lines')
|
||||
# log.warning('Reched end of lines')
|
||||
break
|
||||
|
||||
if i == current:
|
||||
|
@ -261,7 +261,7 @@ class MsgView:
|
|||
j += 1
|
||||
if j < 4:
|
||||
e = e + ' '
|
||||
# logger.info('####: %s', (e, offset, count))
|
||||
# log.info('####: %s', (e, offset, count))
|
||||
self.win.addstr(count, offset, e, attr)
|
||||
offset += len(e)
|
||||
|
||||
|
@ -287,7 +287,7 @@ class MsgView:
|
|||
_type = msg['@type']
|
||||
if _type == 'message':
|
||||
return dt, msg['sender_user_id'], parse_content(msg['content'])
|
||||
logger.debug('Unknown message type: %s', msg)
|
||||
log.debug('Unknown message type: %s', msg)
|
||||
return dt, msg['sender_user_id'], 'unknown msg type: ' + str(msg['content'])
|
||||
|
||||
|
||||
|
@ -299,7 +299,7 @@ def get_last_msg(chat):
|
|||
elif _type == 'messageVoiceNote':
|
||||
return '[voice msg]'
|
||||
else:
|
||||
logger.error(chat)
|
||||
log.error(chat)
|
||||
return f'[unknown type {_type}]'
|
||||
|
||||
|
||||
|
@ -317,7 +317,7 @@ def parse_content(content):
|
|||
elif _type == 'messageVoiceNote':
|
||||
return '[voice msg]'
|
||||
else:
|
||||
logger.debug('Unknown content: %s', content)
|
||||
log.debug('Unknown content: %s', content)
|
||||
return f'[unknown content type {_type}]'
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue