Respect the verbose verbose flag.

This commit is contained in:
2020-05-21 14:22:52 +02:00
parent da67899921
commit 2d7a2be529
3 changed files with 46 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
import os, sets, random, httpClient, json, strformat import os, sets, random, httpClient, json, strformat
import gintro/[gdkpixbuf, gobject, gtk] import gintro/[gdkpixbuf, gobject]
import commands import commands
const const
@@ -28,7 +28,6 @@ type
var var
client = newHttpClient() ## For loading images from the web client = newHttpClient() ## For loading images from the web
verbose = true
######################## ########################
# Constructors # Constructors
@@ -64,8 +63,8 @@ proc newFileOpResult(file: string): FileOpResult =
# Utilities # Utilities
######################## ########################
proc log(msg: string) = proc log(ip: ImageProvider, msg: string) =
if verbose: echo msg if ip.verbose: echo msg
######################## ########################
# Image Provider procs # Image Provider procs
@@ -81,10 +80,10 @@ proc getFox(ip: ImageProvider): FileOpResult =
writeFile(dlFile, imageData) writeFile(dlFile, imageData)
return newFileOpResult(dlFile) return newFileOpResult(dlFile)
except JsonParsingError: except JsonParsingError:
log fmt"Error while fetching from fox API: {getCurrentExceptionMsg()}" ip.log fmt"Error while fetching from fox API: {getCurrentExceptionMsg()}"
return newFileOpResultError("Json parsing error") return newFileOpResultError("Json parsing error")
except KeyError: except KeyError:
log fmt"No image in downloaded data: {getCurrentExceptionMsg()}" ip.log fmt"No image in downloaded data: {getCurrentExceptionMsg()}"
return newFileOpResultError("No image from API") return newFileOpResultError("No image from API")
proc getLocalFile(ip: var ImageProvider): FileOpResult = proc getLocalFile(ip: var ImageProvider): FileOpResult =
@@ -94,12 +93,12 @@ proc getLocalFile(ip: var ImageProvider): FileOpResult =
if ip.files.len < 1: if ip.files.len < 1:
if ip.path == "": if ip.path == "":
return newFileOpResultError("No path for image loading") return newFileOpResultError("No path for image loading")
log "Reloading file list..." ip.log "Reloading file list..."
for file in walkDirRec(ip.path): for file in walkDirRec(ip.path):
let split = splitFile(file) let split = splitFile(file)
if ip.exts.contains(split.ext): if ip.exts.contains(split.ext):
ip.files.add(file) ip.files.add(file)
log fmt"Loaded {ip.files.len} files" ip.log fmt"Loaded {ip.files.len} files"
shuffle(ip.files) shuffle(ip.files)
# Remove the current file after # Remove the current file after
result = newFileOpResult(ip.files[0]) result = newFileOpResult(ip.files[0])

View File

