Skip to content

Commit

Permalink
Merge pull request #26 from flux3dp/dev
Browse files Browse the repository at this point in the history
Release 0.8b13
  • Loading branch information
yen-von committed Mar 16, 2016
2 parents e4ce5cf + b51b896 commit c55f92f
Show file tree
Hide file tree
Showing 21 changed files with 178 additions and 135 deletions.
2 changes: 1 addition & 1 deletion fluxclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ def check_pcl():
return False


__version__ = "0.8b12"
__version__ = "0.8b13"
SUPPORT_PCL = check_pcl()
6 changes: 5 additions & 1 deletion fluxclient/commands/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
import getpass
import sys

from fluxclient.commands.misc import get_or_create_default_key
from fluxclient.upnp.task import UpnpTask


def main():
parser = argparse.ArgumentParser(description='flux printer config tool')
parser.add_argument(dest='uuid', type=str, help='Device UUID')
parser.add_argument('--key', dest='clientkey', type=str, default=None,
help='Client identify key (A RSA pem)')

options = parser.parse_args()
options.clientkey = get_or_create_default_key(options.clientkey)

uuid = UUID(hex=options.uuid)
task = UpnpTask(uuid)
task = UpnpTask(uuid, options.clientkey)

sys.stdout.write("""UUID: %s
Serial: %s
Expand Down
9 changes: 6 additions & 3 deletions fluxclient/commands/config_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
import sys
import re

from fluxclient.commands.misc import get_or_create_default_key
from fluxclient.upnp.misc import parse_network_config as parse_network
from fluxclient.usb.misc import is_serial_port
from fluxclient.upnp.task import UpnpTask
from fluxclient.usb.task import UsbTask, UsbTaskError

logging.basicConfig(format="%(message)s", stream=sys.stdout)
logger = logging.getLogger('')
logger = logging.getLogger(__name__)


def is_uuid(input):
Expand Down Expand Up @@ -59,11 +60,13 @@ def main():
help='Route, example: 192.168.1.1')
parser.add_argument('--dns', dest='ns', type=str, default=None,
help='Route, example: 192.168.1.1')

parser.add_argument(dest='target', type=str,
help='Printer Serial ID or serial port')
parser.add_argument('--key', dest='clientkey', type=str, default=None,
help='Client identify key (A RSA pem)')

opt = parser.parse_args()
opt.clientkey = get_or_create_default_key(options.clientkey)
if opt.debug:
logger.setLevel(logging.DEBUG)
else:
Expand All @@ -78,7 +81,7 @@ def main():
if is_uuid(opt.target):
logger.info("Config network over LAN...")

task = UpnpTask(opt.target)
task = UpnpTask(opt.target, opt.clientkey)
task.require_auth()

if not try_lan_config(task, c):
Expand Down
2 changes: 1 addition & 1 deletion fluxclient/commands/laser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from PIL import Image

logging.basicConfig(format="\033[1;93m%(message)s\033[0m", stream=sys.stderr)
logger = logging.getLogger('')
logger = logging.getLogger(__name__)


def process_svg(options, stream):
Expand Down
24 changes: 24 additions & 0 deletions fluxclient/commands/misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import os

from fluxclient.encryptor import KeyObject


def get_or_create_default_key(path=None):
if path is None:
path = os.path.expanduser("~/.fluxclient_key.pem")

if os.path.exists(path):
try:
with open(path, "rb") as f:
buf = f.read()
return KeyObject.load_keyobj(buf)
except Exception:
raise
os.unlink(path)

key = KeyObject.get_or_create_keyobj(1024)
with open(path, "wb") as f:
f.write(key.private_key_pem)

return key
6 changes: 5 additions & 1 deletion fluxclient/commands/passwd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
import argparse
import sys

from fluxclient.commands.misc import get_or_create_default_key
from fluxclient.upnp.task import UpnpTask


def main():
parser = argparse.ArgumentParser(description='flux printer config tool')
parser.add_argument(dest='uuid', type=str, help='Printer UUID')
parser.add_argument('--key', dest='clientkey', type=str, default=None,
help='Client identify key (A RSA pem)')

options = parser.parse_args()
options.clientkey = get_or_create_default_key(options.clientkey)

uuid = UUID(hex=options.uuid)
task = UpnpTask(uuid)
task = UpnpTask(uuid, options.clientkey)

sys.stdout.write("""UUID: %s
Serial: %s
Expand Down
27 changes: 15 additions & 12 deletions fluxclient/commands/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import logging
import atexit
import sys
import re
import os

from fluxclient.robot.misc import require_robot
from fluxclient.robot_console import RobotConsole
from fluxclient.robot.misc import parse_ipaddr, kill_robot, require_robot
from fluxclient.robot import connect_robot
from fluxclient.commands.misc import get_or_create_default_key


def setup_logger(stdout=sys.stderr, level=logging.DEBUG):
logging.basicConfig(format="%(message)s", stream=stdout)
logger = logging.getLogger('')
logger = logging.getLogger(__name__)
logger.setLevel(level)
return logger

Expand All @@ -41,8 +41,10 @@ def conn_callback(*args):

try:
logger = setup_logger(console)
ipaddr, keyobj = require_robot(options.target, console)
ipaddr, keyobj = require_robot(options.target, options.clientkey,
console)
client = connect_robot(ipaddr=ipaddr, server_key=keyobj,
client_key=options.clientkey,
conn_callback=conn_callback)
robot_client = RobotConsole(client)
logger.info("----> READY")
Expand Down Expand Up @@ -70,8 +72,9 @@ def conn_callback(*args):
sys.stdout.flush()
return True

ipaddr, keyobj = require_robot(options.target)
robot_client = connect_robot(ipaddr=ipaddr, server_key=keyobj,
ipaddr, keyobj = require_robot(options.target, options.clientkey)
robot_client = connect_robot(ipaddr=ipaddr, server_key=keyobj, # noqa
client_key=options.clientkey,
conn_callback=conn_callback)

logger.info("----> READY")
Expand All @@ -88,8 +91,9 @@ def conn_callback(*args):
sys.stdout.flush()
return True

ipaddr, keyobj = require_robot(options.target)
ipaddr, keyobj = require_robot(options.target, options.clientkey)
client = connect_robot(ipaddr=ipaddr, server_key=keyobj,
client_key=options.clientkey,
conn_callback=conn_callback)
robot_client = RobotConsole(client)
logger.info("----> READY")
Expand All @@ -111,17 +115,16 @@ def main():
help="Printer connect with. It can be printer uuid "
"or IP address like 192.168.1.1 or "
"192.168.1.1:23811")
parser.add_argument('--kill', dest='do_kill', action='store_const',
const=True, default=False, help='Use python shell')
parser.add_argument('--ipython', dest='ipython', action='store_const',
const=True, default=False, help='Use python shell')
parser.add_argument('--simple', dest='simple', action='store_const',
const=True, default=False, help='Use python shell')
parser.add_argument('--key', dest='clientkey', type=str, default=None,
help='Client identify key (A RSA pem)')
options = parser.parse_args()
options.clientkey = get_or_create_default_key(options.clientkey)

if options.do_kill:
kill_robot(options.target)
elif options.ipython:
if options.ipython:
ipython_shell(options)
elif options.simple:
simple_shell(options)
Expand Down
15 changes: 10 additions & 5 deletions fluxclient/commands/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
from fluxclient.robot.misc import require_robot
from fluxclient.robot import connect_robot
from fluxclient.hw_profile import HW_PROFILE
from fluxclient.commands.misc import get_or_create_default_key


logging.basicConfig(format="%(message)s", stream=sys.stdout)
logger = logging.getLogger('')
logger = logging.getLogger(__name__)


def prepare_robot(endpoint, server_key):
def prepare_robot(endpoint, server_key, client_key):
def conn_callback(*args):
sys.stdout.write(".")
sys.stdout.flush()
return True
robot = connect_robot(endpoint, server_key, conn_callback)
robot = connect_robot(endpoint, server_key=server_key,
client_key=client_key, conn_callback=conn_callback)
pos = robot.position()
if pos == "CommandTask":
robot.begin_scan()
Expand Down Expand Up @@ -147,15 +149,18 @@ def main():
help="Printer connect with. It can be printer UUID "
"or IP address like 192.168.1.1 or "
"192.168.1.1:23811")
parser.add_argument('--key', dest='clientkey', type=str, default=None,
help='Client identify key (A RSA pem)')
options = parser.parse_args()
options.clientkey = get_or_create_default_key(options.clientkey)

