<s> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import division <EOL> from __future__ import unicode_literals <EOL> import os <EOL> import json <EOL> import glob <EOL> from re import search <EOL> try : <EOL> from . import Serial , Paths <EOL> from . Preferences import Preferences <EOL> from . JSONFile import JSONFile <EOL> from . I18n import I18n <EOL> except : <EOL> from libs . Preferences import Preferences <EOL> from libs . JSONFile import JSONFile <EOL> from libs import Serial , Paths <EOL> from libs . I18n import I18n <EOL> _ = I18n ( ) . translate <EOL> class Menu ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> super ( Menu , self ) . __init__ ( ) <EOL> def createBoardsMenu ( self ) : <EOL> '''<STR_LIT>''' <EOL> vendors = { } <EOL> boards = [ ] <EOL> platformio_data = self . getTemplateMenu ( <EOL> file_name = '<STR_LIT>' , user_path = True ) <EOL> if ( not platformio_data ) : <EOL> return <EOL> platformio_data = json . loads ( platformio_data ) <EOL> for datakey , datavalue in platformio_data . items ( ) : <EOL> for infokey , infovalue in datavalue . items ( ) : <EOL> vendor = datavalue [ '<STR_LIT>' ] <EOL> if ( '<STR_LIT:name>' in infokey ) : <EOL> temp_info = { } <EOL> temp_info [ '<STR_LIT>' ] = infovalue <EOL> temp_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> temp_info [ '<STR_LIT>' ] = True <EOL> temp_info [ '<STR_LIT:args>' ] = { '<STR_LIT>' : datakey } <EOL> children = vendors . setdefault ( vendor , [ ] ) <EOL> children . append ( temp_info ) <EOL> for vendor , children in vendors . items ( ) : <EOL> children = sorted ( children , key = lambda x : x [ '<STR_LIT>' ] ) <EOL> boards . append ( { '<STR_LIT>' : vendor , <EOL> '<STR_LIT>' : children } ) <EOL> boards = sorted ( boards , key = lambda x : x [ '<STR_LIT>' ] ) <EOL> return boards <EOL> def createEnvironmentMenu ( self , empty = False ) : <EOL> '''<STR_LIT>''' <EOL> environments = [ ] <EOL> if ( not empty ) : <EOL> is_native = Preferences ( ) . get ( '<STR_LIT>' , False ) <EOL> type = '<STR_LIT>' if not is_native else '<STR_LIT>' <EOL> env_selecs = Preferences ( ) . get ( type , '<STR_LIT>' ) <EOL> env_boards = self . getTemplateMenu ( <EOL> '<STR_LIT>' , user_path = True ) <EOL> if ( not env_boards ) : <EOL> return <EOL> for board in env_boards : <EOL> for selected in env_selecs : <EOL> try : <EOL> environments . append ( board [ selected ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> except : <EOL> pass <EOL> env_menu = self . getTemplateMenu ( file_name = '<STR_LIT>' ) <EOL> env_menu [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = environments <EOL> self . saveSublimeMenu ( data = env_menu , <EOL> sub_folder = '<STR_LIT>' , <EOL> user_path = True ) <EOL> def createLibraryImportMenu ( self ) : <EOL> """<STR_LIT>""" <EOL> library_paths = Paths . getLibraryFolders ( ) <EOL> added_lib = [ ] <EOL> children = [ ] <EOL> menu_import_lib = self . getTemplateMenu ( file_name = '<STR_LIT>' ) <EOL> for library_dir in library_paths : <EOL> if '<STR_LIT>' not in library_dir : <EOL> temp_info = { } <EOL> temp_info [ '<STR_LIT>' ] = '<STR_LIT:->' <EOL> children . append ( temp_info ) <EOL> sub_path = glob . glob ( library_dir ) <EOL> for library in sub_path : <EOL> if '<STR_LIT>' in library : <EOL> core_subs = os . path . join ( library , '<STR_LIT:*>' ) <EOL> core_subs = glob . glob ( core_subs ) <EOL> for core_sub in core_subs : <EOL> core_sub_subs = os . path . join ( core_sub , '<STR_LIT:*>' ) <EOL> core_sub_subs = glob . glob ( core_sub_subs ) <EOL> for core_lib in core_sub_subs : <EOL> caption = os . path . basename ( core_lib ) <EOL> if caption not in added_lib : <EOL> temp_info = { } <EOL> temp_info [ '<STR_LIT>' ] = caption <EOL> temp_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> temp_info [ '<STR_LIT:args>' ] = { '<STR_LIT>' : library } <EOL> children . append ( temp_info ) <EOL> added_lib . append ( caption ) <EOL> caption = os . path . basename ( library ) <EOL> pio_libs = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if pio_libs in library : <EOL> library_json = os . path . join ( library , '<STR_LIT>' ) <EOL> if ( not os . path . exists ( library_json ) ) : <EOL> library_json = os . path . join ( <EOL> library , '<STR_LIT>' ) <EOL> json = JSONFile ( library_json ) <EOL> json = json . getData ( ) <EOL> if ( json != { } ) : <EOL> caption = json [ '<STR_LIT:name>' ] <EOL> if caption not in added_lib and '<STR_LIT>' not in caption : <EOL> temp_info = { } <EOL> temp_info [ '<STR_LIT>' ] = caption <EOL> temp_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> temp_info [ '<STR_LIT:args>' ] = { '<STR_LIT>' : library } <EOL> children . append ( temp_info ) <EOL> added_lib . append ( caption ) <EOL> menu_import_lib [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = children <EOL> self . saveSublimeMenu ( data = menu_import_lib , <EOL> sub_folder = '<STR_LIT>' , <EOL> user_path = True ) <EOL> def createLibraryExamplesMenu ( self ) : <EOL> """<STR_LIT>""" <EOL> examples = [ ] <EOL> children = [ ] <EOL> library_paths = Paths . getLibraryFolders ( ) <EOL> for path in library_paths : <EOL> sub_paths = glob . glob ( path ) <EOL> for sub in sub_paths : <EOL> sub = os . path . join ( sub , '<STR_LIT:*>' ) <EOL> libs = glob . glob ( sub ) <EOL> for lib in libs : <EOL> caption = os . path . basename ( os . path . dirname ( lib ) ) <EOL> new_caption = search ( r"<STR_LIT>" , caption ) <EOL> if ( new_caption is not None ) : <EOL> caption = new_caption . group ( <NUM_LIT:1> ) <EOL> if os . path . isdir ( lib ) and os . listdir ( lib ) and '<STR_LIT>' in lib : <EOL> file_examples = os . path . join ( lib , '<STR_LIT:*>' ) <EOL> file_examples = glob . glob ( file_examples ) <EOL> for file in file_examples : <EOL> caption_example = os . path . basename ( file ) <EOL> temp_info = { } <EOL> temp_info [ '<STR_LIT>' ] = caption_example <EOL> temp_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> temp_info [ '<STR_LIT:args>' ] = { '<STR_LIT>' : file } <EOL> children . append ( temp_info ) <EOL> temp_info = { } <EOL> temp_info [ '<STR_LIT>' ] = caption <EOL> temp_info [ '<STR_LIT>' ] = children <EOL> examples . append ( temp_info ) <EOL> children = [ ] <EOL> examples . append ( { '<STR_LIT>' : '<STR_LIT:->' } ) <EOL> menu_lib_example = self . getTemplateMenu ( file_name = '<STR_LIT>' ) <EOL> menu_lib_example [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = examples <EOL> self . saveSublimeMenu ( data = menu_lib_example , <EOL> sub_folder = '<STR_LIT>' , <EOL> user_path = True ) <EOL> def createSerialPortsMenu ( self ) : <EOL> '''<STR_LIT>''' <EOL> port_list = Serial . listSerialPorts ( ) <EOL> ip_port = Preferences ( ) . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( ip_port ) : <EOL> port_list . insert ( <NUM_LIT:0> , ip_port ) <EOL> menu_preset = self . getTemplateMenu ( file_name = '<STR_LIT>' ) <EOL> menu_ports = [ <EOL> { "<STR_LIT>" : _ ( "<STR_LIT>" ) , "<STR_LIT>" : "<STR_LIT>" } ] <EOL> for port in port_list : <EOL> temp_info = { } <EOL> temp_info [ '<STR_LIT>' ] = port <EOL> temp_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> temp_info [ '<STR_LIT>' ] = True <EOL> temp_info [ '<STR_LIT:args>' ] = { '<STR_LIT>' : port } <EOL> menu_ports . append ( temp_info ) <EOL> menu_preset [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = menu_ports <EOL> self . saveSublimeMenu ( data = menu_preset , <EOL> sub_folder = '<STR_LIT>' , <EOL> user_path = True ) <EOL> def createMainMenu ( self ) : <EOL> '''<STR_LIT>''' <EOL> boards = self . createBoardsMenu ( ) <EOL> if ( not boards ) : <EOL> return False <EOL> menu_data = self . getTemplateMenu ( file_name = '<STR_LIT>' ) <EOL> for first_menu in menu_data [ <NUM_LIT:0> ] : <EOL> for second_menu in menu_data [ <NUM_LIT:0> ] [ first_menu ] : <EOL> if '<STR_LIT>' in second_menu : <EOL> second_menu [ '<STR_LIT>' ] = _ ( second_menu [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in second_menu : <EOL> if ( second_menu [ '<STR_LIT:id>' ] == '<STR_LIT>' ) : <EOL> second_menu [ '<STR_LIT>' ] = boards <EOL> for third_menu in menu_data [ <NUM_LIT:0> ] [ '<STR_LIT>' ] : <EOL> try : <EOL> for caption in third_menu [ '<STR_LIT>' ] : <EOL> caption [ '<STR_LIT>' ] = _ ( caption [ '<STR_LIT>' ] ) <EOL> try : <EOL> for item in caption [ '<STR_LIT>' ] : <EOL> item [ '<STR_LIT>' ] = _ ( item [ '<STR_LIT>' ] ) <EOL> except : <EOL> pass <EOL> except : <EOL> pass <EOL> self . saveSublimeMenu ( data = menu_data ) <EOL> env_path = Paths . getSublimeMenuPath ( <EOL> '<STR_LIT>' , user_path = True ) <EOL> if ( os . path . isfile ( env_path ) ) : <EOL> self . createEnvironmentMenu ( ) <EOL> self . createLanguageMenu ( ) <EOL> def createLanguageMenu ( self ) : <EOL> """<STR_LIT>""" <EOL> menu_language = [ ] <EOL> lang_ids = I18n ( ) . getLangIds ( ) <EOL> for id_lang in lang_ids : <EOL> lang_names = I18n ( ) . getLangNames ( id_lang ) <EOL> caption = '<STR_LIT>' % ( lang_names [ <NUM_LIT:0> ] , lang_names [ <NUM_LIT:1> ] ) <EOL> options = { } <EOL> options [ '<STR_LIT>' ] = caption <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> options [ '<STR_LIT:args>' ] = { '<STR_LIT>' : id_lang } <EOL> options [ '<STR_LIT>' ] = True <EOL> menu_language . append ( options ) <EOL> menu_preset = self . getTemplateMenu ( file_name = '<STR_LIT>' ) <EOL> menu_preset [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = menu_language <EOL> self . saveSublimeMenu ( data = menu_preset , <EOL> sub_folder = '<STR_LIT>' , <EOL> user_path = True ) <EOL> def getTemplateMenu ( self , file_name , user_path = False ) : <EOL> """<STR_LIT>""" <EOL> file_path = Paths . getTemplateMenuPath ( file_name , user_path ) <EOL> preset_file = JSONFile ( file_path ) <EOL> preset_data = preset_file . getData ( ) <EOL> return preset_data <EOL> def saveTemplateMenu ( self , data , file_name , user_path = False ) : <EOL> """<STR_LIT>""" <EOL> file_path = Paths . getTemplateMenuPath ( file_name , user_path ) <EOL> preset_file = JSONFile ( file_path ) <EOL> preset_file . setData ( data ) <EOL> preset_file . saveData ( ) <EOL> def getSublimeMenu ( self , user_path = False ) : <EOL> """<STR_LIT>""" <EOL> menu_path = Paths . getSublimeMenuPath ( user_path ) <EOL> menu_file = JSONFile ( menu_path ) <EOL> menu_data = menu_file . getData ( ) <EOL> return menu_data <EOL> def saveSublimeMenu ( self , data , sub_folder = False , user_path = False ) : <EOL> """<STR_LIT>""" <EOL> menu_file_path = Paths . getSublimeMenuPath ( sub_folder , user_path ) <EOL> file_menu = JSONFile ( menu_file_path ) <EOL> file_menu . setData ( data ) <EOL> file_menu . saveData ( ) </s>
