<s> from os . path import join <EOL> from ConfigParser import SafeConfigParser <EOL> from collections import namedtuple <EOL> from textwrap import TextWrapper <EOL> from jig . conf import PLUGIN_CONFIG_FILENAME <EOL> from jig . exc import CommandError , ConfigKeyInvalid <EOL> from jig . commands . base import BaseCommand , plugins_by_bundle <EOL> from jig . commands . hints import NO_PLUGINS_INSTALLED , CHANGE_PLUGIN_SETTINGS <EOL> from jig . plugins import ( <EOL> get_jigconfig , set_jigconfig , PluginManager ) <EOL> try : <EOL> import argparse <EOL> except ImportError : <EOL> from backports import argparse <EOL> _parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' , <EOL> usage = '<STR_LIT>' ) <EOL> _subparsers = _parser . add_subparsers ( <EOL> title = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) <EOL> _listparser = _subparsers . add_parser ( <EOL> '<STR_LIT:list>' , help = '<STR_LIT>' , <EOL> usage = '<STR_LIT>' ) <EOL> _listparser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT:.>' , dest = '<STR_LIT:path>' , <EOL> help = '<STR_LIT>' ) <EOL> _listparser . set_defaults ( subcommand = '<STR_LIT:list>' ) <EOL> _aboutparser = _subparsers . add_parser ( <EOL> '<STR_LIT>' , help = '<STR_LIT>' , <EOL> usage = '<STR_LIT>' ) <EOL> _aboutparser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT:.>' , dest = '<STR_LIT:path>' , <EOL> help = '<STR_LIT>' ) <EOL> _aboutparser . set_defaults ( subcommand = '<STR_LIT>' ) <EOL> _setparser = _subparsers . add_parser ( <EOL> '<STR_LIT>' , help = '<STR_LIT>' , <EOL> usage = '<STR_LIT>' ) <EOL> _setparser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT:.>' , dest = '<STR_LIT:path>' , <EOL> help = '<STR_LIT>' ) <EOL> _setparser . add_argument ( <EOL> '<STR_LIT:key>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> _setparser . add_argument ( <EOL> '<STR_LIT:value>' , <EOL> help = '<STR_LIT>' ) <EOL> _setparser . set_defaults ( subcommand = '<STR_LIT>' ) <EOL> try : <EOL> from collections import OrderedDict <EOL> except ImportError : <EOL> from ordereddict import OrderedDict <EOL> def _get_plugin_config_section ( plugin_dir , section ) : <EOL> """<STR_LIT>""" <EOL> config_filename = join ( plugin_dir , PLUGIN_CONFIG_FILENAME ) <EOL> with open ( config_filename , '<STR_LIT:r>' ) as fh : <EOL> config = SafeConfigParser ( ) <EOL> config . readfp ( fh ) <EOL> if not config . has_section ( section ) : <EOL> return OrderedDict ( ) <EOL> return OrderedDict ( config . items ( section ) ) <EOL> SettingsMeta = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Command ( BaseCommand ) : <EOL> parser = _parser <EOL> def process ( self , argv ) : <EOL> subcommand = argv . subcommand <EOL> getattr ( self , subcommand ) ( argv ) <EOL> def _has_plugin ( self , pm , bundle_name , plugin_name ) : <EOL> """<STR_LIT>""" <EOL> for plugin in pm . plugins : <EOL> if plugin . name == plugin_name and plugin . bundle == bundle_name : <EOL> return True <EOL> return False <EOL> def _settings ( self , pm ) : <EOL> """<STR_LIT>""" <EOL> bundles = plugins_by_bundle ( pm ) <EOL> sort_bundles = sorted ( bundles . items ( ) , key = lambda b : b [ <NUM_LIT:0> ] ) <EOL> for name , plugins in sort_bundles : <EOL> sort_plugins = sorted ( plugins , key = lambda p : p . name ) <EOL> for plugin in sort_plugins : <EOL> local_config = OrderedDict ( plugin . config . items ( ) ) <EOL> default_config = _get_plugin_config_section ( <EOL> plugin . path , '<STR_LIT>' ) <EOL> settings_about = _get_plugin_config_section ( <EOL> plugin . path , '<STR_LIT>' ) <EOL> merged_config = OrderedDict ( ) <EOL> merged_config . update ( default_config ) <EOL> merged_config . update ( local_config ) <EOL> sort_config = sorted ( merged_config . items ( ) , key = lambda s : s [ <NUM_LIT:0> ] ) <EOL> for key , value in sort_config : <EOL> yield SettingsMeta ( <EOL> plugin , key , value , <EOL> default_config . get ( key , None ) , <EOL> settings_about . get ( key , None ) ) <EOL> def list ( self , argv ) : <EOL> """<STR_LIT>""" <EOL> path = argv . path <EOL> with self . out ( ) as printer : <EOL> config = get_jigconfig ( path ) <EOL> pm = PluginManager ( config ) <EOL> if not pm . plugins : <EOL> printer ( u'<STR_LIT>' ) <EOL> printer ( NO_PLUGINS_INSTALLED ) <EOL> return <EOL> settings = list ( self . _settings ( pm ) ) <EOL> for meta in settings : <EOL> printer ( <EOL> u'<STR_LIT>' . format ( <EOL> bundle = meta . plugin . bundle , plugin = meta . plugin . name , <EOL> config_key = meta . key , config_value = meta . value <EOL> ) <EOL> ) <EOL> if not settings : <EOL> printer ( u'<STR_LIT>' ) <EOL> printer ( CHANGE_PLUGIN_SETTINGS ) <EOL> def about ( self , argv ) : <EOL> """<STR_LIT>""" <EOL> path = argv . path <EOL> def wrap ( payload ) : <EOL> indent = '<STR_LIT:U+0020>' <EOL> tw = TextWrapper ( <EOL> width = <NUM_LIT> , <EOL> initial_indent = indent , <EOL> subsequent_indent = indent ) <EOL> return u'<STR_LIT:\n>' . join ( tw . wrap ( payload ) ) <EOL> with self . out ( ) as printer : <EOL> config = get_jigconfig ( path ) <EOL> pm = PluginManager ( config ) <EOL> if not pm . plugins : <EOL> printer ( u'<STR_LIT>' ) <EOL> printer ( NO_PLUGINS_INSTALLED ) <EOL> return <EOL> settings = list ( self . _settings ( pm ) ) <EOL> for meta in settings : <EOL> printer ( u'<STR_LIT>' . format ( <EOL> bundle = meta . plugin . bundle , plugin = meta . plugin . name , <EOL> config_key = meta . key ) ) <EOL> printer ( u'<STR_LIT>' . format ( meta . default ) ) <EOL> if meta . about : <EOL> printer ( wrap ( meta . about . strip ( ) ) ) <EOL> printer ( u'<STR_LIT>' ) <EOL> if not settings : <EOL> printer ( u'<STR_LIT>' ) <EOL> def set ( self , argv ) : <EOL> """<STR_LIT>""" <EOL> path = argv . path <EOL> key = argv . key <EOL> key_parts = key . split ( '<STR_LIT:.>' , <NUM_LIT:3> ) <EOL> config_value = argv . value <EOL> with self . out ( ) : <EOL> if len ( key_parts ) != <NUM_LIT:3> : <EOL> raise ConfigKeyInvalid ( <EOL> '<STR_LIT>' . format ( key ) ) <EOL> bundle , plugin , config_key = key_parts <EOL> config = get_jigconfig ( path ) <EOL> pm = PluginManager ( config ) <EOL> if not self . _has_plugin ( pm , bundle , plugin ) : <EOL> raise CommandError ( '<STR_LIT>' . format ( <EOL> plugin ) ) <EOL> section_name = '<STR_LIT>' . format ( <EOL> bundle , plugin ) <EOL> pm . config . set ( section_name , config_key , config_value ) <EOL> set_jigconfig ( path , pm . config ) </s>
