Skip to content

Commit 0b8ce08

Browse files
committed
Convert server SketchCreator class into a simple Python module
1 parent af71614 commit 0b8ce08

File tree

3 files changed

+86
-105
lines changed

3 files changed

+86
-105
lines changed

ardublocklyserver/actions.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
#
33
# Collection of actions to the ardublocklyserver for relieved HTTP requests.
44
#
5-
# Copyright (c) 2015 carlosperate https://github.com/carlosperate/
5+
# Copyright (c) 2017 carlosperate https://github.com/carlosperate/
66
# Licensed under the Apache License, Version 2.0 (the "License"):
77
# http://www.apache.org/licenses/LICENSE-2.0
88
#
9-
from __future__ import unicode_literals, absolute_import
9+
from __future__ import unicode_literals, absolute_import, print_function
1010
import subprocess
1111
import locale
1212
import time
@@ -25,7 +25,7 @@
2525
import tkinter.filedialog as tkFileDialog
2626

2727
from ardublocklyserver.compilersettings import ServerCompilerSettings
28-
from ardublocklyserver.sketchcreator import SketchCreator
28+
from ardublocklyserver import sketchcreator
2929
import ardublocklyserver.six.six.moves as six_moves
3030
from ardublocklyserver.six import six
3131
import ardublocklyserver.gui as gui
@@ -145,15 +145,15 @@ def load_arduino_cli(sketch_path=None):
145145

146146
def create_sketch_default():
147147
settings = ServerCompilerSettings()
148-
return SketchCreator().create_sketch(
149-
settings.sketch_dir, sketch_name=settings.sketch_name)
148+
return sketchcreator.create_sketch(
149+
sketch_dir=settings.sketch_dir, sketch_name=settings.sketch_name)
150150

151151

152152
def create_sketch_from_string(sketch_code):
153153
settings = ServerCompilerSettings()
154-
return SketchCreator().create_sketch(
155-
settings.sketch_dir, sketch_name=settings.sketch_name,
156-
sketch_code=sketch_code)
154+
return sketchcreator.create_sketch(
155+
sketch_dir=settings.sketch_dir, sketch_name=settings.sketch_name,
156+
sketch_code=sketch_code)
157157

158158

159159
#

ardublocklyserver/sketchcreator.py

Lines changed: 65 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,100 +2,84 @@
22
#
33
# SketchCreator class creates an Arduino Sketch source code file.
44
#
5-
# Copyright (c) 2015 carlosperate https://github.com/carlosperate/
5+
# Copyright (c) 2017 carlosperate https://github.com/carlosperate/
66
# Licensed under the Apache License, Version 2.0 (the "License"):
77
# http://www.apache.org/licenses/LICENSE-2.0
88
#
9-
from __future__ import unicode_literals, absolute_import
9+
from __future__ import unicode_literals, absolute_import, print_function
1010
import codecs
1111
import os
1212

1313
from ardublocklyserver.six import six
1414

15+
# Default blinky sketch
16+
default_sketch_code = """int led = 13;
17+
void setup() {
18+
pinMode(led, OUTPUT);
19+
}
20+
void loop() {
21+
digitalWrite(led, HIGH);
22+
delay(1000);
23+
digitalWrite(led, LOW);
24+
delay(1000);
25+
}
26+
"""
1527

16-
class SketchCreator(object):
17-
"""
18-
Creates an Arduino Sketch.
19-
"""
28+
# Default sketch name
29+
default_sketch_name = 'ArdublocklySketch'
2030

21-
#
22-
# Metaclass methods
23-
#
24-
def __init__(self, sketch_name=None):
25-
# Default sketch, blink builtin LED
26-
self._default_sketch_code = \
27-
'int led = 13;\n' \
28-
'void setup() {\n' \
29-
' pinMode(led, OUTPUT);\n' \
30-
'}\n' \
31-
'void loop() {\n' \
32-
' digitalWrite(led, HIGH);\n' \
33-
' delay(1000);\n' \
34-
' digitalWrite(led, LOW);\n' \
35-
' delay(1000);\n' \
36-
'}\n'
37-
# Default sketch name
38-
self._default_sketch_name = 'ArdublocklySketch'
3931

