Skip to content

Commit

Permalink
Finished first dirty prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
miloserdow committed Jan 14, 2018
1 parent ced947b commit d68820a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 20 deletions.
5 changes: 0 additions & 5 deletions driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ def __init__(self):
self.logger = logging.getLogger('leet')
self.logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('/tmp/leet.log')
#ch = logging.StreamHandler()
fh.setLevel(logging.DEBUG)
#ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
#ch.setFormatter(formatter)
self.logger.addHandler(fh)
#self.logger.addHandler(ch)

def log(self, msg):
self.logger.info(msg)

if __name__ == '__main__':
logger = LeetLogger()
curses.wrapper(MainWindow, logger)
#print(logger)
21 changes: 16 additions & 5 deletions file_browser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/usr/bin/env python3

# Copyright by CCEx under GPLv3 license (c) 2017

import curses
import time
import random
Expand Down Expand Up @@ -95,9 +99,10 @@ def traverse(self):
yield kid, depth + 1

class FileBrowser:
def __init__(self, stdscreen):
def __init__(self, stdscreen, logger):
self.window = stdscreen.subwin(0,0)
self.window.keypad(1)
self.logger = logger
cgitb.enable(format="text")
self.ESC = 27
self.result = ''
Expand All @@ -122,8 +127,13 @@ def display(self):
getattr(data, pending_action)()
pending_action = None
elif pending_save:
self.result = data.name
return
if os.path.isdir(data.name):
pending_save = False
if data.name != '.' or data.name != '..':
getattr(data, 'expand')()
else: # result is file, ok!
self.result = data.name
return
else:
self.window.attrset(curses.color_pair(0))
if 0 <= line - offset < curses.LINES - 1:
Expand All @@ -141,8 +151,9 @@ def display(self):
pending_action = 'expand'
elif ch == curses.KEY_LEFT:
pending_action = 'collapse'
elif ch == self.ESC:
elif ch == self.ESC:
os.system("clear")
return
elif ch == ord('\n'):
elif ch == ord('\n'):
pending_save = True
curidx %= line
70 changes: 60 additions & 10 deletions ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,37 @@
# Copyright by CCEx under GPLv3 license (c) 2017

import curses
import os
import json
from crypto import CaesarCipher
from curses import panel
from curses.textpad import Textbox, rectangle
from file_browser import FileBrowser
from crypto import file_as_str, save_to_file
from functools import partial

class WarningWindow:
def __init__(self, msg):
self.msg = msg

def display(self):
box = curses.newwin(8, 55, 0, 0)
box.addstr(1,1, self.msg)
box.addstr(3,1, "Press any key to continue")
box.border()
curses.doupdate()

# Get a key and clear it
box.getch()
box.erase()
os.system("clear")

class StringInput:
def __init__(self, msg):
self.msg = msg

def get_line(self) -> str:
curses.curs_set(1)
#curses.idlok(1)

inp = curses.newwin(8, 55, 0,0)
inp.addstr(1,1, self.msg)
Expand All @@ -31,6 +48,7 @@ def get_line(self) -> str:

text = tb.gather()
curses.curs_set(0)
curses.doupdate()
return text.strip()[:-1].strip()

class Menu(object):
Expand Down Expand Up @@ -92,19 +110,15 @@ class MainWindow(object):
def __init__(self, stdscreen, logger):
self.screen = stdscreen
self.logger = logger
self.keys = []
self.keys = self._readkeys()
self.selected_key = -1

curses.curs_set(0)

#si = StringInput()
#logger.log("Line: "+si.get_line())
#return

submenu_items = [
('Create new key', self.add_key),
('List keys', self.list_keys),
('Remove existing key', curses.beep)
('Remove existing key', self.delete_key)
]
submenu = Menu(submenu_items, self.screen)

Expand All @@ -116,8 +130,21 @@ def __init__(self, stdscreen, logger):
main_menu = Menu(main_menu_items, self.screen)
main_menu.display()

def _readkeys(self):
if not os.path.isfile('.keys.json'):
return [] # no keys saved
with open('.keys.json', 'r') as f:
return list(json.loads(f.read()))

def encrypt_file_dlg(self):
input_file = self.get_file()
if input_file in ['', ' ', '.', '..']:
return
if len(self.keys) == 0:
warn = WarningWindow("You must add at least one key first!")
warn.display()
return

self.logger.log('Encrypting '+input_file)
cs = CaesarCipher(self.keys[self.selected_key])
M = file_as_str(input_file)
Expand All @@ -130,6 +157,13 @@ def encrypt_file_dlg(self):

def decrypt_file_dlg(self):
input_file = self.get_file()
if input_file in ['', ' ', '.', '..']:
return
if len(self.keys) == 0:
warn = WarningWindow("You must add at least one key first!")
warn.display()
return

self.logger.log('Decrypting '+input_file)
cs = CaesarCipher(self.keys[self.selected_key])
C = file_as_str(input_file)
Expand All @@ -141,7 +175,7 @@ def decrypt_file_dlg(self):
self.logger.log('Saved plain text to ' + ofname)

def get_file(self):
fb = FileBrowser(self.screen)
fb = FileBrowser(self.screen, self.logger)
fb.display()
self.logger.log('Result of file selection window: '+fb.result)
return fb.result
Expand All @@ -158,13 +192,29 @@ def add_key(self):

self.keys.append(key)
self.selected_key = len(self.keys) - 1

with open('.keys.json', 'w') as f: # save key
f.write(json.dumps(self.keys))

def set_key(self, i):
self.selected_key = i
for ch in "Done":
self.screen.echochar(ch)
warn = WarningWindow("Key selected")
warn.display()

def list_keys(self):
key_list = [(key, partial(self.set_key, i)) for i, key in enumerate(self.keys)]
m = Menu(key_list, self.screen)
m.display()

def _del_key(self, key):
if key in self.keys:
self.keys.remove(key)
warn = WarningWindow("Key removed!")
warn.display()

def delete_key(self):
m = key_list = [(key, partial(self._del_key, key)) for key in self.keys]
m = Menu(key_list, self.screen)
m.display()
with open('.keys.json', 'w') as f: # save changes
f.write(json.dumps(self.keys))

0 comments on commit d68820a

Please sign in to comment.