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
0b8ce08d
Commit
0b8ce08d
authored
Apr 25, 2017
by
carlosperate
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert server SketchCreator class into a simple Python module
parent
af716146
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
105 deletions
+86
-105
ardublocklyserver/actions.py
ardublocklyserver/actions.py
+8
-8
ardublocklyserver/sketchcreator.py
ardublocklyserver/sketchcreator.py
+65
-81
ardublocklyserver/tests/sketchcreator_test.py
ardublocklyserver/tests/sketchcreator_test.py
+13
-16
No files found.
ardublocklyserver/actions.py
View file @
0b8ce08d
...
@@ -2,11 +2,11 @@
...
@@ -2,11 +2,11 @@
#
#
# Collection of actions to the ardublocklyserver for relieved HTTP requests.
# Collection of actions to the ardublocklyserver for relieved HTTP requests.
#
#
# 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"):
# Licensed under the Apache License, Version 2.0 (the "License"):
# http://www.apache.org/licenses/LICENSE-2.0
# 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
subprocess
import
subprocess
import
locale
import
locale
import
time
import
time
...
@@ -25,7 +25,7 @@ except ImportError:
...
@@ -25,7 +25,7 @@ except ImportError:
import
tkinter.filedialog
as
tkFileDialog
import
tkinter.filedialog
as
tkFileDialog
from
ardublocklyserver.compilersettings
import
ServerCompilerSettings
from
ardublocklyserver.compilersettings
import
ServerCompilerSettings
from
ardublocklyserver
.sketchcreator
import
SketchC
reator
from
ardublocklyserver
import
sketchc
reator
import
ardublocklyserver.six.six.moves
as
six_moves
import
ardublocklyserver.six.six.moves
as
six_moves
from
ardublocklyserver.six
import
six
from
ardublocklyserver.six
import
six
import
ardublocklyserver.gui
as
gui
import
ardublocklyserver.gui
as
gui
...
@@ -145,15 +145,15 @@ def load_arduino_cli(sketch_path=None):
...
@@ -145,15 +145,15 @@ def load_arduino_cli(sketch_path=None):
def
create_sketch_default
():
def
create_sketch_default
():
settings
=
ServerCompilerSettings
()
settings
=
ServerCompilerSettings
()
return
SketchCreator
()
.
create_sketch
(
return
sketchcreator
.
create_sketch
(
settings
.
sketch_dir
,
sketch_name
=
settings
.
sketch_name
)
sketch_dir
=
settings
.
sketch_dir
,
sketch_name
=
settings
.
sketch_name
)
def
create_sketch_from_string
(
sketch_code
):
def
create_sketch_from_string
(
sketch_code
):
settings
=
ServerCompilerSettings
()
settings
=
ServerCompilerSettings
()
return
SketchCreator
()
.
create_sketch
(
return
sketchcreator
.
create_sketch
(
settings
.
sketch_dir
,
sketch_name
=
settings
.
sketch_name
,
sketch_dir
=
settings
.
sketch_dir
,
sketch_name
=
settings
.
sketch_name
,
sketch_code
=
sketch_code
)
sketch_code
=
sketch_code
)
#
#
...
...
ardublocklyserver/sketchcreator.py
View file @
0b8ce08d
...
@@ -2,100 +2,84 @@
...
@@ -2,100 +2,84 @@
#
#
# SketchCreator class creates an Arduino Sketch source code file.
# SketchCreator class creates an Arduino Sketch source code 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"):
# Licensed under the Apache License, Version 2.0 (the "License"):
# http://www.apache.org/licenses/LICENSE-2.0
# 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
codecs
import
codecs
import
os
import
os
from
ardublocklyserver.six
import
six
from
ardublocklyserver.six
import
six
# Default blinky sketch
default_sketch_code
=
"""int led = 13;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
"""
class
SketchCreator
(
object
):
# Default sketch name
"""
default_sketch_name
=
'ArdublocklySketch'
Creates an Arduino Sketch.
"""
#
# Metaclass methods
#
def
__init__
(
self
,
sketch_name
=
None
):
# Default sketch, blink builtin LED
self
.
_default_sketch_code
=
\
'int led = 13;
\n
'
\
'void setup() {
\n
'
\
' pinMode(led, OUTPUT);
\n
'
\
'}
\n
'
\
'void loop() {
\n
'
\
' digitalWrite(led, HIGH);
\n
'
\
' delay(1000);
\n
'
\
' digitalWrite(led, LOW);
\n
'
\
' delay(1000);
\n
'
\
'}
\n
'
# Default sketch name
self
.
_default_sketch_name
=
'ArdublocklySketch'
#
def
create_sketch
(
sketch_dir
,
sketch_name
=
None
,
sketch_code
=
None
):
# Creating files
"""Create an Arduino Sketch file into the given directory.
#
def
create_sketch
(
self
,
sketch_dir
,
sketch_name
=
None
,
sketch_code
=
None
):
"""
Creates the Arduino sketch with either the default blinky code or the
code defined in the input parameter.
:param sketch_dir: Location for the sketch.
:param sketch_name: Optional name for the sketch.
:param sketch_code: Optional unicode string with the code for the
sketch.
:return: Unicode string with full path to the sketch file
Return None indicates an error has occurred.
"""
# Check the code first, to not create sketch file if invalid
if
sketch_code
is
None
:
code_to_write
=
self
.
_default_sketch_code
else
:
if
isinstance
(
sketch_code
,
six
.
string_types
):
code_to_write
=
sketch_code
else
:
print
(
'The sketch code given is not a valid string !!!'
)
return
None
# Check validity and create the sketch path
Creates an Arduino sketch with either the default blinky code or the
if
sketch_name
is
None
:
code defined in the input parameter.
sketch_name
=
self
.
_default_sketch_name
sketch_path
=
self
.
build_sketch_path
(
sketch_dir
,
sketch_name
)
if
sketch_path
is
None
:
return
None
try
:
:param sketch_dir: Location for the sketch.
arduino_sketch
=
codecs
.
open
(
sketch_path
,
'wb+'
,
encoding
=
'utf-8'
)
:param sketch_name: Optional name for the sketch.
try
:
:param sketch_code: Optional unicode string with the code for the sketch.
arduino_sketch
.
write
(
code_to_write
)
:return: Unicode string with full path to the sketch file
finally
:
Return None indicates an error has occurred.
arduino_sketch
.
close
()
"""
except
Exception
as
e
:
# Check the code first, to not create sketch file if invalid
print
(
e
)
if
sketch_code
is
None
:
print
(
'Arduino sketch could not be created !!!'
)
code_to_write
=
default_sketch_code
else
:
if
isinstance
(
sketch_code
,
six
.
string_types
):
code_to_write
=
sketch_code
else
:
print
(
'The sketch code given is not a valid string !!!'
)
return
None
return
None
# Check validity and create the sketch path
if
sketch_name
is
None
:
sketch_name
=
default_sketch_name
sketch_path
=
build_sketch_path
(
sketch_dir
,
sketch_name
)
try
:
with
codecs
.
open
(
sketch_path
,
'wb+'
,
encoding
=
'utf-8'
)
as
sketch_f
:
sketch_f
.
write
(
code_to_write
)
except
Exception
as
e
:
print
(
e
)
print
(
'Arduino sketch could not be created !!!'
)
return
None
return
sketch_path
return
sketch_path
@
staticmethod
def
build_sketch_path
(
sketch_dir
,
sketch_name
):
def
build_sketch_path
(
sketch_dir
,
sketch_name
):
"""
"""Create the Arduino Sketch folder required for a valid Sketch.
If a valid directory is provided, it creates the Arduino sketch folder
(if it does not exists already) and returns a string pointing to the
If a valid directory is provided, it creates the Arduino sketch folder
sketch file path.
(if it does not exists already) and returns a string pointing to the
:return: unicode string with full path to the sketch file.
sketch file path.
Returns None indicates an error has occurred.
:return: unicode string with full path to the sketch file.
"""
Return None indicates an error has occurred.
sketch_path
=
None
"""
if
os
.
path
.
isdir
(
sketch_dir
):
sketch_path
=
None
sketch_path
=
os
.
path
.
join
(
sketch_dir
,
sketch_name
)
if
os
.
path
.
isdir
(
sketch_dir
):
if
not
os
.
path
.
exists
(
sketch_path
):
sketch_path
=
os
.
path
.
join
(
sketch_dir
,
sketch_name
)
os
.
makedirs
(
sketch_path
)
if
not
os
.
path
.
exists
(
sketch_path
):
sketch_path
=
os
.
path
.
join
(
sketch_path
,
sketch_name
+
'.ino'
)
os
.
makedirs
(
sketch_path
)
else
:
sketch_path
=
os
.
path
.
join
(
sketch_path
,
sketch_name
+
'.ino'
)
print
(
'The sketch directory "%s" does not exists !!!'
%
sketch_dir
)
else
:
return
sketch_path
print
(
'The sketch directory "%s" does not exists !!!'
%
sketch_dir
)
return
sketch_path
ardublocklyserver/tests/sketchcreator_test.py
View file @
0b8ce08d
...
@@ -14,14 +14,14 @@ import shutil
...
@@ -14,14 +14,14 @@ import shutil
import
unittest
import
unittest
try
:
try
:
from
ardublocklyserver
.sketchcreator
import
SketchC
reator
from
ardublocklyserver
import
sketchc
reator
from
ardublocklyserver.compilersettings
import
ServerCompilerSettings
from
ardublocklyserver.compilersettings
import
ServerCompilerSettings
except
ImportError
:
except
ImportError
:
import
sys
import
sys
file_dir
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
file_dir
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
package_dir
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
file_dir
))
package_dir
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
file_dir
))
sys
.
path
.
insert
(
0
,
package_dir
)
sys
.
path
.
insert
(
0
,
package_dir
)
from
ardublocklyserver
.sketchcreator
import
SketchC
reator
from
ardublocklyserver
import
sketchc
reator
from
ardublocklyserver.compilersettings
import
ServerCompilerSettings
from
ardublocklyserver.compilersettings
import
ServerCompilerSettings
...
@@ -36,18 +36,17 @@ class SketchCreatorTestCase(unittest.TestCase):
...
@@ -36,18 +36,17 @@ class SketchCreatorTestCase(unittest.TestCase):
def
test_create_sketch
(
self
):
def
test_create_sketch
(
self
):
""" Tests to see if an Arduino Sketch is created in a new location. """
""" Tests to see if an Arduino Sketch is created in a new location. """
# First test with the default name
# First test with the default name
sketch_creator
=
SketchCreator
()
sketch_dir
=
os
.
getcwd
()
sketch_dir
=
os
.
getcwd
()
final_ino_path
=
os
.
path
.
join
(
final_ino_path
=
os
.
path
.
join
(
sketch_dir
,
sketch_dir
,
sketch
_creator
.
_
default_sketch_name
,
sketch
creator
.
default_sketch_name
,
'%s.ino'
%
sketch
_creator
.
_
default_sketch_name
)
'%s.ino'
%
sketch
creator
.
default_sketch_name
)
#
It should be sav
e to create and delete the ino file in the test folder
#
Should be saf
e to create and delete the ino file in the test folder
if
os
.
path
.
exists
(
final_ino_path
):
if
os
.
path
.
exists
(
final_ino_path
):
os
.
remove
(
final_ino_path
)
os
.
remove
(
final_ino_path
)
self
.
assertFalse
(
os
.
path
.
isfile
(
final_ino_path
))
self
.
assertFalse
(
os
.
path
.
isfile
(
final_ino_path
))
# Checks the file is saved, and saved to the right location
# Checks the file is saved, and saved to the right location
created_sketch_path
=
sketch
_
creator
.
create_sketch
(
sketch_dir
)
created_sketch_path
=
sketchcreator
.
create_sketch
(
sketch_dir
)
self
.
assertEqual
(
final_ino_path
,
created_sketch_path
)
self
.
assertEqual
(
final_ino_path
,
created_sketch_path
)
self
.
assertTrue
(
os
.
path
.
isfile
(
final_ino_path
))
self
.
assertTrue
(
os
.
path
.
isfile
(
final_ino_path
))
...
@@ -70,7 +69,7 @@ class SketchCreatorTestCase(unittest.TestCase):
...
@@ -70,7 +69,7 @@ class SketchCreatorTestCase(unittest.TestCase):
self
.
assertFalse
(
os
.
path
.
isfile
(
final_ino_path
))
self
.
assertFalse
(
os
.
path
.
isfile
(
final_ino_path
))
# Checks the file is saved, and saved to the right location
# Checks the file is saved, and saved to the right location
created_sketch_path
=
sketch
_
creator
.
create_sketch
(
created_sketch_path
=
sketchcreator
.
create_sketch
(
sketch_dir_unicode
,
sketch_name
=
sketch_name
)
sketch_dir_unicode
,
sketch_name
=
sketch_name
)
self
.
assertEqual
(
final_ino_path
,
created_sketch_path
)
self
.
assertEqual
(
final_ino_path
,
created_sketch_path
)
self
.
assertTrue
(
os
.
path
.
isfile
(
final_ino_path
))
self
.
assertTrue
(
os
.
path
.
isfile
(
final_ino_path
))
...
@@ -85,20 +84,19 @@ class SketchCreatorTestCase(unittest.TestCase):
...
@@ -85,20 +84,19 @@ class SketchCreatorTestCase(unittest.TestCase):
# Test for failure on invalid sketch path
# Test for failure on invalid sketch path
random_invalid_path
=
os
.
path
.
join
(
os
.
getcwd
(),
'raNd_dIr'
)
random_invalid_path
=
os
.
path
.
join
(
os
.
getcwd
(),
'raNd_dIr'
)
self
.
assertFalse
(
os
.
path
.
isdir
(
random_invalid_path
))
self
.
assertFalse
(
os
.
path
.
isdir
(
random_invalid_path
))
sketch_creator
=
SketchCreator
()
created_sketch_path
=
sketchcreator
.
create_sketch
(
random_invalid_path
)
created_sketch_path
=
sketch_creator
.
create_sketch
(
random_invalid_path
)
self
.
assertIsNone
(
created_sketch_path
)
self
.
assertIsNone
(
created_sketch_path
)
self
.
assertFalse
(
os
.
path
.
isdir
(
random_invalid_path
))
self
.
assertFalse
(
os
.
path
.
isdir
(
random_invalid_path
))
# Test for failure on invalid sketch code
# Test for failure on invalid sketch code
sketch_path
=
os
.
getcwd
()
sketch_path
=
os
.
getcwd
()
sketch_folder_path
=
os
.
path
.
join
(
sketch_folder_path
=
os
.
path
.
join
(
sketch_path
,
sketch
_creator
.
_
default_sketch_name
)
sketch_path
,
sketch
creator
.
default_sketch_name
)
if
os
.
path
.
isdir
(
sketch_folder_path
):
if
os
.
path
.
isdir
(
sketch_folder_path
):
shutil
.
rmtree
(
sketch_folder_path
)
shutil
.
rmtree
(
sketch_folder_path
)
self
.
assertFalse
(
os
.
path
.
isdir
(
sketch_folder_path
))
self
.
assertFalse
(
os
.
path
.
isdir
(
sketch_folder_path
))
invalid_sketch_code
=
True
invalid_sketch_code
=
True
created_sketch_path
=
sketch
_
creator
.
create_sketch
(
created_sketch_path
=
sketchcreator
.
create_sketch
(
sketch_path
,
sketch_code
=
invalid_sketch_code
)
sketch_path
,
sketch_code
=
invalid_sketch_code
)
self
.
assertIsNone
(
created_sketch_path
)
self
.
assertIsNone
(
created_sketch_path
)
self
.
assertFalse
(
os
.
path
.
isdir
(
sketch_folder_path
))
self
.
assertFalse
(
os
.
path
.
isdir
(
sketch_folder_path
))
...
@@ -107,16 +105,15 @@ class SketchCreatorTestCase(unittest.TestCase):
...
@@ -107,16 +105,15 @@ class SketchCreatorTestCase(unittest.TestCase):
# File creation with code
# File creation with code
#
#
def
test_create_sketch_with_code
(
self
):
def
test_create_sketch_with_code
(
self
):
sketch_creator
=
SketchCreator
()
sketch_dir
=
os
.
getcwd
()
sketch_dir
=
os
.
getcwd
()
sketch_ino_location
=
os
.
path
.
join
(
sketch_ino_location
=
os
.
path
.
join
(
sketch_dir
,
sketch_dir
,
sketch
_creator
.
_
default_sketch_name
,
sketch
creator
.
default_sketch_name
,
'%s.ino'
%
sketch
_creator
.
_
default_sketch_name
)
'%s.ino'
%
sketch
creator
.
default_sketch_name
)
sketch_code_write
=
'Unicode test (ろΓαζςÂaé) on: %s'
%
\
sketch_code_write
=
'Unicode test (ろΓαζςÂaé) on: %s'
%
\
time
.
strftime
(
"%Y-%m-%d %H:%M:%S"
)
time
.
strftime
(
"%Y-%m-%d %H:%M:%S"
)
sketch_return_location
=
sketch
_
creator
.
create_sketch
(
sketch_return_location
=
sketchcreator
.
create_sketch
(
sketch_dir
,
sketch_code
=
sketch_code_write
)
sketch_dir
,
sketch_code
=
sketch_code_write
)
self
.
assertEqual
(
sketch_return_location
,
sketch_ino_location
)
self
.
assertEqual
(
sketch_return_location
,
sketch_ino_location
)
...
...
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