def format_available_options(items, head_width, head_color='cyan', default=None, default_mark="[DEFAULT]", default_mark_color='red'): from ano.filters import colorize default_mark = colorize(default_mark + ' ', default_mark_color) lines = ['%s: %s%s' % (colorize('%%%ds' % head_width % key, head_color), default_mark if key == default else '', val) for key, val in items] return '\n'.join(lines)
def main(): e = Environment() e.load() conf = configure() try: current_command = sys.argv[1] except IndexError: current_command = None parser = argparse.ArgumentParser(prog='ano', formatter_class=FlexiFormatter, description=__doc__) subparsers = parser.add_subparsers() is_command = lambda x: inspect.isclass(x) and issubclass(x, Command) and x != Command commands = [cls(e) for _, cls in inspect.getmembers(ano.commands, is_command)] for cmd in commands: p = subparsers.add_parser(cmd.name, formatter_class=FlexiFormatter, help=cmd.help_line) if current_command != cmd.name: continue cmd.setup_arg_parser(p) p.set_defaults(func=cmd.run, **conf.as_dict(cmd.name)) args = parser.parse_args() try: run_anywhere = "init clean list-models serial version" e.process_args(args) if current_command not in run_anywhere: if os.path.isdir(e.output_dir): # we have an output dir so we'll pretend this is a project folder None elif e.src_dir is None or not os.path.isdir(e.src_dir): raise Abort("No project found in this directory.") if current_command not in run_anywhere: # For valid projects create .build & lib if not os.path.isdir(e.build_dir): os.makedirs(e.build_dir) if not os.path.isdir(e.lib_dir): os.makedirs(e.lib_dir) with open('lib/.holder', 'w') as f: f.write("") args.func(args) except Abort as exc: print colorize(str(exc), 'red') sys.exit(1) except KeyboardInterrupt: print 'Terminated by user' finally: e.dump()
def guess_serial_port(self): print 'Guessing serial port ...', ports = self.list_serial_ports() if ports: result = ports[0] print colorize(result, 'yellow') return result print colorize('FAILED', 'red') raise Abort("No device matching following was found: %s" % (''.join(['\n - ' + p for p in self.serial_port_patterns()])))
def arduino_lib_version(self): self.find_arduino_file('version.txt', ['lib'], human_name='Arduino lib version file (version.txt)') if 'arduino_lib_version' not in self: with open(self['version.txt']) as f: print 'Detecting Arduino software version ... ', v_string = f.read().strip() v = Version.parse(v_string) self['arduino_lib_version'] = v print colorize("%s (%s)" % (v, v_string), 'green') return self['arduino_lib_version']
def guess_serial_port(self): print 'Guessing serial port ...', ports = self.list_serial_ports() if ports: result = ports[0] print colorize(result, 'yellow') return result print colorize('FAILED', 'red') raise Abort( "No device matching following was found: %s" % (''.join(['\n - ' + p for p in self.serial_port_patterns()])))
def arduino_lib_version(self): self.find_arduino_file( 'version.txt', ['lib'], human_name='Arduino lib version file (version.txt)') if 'arduino_lib_version' not in self: with open(self['version.txt']) as f: print 'Detecting Arduino software version ... ', v_string = f.read().strip() v = Version.parse(v_string) self['arduino_lib_version'] = v print colorize("%s (%s)" % (v, v_string), 'green') return self['arduino_lib_version']
def format_available_options(items, head_width, head_color='cyan', default=None, default_mark="[DEFAULT]", default_mark_color='red'): from ano.filters import colorize default_mark = colorize(default_mark + ' ', default_mark_color) lines = [ '%s: %s%s' % (colorize('%%%ds' % head_width % key, head_color), default_mark if key == default else '', val) for key, val in items ] return '\n'.join(lines)
def _find(self, key, items, places, human_name, join, multi, optional): """ Search for file-system entry with any name passed in `items` on all paths provided in `places`. Use `key` as a cache key. If `join` is True result will be a path join of place/item, otherwise only place is taken as result. Return first found match unless `multi` is True. In that case a list with all fount matches is returned. Raise `Abort` if no matches were found. """ if key in self: return self[key] human_name = human_name or key # expand env variables in `places` and split on colons places = itertools.chain.from_iterable( os.path.expandvars(p).split(os.pathsep) for p in places) places = map(os.path.expanduser, places) glob_places = itertools.chain.from_iterable(glob(p) for p in places) print 'Searching for', human_name, '...', results = [] for p in glob_places: for i in items: path = os.path.join(p, i) if os.path.exists(path): result = path if join else p if not multi: print colorize(result, 'green') self[key] = result return result results.append(result) if results: if len(results) > 1: formatted_results = ''.join(['\n - ' + x for x in results]) print colorize('found multiple: %s' % formatted_results, 'green') else: print colorize(results[0], 'green') self[key] = results return results print colorize('FAILED', 'red') if not optional: raise Abort("%s not found. Searched in following places: %s" % (human_name, ''.join(['\n - ' + p for p in places]))) else: self[key] = None return results
def _find(self, key, items, places, human_name, join, multi, optional): """ Search for file-system entry with any name passed in `items` on all paths provided in `places`. Use `key` as a cache key. If `join` is True result will be a path join of place/item, otherwise only place is taken as result. Return first found match unless `multi` is True. In that case a list with all fount matches is returned. Raise `Abort` if no matches were found. """ if key in self: return self[key] human_name = human_name or key # expand env variables in `places` and split on colons places = itertools.chain.from_iterable(os.path.expandvars(p).split(os.pathsep) for p in places) places = map(os.path.expanduser, places) glob_places = itertools.chain.from_iterable(glob(p) for p in places) print 'Searching for', human_name, '...', results = [] for p in glob_places: for i in items: path = os.path.join(p, i) if os.path.exists(path): result = path if join else p if not multi: print colorize(result, 'green') self[key] = result return result results.append(result) if results: if len(results) > 1: formatted_results = ''.join(['\n - ' + x for x in results]) print colorize('found multiple: %s' % formatted_results, 'green') else: print colorize(results[0], 'green') self[key] = results return results print colorize('FAILED', 'red') if not optional: raise Abort("%s not found. Searched in following places: %s" % (human_name, ''.join(['\n - ' + p for p in places]))) else: self[key] = None return results
def main(): e = Environment() e.load() conf = configure() try: current_command = sys.argv[1] except IndexError: current_command = None parser = argparse.ArgumentParser(prog='ano', formatter_class=FlexiFormatter, description=__doc__) subparsers = parser.add_subparsers() is_command = lambda x: inspect.isclass(x) and issubclass(x, Command ) and x != Command commands = [ cls(e) for _, cls in inspect.getmembers(ano.commands, is_command) ] for cmd in commands: p = subparsers.add_parser(cmd.name, formatter_class=FlexiFormatter, help=cmd.help_line) if current_command != cmd.name: continue cmd.setup_arg_parser(p) p.set_defaults(func=cmd.run, **conf.as_dict(cmd.name)) args = parser.parse_args() try: run_anywhere = "init clean list-models serial version" e.process_args(args) if current_command not in run_anywhere: if os.path.isdir(e.output_dir): # we have an output dir so we'll pretend this is a project folder None elif e.src_dir is None or not os.path.isdir(e.src_dir): raise Abort("No project found in this directory.") if current_command not in run_anywhere: # For valid projects create .build & lib if not os.path.isdir(e.build_dir): os.makedirs(e.build_dir) if not os.path.isdir(e.lib_dir): os.makedirs(e.lib_dir) with open('lib/.holder', 'w') as f: f.write("") args.func(args) except Abort as exc: print colorize(str(exc), 'red') sys.exit(1) except KeyboardInterrupt: print 'Terminated by user' finally: e.dump()