40-
#
41-
# Creating files
42-
#
43-
def create_sketch(self, sketch_dir, sketch_name=None, sketch_code=None):
44-
"""
45-
Creates the Arduino sketch with either the default blinky code or the
46-
code defined in the input parameter.
47-
:param sketch_dir: Location for the sketch.
48-
:param sketch_name: Optional name for the sketch.
49-
:param sketch_code: Optional unicode string with the code for the
50-
sketch.
51-
:return: Unicode string with full path to the sketch file
52-
Return None indicates an error has occurred.
53-
"""
54-
# Check the code first, to not create sketch file if invalid
55-
if sketch_code is None:
56-
code_to_write = self._default_sketch_code
57-
else:
58-
if isinstance(sketch_code, six.string_types):
59-
code_to_write = sketch_code
60-
else:
61-
print('The sketch code given is not a valid string !!!')
62-
return None
32+
def create_sketch(sketch_dir, sketch_name=None, sketch_code=None):
33+
"""Create an Arduino Sketch file into the given directory.
6334
64-
# Check validity and create the sketch path
65-
if sketch_name is None:
66-
sketch_name = self._default_sketch_name
67-
sketch_path = self.build_sketch_path(sketch_dir, sketch_name)
68-
if sketch_path is None:
69-
return None
35+
Creates an Arduino sketch with either the default blinky code or the
36+
code defined in the input parameter.
7037
71-
try:
72-
arduino_sketch = codecs.open(sketch_path, 'wb+', encoding='utf-8')
73-
try:
74-
arduino_sketch.write(code_to_write)
75-
finally:
76-
arduino_sketch.close()
77-
except Exception as e:
78-
print(e)
79-
print('Arduino sketch could not be created !!!')
38+
:param sketch_dir: Location for the sketch.
39+
:param sketch_name: Optional name for the sketch.
40+
:param sketch_code: Optional unicode string with the code for the sketch.
41+
:return: Unicode string with full path to the sketch file
42+
Return None indicates an error has occurred.
43+
"""
44+
# Check the code first, to not create sketch file if invalid
45+
if sketch_code is None:
46+
code_to_write = default_sketch_code
47+
else:
48+
if isinstance(sketch_code, six.string_types):
49+
code_to_write = sketch_code
50+
else:
51+
print('The sketch code given is not a valid string !!!')
8052
return None
53+
# Check validity and create the sketch path
54+
if sketch_name is None:
55+
sketch_name = default_sketch_name
56+
sketch_path = build_sketch_path(sketch_dir, sketch_name)
57+
try:
58+
with codecs.open(sketch_path, 'wb+', encoding='utf-8') as sketch_f:
59+
sketch_f.write(code_to_write)
60+
except Exception as e:
61+
print(e)
62+
print('Arduino sketch could not be created !!!')
63+
return None
8164

82-
return sketch_path
65+
return sketch_path
8366

84-
@staticmethod
85-
def build_sketch_path(sketch_dir, sketch_name):
86-
"""
87-
If a valid directory is provided, it creates the Arduino sketch folder
88-
(if it does not exists already) and returns a string pointing to the
89-
sketch file path.
90-
:return: unicode string with full path to the sketch file.
91-
Returns None indicates an error has occurred.
92-
"""
93-
sketch_path = None
94-
if os.path.isdir(sketch_dir):
95-
sketch_path = os.path.join(sketch_dir, sketch_name)
96-
if not os.path.exists(sketch_path):
97-
os.makedirs(sketch_path)
98-
sketch_path = os.path.join(sketch_path, sketch_name + '.ino')
99-
else:
100-
print('The sketch directory "%s" does not exists !!!' % sketch_dir)
101-
return sketch_path
67+
68+
def build_sketch_path(sketch_dir, sketch_name):
69+
"""Create the Arduino Sketch folder required for a valid Sketch.
70+
71+
If a valid directory is provided, it creates the Arduino sketch folder
72+
(if it does not exists already) and returns a string pointing to the
73+
sketch file path.
74+
:return: unicode string with full path to the sketch file.
75+
Return None indicates an error has occurred.
76+
"""
77+
sketch_path = None
78+
if os.path.isdir(sketch_dir):
79+
sketch_path = os.path.join(sketch_dir, sketch_name)
80+
if not os.path.exists(sketch_path):
81+
os.makedirs(sketch_path)
82+
sketch_path = os.path.join(sketch_path, sketch_name + '.ino')
83+
else:
84+
print('The sketch directory "%s" does not exists !!!' % sketch_dir)
85+
return sketch_path

ardublocklyserver/tests/sketchcreator_test.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
import unittest
1515

1616
try:
17-
from ardublocklyserver.sketchcreator import SketchCreator
17+
from ardublocklyserver import sketchcreator
1818
from ardublocklyserver.compilersettings import ServerCompilerSettings
1919
except ImportError:
2020
import sys
2121
file_dir = os.path.dirname(os.path.realpath(__file__))
2222
package_dir = os.path.dirname(os.path.dirname(file_dir))
2323
sys.path.insert(0, package_dir)
24-
from ardublocklyserver.sketchcreator import SketchCreator
24+
from ardublocklyserver import sketchcreator
2525
from ardublocklyserver.compilersettings import ServerCompilerSettings
2626

