diff --git a/README.md b/README.md index 50403b8..30ae6b8 100644 --- a/README.md +++ b/README.md @@ -174,15 +174,15 @@ Some utility methods are provided and are still in development: ## Convenience and scaffolding methods -### create_project(PROJECT_NAME, PATH) +### create_sketch(SKETCH_NAME, PATH) Will create a new Python file (`.py`) with the specified name at the provided path. Example: ```Python -create_project('my_arduino_sketch') -create_project('my_arduino_sketch', 'tmp') -create_project('main') +create_sketch('my_arduino_sketch') +create_sketch('my_arduino_sketch', 'tmp') +create_sketch('main') ``` If the destination `.py` file exists, a timestamp in _microseconds_ will be appended to the name. diff --git a/arduino/arduino.py b/arduino/arduino.py index 9ff4896..1e9a2eb 100644 --- a/arduino/arduino.py +++ b/arduino/arduino.py @@ -82,21 +82,23 @@ def delay(_ms): # HELPERS -def create_new_sketch(sketch_name = None, path = '.'): +def create_sketch(sketch_name = None, path = '.', overwrite = False): + if sketch_name is None: + sketch_name = 'main' new_sketch_path = f'{path}/{sketch_name}.py' try: - open(new_sketch_path, 'x') + open(new_sketch_path, 'r') + if not overwrite: + sketch_name = f'{sketch_name}_{ticks_us()}' except OSError: - sketch_name = f'{sketch_name}_{ticks_us()}' + pass - if sketch_name is None: - sketch_name = f'main_{ticks_us()}.py' template_path = '/'.join(__file__.split('/')[:-1]) + '/template.py' template_sketch = open(template_path, 'r') new_sketch_path = f'{path}/{sketch_name}.py' - with open(f'{path}/{sketch_name}.py', 'w') as f: + with open(new_sketch_path, 'w') as f: sketch_line = None while sketch_line is not '': sketch_line = template_sketch.readline()