You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.8 KiB
Python

import glob
import math
import mimetypes
import os
3 years ago
import queue
import subprocess
import urllib
import yaml
from random import choice
3 years ago
import kode256
from base64 import b64encode
from PIL import Image
from bureau import Bureau, add_command, add_api
class Soup(Bureau):
"""
This bureau cooks few texts with xpub's python function recipes .
"""
name = "Canteen of the Screenless Office"
prefix = "SB"
version = 0
3 years ago
input_queue = []
3 years ago
pattern = ''
def __init__(self):
Bureau.__init__(self)
@add_command("1sentence", "1 Sentence Game Ideas")
def print_game(self):
"""
Prints one entry from the one sentence game ideas.
"""
onesentencegames = os.path.join(self.mdir, 'onesentenceg.yml')
with open(onesentencegames, 'r') as f:
games = yaml.load(f)
game = choice(games['games'])
self.print_small(game)
3 years ago
@add_command("pa", 'Set Pattern')
def print_pattern(self, data):
"""
Prints a pattern composed with A and B characters.
"""
3 years ago
pattern = data
3 years ago
self.pattern = pattern
3 years ago
self.print_small(pattern)
@add_command('list', 'Return a list of contents')
def list_contents(self):
prn = self._get_small_printer()
3 years ago
contents = []
3 years ago
files = glob.glob(os.path.join(self.mdir, 'contents', '*.txt'))
3 years ago
for file in files:
3 years ago
filename, ext = os.path.basename(file).split('.')
3 years ago
svg = kode256.svg("SBaq." + filename)
encoded_svg = b64encode(svg.encode()).decode()
encoded_data = "data:image/svg+xml;charset=utf-8;base64," + encoded_svg
with open(os.path.join(self.mdir, 'contents', filename + '.txt'), 'r') as f:
text = f.read()
contents.append({
"title": filename,
"barcode": encoded_data,
"content": text
})
self.print_full('files.html', contents=contents)
@add_command('tp', 'Print a short excerpt of a text')
def print_preview(self, data):
filename, _ = data.split(".")
with open(os.path.join(self.mdir, 'contents', filename + '.txt'), 'r') as f:
text = f.read()
self.print_small(filename + '\n' + text[:200] + '...')
3 years ago
3 years ago
@add_command('aq', 'Add a text to the queue')
def add_to_queue(self, data):
filename, _ = data.split(".")
self.input_queue.append(filename)
@add_command('cq', 'clear inputs in the queue')
def clear_q(self):
self.input_queue = []
self.print_small("queue cleared!")
# @add_command('fx', 'execute the chosen function')
# def ex_function
3 years ago
@add_command('wv', 'veawe 2 texts')
def weave(self):
result = ''
with open(os.path.join(self.mdir, 'contents', self.input_queue[0] + '.txt'), 'r') as f:
list_a = f.read().split(' ')
with open(os.path.join(self.mdir, 'contents', self.input_queue[1] + '.txt'), 'r') as f:
list_b = f.read().split(' ')
self.print_small(f'pattern: {self.pattern}')
self.print_small(
f'in the queue: {self.input_queue[0]}, {self.input_queue[1]}')
3 years ago
text_a_cursor = 0
text_b_cursor = 0
length = len(list_a) + len(list_b)
3 years ago
repeated_pattern = (self.pattern * (length // len(self.pattern)))
# + self.pattern[:(length % len(self.pattern))]
try:
for choice in repeated_pattern:
if choice == 'A':
result += list_a[text_a_cursor]
text_a_cursor += 1
if choice == 'B':
result += list_b[text_b_cursor]
text_b_cursor += 1
except Exception:
pass
3 years ago
self.print_small(result)
def main():
sb = Soup()
sb.run()
if __name__ == "__main__":
main()