2727

@@ -36,18 +36,17 @@ class SketchCreatorTestCase(unittest.TestCase):
3636
def test_create_sketch(self):
3737
""" Tests to see if an Arduino Sketch is created in a new location. """
3838
# First test with the default name
39-
sketch_creator = SketchCreator()
4039
sketch_dir = os.getcwd()
4140
final_ino_path = os.path.join(
4241
sketch_dir,
43-
sketch_creator._default_sketch_name,
44-
'%s.ino' % sketch_creator._default_sketch_name)
45-
# It should be save to create and delete the ino file in the test folder
42+
sketchcreator.default_sketch_name,
43+
'%s.ino' % sketchcreator.default_sketch_name)
44+
# Should be safe to create and delete the ino file in the test folder
4645
if os.path.exists(final_ino_path):
4746
os.remove(final_ino_path)
4847
self.assertFalse(os.path.isfile(final_ino_path))
4948
# Checks the file is saved, and saved to the right location
50-
created_sketch_path = sketch_creator.create_sketch(sketch_dir)
49+
created_sketch_path = sketchcreator.create_sketch(sketch_dir)
5150
self.assertEqual(final_ino_path, created_sketch_path)
5251
self.assertTrue(os.path.isfile(final_ino_path))
5352

@@ -70,7 +69,7 @@ def test_create_sketch(self):
7069
self.assertFalse(os.path.isfile(final_ino_path))
7170

7271
# Checks the file is saved, and saved to the right location
73-
created_sketch_path = sketch_creator.create_sketch(
72+
created_sketch_path = sketchcreator.create_sketch(
7473
sketch_dir_unicode, sketch_name=sketch_name)
7574
self.assertEqual(final_ino_path, created_sketch_path)
7675
self.assertTrue(os.path.isfile(final_ino_path))
@@ -85,20 +84,19 @@ def test_create_sketch_invalid(self):
8584
# Test for failure on invalid sketch path
8685
random_invalid_path = os.path.join(os.getcwd(), 'raNd_dIr')
8786
self.assertFalse(os.path.isdir(random_invalid_path))
88-
sketch_creator = SketchCreator()
89-
created_sketch_path = sketch_creator.create_sketch(random_invalid_path)
87+
created_sketch_path = sketchcreator.create_sketch(random_invalid_path)
9088
self.assertIsNone(created_sketch_path)
9189
self.assertFalse(os.path.isdir(random_invalid_path))
9290

9391
# Test for failure on invalid sketch code
9492
sketch_path = os.getcwd()
9593
sketch_folder_path = os.path.join(
96-
sketch_path, sketch_creator._default_sketch_name)
94+
sketch_path, sketchcreator.default_sketch_name)
9795
if os.path.isdir(sketch_folder_path):
9896
shutil.rmtree(sketch_folder_path)
9997
self.assertFalse(os.path.isdir(sketch_folder_path))
10098
invalid_sketch_code = True
101-
created_sketch_path = sketch_creator.create_sketch(
99+
created_sketch_path = sketchcreator.create_sketch(
102100
sketch_path, sketch_code=invalid_sketch_code)
103101
self.assertIsNone(created_sketch_path)
104102
self.assertFalse(os.path.isdir(sketch_folder_path))
@@ -107,16 +105,15 @@ def test_create_sketch_invalid(self):
107105
# File creation with code
108106
#
109107
def test_create_sketch_with_code(self):
110-
sketch_creator = SketchCreator()
111108
sketch_dir = os.getcwd()
112109
sketch_ino_location = os.path.join(
113110
sketch_dir,
114-
sketch_creator._default_sketch_name,
115-
'%s.ino' % sketch_creator._default_sketch_name)
111+
sketchcreator.default_sketch_name,
112+
'%s.ino' % sketchcreator.default_sketch_name)
116113
sketch_code_write = 'Unicode test (ろΓαζςÂaé) on: %s' % \
117114
time.strftime("%Y-%m-%d %H:%M:%S")
118115

119-
sketch_return_location = sketch_creator.create_sketch(
116+
sketch_return_location = sketchcreator.create_sketch(
120117
sketch_dir, sketch_code=sketch_code_write)
121118
self.assertEqual(sketch_return_location, sketch_ino_location)
122119

0 commit comments

Comments
 (0)