# -*- coding: utf-8 -*-
from chibi.file import Chibi_file
from chibi_sshd.snippets import to_string, from_string
from chibi_hybrid import Class_property


class Chibi_sshd_conf( Chibi_file ):
    @Class_property
    def default( cls ):
        """
        atajo para leer el sshd config del sistema
        """
        return cls( "/etc/ssh/sshd_config" )

    def read( self ):
        string = super().read()
        result = from_string( string )
        return result

    def write( self, data ):
        result = to_string( data )
        super().write( result )


class Chibi_authorized_keys( Chibi_file ):
    @Class_property
    def default( cls ):
        """
        atajo para leer el las llaves autorizadas del user
        """
        return cls( "~/.ssh/authorized_keys" )

    def read( self ):
        string = super().read()
        result = string.split( '\n' )
        return result

    def write( self, data ):
        if isinstance( data, list ):
            result = "\n".join( data )
        else:
            raise NotImplementedError
        super().write( result )

    def append( self, data ):
        if isinstance( data, str ):
            current = self.read()
            current.append( data )
            self.write( current )
        else:
            raise NotImplementedError
