<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import division <EOL> from __future__ import unicode_literals <EOL> import glob <EOL> import time <EOL> from . import pyserial <EOL> from . import sys_info <EOL> from . import settings <EOL> from . import board_port <EOL> if sys_info . get_os_name ( ) == '<STR_LIT>' : <EOL> if sys_info . get_python_version ( ) < <NUM_LIT:3> : <EOL> import _winreg as winreg <EOL> else : <EOL> import winreg <EOL> def list_board_ports ( ) : <EOL> board_ports = [ ] <EOL> serial_ports = list_serial_ports ( ) <EOL> for serial_port in serial_ports : <EOL> board_name = resolve_device_attached_to ( serial_port ) <EOL> label = serial_port <EOL> if board_name : <EOL> label += '<STR_LIT>' % board_name <EOL> port = board_port . BoardPort ( ) <EOL> port . set_address ( serial_port ) <EOL> port . set_protocol ( '<STR_LIT>' ) <EOL> port . set_board_name ( board_name ) <EOL> port . set_label ( label ) <EOL> board_ports . append ( port ) <EOL> return board_ports <EOL> def resolve_device_attached_to ( serial_port ) : <EOL> device_name = '<STR_LIT>' <EOL> return device_name <EOL> def list_serial_ports ( ) : <EOL> os_name = sys_info . get_os_name ( ) <EOL> if os_name == "<STR_LIT>" : <EOL> serial_ports = list_win_serial_ports ( ) <EOL> elif os_name == '<STR_LIT>' : <EOL> serial_ports = list_osx_serial_ports ( ) <EOL> else : <EOL> serial_ports = list_linux_serial_ports ( ) <EOL> serial_ports . sort ( ) <EOL> return serial_ports <EOL> def list_win_serial_ports ( ) : <EOL> serial_ports = [ ] <EOL> has_ports = False <EOL> path = '<STR_LIT>' <EOL> try : <EOL> reg = winreg . OpenKey ( winreg . HKEY_LOCAL_MACHINE , path , ) <EOL> has_ports = True <EOL> except WindowsError : <EOL> pass <EOL> if has_ports : <EOL> for i in range ( <NUM_LIT> ) : <EOL> try : <EOL> name , value , type = winreg . EnumValue ( reg , i ) <EOL> except WindowsError : <EOL> pass <EOL> else : <EOL> serial_ports . append ( value ) <EOL> return serial_ports <EOL> def list_osx_serial_ports ( ) : <EOL> serial_ports = [ ] <EOL> dev_path = '<STR_LIT>' <EOL> dev_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for dev_name in dev_names : <EOL> pattern = dev_path + dev_name <EOL> serial_ports += glob . glob ( pattern ) <EOL> return serial_ports <EOL> def list_linux_serial_ports ( ) : <EOL> serial_ports = [ ] <EOL> dev_path = '<STR_LIT>' <EOL> dev_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for dev_name in dev_names : <EOL> pattern = dev_path + dev_name <EOL> serial_ports += glob . glob ( pattern ) <EOL> return serial_ports <EOL> def check_target_serial_port ( ) : <EOL> arduino_settings = settings . get_arduino_settings ( ) <EOL> serial_ports = list_serial_ports ( ) <EOL> target_serial_port = arduino_settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if serial_ports and not target_serial_port in serial_ports : <EOL> target_serial_port = serial_ports [ <NUM_LIT:0> ] <EOL> arduino_settings . set ( '<STR_LIT>' , target_serial_port ) <EOL> return target_serial_port <EOL> def touch_port ( serial_port , baudrate ) : <EOL> ser = pyserial . Serial ( ) <EOL> ser . port = serial_port <EOL> ser . baudrate = baudrate <EOL> ser . bytesize = pyserial . EIGHTBITS <EOL> ser . stopbits = pyserial . STOPBITS_ONE <EOL> ser . parity = pyserial . PARITY_NONE <EOL> try : <EOL> ser . open ( ) <EOL> except pyserial . SerialException : <EOL> pass <EOL> else : <EOL> ser . setDTR ( True ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> ser . setDTR ( False ) <EOL> ser . close ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def auto_reset ( serial_port ) : <EOL> ser = pyserial . Serial ( ) <EOL> ser . port = serial_port <EOL> try : <EOL> ser . open ( ) <EOL> except pyserial . SerialException : <EOL> pass <EOL> else : <EOL> ser . setRTS ( False ) <EOL> ser . setDTR ( False ) <EOL> ser . setDTR ( True ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> ser . setDTR ( False ) <EOL> ser . setRTS ( True ) <EOL> ser . setDTR ( True ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> ser . setDTR ( False ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> ser . write ( '<STR_LIT>' ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> ser . close ( ) <EOL> def wait_for_port ( upload_port , before_ports , message_queue ) : <EOL> elapsed = <NUM_LIT:0> <EOL> os_name = sys_info . get_os_name ( ) <EOL> new_port = '<STR_LIT>' <EOL> while elapsed < <NUM_LIT:1000> : <EOL> now_ports = list_serial_ports ( ) <EOL> diff_ports = remove_ports ( now_ports , before_ports ) <EOL> message_queue . put ( '<STR_LIT>' , before_ports , <EOL> now_ports , diff_ports ) <EOL> if diff_ports : <EOL> new_port = diff_ports [ <NUM_LIT:0> ] <EOL> message_queue . put ( '<STR_LIT>' , new_port ) <EOL> break <EOL> before_ports = now_ports <EOL> time . sleep ( <NUM_LIT> ) <EOL> elapsed += <NUM_LIT> <EOL> if ( ( os_name != '<STR_LIT>' and elapsed >= <NUM_LIT> ) or elapsed >= <NUM_LIT> ) and ( upload_port in now_ports ) : <EOL> new_port = upload_port <EOL> message_queue . put ( '<STR_LIT>' , <EOL> upload_port ) <EOL> break <EOL> if new_port == '<STR_LIT>' : <EOL> txt = "<STR_LIT>" <EOL> txt += '<STR_LIT>' <EOL> txt += "<STR_LIT>" <EOL> txt += '<STR_LIT>' <EOL> message_queue . put ( txt ) <EOL> return new_port <EOL> def remove_ports ( now_ports , before_ports ) : <EOL> ports = now_ports [ : ] <EOL> for port in before_ports : <EOL> if port in ports : <EOL> ports . remove ( port ) <EOL> return ports </s>
