# -*- coding: utf-8 -*-
'''
Created : 2015-04-03

@author: Eric Lapouyade
'''

from textops import TextOp
import re

class length(TextOp): fn = staticmethod(lambda text: len(text))
class echo(TextOp): fn = staticmethod(lambda text: text)

class splitln(TextOp):
    @classmethod
    def op(cls,text,*args,**kwargs):
	return cls._tolist(text)

class StrOp(TextOp):
    @classmethod
    def op(cls,text,*args,**kwargs):
        if isinstance(text, basestring):
            return cls.fn(text,*args,**kwargs)
        elif isinstance(text, list):
            return [ cls.fn(line,*args,**kwargs) for line in text ]
        return cls.gop(text,*args,**kwargs)
    @classmethod
    def gop(cls,text,*args,**kwargs):
        for line in text:
            yield cls.fn(line,*args,**kwargs)

class cut(StrOp):
    @classmethod
    def split(cls, text, sep):
        return text.split(sep)

    @classmethod
    def fn(cls, text, sep=None, col=None, not_present_value='', *args,**kwargs):
        if isinstance(col, basestring):
            col = [int(i) for i in col.split(',')]
        if isinstance(col,(list,tuple)):
            nbcol = len(col)
            line_cols = cls.split(text,sep)
            nblinecol = len(line_cols)
            return [ line_cols[c] if c < nblinecol else not_present_value for c in col ]
        else:
            line_cols = cls.split(text,sep)
            nblinecol = len(line_cols)
            if col == None:
                return line_cols
            elif col < nblinecol:
                return line_cols[col]
            else:
                return not_present_value

class cutre(cut):
    @classmethod
    def split(cls, text, sep):
        if hasattr(sep,'match'):
            return sep.split(text)
        return re.split(sep,text)

class cutca(cut):
    @classmethod
    def split(cls, text, sep):
        if hasattr(sep,'match'):
            m = sep.match(text)
        else:
            m = re.match(sep,text)
        return m.groups() if m else []

class cutdct(cut):
    @classmethod
    def split(cls, text, sep):
        if hasattr(sep,'match'):
            m = sep.match(text)
        else:
            m = re.match(sep,text)
        return m.groupdict() if m else {}

class cutkv(cut):
    @classmethod
    def split(cls, text, sep, key_name = 'key'):
        if hasattr(sep,'match'):
            m = sep.match(text)
        else:
            m = re.match(sep,text)
        if m:
            dct = m.groupdict()
            kv = dct.get(key_name)
            if kv:
                return { kv : dct }
        return {}
@@ -831,35 +831,51 @@
 t(cls, text, sep
+, *args,**kwargs
 ):%0A
-
         return t
@@ -1183,32 +1183,48 @@
 s.split(text,sep
+, *args,**kwargs
 )%0A            nb
@@ -1393,17 +1393,70 @@
 text,sep
-)
+, *args,**kwargs)%0A            print 'fn :',args,kwargs
 %0A       
@@ -1735,32 +1735,48 @@
 t(cls, text, sep
+, *args,**kwargs
 ):%0A        if ha
@@ -1920,32 +1920,48 @@
 t(cls, text, sep
+, *args,**kwargs
 ):%0A        if ha
@@ -2165,16 +2165,32 @@
 ext, sep
+, *args,**kwargs
 ):%0A     
@@ -2422,18 +2422,91 @@
  = 'key'
-):
+, *args,**kwargs):%0A        # Use named 'key_name' parameter, not postionnal
 %0A       

fix cutkv key_name params