From 1cc04dfe513565f56f311ffd1d03608194f13992 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Sun, 15 Feb 2015 14:51:37 -0800 Subject: [PATCH] better searching for prefs files (MacOS X) - MacOS X can have `preferences.txt` in `~/Library/Arduino`, `~/Library/Arduino15`, and who knows what else --- ino/environment.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ino/environment.py b/ino/environment.py index 8cb27e8..5be8720 100644 --- a/ino/environment.py +++ b/ino/environment.py @@ -77,10 +77,16 @@ class Environment(dict): '/usr/local/share/arduino', '/usr/share/arduino', ] + arduino_prefs_file = None + arduino_prefs_file_guesses = [ + os.path.expanduser('~/.arduino/') + ] if platform.system() == 'Darwin': arduino_dist_dir_guesses.insert(0, '/Applications/Arduino.app/Contents/Resources/Java') + arduino_prefs_file_guesses.insert(0, os.path.expanduser('~/Library/Arduino*')) + default_board_model = 'uno' ino = sys.argv[0] @@ -194,6 +200,9 @@ def find_arduino_tool(self, key, dirname_parts, items=None, human_name=None, mul places = self.arduino_dist_places(dirname_parts) + ['$PATH'] return self.find_file(key, items, places, human_name, multi=multi) + def find_prefs_file(self, key=None, items=None, human_name=None, multi=False): + return self.find_file(key, items=items or ['preferences.txt'], places=self.arduino_prefs_file_places(), human_name=human_name, multi=multi) + def arduino_dist_places(self, dirname_parts): """ For `dirname_parts` like [a, b, c] return list of @@ -208,6 +217,32 @@ def arduino_dist_places(self, dirname_parts): places = self.arduino_dist_dir_guesses return [os.path.join(p, *dirname_parts) for p in places] + def arduino_prefs_file_places(self): + if 'arduino_prefs_file' in self: + places = [self['arduino_prefs_file']] + else: + places = self.arduino_prefs_file_guesses + return places + + def arduino_ide_pref(self, key): + """ + Try to retrieve an entry from the preferences of the arduino IDE given its key + """ + + prefs_file = self.find_prefs_file('prefs_file') + + if os.path.isfile(prefs_file): + fp = open(prefs_file) + prefs = fp.read().split('\n') + fp.close() + + for line in prefs: + kv = line.split('=') + if len(kv) == 2 and kv[0] == key: + return kv[1].strip() + + return None + def board_models(self): if 'board_models' in self: return self['board_models']