Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ardublockly
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
ardublockly
Commits
af716146
Commit
af716146
authored
Apr 25, 2017
by
carlosperate
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update formatting of server std output
parent
dc9b6563
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
92 deletions
+84
-92
ardublocklyserver/compilersettings.py
ardublocklyserver/compilersettings.py
+48
-55
start.py
start.py
+36
-37
No files found.
ardublocklyserver/compilersettings.py
View file @
af716146
...
...
@@ -2,7 +2,7 @@
#
# Save and retrieve the compiler settings into a text file.
#
# Copyright (c) 201
5
carlosperate https://github.com/carlosperate/
# Copyright (c) 201
7
carlosperate https://github.com/carlosperate/
# Licensed under the Apache License, Version 2.0 (the "License"):
# http://www.apache.org/licenses/LICENSE-2.0
#
...
...
@@ -10,19 +10,12 @@
# writes the Arduino IDE settings into a file.
# On first invocation of the singleton it reads the settings from the file.
#
from
__future__
import
unicode_literals
,
absolute_import
from
__future__
import
unicode_literals
,
absolute_import
,
print_function
import
os
import
re
import
sys
import
codecs
#try:
# # 2.x name
# import ConfigParser
#except ImportError:
# # 3.x name
# import configparser as ConfigParser
# configparser port of the 3.5 version to support unicode across all versions
from
ardublocklyserver
import
configparser
import
ardublocklyserver.serialport
...
...
@@ -143,15 +136,15 @@ class ServerCompilerSettings(object):
'JavaApplicationStub'
)):
new_compiler_dir
=
os
.
path
.
join
(
new_compiler_dir
,
'Contents'
,
'MacOS'
,
'JavaApplicationStub'
)
print
(
'
\n
Compiler file in OS X located within the app bundle.'
)
print
(
'Compiler file in OS X located within the app bundle.'
)
elif
os
.
path
.
isfile
(
os
.
path
.
join
(
new_compiler_dir
,
'Contents'
,
'MacOS'
,
'Arduino'
)):
new_compiler_dir
=
os
.
path
.
join
(
new_compiler_dir
,
'Contents'
,
'MacOS'
,
'Arduino'
)
print
(
'
\n
Compiler file in OS X located within the app bundle.'
)
print
(
'Compiler file in OS X located within the app bundle.'
)
else
:
print
(
'Could not locate the Arduino executable within the OS
X
'
'app bundle. These are the available files:'
)
print
(
'Could not locate the Arduino executable within the OS '
'
X
app bundle. These are the available files:'
)
try
:
print
(
'%s'
%
os
.
listdir
(
'%s/Contents/MacOS/'
%
new_compiler_dir
))
...
...
@@ -161,11 +154,11 @@ class ServerCompilerSettings(object):
# Check directory
if
os
.
path
.
isfile
(
new_compiler_dir
):
self
.
__compiler_dir
=
new_compiler_dir
print
(
'
\n
Compiler directory set to:
\n\t
%s'
%
self
.
__compiler_dir
)
print
(
'Compiler directory set to:
\n\t
%s'
%
self
.
__compiler_dir
)
self
.
save_settings
()
else
:
print
(
'
\n
The provided compiler path is not valid !!!'
)
print
(
'
\t
'
+
new_compiler_dir
)
print
(
'
The provided compiler path is not valid !!!'
'
\n\t
%s'
%
new_compiler_dir
)
if
self
.
__compiler_dir
:
print
(
'Previous compiler path maintained:
\n\t
%s'
%
self
.
__compiler_dir
)
...
...
@@ -185,7 +178,7 @@ class ServerCompilerSettings(object):
if
os
.
path
.
exists
(
new_compiler_dir
):
self
.
__compiler_dir
=
new_compiler_dir
else
:
print
(
'
\n
The provided compiler path in the settings file is not '
+
print
(
'
The provided compiler path in the settings file is not '
'valid:'
)
print
(
'
\t
%s'
%
new_compiler_dir
)
self
.
set_compiler_dir_default
()
...
...
@@ -201,11 +194,11 @@ class ServerCompilerSettings(object):
""" Only accept letters, numbers, underscores and dashes. """
if
re
.
match
(
"^[\w\d_-]*$"
,
new_sketch_name
):
self
.
__sketch_name
=
new_sketch_name
print
(
'
\n
Sketch name set to:
\n\t
%s'
%
self
.
__sketch_name
)
print
(
'Sketch name set to:
\n\t
%s'
%
self
.
__sketch_name
)
self
.
save_settings
()
else
:
print
(
'
\n
Provided Sketch name contains invalid characters: !!!'
)
print
(
'
\t
%s'
%
new_sketch_name
)
print
(
'
Provided Sketch name contains invalid characters: !!!'
'
\n
\t
%s'
%
new_sketch_name
)
if
self
.
__sketch_name
:
print
(
'Previous Sketch name maintained:
\n\t
%s'
%
self
.
__sketch_name
)
...
...
@@ -225,8 +218,8 @@ class ServerCompilerSettings(object):
if
re
.
match
(
"^[\w\d_-]*$"
,
new_sketch_name
):
self
.
__sketch_name
=
new_sketch_name
else
:
print
(
'
\n
Settings file Sketch name contains invalid characters:'
)
print
(
'
\t
%s'
%
new_sketch_name
.
decode
(
"utf8"
))
print
(
'
Settings file Sketch name contains invalid characters:'
'
\n
\t
%s'
%
new_sketch_name
.
decode
(
"utf8"
))
self
.
set_sketch_name_default
()
print
(
'Default Sketch name set:
\n\t
%s'
%
self
.
__sketch_name
)
...
...
@@ -240,11 +233,11 @@ class ServerCompilerSettings(object):
""" The sketch directory must be a folder """
if
os
.
path
.
isdir
(
new_sketch_dir
):
self
.
__sketch_dir
=
new_sketch_dir
print
(
'
\n
Sketch directory set to:
\n\t
%s'
%
self
.
__sketch_dir
)
print
(
'Sketch directory set to:
\n\t
%s'
%
self
.
__sketch_dir
)
self
.
save_settings
()
else
:
print
(
'
\n
The provided sketch directory is not valid !!!'
)
print
(
'
\t
%s'
%
new_sketch_dir
)
print
(
'
The provided sketch directory is not valid !!!'
'
\n
\t
%s'
%
new_sketch_dir
)
if
self
.
__sketch_dir
:
print
(
'Previous Sketch directory maintained:
\n\t
%s'
%
self
.
__sketch_dir
)
...
...
@@ -265,8 +258,8 @@ class ServerCompilerSettings(object):
if
os
.
path
.
isdir
(
new_sketch_dir
):
self
.
__sketch_dir
=
new_sketch_dir
else
:
print
(
'
\n
Settings file sketch directory is not valid:'
)
print
(
'
\t
%s'
%
new_sketch_dir
)
print
(
'
Settings file sketch directory is not valid:'
'
\n
\t
%s'
%
new_sketch_dir
)
self
.
set_sketch_dir_default
()
print
(
'Default Sketch directory set:
\n\t
%s'
%
self
.
__sketch_dir
)
...
...
@@ -280,11 +273,11 @@ class ServerCompilerSettings(object):
if
new_board
in
self
.
__arduino_types
:
self
.
__arduino_board_value
=
self
.
__arduino_types
[
new_board
]
self
.
__arduino_board_key
=
new_board
print
(
'
\n
Arduino Board set to:
\n\t
%s'
%
self
.
__arduino_board_key
)
print
(
'Arduino Board set to:
\n\t
%s'
%
self
.
__arduino_board_key
)
self
.
save_settings
()
else
:
print
(
'
\n
Provided Arduino Board does not exist: !!!'
)
print
(
'
\t
%s'
%
new_board
)
print
(
'
Provided Arduino Board does not exist: !!!'
'
\n
\t
%s'
%
new_board
)
if
self
.
__arduino_board_key
and
self
.
__arduino_board_value
:
print
(
'Previous Arduino board type maintained:
\n\t
%s'
%
self
.
__arduino_board_key
)
...
...
@@ -306,8 +299,8 @@ class ServerCompilerSettings(object):
self
.
__arduino_board_value
=
self
.
__arduino_types
[
new_board
]
self
.
__arduino_board_key
=
new_board
else
:
print
(
'
\n
Settings file Arduino Board does not exist:'
)
print
(
'
\t
%s'
%
new_board
)
print
(
'
Settings file Arduino Board does not exist:
\n\t
%s'
%
new_board
)
self
.
set_arduino_board_default
()
print
(
'Default Arduino board type set:
\n\t
%s'
%
self
.
__arduino_board_key
)
...
...
@@ -334,12 +327,12 @@ class ServerCompilerSettings(object):
"""
self
.
populate_serial_port_list
()
if
not
self
.
__serial_ports
:
print
(
'
\n
There are no available Serial Ports !!!'
)
print
(
'There are no available Serial Ports !!!'
)
self
.
__serial_port_key
=
None
self
.
__serial_port_value
=
None
self
.
save_settings
()
elif
self
.
__serial_port_value
not
in
self
.
__serial_ports
.
values
():
print
(
'
\n
The selected Serial Port is no longer available !!!'
)
print
(
'The selected Serial Port is no longer available !!!'
)
self
.
__serial_port_key
=
None
self
.
__serial_port_value
=
None
self
.
save_settings
()
...
...
@@ -366,18 +359,18 @@ class ServerCompilerSettings(object):
# Now we check if the Port is still available
self
.
populate_serial_port_list
()
if
not
self
.
__serial_ports
:
print
(
'
\n
There are no available Serial Ports !!!'
)
print
(
'There are no available Serial Ports !!!'
)
self
.
__serial_port_key
=
None
self
.
__serial_port_value
=
None
elif
self
.
__serial_port_value
not
in
self
.
__serial_ports
.
values
():
print
(
'
\n
The selected Serial Port is no longer available !!!'
)
print
(
'The selected Serial Port is no longer available !!!'
)
self
.
__serial_port_key
=
None
self
.
__serial_port_value
=
None
print
(
'
\n
Serial Port set to:
\n\t
%s'
%
self
.
__serial_port_value
)
print
(
'Serial Port set to:
\n\t
%s'
%
self
.
__serial_port_value
)
self
.
save_settings
()
else
:
print
(
'
\n
Provided Serial Port is not valid: !!!'
)
print
(
'
\t
%s'
%
new_port
)
print
(
'
Provided Serial Port is not valid: !!!'
'
\n
\t
%s'
%
new_port
)
if
self
.
__serial_port_key
and
self
.
__serial_port_value
:
print
(
'Previous Serial Port maintained:
\n\t
%s'
%
self
.
__serial_port_value
)
...
...
@@ -420,8 +413,8 @@ class ServerCompilerSettings(object):
self
.
__serial_port_value
=
value
set_default
=
False
if
set_default
:
print
(
'
\n
Settings file Serial Port is not currently available:'
)
print
(
'
\t
%s'
%
new_port_value
)
print
(
'
Settings file Serial Port is not currently available:'
'
\n
\t
%s'
%
new_port_value
)
self
.
set_serial_port_default
()
print
(
'Default Serial Port set:
\n\t
%s'
%
self
.
__serial_port_value
)
...
...
@@ -434,12 +427,12 @@ class ServerCompilerSettings(object):
"""
self
.
populate_serial_port_list
()
if
not
self
.
__serial_ports
:
print
(
'
\n
There are no available Serial Ports !!!'
)
print
(
'There are no available Serial Ports !!!'
)
self
.
__serial_port_key
=
None
self
.
__serial_port_value
=
None
self
.
save_settings
()
elif
self
.
__serial_port_value
not
in
self
.
__serial_ports
.
values
():
print
(
'
\n
The selected Serial Port is no longer available !!!'
)
print
(
'The selected Serial Port is no longer available !!!'
)
self
.
__serial_port_key
=
None
self
.
__serial_port_value
=
None
self
.
save_settings
()
...
...
@@ -481,12 +474,12 @@ class ServerCompilerSettings(object):
def
set_load_ide
(
self
,
new_load_option
):
if
new_load_option
in
self
.
__ide_load_options
:
self
.
__load_ide_option
=
new_load_option
print
(
'
\n
IDE options set to:
\n\t
%s'
%
print
(
'IDE options set to:
\n\t
%s'
%
self
.
__ide_load_options
[
self
.
__load_ide_option
])
self
.
save_settings
()
else
:
print
(
'
\n
The provided "Load IDE option" is not valid !!!'
)
print
(
'
\t
%s'
%
new_load_option
)
print
(
'
The provided "Load IDE option" is not valid !!!'
'
\n
\t
%s'
%
new_load_option
)
if
self
.
__load_ide_option
:
print
(
'Previous "Load IDE option" maintained:
\n\t
%s'
%
self
.
__ide_load_options
[
self
.
__load_ide_option
])
...
...
@@ -506,8 +499,8 @@ class ServerCompilerSettings(object):
if
new_load_option
in
self
.
__ide_load_options
:
self
.
__load_ide_option
=
new_load_option
else
:
print
(
'
\n
Settings file "Load IDE option" is not valid:'
)
print
(
'
\t
%s'
%
new_load_option
)
print
(
'
Settings file "Load IDE option" is not valid:'
'
\n
\t
%s'
%
new_load_option
)
self
.
set_load_ide_default
()
print
(
'Default "Load IDE option" set:
\n\t
%s'
%
self
.
__load_ide_option
)
...
...
@@ -583,18 +576,18 @@ class ServerCompilerSettings(object):
self
.
set_sketch_dir_from_file
(
settings_dict
[
'sketch_directory'
])
self
.
set_load_ide_from_file
(
settings_dict
[
'ide_load'
])
else
:
print
(
'
\n
Settings will be set to the default values.'
)
print
(
'Settings will be set to the default values.'
)
self
.
set_default_settings
()
# Printing the settings to be able to easily spot issues at load
print
(
'
\n
Final settings loaded:'
)
print
(
'Final settings loaded:'
)
print
(
'
\t
Compiler directory: %s'
%
self
.
__compiler_dir
)
print
(
'
\t
Arduino Board Key: %s'
%
self
.
__arduino_board_key
)
print
(
'
\t
Arduino Board Value: %s'
%
self
.
__arduino_board_value
)
print
(
'
\t
Serial Port Value: %s'
%
self
.
__serial_port_value
)
print
(
'
\t
Sketch Name: %s'
%
self
.
__sketch_name
)
print
(
'
\t
Sketch Directory: %s'
%
self
.
__sketch_dir
)
print
(
'
\t
Load IDE option: %s
\n
'
%
self
.
__load_ide_option
)
print
(
'
\t
Load IDE option: %s'
%
self
.
__load_ide_option
)
# The read X_from_file() functions do not save new settings and neither
# does the set_default_settings() function, so save them either way.
...
...
@@ -623,11 +616,11 @@ class ServerCompilerSettings(object):
settings_parser
.
get
(
'Arduino_Sketch'
,
'sketch_directory'
)
settings_dict
[
'ide_load'
]
=
\
settings_parser
.
get
(
'Ardublockly'
,
'ide_load'
)
print
(
'
\n
Settings loaded from:'
)
print
(
'
Settings loaded from:
\n\t
%s'
%
self
.
__settings_path
)
except
Exception
as
e
:
print
(
'
\n
Settings file corrupted or not found in:'
)
print
(
'Settings file corrupted or not found in:
\n\t
%s'
%
self
.
__settings_path
)
settings_dict
=
None
print
(
'
\t
%s'
%
self
.
__settings_path
)
return
settings_dict
def
delete_settings_file
(
self
):
...
...
start.py
View file @
af716146
#!/usr/bin/env python
2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Entry point for the ArdublocklyServer application.
#
# Copyright (c) 201
5
carlosperate https://github.com/carlosperate/
# Copyright (c) 201
7
carlosperate https://github.com/carlosperate/
# Licensed under the Apache License, Version 2.0 (the "License"):
# http://www.apache.org/licenses/LICENSE-2.0
#
from
__future__
import
unicode_literals
,
absolute_import
from
__future__
import
unicode_literals
,
absolute_import
,
print_function
import
os
import
re
import
sys
...
...
@@ -22,8 +22,9 @@ import ardublocklyserver.compilersettings
def
open_browser
(
open_file
):
"""
Start a browser in a separate thread after waiting for half a second.
"""Start a browser in a separate thread after waiting for half a second.
:param open_file: URL for the browser to open.
"""
def
_open_browser
():
webbrowser
.
get
().
open
(
'http://localhost:%s/%s'
%
...
...
@@ -34,40 +35,37 @@ def open_browser(open_file):
def
find_ardublockly_dir
(
search_path
):
"""
Navigates within each node of a given path and tries to find the Ardublockly
project root directory. It assumes that the project root with have an folder
"""Find the Ardublockly project directory absolute path.
Navigates within each node of given path and tries to find the Ardublockly
project root directory. Assumes that the project root will have an folder
name ardublockly with an index.html file inside.
This function is required because this script can end up in executable form
in different locations of the project folder, and a literal relative path
should not be assumed.
in different locations of the project folder depending on the platform.
:param search_path: Path in which to find the Ardublockly root project
folder.
:return: Path to the Ardublockly root folder.
If not found returns None.
:param search_path: Path starting point to search the Ardublockly project
root folder.
:return: Path to the Ardublockly root folder. If not found returns None.
"""
path_to_navigate
=
os
.
path
.
normpath
(
search_path
)
# Navigate through each path node starting at the bottom until there are
# no folders left
# Navigate through each path node from the bottom up
while
path_to_navigate
:
# Check if file
/ardublo
kly/index.html exists within current path
# Check if file
ardubloc
kly/index.html exists within current path
if
os
.
path
.
isfile
(
os
.
path
.
join
(
path_to_navigate
,
'ardublockly'
,
'index.html'
)):
# Found the right folder
, return it
# Found the right folder
return
path_to_navigate
path_to_navigate
=
os
.
path
.
dirname
(
path_to_navigate
)
# The right folder wasn't found, so return input path
# The right folder wasn't found, so return None to indicate failure
return
None
def
parsing_cl_args
():
"""
Processes the command line arguments. Arguments supported:
"""Process the command line arguments.
Arguments supported:
-h / --help
-s / --serverroot <working directory>
:return: Dictionary with available options(keys) and value(value).
"""
# Set option defaults
...
...
@@ -100,41 +98,42 @@ def parsing_cl_args():
# fails silently maintaining the current working directory.
# Use regular expressions to catch this corner case.
if
re
.
match
(
"^[a-zA-Z]:$"
,
arg
):
print
(
'The windows drive letter needs to end in a slash, '
+
print
(
'The windows drive letter needs to end in a slash, '
'eg. %s
\\
'
%
arg
)
sys
.
exit
(
1
)
# Check if the value is a valid directory
arg
=
os
.
path
.
normpath
(
arg
)
if
os
.
path
.
isdir
(
arg
):
server_root
=
arg
print
(
'Parsed "%s" flag with "%s" value.'
%
(
opt
,
arg
))
print
(
'Parsed "%s" flag with "%s" value.'
%
(
opt
,
arg
))
else
:
print
(
'Invalid directory "'
+
arg
+
'".'
)
sys
.
exit
(
1
)
elif
opt
in
(
'-b'
,
'--nobrowser'
):
launch_browser
=
False
print
(
'Parsed "%s" flag. No browser will be opened.'
%
opt
)
print
(
'Parsed "%s" flag. No browser will be opened.'
%
opt
)
elif
opt
in
(
'-f'
,
'--findprojectroot'
):
find_project_root
=
True
print
(
'Parsed "%s" flag. The ardublockly project root will be '
'set as the server root directory.'
%
opt
)
print
(
'Parsed "%s" flag. The ardublockly project root will be '
'set as the server root directory.'
%
opt
)
else
:
print
(
'Flag
'
+
opt
+
' not recognised.'
)
print
(
'Flag
"%s" not recognised.'
%
opt
)
return
find_project_root
,
launch_browser
,
server_root
def
main
():
"""
"""Main entry point for the application.
Initialises the Settings singleton, resolves paths, and starts the server.
"""
print
(
'Running Python %s (%s bit) on %s'
%
(
platform
.
python_version
(),
(
struct
.
calcsize
(
'P'
)
*
8
),
platform
.
platform
()))
print
(
'
\n
\n
======= Parsing Command line arguments =======
\n
'
)
print
(
'
\n
======= Parsing Command line arguments =======
'
)
find_project_root
,
launch_browser
,
server_root
=
parsing_cl_args
()
print
(
'
\n
\n
======= Resolving server and project paths =======
\n
'
)
print
(
'
\n
======= Resolving server and project paths =======
'
)
# Based on command line options, set the server root to the ardublockly
# project root directory, a directory specified in the arguments, or by
# default to the project root directory.
...
...
@@ -144,7 +143,7 @@ def main():
print
(
'The Ardublockly project root folder could not be found within '
'the %s directory !'
%
this_file_dir
)
sys
.
exit
(
1
)
print
(
"Ardublockly root directory: %s"
%
ardublockly_root_dir
)
print
(
'Ardublockly root directory: %s'
%
ardublockly_root_dir
)
if
find_project_root
is
True
or
server_root
is
None
:
server_root
=
ardublockly_root_dir
...
...
@@ -153,19 +152,19 @@ def main():
if
not
os
.
path
.
commonprefix
([
server_root
,
ardublockly_root_dir
]):
print
(
'The Ardublockly project folder needs to be accessible from '
'the server root directory !'
)
print
(
"Selected server root: %s"
%
server_root
)
print
(
'Selected server root: %s'
%
server_root
)
print
(
'
\n
\n
======= Loading Settings ======='
)
print
(
'
\n
======= Loading Settings ======='
)
# ServerCompilerSettings is a singleton, no need to save instance
ardublocklyserver
.
compilersettings
.
ServerCompilerSettings
(
ardublockly_root_dir
)
print
(
'
\n
\n
======= Starting Server =======
\n
'
)
print
(
'
\n
======= Starting Server =======
'
)
if
launch_browser
:
# Find the relative path from server root to ardublockly html
ardublockly_html_dir
=
os
.
path
.
join
(
ardublockly_root_dir
,
'ardublockly'
)
relative_path
=
os
.
path
.
relpath
(
ardublockly_html_dir
,
server_root
)
print
(
"Ardublockly page relative path from server root: %s"
%
print
(
'Ardublockly page relative path from server root:
\n\t
/%s/'
%
relative_path
)
open_browser
(
relative_path
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment