# -*- coding: utf-8 -*-

# Copyright (C) 2011 Romain Bignon, Laurent Bachelier
#
# This file is part of ass2m.
#
# ass2m is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ass2m is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with ass2m. If not, see <http://www.gnu.org/licenses/>.

import os
import sys

from ass2m.plugin import Plugin
from ass2m.cmd import Command


__all__ = ['ConfigPlugin']


class ResolveCmd(Command):
    DESCRIPTION = 'Get the storage name of a path'

    @staticmethod
    def configure_parser(parser):
        parser.add_argument('path', nargs='+')

    def cmd(self, args):
        for path in args.path:
            if not os.path.exists(path):
                print >>sys.stderr, 'Error: Path "%s" does not exist.' % path
                continue

            f = self.storage.get_file_from_realpath(os.path.realpath(path))
            if not f:
                print >>sys.stderr, 'Error: Path "%s" is not in working directory.' % path
                continue
            print '%s => %s' % (f.path, os.path.join(self.storage.path, f._get_confname()))


class ListConfigCmd(Command):
    DESCRIPTION = 'Get all the values of the global config'

    def cmd(self, args):
        c = self.storage.get_config()
        self._print_config(c.data)

    def _print_config(self, data):
        for sectionkey, section in data.iteritems():
            for key, value in section.iteritems():
                print "%s.%s=%s" % (sectionkey, key, value)


class ConfigPlugin(Plugin):
    def init(self):
        self.register_cli_command('config', 'Interact with the stored configurations')
        self.register_cli_command('config', 'resolve', ResolveCmd)
        self.register_cli_command('config', 'global', 'list', ListConfigCmd)

@@ -1633,16 +1633,119 @@
 onfig'%0A%0A
+    @staticmethod%0A    def configure_parser(parser):%0A        parser.add_argument('section', nargs='?')%0A%0A
     def 
@@ -1770,16 +1770,21 @@
        c
+onfig
  = self.
@@ -1836,13 +1836,32 @@
 ig(c
-.data
+onfig.data, args.section
 )%0A%0A 
@@ -1891,16 +1891,39 @@
 lf, data
+, filter_section = None
 ):%0A     
@@ -1962,32 +1962,107 @@
 ta.iteritems():%0A
+            if filter_section is None or sectionkey == filter_section:%0A    
             for 
@@ -2088,32 +2088,36 @@
 on.iteritems():%0A
+    
                 
@@ -2364,16 +2364,112 @@
 lveCmd)%0A
+        self.register_cli_command('config', 'global', 'Interact with the global configuration')%0A
         

Add optional argument to filter the config list