Initial commit

This commit is contained in:
PhilipTrauner 2015-10-25 17:32:23 +01:00
commit d9895ec3f9
11 changed files with 288 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
.DS_Store
*.pyc
__pycache__

6
.gitmodules vendored Normal file
View file

@ -0,0 +1,6 @@
[submodule "Shared"]
path = Shared
url = git://github.com/PhilipTrauner/fl0w-Shared.git
[submodule "Sublime/fl0w/Shared"]
path = Sublime/fl0w/Shared
url = git://github.com/PhilipTrauner/fl0w-Shared.git

0
Link/__init__.py Normal file
View file

2
Server.py Normal file
View file

@ -0,0 +1,2 @@
import fl0w
import Server.Server

68
Server/AsyncServer.py Normal file
View file

@ -0,0 +1,68 @@
from ESock import ESock
import DataTypes
import socket
import _thread
class Server:
def __init__(self, host, port, handler, handler_args=[]):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind((host, port))
self.sock.listen(2)
self.handler = handler
self.broadcast = Server.Broadcast()
while 1:
sock, info = self.sock.accept()
_thread.start_new_thread(self.controller, (sock, info, handler_args))
def controller(self, sock, info, handler_args):
sock = ESock(sock)
handler = self.handler(sock, info, self.broadcast, **handler_args)
self.broadcast.add(sock)
while 1:
try:
data, type = handler.sock.recv()
handler.handle(data, type)
except (socket.error, OSError):
self.broadcast.remove(handler.sock)
handler.finish()
handler.sock.close()
_thread.exit()
class Handler:
def __init__(self, sock, info, broadcast, **kwargs):
self.sock = sock
self.info = info
self.broadcast = broadcast
self.kwargs = kwargs
self.setup()
def setup(self):
pass
def handle(self, data, type):
pass
def finish(self):
pass
class Broadcast:
def __init__(self):
self.socks = []
def broadcast(self, data, exclude=[]):
for sock in self.socks:
if not sock in exclude:
sock.send(data)
def remove(self, sock):
if sock in self.socks:
del self.socks[self.socks.index(sock)]
def add(self, sock):
if not sock in self.socks:
self.socks.append(sock)

47
Server/Server.py Normal file
View file

@ -0,0 +1,47 @@
import Logging
import DataTypes
from .AsyncServer import Server
class vHandler(Server.Handler):
def setup(self):
Logging.info("Handler for '%s' initalised." % self.info[0])
self.authed = False
self.users = self.kwargs.pop("users")
def handle(self, data, type):
if not self.authed:
if type == DataTypes.json:
self.auth(data)
def finish(self):
Logging.info("%s disconnected." % self.info[0])
def auth(self, collection):
if "auth" in collection:
if "user" and "pw" in collection["auth"]:
temp_user = User(collection["auth"]["user"], collection["auth"]["pw"])
if temp_user in self.users:
self.sock.send({"auth" : "success"})
self.authed = True
return
self.sock.close()
class User:
def __init__(self, username, password):
self.username = username
self.password = password
def __eq__(self, other):
if self.username == other.username and self.password == other.password:
return True
return False
def __repr__(self):
return self.username
server = Server("127.0.0.1", 1337, vHandler, {"users" : [User("test", "123")]})

0
Server/__init__.py Normal file
View file

View file

@ -0,0 +1,6 @@
[
{
"caption": "fl0w",
"command": "fl0w"
}
]

View file

@ -0,0 +1,85 @@
class Input:
def __init__(self, caption, initial_text="", on_done=None, on_change=None, on_cancel=None):
self.caption = caption
self.initial_text = initial_text
self.on_done = on_done
self.on_change = on_change
self.on_cancel = on_cancel
def invoke(self, window):
window.show_input_panel(self.caption, self.initial_text,
self.on_done, self.on_change, self.on_cancel)
class Item:
def __init__(self, name, description="", action=None, items=None, input=None):
self.name = name
self.description = description
self.action = action
self.items = items
self.input = input
def __eq__(self, other):
return self.__dict__ == other.__dict__
class Items:
def __init__(self, selected_index=-1, on_highlight=None):
self.selected_index = selected_index
self.on_highlight = on_highlight
self.items = {}
self.item_names = []
self.window = None
self.back = None
def invoke(self, window, back=None):
self.window = window
if back:
items = [["Back", "Back to previous menu"]] + self.item_names[:]
print(items)
self.back = back
else:
items = self.item_names
self.back = None
window.show_quick_panel(items, self._action,
flags=0, selected_index=self.selected_index,
on_highlight=self.on_highlight)
def _action(self, item_id):
if item_id != -1:
if self.back:
if item_id != 0:
item = self.items[item_id - 1]
else:
self.back.invoke(self.window)
return
else:
item = self.items[item_id]
if item.action != None:
item.action()
if item.input != None:
item.input.invoke(self.window)
if item.items != None:
item.items.invoke(self.window, back=self)
def add_item(self, item):
if len(self.items) > 0:
item_id = tuple(self.items.keys())[-1] + 1
else:
item_id = 0
self.items[item_id] = item
if item.description != None:
self.item_names.append([item.name, item.description])
else:
self.item_names.append([item.name, ""])
def rm_item(self, item):
if item in self.items.values():
for i in self.items:
if self.items[i] == item:
del self.items[i]
del self.item_names[i]
return True
return False

67
Sublime/fl0w/fl0w.py Normal file
View file

@ -0,0 +1,67 @@
from sys import path
import os
path.append(os.path.dirname(os.path.realpath(__file__)))
import sublime
import sublime_plugin
import random
from SublimeMenu import *
class Fl0w:
def __init__(self):
self.window = None
self.main_menu = Items()
self.link_menu = Items()
self.link_menu.add_item(Item("Compile", action=self.info))
self.main_menu.add_item(Item("Connect", "Connect to a fl0w server", action=self.connect))
self.main_menu.add_item(Item("Set Link", items=self.link_menu))
def info(self):
sublime.message_dialog("No")
def connect(self):
Input("IP:Port", on_done=self.set_ip).invoke(self.window)
Input("User:", on_done=self.set_ip).invoke(self.window)
Input("Password:", on_done=self.set_ip).invoke(self.window)
def set_ip(self, ip_pair):
print(ip_pair)
def authed(self):
self.main_menu.add_item(Item("Compile", action=self.info))
self.main_menu.add_item(Item("Resync", action=self.info))
self.main_menu.add_item(Item("Disconnect", action=self.info))
self.main_menu.add_item(Item("Backup", action=self.info))
class Fl0wCommand(sublime_plugin.WindowCommand):
def run(self, menu=None, action=None):
if fl0w.window == None:
fl0w.window = self.window
fl0w.main_menu.invoke(self.window)
"""
def test():
print("Testing :3")
def test_input(user_input):
print(user_input)
item_handler = Items()
sub_item_handler = Items()
sub0_test = Item("Test", description="test", action=lol, items=sub_item_handler)
sub0_test1 = Item("Test1", action=test)
sub1_test1 = Item("Test1", action=test, input=Input("Test:", initial_text="Test", on_done=test_input))
sub_item_handler.add_item(sub1_test1)
item_handler.add_item(sub0_test)
item_handler.add_item(sub0_test1)
"""
fl0w = Fl0w()

3
fl0w.py Normal file
View file

@ -0,0 +1,3 @@
from sys import path
from os.path import abspath
path.append(abspath("Shared"))