import argparse
import sys
import os
import six
from random import randint
import random
from PIL import Image , ImageOps
import curses
import sys , termios , tty , os , time
import apirequest
import imageprinter
import requests
class Printer ( ) :
ESC = b ' \x1B '
CHANGE_EMULATION = ESC + b ' \x7B ' + b ' \x65 '
LINE_FEED = b ' \n '
CARIDGE_RET = b ' \x0D '
CHARWIDTH = 78
strokes = 0
debug = True
target = " /dev/usb/lp0 "
if debug :
target = " /dev/tty "
def printchar ( self , char ) :
with open ( self . target , ' wb ' ) as printer :
if self . debug :
printer . write ( bytes ( str ( char ) , ' utf-8 ' ) )
else :
printer . write ( self . ESC + b " \x7B " + b " \x41 " )
char = self . strokes * " " + char
printer . write ( bytes ( str ( char ) , ' utf-8 ' ) )
printer . write ( self . ESC + b " \x25 " + b " \x46 " + b " \x0F " + b " \x00 " + b " \x04 " + b " \x08 " + b " \x00 " )
self . strokes + = 1
if ( self . strokes > self . CHARWIDTH ) :
printer . write ( self . LINE_FEED )
self . strokes = 0
def printstring ( self , string ) :
with open ( self . target , ' wb ' ) as printer :
if self . debug :
printer . write ( bytes ( str ( string ) , ' utf-8 ' ) )
else :
printer . write ( self . ESC + b " \x7B " + b " \x41 " )
printer . write ( bytes ( str ( string ) , ' utf-8 ' ) )
printer . write ( self . ESC + b " \x25 " + b " \x46 " + b " \x0F " + b " \x00 " + b " \x04 " + b " \x08 " + b " \x00 " )
self . newline ( )
def newline ( self ) :
global strokes
with open ( self . target , ' wb ' ) as printer :
printer . write ( self . LINE_FEED )
self . strokes = 0
class Terminal ( ) :
lineinput = " "
commandMode = True
writeloop = True
def __init__ ( self , printer , persons ) :
self . printer = printer
self . persons = persons
def waittostart ( self ) :
self . writeloop = True
while self . writeloop :
char = self . getch ( )
self . writeloop = False
self . lineinput = " "
continue
def waitforinput ( self ) :
self . writeloop = True
while self . writeloop :
char = self . getch ( )
if ( ord ( char ) == 27 ) :
print ( " exit publication :-( " )
exit ( 0 )
if ( ord ( char ) == 13 ) :
self . printer . newline ( )
self . command ( self . lineinput )
self . lineinput = " "
else :
self . lineinput + = char
self . printer . printchar ( char )
def getch ( self ) :
fd = sys . stdin . fileno ( )
old_settings = termios . tcgetattr ( fd )
try :
tty . setraw ( sys . stdin . fileno ( ) )
ch = sys . stdin . read ( 1 )
finally :
termios . tcsetattr ( fd , termios . TCSADRAIN , old_settings )
return ch
def command ( self , command ) :
#print("received command: "+str(command))
if " help " == str ( command ) :
self . printer . printstring ( " type commands to print the publication " )
self . printer . printstring ( " help: " )
self . printer . printstring ( " get help on the commands " )
#self.writeloop = False
elif str ( command ) . split ( " " ) [ 0 ] == " exercise " or str ( command ) . split ( " " ) [ 0 ] == " thesis " :
part = str ( command ) . split ( " " ) [ 0 ]
try :
arguments = str ( command . strip ( ) ) . split ( " " )
arguments . pop ( 0 )
for i , arg in enumerate ( arguments ) :
if arg . isdigit ( ) :
number = int ( arg )
if number > len ( apirequest . getcategory ( part ) ) :
self . printer . printstring ( " Number must be inbetween 1 and " + str ( len ( apirequest . getcategory ( part ) ) ) )
else :
result = apirequest . get ( part , number )
for line in result :
self . printer . printstring ( line )
self . printer . newline ( )
else :
raise Exeption ( " not a number " )
except :
self . printer . printstring ( " ERROR: no argument given. Usage: " + part + " <number> " )
elif str ( command ) . split ( " " ) [ 0 ] == " whois " :
if str ( command ) . split ( " " ) [ 1 ] in self . persons :
self . printer . printstring ( self . persons [ str ( command ) . split ( " " ) [ 1 ] ] )
else :
self . printer . printstring ( " Sorry not found, try " + random . choice ( list ( self . persons ) ) )
elif " exit " == str ( command ) :
self . printer . printstring ( " Imprint " )
result = apirequest . get ( " imprint " , 1 )
for line in result :
self . printer . printstring ( line )
self . printer . newline ( )
main ( )
else :
self . printer . printstring ( " ~ command not found. try help for a list of available commands " )
self . printer . newline ( )
#if the command is valid we want to execute the command and continue the story
#self.writeloop = False
persons = {
" Kittler " : " Friedrich A. Kittler (June 12, 1943 – October 18, 2011) was a literary scholar and a media theorist. His works relate to media, technology, and the military. " ,
" Alex " : " Alexander Roidl created Poetic Software as part of his Master ' s thesis "
}
printer = Printer ( )
term = Terminal ( printer , persons )
def printimagefromurl ( url ) :
page = requests . get ( url )
f_ext = os . path . splitext ( url ) [ - 1 ]
f_name = ' img {} ' . format ( f_ext )
with open ( f_name , ' wb ' ) as f :
f . write ( page . content )
imageprinter . print ( f_name )
def main ( ) :
term . waittostart ( )
printer . printstring ( " W E L C O M E ! " )
printimagefromurl ( ' https://apod.nasa.gov/apod/image/1701/potw1636aN159_HST_2048.jpg ' )
printer . printstring ( " --------------- " )
printer . printstring ( " POETIC SOFTWARE " )
printer . printstring ( " --------------- " )
printer . printstring ( " type help for help " )
printer . newline ( )
result = apirequest . get ( " introduction " , 1 )
for line in result :
printer . printstring ( line )
printer . newline ( )
printer . printstring ( " theses on POETIC SOFTWARE " )
result = apirequest . getcategory ( " thesis " )
for i , line in enumerate ( result ) :
printer . printstring ( str ( i + 1 ) + " " + line )
printer . newline ( )
printer . printstring ( " exercises in POETIC SOFTWARE " )
result = apirequest . getcategory ( " exercise " )
for i , line in enumerate ( result ) :
printer . printstring ( str ( i + 1 ) + " " + line )
printer . newline ( )
printer . printstring ( " SELECT which one to see, for exercise type: exercise <number> and for thesis type: thesis <number> " )
printer . newline ( )
term . waitforinput ( )
main ( )