|
2 | 2 | # |
3 | 3 | # SketchCreator class creates an Arduino Sketch source code file. |
4 | 4 | # |
5 | | -# Copyright (c) 2015 carlosperate https://github.com/carlosperate/ |
| 5 | +# Copyright (c) 2017 carlosperate https://github.com/carlosperate/ |
6 | 6 | # Licensed under the Apache License, Version 2.0 (the "License"): |
7 | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
8 | 8 | # |
9 | | -from __future__ import unicode_literals, absolute_import |
| 9 | +from __future__ import unicode_literals, absolute_import, print_function |
10 | 10 | import codecs |
11 | 11 | import os |
12 | 12 |
|
13 | 13 | from ardublocklyserver.six import six |
14 | 14 |
|
| 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 | +""" |
15 | 27 |
|
16 | | -class SketchCreator(object): |
17 | | - """ |
18 | | - Creates an Arduino Sketch. |
19 | | - """ |
| 28 | +# Default sketch name |
| 29 | +default_sketch_name = 'ArdublocklySketch' |
20 | 30 |
|
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' |
39 | 31 |
|
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. |
63 | 34 |
|
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. |
70 | 37 |
|
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 !!!') |
80 | 52 | 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 |
81 | 64 |
|
82 | | - return sketch_path |
| 65 | + return sketch_path |
83 | 66 |
|
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 |
0 commit comments