Mon, 20 Jun 2022 17:27:30 +0300
Add a basic message log
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
1 | #!/usr/bin/env python |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
2 | # coding: utf-8 |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
3 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
4 | ''' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
5 | Provides facilities to converting identifier cases. |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
6 | ''' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
7 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
8 | # |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
9 | # Copyright 2015 Teemu Piippo |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
10 | # All rights reserved. |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
11 | # |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
12 | # Redistribution and use in source and binary forms, with or without |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
13 | # modification, are permitted provided that the following conditions |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
14 | # are met: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
15 | # |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
16 | # 1. Redistributions of source code must retain the above copyright |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
17 | # notice, this list of conditions and the following disclaimer. |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
18 | # 2. Redistributions in binary form must reproduce the above copyright |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
19 | # notice, this list of conditions and the following disclaimer in the |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
20 | # documentation and/or other materials provided with the distribution. |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
21 | # 3. Neither the name of the copyright holder nor the names of its |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
22 | # contributors may be used to endorse or promote products derived from |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
23 | # this software without specific prior written permission. |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
24 | # |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
25 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
26 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
28 | # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
29 | # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
30 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
31 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
32 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
33 | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
35 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
36 | # |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
37 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
38 | import string |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
39 | import re |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
40 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
41 | def to_one_case (name, tolower): |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
42 | ''' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
43 | Convers name to either all uppercase or all lowercase (depending on the truth value of tolower), using underscores |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
44 | as delimiters. |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
45 | ''' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
46 | result = "" |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
47 | targetSet = (string.ascii_lowercase if tolower else string.ascii_uppercase) + string.digits |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | inverseSet = (string.ascii_uppercase if tolower else string.ascii_lowercase) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
49 | isUnderscorable = lambda ch: ch in string.ascii_uppercase \ |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
50 | or ch in string.whitespace or ch in string.punctuation |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
51 | previousWasUnderscorable = isUnderscorable (name[0]) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
52 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
53 | for ch in name: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
54 | if isUnderscorable (ch) and result != "" and not previousWasUnderscorable: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
55 | result += "_" |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
56 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
57 | if ch in inverseSet: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
58 | result += (ch.lower() if tolower else ch.upper()) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
59 | elif ch in targetSet: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
60 | result += ch |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
61 | previousWasUnderscorable = isUnderscorable (ch) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
62 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
63 | return result |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
64 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
65 | def to_camel_case (name, java = False): |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
66 | ''' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
67 | Converts name to camelcase. If java is False, the first letter will be lowercase, otherwise it will be uppercase. |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
68 | ''' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
69 | result = "" |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
70 | wantUpperCase = False |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
71 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
72 | # If it's all uppercase, make it all lowercase so that this algorithm can digest it |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
73 | if name == name.upper(): |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
74 | name = name.lower() |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
75 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
76 | for ch in name: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
77 | if ch == '_': |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
78 | wantUpperCase = True |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
79 | continue |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
80 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
81 | if wantUpperCase: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
82 | if ch in string.ascii_lowercase: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
83 | ch = ch.upper() |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
84 | wantUpperCase = False |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
85 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
86 | result += ch |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
87 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
88 | if java: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
89 | match = re.match (r'^([A-Z]+)([A-Z].+)$', result) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
90 | if match: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
91 | result = match.group (1).lower() + match.group (2) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
92 | else: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
93 | result = result[0].lower() + result[1:] |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
94 | else: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
95 | result = result[0].upper() + result[1:] |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
96 | return result |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
97 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
98 | case_styles = { |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
99 | 'lower': lambda name: to_one_case (name, tolower=True), |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
100 | 'upper': lambda name: to_one_case (name, tolower=False), |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
101 | 'camel': lambda name: to_camel_case (name, java=False), |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
102 | 'java': lambda name: to_camel_case (name, java=True), |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
103 | } |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
104 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
105 | def convert_case (name, style): |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
106 | ''' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
107 | Converts name to the given style. Style may be one of: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
108 | - 'lower': 'foo_barBaz' -> 'foo_bar_baz' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
109 | - 'upper': 'foo_barBaz' -> 'FOO_BAR_BAZ' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
110 | - 'camel': 'foo_barBaz' -> 'FooBarBaz' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
111 | - 'java': 'foo_barBaz' -> 'fooBarBaz' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
112 | ''' |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
113 | try: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
114 | stylefunc = case_styles[style] |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
115 | except KeyError: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
116 | validstyles = ', '.join (sorted (case_styles.keys())) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
117 | raise ValueError ('Unknown style "%s", should be one of: %s' % (style, validstyles)) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
118 | else: |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
119 | return stylefunc (name) |