Added default pipes

This commit is contained in:
2022-05-22 20:27:58 +02:00
parent 824d491ebc
commit cfb8838c73
32 changed files with 190 additions and 65 deletions

View File

@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
"""
cpl-core sh-edraft Common Python library
~~~~~~~~~~~~~~~~~~~
sh-edraft Common Python library
:copyright: (c) 2020 - 2022 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'cpl_core.pipes'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.7.dev1'
from collections import namedtuple
# imports:
from .bool_pipe import BoolPipe
from .first_char_to_lower_pipe import FirstCharToLowerPipe
from .first_to_upper_pipe import FirstToUpperPipe
from .ip_address_pipe import IPAddressPipe
from .pipe_abc import PipeABC
from .to_camel_case_pipe import ToCamelCasePipe
from .to_snake_case_pipe import ToSnakeCasePipe
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='7.dev1')

View File

@@ -0,0 +1,9 @@
from cpl_core.pipes.pipe_abc import PipeABC
class BoolPipe(PipeABC):
def __init__(self): pass
def transform(self, value: bool, *args):
return 'True' if value else 'False'

View File

@@ -0,0 +1,20 @@
from cpl_core.pipes.pipe_abc import PipeABC
class FirstCharToLowerPipe(PipeABC):
def __init__(self): pass
def transform(self, value: any, *args):
r"""Converts first char to lower
Parameter
---------
value: :class:`str`
String to convert
Returns
-------
String with first char as lower
"""
return f'{value[0].lower()}{value[1:]}'

View File

@@ -0,0 +1,20 @@
from cpl_core.pipes.pipe_abc import PipeABC
class FirstToUpperPipe(PipeABC):
def __init__(self): pass
def transform(self, value: str, *args):
r"""Converts first char to upper
Parameter
---------
chars: :class:`str`
String to convert
Returns
-------
String with first char as upper
"""
return f'{value[0].upper()}{value[1:]}'

View File

@@ -0,0 +1,24 @@
from cpl_core.pipes.pipe_abc import PipeABC
class IPAddressPipe(PipeABC):
def __init__(self): pass
def transform(self, value: list[int], *args):
string = ""
if len(value) != 4:
raise Exception('Invalid IP')
for i in range(0, len(value)):
byte = value[i]
if byte > 255:
raise Exception('Invalid IP')
if i == len(value) - 1:
string += f'{byte}'
else:
string += f'{byte}.'
return string

View File

@@ -0,0 +1,10 @@
from abc import ABC, abstractmethod
class PipeABC(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
def transform(self, value: any, *args): pass

View File

@@ -0,0 +1,28 @@
import string
from cpl_core.pipes import PipeABC
class ToCamelCasePipe(PipeABC):
def __init__(self): pass
def transform(self, value: str, *args) -> str:
r"""Converts string to camel case
Parameter
---------
chars: :class:`str`
String to convert
Returns
-------
String converted to CamelCase
"""
converted_name = value
char_set = string.punctuation + ' '
for char in char_set:
if char in converted_name:
converted_name = ''.join(word.title() for word in converted_name.split(char))
return converted_name

View File

@@ -0,0 +1,29 @@
import re
from cpl_core.pipes import PipeABC
class ToSnakeCasePipe(PipeABC):
def __init__(self): pass
def transform(self, value: str, *args) -> str:
r"""Converts string to snake case
Parameter
---------
chars: :class:`str`
String to convert
Returns
-------
String converted to snake_case
"""
# convert to train-case to CamelCase
if '-' in value:
value = ''.join(word.title() for word in value.split('-'))
pattern1 = re.compile(r'(.)([A-Z][a-z]+)')
pattern2 = re.compile(r'([a-z0-9])([A-Z])')
file_name = re.sub(pattern1, r'\1_\2', value)
return re.sub(pattern2, r'\1_\2', file_name).lower()