cic-cli/cic/output.py

54 lines
1.1 KiB
Python
Raw Normal View History

# standard imports
import os
import sys
2021-10-15 12:48:14 +02:00
import logging
2021-10-19 14:18:47 +02:00
import urllib.request
2021-10-15 12:48:14 +02:00
logg = logging.getLogger(__name__)
class OutputWriter:
2021-10-12 08:39:20 +02:00
def __init__(self, *args, **kwargs):
pass
def write(self, k, v):
raise NotImplementedError()
2021-10-12 08:39:20 +02:00
class StdoutWriter(OutputWriter):
def write(self, k, v):
sys.stdout.write('{}\t{}\n'.format(k, v))
class KVWriter(OutputWriter):
2021-10-12 08:39:20 +02:00
def __init__(self, path=None, *args, **kwargs):
2021-10-19 11:18:22 +02:00
try:
os.stat(path)
except FileNotFoundError:
os.makedirs(path)
self.path = path
def write(self, k, v):
fp = os.path.join(self.path, str(k))
2021-10-15 12:48:14 +02:00
logg.debug('path write {} {}'.format(fp, str(v)))
f = open(fp, 'wb')
f.write(v)
f.close()
2021-10-19 14:18:47 +02:00
class HTTPWriter(OutputWriter):
def __init__(self, path=None, *args, **kwargs):
super(HTTPWriter, self).__init__(*args, **kwargs)
self.path = path
def write(self, k, v):
rq = urllib.request.Request(self.path, method='POST', data=v)
r = urllib.request.urlopen(rq)
logg.info('proof submited at {}'.format(r.read))