# Path: extras/python/iotsaControl/consts.py
# class IotsaError(RuntimeError):
#     pass
# 
# VERBOSE=[]


from future import standard_library
from builtins import str
from builtins import object
from .consts import IotsaError, VERBOSE
import os
import esptool
import urllib.request
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
standard_library.install_aliases()


class DFU(object):
    """Handle Resetting flash and uploading initial binary over Serial connection (or USB)"""
    def __init__(self, port=None):
        self.port = port
        
    def _run(self, cmd, args=[], nostub=False):
        """Run a single esptool command"""
        command = ["--after", "no_reset"]
        if self.port:
            command += ["--port", self.port]
        if nostub:
            command += ["--no-stub"]
        command += [cmd]
        command += args
        try:
            esptool.main(command)
        except esptool.FatalError as e:
            raise IotsaError(str(e))
            
    def dfuWait(self):
        """Test to see whether a board in programmable mode is connected"""
        try:
            self._run("chip_id", nostub=True)

# Path: extras/python/iotsaControl/consts.py
# class IotsaError(RuntimeError):
#     pass
# 
# VERBOSE=[]


# Path: extras/python/iotsaControl/dfu.py
from future import standard_library
from builtins import str
from builtins import object
from .consts import IotsaError, VERBOSE
import os
import esptool
import urllib.request

from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
standard_library.install_aliases()


class DFU(object):
    """Handle Resetting flash and uploading initial binary over Serial connection (or USB)"""
    def __init__(self, port=None):
        self.port = port
        
    def _run(self, cmd, args=[], nostub=False):
        """Run a single esptool command"""
        command = ["--after", "no_reset"]
        if self.port:
            command += ["--port", self.port]
        if nostub:
            command += ["--no-stub"]
        command += [cmd]
        command += args
        try:
            esptool.main(command)
        except esptool.FatalError as e:
            raise IotsaError(str(e))
            
    def dfuWait(self):
        """Test to see whether a board in programmable mode is connected"""
        try:
            self._run("chip_id", nostub=True)

        except IotsaError: