made circles main.py pep8 compliant

This commit is contained in:
Kevin Froman 2020-01-15 00:25:16 -06:00
parent 0cd533db60
commit abddeecda7
1 changed files with 51 additions and 29 deletions

View File

@ -1,9 +1,26 @@
''' """Onionr - Private P2P Communication.
Onionr - Private P2P Communication
This default plugin handles "flow" messages (global chatroom style communication) This default plugin handles "flow" messages
''' (global chatroom style communication)
''' """
import sys
import os
import deadsimplekv as simplekv
from utils import identifyhome, reconstructhash
from coredb import blockmetadb
import threading
import time
import locale
from onionrblocks.onionrblockapi import Block
import logger
import onionrblocks
from onionrutils import escapeansi, epoch, bytesconverter
locale.setlocale(locale.LC_ALL, '')
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
# import after path insert
import flowapi # noqa
"""
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
@ -16,25 +33,15 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' """
# Imports some useful libraries
import threading, time, locale, sys, os
from onionrblocks.onionrblockapi import Block
import logger, config, onionrblocks
from onionrutils import escapeansi, epoch, bytesconverter
locale.setlocale(locale.LC_ALL, '')
from coredb import blockmetadb
from utils import identifyhome, reconstructhash
import deadsimplekv as simplekv
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
import flowapi # import after path insert
flask_blueprint = flowapi.flask_blueprint flask_blueprint = flowapi.flask_blueprint
security_whitelist = ['staticfiles.boardContent', 'staticfiles.board'] security_whitelist = ['staticfiles.boardContent', 'staticfiles.board']
plugin_name = 'flow' plugin_name = 'flow'
PLUGIN_VERSION = '0.0.1' PLUGIN_VERSION = '0.0.1'
class OnionrFlow: class OnionrFlow:
def __init__(self): def __init__(self):
self.alreadyOutputed = [] self.alreadyOutputed = []
@ -43,12 +50,15 @@ class OnionrFlow:
return return
def start(self): def start(self):
logger.warn("Please note: everything said here is public, even if a random channel name is used.", terminal=True) logger.warn(
"Please note: everything said here is public, " +
"even if a random channel name is used.", terminal=True)
message = "" message = ""
self.flowRunning = True self.flowRunning = True
try: try:
self.channel = logger.readline("Enter a channel name or none for default:").strip() self.channel = logger.readline(
except (KeyboardInterrupt, EOFError) as e: "Enter a channel name or none for default:").strip()
except (KeyboardInterrupt, EOFError):
self.flowRunning = False self.flowRunning = False
newThread = threading.Thread(target=self.showOutput, daemon=True) newThread = threading.Thread(target=self.showOutput, daemon=True)
newThread.start() newThread.start()
@ -56,7 +66,8 @@ class OnionrFlow:
if self.channel == "": if self.channel == "":
self.channel = "global" self.channel = "global"
try: try:
message = logger.readline('\nInsert message into flow:').strip().replace('\n', '\\n').replace('\r', '\\r') message = logger.readline('\nInsert message into flow:').strip().replace(
'\n', '\\n').replace('\r', '\\r')
except EOFError: except EOFError:
pass pass
except KeyboardInterrupt: except KeyboardInterrupt:
@ -67,7 +78,10 @@ class OnionrFlow:
expireTime = epoch.get_epoch() + 43200 expireTime = epoch.get_epoch() + 43200
if len(message) > 0: if len(message) > 0:
logger.info('Inserting message as block...', terminal=True) logger.info('Inserting message as block...', terminal=True)
onionrblocks.insert(message, header='brd', expire=expireTime, meta={'ch': self.channel}) onionrblocks.insert(message, header='brd',
expire=expireTime,
meta = {
'ch': self.channel})
logger.info("Flow is exiting, goodbye", terminal=True) logger.info("Flow is exiting, goodbye", terminal=True)
return return
@ -86,19 +100,24 @@ class OnionrFlow:
continue continue
if not self.flowRunning: if not self.flowRunning:
break break
logger.info('\n------------------------', prompt = False, terminal=True) logger.info('\n------------------------',
prompt=False, terminal=True)
content = block.getContent() content = block.getContent()
# Escape new lines, remove trailing whitespace, and escape ansi sequences # Escape new lines, remove trailing whitespace, and escape ansi sequences
content = escapeansi.escape_ANSI(content.replace('\n', '\\n').replace('\r', '\\r').strip()) content = escapeansi.escape_ANSI(content.replace(
logger.info(block.getDate().strftime("%m/%d %H:%M") + ' - ' + logger.colors.reset + content, prompt = False, terminal=True) '\n', '\\n').replace('\r', '\\r').strip())
logger.info(block.getDate().strftime(
"%m/%d %H:%M") + ' - ' + logger.colors.reset + content, prompt=False, terminal=True)
self.alreadyOutputed.append(b_hash) self.alreadyOutputed.append(b_hash)
time.sleep(5) time.sleep(5)
except KeyboardInterrupt: except KeyboardInterrupt:
self.flowRunning = False self.flowRunning = False
def on_flow_cmd(api, data=None): def on_flow_cmd(api, data=None):
OnionrFlow().start() OnionrFlow().start()
def on_softreset(api, data=None): def on_softreset(api, data=None):
try: try:
os.remove(identifyhome.identify_home() + '/board-index.cache.json') os.remove(identifyhome.identify_home() + '/board-index.cache.json')
@ -106,13 +125,16 @@ def on_softreset(api, data=None):
except FileNotFoundError: except FileNotFoundError:
pass pass
def on_processblocks(api, data=None): def on_processblocks(api, data=None):
metadata = data['block'].bmetadata # Get the block metadata metadata = data['block'].bmetadata # Get the block metadata
if data['type'] != 'brd': if data['type'] != 'brd':
return return
b_hash = reconstructhash.deconstruct_hash(data['block'].hash) # Get the 0-truncated block hash b_hash = reconstructhash.deconstruct_hash(
board_cache = simplekv.DeadSimpleKV(identifyhome.identify_home() + '/board-index.cache.json', flush_on_exit=False) # get the board index cache data['block'].hash) # Get the 0-truncated block hash
board_cache = simplekv.DeadSimpleKV(identifyhome.identify_home(
) + '/board-index.cache.json', flush_on_exit=False) # get the board index cache
board_cache.refresh() board_cache.refresh()
# Validate the channel name is sane for caching # Validate the channel name is sane for caching
try: try:
@ -124,7 +146,7 @@ def on_processblocks(api, data=None):
ch = 'global' ch = 'global'
elif ch_len > 12: elif ch_len > 12:
return return
existing_posts = board_cache.get(ch) existing_posts = board_cache.get(ch)
if existing_posts is None: if existing_posts is None:
existing_posts = [] existing_posts = []