@@ -1,5 +1,5 @@
import os, options, strformat import os, options, strformat
import gintro/[glib, gobject, gdkpixbuf, gtk, gio] import gintro/[glib, gobject, gtk, gio]
import gintro/gdk except Window import gintro/gdk except Window
import argparse except run import argparse except run
import providers, server, commands import providers, server, commands
@@ -21,12 +21,16 @@ var
window: ApplicationWindow window: ApplicationWindow
label: Label label: Label
# Server vor recieving commands from external tools # Server vor recieving commands from external tools
serverWorker: system.Thread[void] serverWorker: system.Thread[ServerArgs]
proc enumToStrings(en: typedesc): seq[string] = proc enumToStrings(en: typedesc): seq[string] =
for x in en: for x in en:
result.add $x result.add $x
proc log(things: varargs[string, `$`]) =
if args.verbose:
echo things.join()
proc notify(label: Label, message: string = "") = proc notify(label: Label, message: string = "") =
## Shows the notification box in the lower left corner. ## Shows the notification box in the lower left corner.
## If no message is passed, the box will be hidden ## If no message is passed, the box will be hidden
@@ -67,7 +71,7 @@ proc newArgs(): Option[Args] =
proc updateImage(image: Image): bool = proc updateImage(image: Image): bool =
## Updates the UI with a new image ## Updates the UI with a new image
try: try:
if (args.verbose): echo "Refreshing..." if (args.verbose): log "Refreshing..."
var wWidth, wHeight: int var wWidth, wHeight: int
window.getSize(wWidth, wHeight) window.getSize(wWidth, wHeight)
@@ -82,7 +86,7 @@ proc updateImage(image: Image): bool =
let let
e = getCurrentException() e = getCurrentException()
msg = getCurrentExceptionMsg() msg = getCurrentExceptionMsg()
echo "Got exception ", repr(e), " with message ", msg log "Got exception ", repr(e), " with message ", msg
return false return false
proc forceUpdate(action: SimpleAction; parameter: Variant; image: Image) = proc forceUpdate(action: SimpleAction; parameter: Variant; image: Image) =
@@ -103,17 +107,17 @@ proc checkServerChannel(image: Image): bool =
if tried.dataAvailable: if tried.dataAvailable:
let msg: CommandMessage = tried.msg let msg: CommandMessage = tried.msg
echo "Main app got message: ", msg.command log "Main app got message: ", msg.command
case msg.command case msg.command
of cRefresh: of cRefresh:
discard updateImage(image) discard updateImage(image)
of cTimeout: of cTimeout:
let val = msg.parameter.parseInt * 1000 let val = msg.parameter.parseInt * 1000
echo "Setting timeout to ", val log "Setting timeout to ", val
args.timeout = val args.timeout = val
else: else:
echo "Command ignored: ", msg.command log "Command ignored: ", msg.command
sleep(100) sleep(100)
result = true result = true
@@ -129,11 +133,11 @@ proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: Applicat
proc cleanUp(w: ApplicationWindow, app: Application) = proc cleanUp(w: ApplicationWindow, app: Application) =
## Stop the control server and exit the GTK application ## Stop the control server and exit the GTK application
echo "Stopping control server..." log "Stopping control server..."
closeServer() closeServer()
serverWorker.joinThread() serverWorker.joinThread()
chan.close() chan.close()
echo "Server stopped." log "Server stopped."
app.quit() app.quit()
proc quit(action: SimpleAction; parameter: Variant; app: Application) = proc quit(action: SimpleAction; parameter: Variant; app: Application) =
@@ -199,7 +203,8 @@ proc appActivate(app: Application) =
chan.open() chan.open()
## Start the server for handling incoming commands ## Start the server for handling incoming commands
createThread(serverWorker, runServer) let serverArgs = newServerArgs(args.verbose)
createThread(serverWorker, runServer, serverArgs)
discard idleAdd(checkServerChannel, image) discard idleAdd(checkServerChannel, image)
when isMainModule: when isMainModule:

View File

@@ -1,8 +1,20 @@
import net, json import net, json, strutils
import commands import commands
type
ServerArgs* = object of RootObj
verbose: bool
var var
chan*: Channel[CommandMessage] chan*: Channel[CommandMessage]
verbose: bool
proc newServerArgs*(verbose: bool): ServerArgs =
ServerArgs(verbose: verbose)
proc log(things: varargs[string, `$`]) =
if verbose:
echo things.join()
proc closeServer*() = proc closeServer*() =
## Sends a "Close" command to the server ## Sends a "Close" command to the server
@@ -12,36 +24,37 @@ proc closeServer*() =
socket.send(c.wrap) socket.send(c.wrap)
socket.close() socket.close()
proc runServer*() = proc runServer*[ServerArgs](arg: ServerArgs) {.thread, nimcall.} =
var server = newSocket() verbose = arg.verbose
var server = net.newSocket()
server.bindAddr(Port(defaultPort)) server.bindAddr(Port(defaultPort))
server.listen() server.listen()
echo "Control server is listening" log "Control server is listening"
while true: while true:
# Process client requests # Process client requests
var client = newSocket() var client = net.newSocket()
server.accept(client) server.accept(client)
echo("Client connected") log "Client connected"
try: try:
var line = client.recvLine() var line = client.recvLine()
if line == "": if line == "":
echo "No data from client" log "No data from client"
continue continue
var jsonData = parseJson(line) var jsonData = parseJson(line)
let msg = jsonData.to(CommandMessage) let msg = jsonData.to(CommandMessage)
case msg.command case msg.command
of cClose: of cClose:
echo "Server recieved termination command. Exiting." log "Server recieved termination command. Exiting."
break break
else: else:
# Pass command from client to main applicaiton # Pass command from client to main applicaiton
chan.send(msg) chan.send(msg)
except OSError: except OSError:
echo "Server error: ", getCurrentExceptionMsg() log "Server error: ", getCurrentExceptionMsg()
except: except:
echo "Invalid command from client: ", getCurrentExceptionMsg() log "Invalid command from client: ", getCurrentExceptionMsg()
echo repr(getCurrentException()) log repr(getCurrentException())
server.close() server.close()