codekingpro/portable-devtools
115k
1# The MIT License (MIT)2#3# Copyright (c) 2015 Taka Okunishi4#5# Permission is hereby granted, free of charge, to any person obtaining a copy6# of this software and associated documentation files (the "Software"), to deal7# in the Software without restriction, including without limitation the rights8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9# copies of the Software, and to permit persons to whom the Software is10# furnished to do so, subject to the following conditions:11#12# The above copyright notice and this permission notice shall be included in13# all copies or substantial portions of the Software.14#15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21# SOFTWARE.22#23# Copyright © 2015-2018 Taka Okunishi <okunishinishi@gmail.com>.24# Copyright © 2020 Louis-Philippe Véronneau <pollo@debian.org>25 26import re27 28 29def uplowcase(string, case):30 """Convert string into upper or lower case.31 32 Args:33 string: String to convert.34 35 Returns:36 string: Uppercase or lowercase case string.37 38 """39 if case == 'up':40 return str(string).upper()41 elif case == 'low':42 return str(string).lower()43 44 45def capitalcase(string):46 """Convert string into capital case.47 First letters will be uppercase.48 49 Args:50 string: String to convert.51 52 Returns:53 string: Capital case string.54 55 """56 57 string = str(string)58 if not string:59 return string60 return uplowcase(string[0], 'up') + string[1:]61 62 63def camelcase(string):64 """ Convert string into camel case.65 66 Args:67 string: String to convert.68 69 Returns:70 string: Camel case string.71 72 """73 74 string = re.sub(r"^[\-_\.]", '', str(string))75 if not string:76 return string77 return (uplowcase(string[0], 'low')78 + re.sub(r"[\-_\.\s]([a-z0-9])",79 lambda matched: uplowcase(matched.group(1), 'up'),80 string[1:]))81 82 83def snakecase(string):84 """Convert string into snake case.85 Join punctuation with underscore86 87 Args:88 string: String to convert.89 90 Returns:91 string: Snake cased string.92 93 """94 95 string = re.sub(r"[\-\.\s]", '_', str(string))96 if not string:97 return string98 return (uplowcase(string[0], 'low')99 + re.sub(r"[A-Z0-9]",100 lambda matched: '_' + uplowcase(matched.group(0), 'low'),101 string[1:]))102 103 104def spinalcase(string):105 """Convert string into spinal case.106 Join punctuation with hyphen.107 108 Args:109 string: String to convert.110 111 Returns:112 string: Spinal cased string.113 114 """115 116 return re.sub(r"_", "-", snakecase(string))117 118 119def pascalcase(string):120 """Convert string into pascal case.121 122 Args:123 string: String to convert.124 125 Returns:126 string: Pascal case string.127 128 """129 130 return capitalcase(camelcase(string))131 