if options.debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)

endpoint, server_key = require_robot(options.target)
robot = prepare_robot(endpoint, server_key)
endpoint, server_key = require_robot(options.target, options.clientkey)
robot = prepare_robot(endpoint, server_key, options.clientkey)
robot.set_scanlen(HW_PROFILE['model-1']['scan_full_len'] / 400.)

if not options.auto:
Expand Down
3 changes: 2 additions & 1 deletion fluxclient/commands/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from fluxclient.upnp.misc import parse_network_config

logging.basicConfig(format="%(message)s", stream=sys.stdout)
logger = logging.getLogger('')
logger = logging.getLogger(__name__)


def do_auth(task):
Expand Down Expand Up @@ -98,6 +98,7 @@ def do_scan_wifi(task):
print("%-30s %-4s %s" % (l.get("ssid"), l.get("rssi"),
l.get("security")))


def cmdline(task):
while True:
printhelp()
Expand Down
32 changes: 13 additions & 19 deletions fluxclient/encryptor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from io import BytesIO
from hashlib import sha1
import os
import warnings

from Crypto.Signature import PKCS1_v1_5
from Crypto.Cipher import PKCS1_OAEP
Expand All @@ -16,26 +16,16 @@ def load_keyobj(cls, pem_or_der):
return cls(ref)

