+ flow plugin is now working

+ added escapeAnsi function to utils
This commit is contained in:
Kevin Froman 2018-07-12 02:37:10 -05:00
parent d879383a8a
commit 865bc94ccb
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
3 changed files with 52 additions and 6 deletions

View File

@ -177,7 +177,7 @@ class OnionrCommunicatorDaemon:
logger.info('Block passed proof, saving.') logger.info('Block passed proof, saving.')
self._core.setData(content) self._core.setData(content)
self._core.addToBlockDB(blockHash, dataSaved=True) self._core.addToBlockDB(blockHash, dataSaved=True)
self._core.utils.processBlockMetadata(blockHash) # caches block metadata values to block database self._core._utils.processBlockMetadata(blockHash) # caches block metadata values to block database
else: else:
logger.warn('POW failed for block ' + blockHash) logger.warn('POW failed for block ' + blockHash)
else: else:

View File

@ -18,7 +18,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
# Misc functions that do not fit in the main api, but are useful # Misc functions that do not fit in the main api, but are useful
import getpass, sys, requests, os, socket, hashlib, logger, sqlite3, config, binascii, time, base64, json, glob, shutil, math, json import getpass, sys, requests, os, socket, hashlib, logger, sqlite3, config, binascii, time, base64, json, glob, shutil, math, json, re
import nacl.signing, nacl.encoding import nacl.signing, nacl.encoding
from onionrblockapi import Block from onionrblockapi import Block
import onionrexceptions import onionrexceptions
@ -250,9 +250,17 @@ class OnionrUtils:
''' '''
Read metadata from a block and cache it to the block database Read metadata from a block and cache it to the block database
''' '''
myBlock = Block(myBlock, self._core) myBlock = Block(blockHash, self._core)
self._core.updateBlockInfo(blockHash, 'dataType', myBlock.getType()) self._core.updateBlockInfo(blockHash, 'dataType', myBlock.getType())
def escapeAnsi(self, line):
'''
Remove ANSI escape codes from a string with regex
taken or adapted from: https://stackoverflow.com/a/38662876
'''
ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')
return ansi_escape.sub('', line)
def getBlockDBHash(self): def getBlockDBHash(self):
''' '''

View File

@ -19,17 +19,54 @@
''' '''
# Imports some useful libraries # Imports some useful libraries
import logger, config import logger, config, threading, time
from onionrblockapi import Block from onionrblockapi import Block
plugin_name = 'flow' plugin_name = 'flow'
class OnionrFlow: class OnionrFlow:
def __init__(self): def __init__(self):
logger.info("HELLO") self.myCore = pluginapi.get_core()
self.alreadyOutputed = []
self.flowRunning = False
return return
def start(self):
message = ""
self.flowRunning = True
newThread = threading.Thread(target=self.showOutput)
newThread.start()
while self.flowRunning:
try:
message = logger.readline('\nInsert message into flow:').strip().replace('\n', '\\n').replace('\r', '\\r')
except EOFError:
pass
except KeyboardInterrupt:
self.flowRunning = False
if message == "q":
self.flowRunning = False
if len(message) > 0:
self.myCore.insertBlock(message)
logger.info("Flow is exiting, goodbye")
return
def showOutput(self):
while self.flowRunning:
for blockHash in self.myCore.getBlocksByType('txt'):
if blockHash in self.alreadyOutputed:
continue
if not self.flowRunning:
break
logger.info('\n------------------------')
block = Block(blockHash, self.myCore)
content = block.getContent()
# Escape new lines, remove trailing whitespace, and escape ansi sequences
content = self.myCore._utils.escapeAnsi(content.replace('\n', '\\n').replace('\r', '\\r').strip())
logger.info("\n" + block.getDate().strftime("%m/%d %H:%M") + ' - ' + '\033[0;0m' + content)
self.alreadyOutputed.append(blockHash)
time.sleep(5)
def on_init(api, data = None): def on_init(api, data = None):
''' '''
@ -42,6 +79,7 @@ def on_init(api, data = None):
# by simply referencing the variable `pluginapi`. # by simply referencing the variable `pluginapi`.
global pluginapi global pluginapi
pluginapi = api pluginapi = api
api.commands.register(['flow'], OnionrFlow) flow = OnionrFlow()
api.commands.register(['flow'], flow.start)
api.commands.register_help('flow', 'Open the flow messaging interface') api.commands.register_help('flow', 'Open the flow messaging interface')
return return