@classmethod
def get_or_create_keyobj(cls, path=None):
if path is None:
path = os.path.expanduser("~/.fluxclient_key.pem")

if os.path.exists(path):
try:
with open(path, "rb") as f:
buf = f.read()
ref = RSA.importKey(buf)
return cls(ref)
except Exception:
raise
os.unlink(path)

ref = RSA.generate(1024)
with open(path, "wb") as f:
f.write(ref.exportKey("PEM"))

def new_keyobj(cls, size=4096):
ref = RSA.generate(size)
return cls(ref)

@classmethod
def get_or_create_keyobj(cls, path=None):
warnings.warn("get_or_create_keyobj is deprecated", DeprecationWarning)
from fluxclient.commands.misc import get_or_create_default_key
return get_or_create_default_key(path)

def __init__(self, ref_keyobj):
self._key = ref_keyobj

Expand Down Expand Up @@ -77,6 +67,10 @@ def public_key_pem(self):
def public_key_der(self):
return self._key.publickey().exportKey("DER")

@property
def private_key_pem(self):
return self._key.exportKey("PEM")

def sign(self, message):
chip = PKCS1_v1_5.new(self._key)
return chip.sign(CryptoSHA.new(message))
Expand Down
8 changes: 6 additions & 2 deletions fluxclient/fcode/fcode_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ def __init__(self):
self.filament_this_layer = [0., 0., 0.]
self.current_pos = [0.0, 0.0, HW_PROFILE['model-1']['height'], 0.0, 0.0, 0.0] # X, Y, Z, E1, E2, E3 -> recording the position of each axis
self.path = [[[0.0, 0.0, HW_PROFILE['model-1']['height'], 3]]] # recording the path extruder go through
self.path = [[]] # recording the path extruder go through
self.empty_layer = []
self.counter_between_layers = 0
self.record_z = 0.0

def get_path(self):
return self.path

def process_path(self, comment, move_flag, extrude_flag):
"""
convert to path list(for visualizing)
Expand All @@ -31,8 +35,6 @@ def process_path(self, comment, move_flag, extrude_flag):
if move_flag:
if 'infill' in comment:
line_type = 0
elif 'perimeter' in comment:
line_type = 1
elif 'support' in comment:
line_type = 2
elif 'move' in comment:
Expand All @@ -47,6 +49,8 @@ def process_path(self, comment, move_flag, extrude_flag):
self.layer_now = int(tmp)
self.path.append([self.path[-1][-1][:3] + [line_type]])
self.filament_this_layer = self.filament[:]
elif 'perimeter' in comment:
line_type = 1
elif 'skirt' in comment:
line_type = 4
elif 'draw' in comment:
Expand Down
12 changes: 9 additions & 3 deletions fluxclient/fcode/g_to_f.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from fluxclient.fcode.fcode_base import FcodeBase
from fluxclient.hw_profile import HW_PROFILE

logger = logging.getLogger("g_to_f")
logger = logging.getLogger(__name__)


class GcodeToFcode(FcodeBase):
Expand Down Expand Up @@ -50,6 +50,12 @@ def __init__(self, version=1, head_type="EXTRUDER", ext_metadata={}):

self.config = None

def get_metadata(self):
return self.md

def get_img(self):
return self.image

def header(self):
"""
simple header for fcode version 1
Expand Down Expand Up @@ -97,7 +103,7 @@ def XYZEF(self, input_list):
command |= (1 << (2 - self.tool))
number[4 + self.tool] = float(i[1:]) * self.unit
else:
print(i, file=sys.stderr)
logger.debug('XYZEF fail:' + i)

return command, number

Expand Down Expand Up @@ -307,7 +313,7 @@ def process(self, input_stream, output_stream):
if line[0] in ['M400']:
pass
else:
print('Undefine gcode', line, file=sys.stderr)
logger.debug('Undefine gcode: {}'.format(line))
# raise ValueError('Undefine gcode', line)
output_stream.write(struct.pack('<I', self.crc))
output_stream.seek(len(self.header()), 0)
Expand Down
Loading

0 comments on commit c55f92f

Please sign in to comment.