xulbux 1.5.5__py3-none-any.whl → 1.5.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of xulbux might be problematic. Click here for more details.
- xulbux/__init__.py +28 -18
- xulbux/{__help__.py → _cli_.py} +23 -45
- xulbux/_consts_.py +108 -111
- xulbux/xx_code.py +31 -28
- xulbux/xx_color.py +364 -205
- xulbux/xx_console.py +381 -0
- xulbux/xx_data.py +358 -261
- xulbux/xx_env_path.py +113 -0
- xulbux/xx_file.py +22 -16
- xulbux/xx_format_codes.py +156 -91
- xulbux/xx_json.py +38 -18
- xulbux/xx_path.py +38 -23
- xulbux/xx_regex.py +64 -29
- xulbux/xx_string.py +104 -47
- xulbux/xx_system.py +37 -26
- {xulbux-1.5.5.dist-info → xulbux-1.5.8.dist-info}/METADATA +23 -19
- xulbux-1.5.8.dist-info/RECORD +20 -0
- {xulbux-1.5.5.dist-info → xulbux-1.5.8.dist-info}/WHEEL +1 -1
- xulbux-1.5.8.dist-info/entry_points.txt +3 -0
- xulbux/xx_cmd.py +0 -240
- xulbux/xx_env_vars.py +0 -60
- xulbux-1.5.5.dist-info/RECORD +0 -20
- xulbux-1.5.5.dist-info/entry_points.txt +0 -2
- {xulbux-1.5.5.dist-info → xulbux-1.5.8.dist-info}/licenses/LICENSE +0 -0
xulbux/__init__.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"""
|
|
2
|
-
>>> import
|
|
2
|
+
>>> import xulbux as xx
|
|
3
3
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
4
4
|
• CUSTOM TYPES:
|
|
5
5
|
• rgba(int,int,int,float)
|
|
@@ -9,35 +9,45 @@
|
|
|
9
9
|
• FILE OPERATIONS xx.File
|
|
10
10
|
• JSON FILE OPERATIONS xx.Json
|
|
11
11
|
• SYSTEM ACTIONS xx.System
|
|
12
|
-
• MANAGE
|
|
13
|
-
•
|
|
14
|
-
• PRETTY PRINTING
|
|
15
|
-
•
|
|
12
|
+
• MANAGE THE ENV PATH VAR xx.EnvPath
|
|
13
|
+
• CONSOLE LOG AND ACTIONS xx.Console
|
|
14
|
+
• EASY PRETTY PRINTING xx.FormatCodes
|
|
15
|
+
• WORKING WITH COLORS xx.Color
|
|
16
16
|
• DATA OPERATIONS xx.Data
|
|
17
|
-
•
|
|
17
|
+
• STRING OPERATIONS xx.String
|
|
18
18
|
• CODE STRING OPERATIONS xx.Code
|
|
19
19
|
• REGEX PATTERN TEMPLATES xx.Regex
|
|
20
20
|
"""
|
|
21
21
|
|
|
22
|
-
__version__ =
|
|
23
|
-
__author__ =
|
|
24
|
-
__email__ =
|
|
25
|
-
__license__ =
|
|
26
|
-
__copyright__ =
|
|
27
|
-
__url__ =
|
|
28
|
-
__description__ =
|
|
22
|
+
__version__ = "1.5.8"
|
|
23
|
+
__author__ = "XulbuX"
|
|
24
|
+
__email__ = "xulbux.real@gmail.com"
|
|
25
|
+
__license__ = "MIT"
|
|
26
|
+
__copyright__ = "Copyright (c) 2024 XulbuX"
|
|
27
|
+
__url__ = "https://github.com/XulbuX-dev/Python/tree/main/Libraries/XulbuX"
|
|
28
|
+
__description__ = "A library which includes a lot of really helpful functions."
|
|
29
29
|
__all__ = [
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
"_consts_",
|
|
31
|
+
"xx_console",
|
|
32
|
+
"xx_code",
|
|
33
|
+
"xx_color",
|
|
34
|
+
"xx_data",
|
|
35
|
+
"xx_env_path",
|
|
36
|
+
"xx_file",
|
|
37
|
+
"xx_format_codes",
|
|
38
|
+
"xx_json",
|
|
39
|
+
"xx_path",
|
|
40
|
+
"xx_regex",
|
|
41
|
+
"xx_string",
|
|
42
|
+
"xx_system",
|
|
32
43
|
]
|
|
33
44
|
|
|
34
|
-
from .__help__ import help
|
|
35
45
|
from ._consts_ import *
|
|
36
|
-
from .xx_cmd import *
|
|
37
46
|
from .xx_code import *
|
|
38
47
|
from .xx_color import *
|
|
48
|
+
from .xx_console import *
|
|
39
49
|
from .xx_data import *
|
|
40
|
-
from .
|
|
50
|
+
from .xx_env_path import *
|
|
41
51
|
from .xx_file import *
|
|
42
52
|
from .xx_format_codes import *
|
|
43
53
|
from .xx_json import *
|
xulbux/{__help__.py → _cli_.py}
RENAMED
|
@@ -1,50 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
try: from .xx_cmd import *
|
|
6
|
-
except: from xx_cmd import *
|
|
1
|
+
from . import __version__
|
|
2
|
+
from ._consts_ import DEFAULT
|
|
3
|
+
from .xx_format_codes import FormatCodes
|
|
4
|
+
from .xx_console import Console
|
|
7
5
|
|
|
8
|
-
import os as _os
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def get_version(file:str = '__init__.py', var:str = '__version__') -> str:
|
|
14
|
-
try:
|
|
15
|
-
from . import var
|
|
16
|
-
return var
|
|
17
|
-
except ImportError:
|
|
18
|
-
init_path = _os.path.join(_os.path.dirname(__file__), file)
|
|
19
|
-
if _os.path.isfile(init_path):
|
|
20
|
-
with open(init_path, encoding='utf-8') as f:
|
|
21
|
-
for line in f:
|
|
22
|
-
if line.startswith(var): return line.split('=')[-1].strip().strip('\'"')
|
|
23
|
-
return 'unknown'
|
|
24
|
-
|
|
25
|
-
def help():
|
|
7
|
+
def help_command():
|
|
26
8
|
"""Show some info about the library, with a brief explanation of how to use it."""
|
|
27
9
|
color = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
10
|
+
"lib": DEFAULT.color["ice"],
|
|
11
|
+
"import": DEFAULT.color["red"],
|
|
12
|
+
"class": DEFAULT.color["lavender"],
|
|
13
|
+
"types": DEFAULT.color["lightblue"],
|
|
14
|
+
"punctuators": DEFAULT.color["darkgray"],
|
|
33
15
|
}
|
|
34
16
|
FormatCodes.print(
|
|
35
|
-
|
|
17
|
+
rf""" [_|b|#7075FF] __ __
|
|
36
18
|
[b|#7075FF] _ __ __ __/ / / /_ __ ___ __
|
|
37
19
|
[b|#7075FF] | |/ // / / / / / __ \/ / / | |/ /
|
|
38
|
-
[b|#7075FF] > , </ /_/ / /_/ /_/ / /_/ /> , <
|
|
39
|
-
[b|#7075FF]/_/|_|\____/\__/\____/\____//_/|_| [*|BG:{DEFAULT.color['gray']}|#000] v[b]{
|
|
20
|
+
[b|#7075FF] > , </ /_/ / /_/ /_/ / /_/ /> , <
|
|
21
|
+
[b|#7075FF]/_/|_|\____/\__/\____/\____//_/|_| [*|BG:{DEFAULT.color['gray']}|#000] v[b]{__version__} [*]
|
|
40
22
|
|
|
41
23
|
[i|{DEFAULT.color['coral']}]A TON OF COOL FUNCTIONS, YOU NEED![*]
|
|
42
24
|
|
|
43
25
|
[b|#75A2FF]Usage:[*]
|
|
44
26
|
[{color['punctuators']}]# GENERAL LIBRARY[*]
|
|
45
|
-
[{color['import']}]import [{color['lib']}]
|
|
27
|
+
[{color['import']}]import [{color['lib']}]xulbux [{color['import']}]as [{color['lib']}]xx[*]
|
|
46
28
|
[{color['punctuators']}]# CUSTOM TYPES[*]
|
|
47
|
-
[{color['import']}]from [{color['lib']}]
|
|
29
|
+
[{color['import']}]from [{color['lib']}]xulbux [{color['import']}]import [{color['lib']}]rgba[{color['punctuators']}], [{color['lib']}]hsla[{color['punctuators']}], [{color['lib']}]hexa[*]
|
|
48
30
|
|
|
49
31
|
[b|#75A2FF]Includes:[*]
|
|
50
32
|
[dim](•) CUSTOM TYPES:
|
|
@@ -55,20 +37,16 @@ def help():
|
|
|
55
37
|
[dim](•) FILE OPERATIONS [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]File[*]
|
|
56
38
|
[dim](•) JSON FILE OPERATIONS [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]Json[*]
|
|
57
39
|
[dim](•) SYSTEM ACTIONS [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]System[*]
|
|
58
|
-
[dim](•) MANAGE
|
|
59
|
-
[dim](•)
|
|
60
|
-
[dim](•) PRETTY PRINTING
|
|
61
|
-
[dim](•)
|
|
40
|
+
[dim](•) MANAGE THE ENV PATH VAR [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]EnvPath[*]
|
|
41
|
+
[dim](•) CONSOLE LOG AND ACTIONS [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]Console[*]
|
|
42
|
+
[dim](•) EASY PRETTY PRINTING [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]FormatCodes[*]
|
|
43
|
+
[dim](•) WORKING WITH COLORS [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]Color[*]
|
|
62
44
|
[dim](•) DATA OPERATIONS [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]Data[*]
|
|
63
|
-
[dim](•)
|
|
45
|
+
[dim](•) STRING OPERATIONS [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]String[*]
|
|
64
46
|
[dim](•) CODE STRING OPERATIONS [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]Code[*]
|
|
65
47
|
[dim](•) REGEX PATTERN TEMPLATES [{color['lib']}]xx[{color['punctuators']}].[{color['class']}]Regex[*]
|
|
66
48
|
[_]
|
|
67
49
|
[dim](Press any key to exit...)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if __name__ == '__main__':
|
|
74
|
-
help()
|
|
50
|
+
"""
|
|
51
|
+
)
|
|
52
|
+
Console.pause_exit(pause=True)
|
xulbux/_consts_.py
CHANGED
|
@@ -1,147 +1,144 @@
|
|
|
1
1
|
class DEFAULT:
|
|
2
2
|
|
|
3
|
-
text_color:str =
|
|
4
|
-
color:dict[str,str] = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
3
|
+
text_color: str = "#95B5FF"
|
|
4
|
+
color: dict[str, str] = {
|
|
5
|
+
"white": "#F1F2FF",
|
|
6
|
+
"lightgray": "#B6B7C0",
|
|
7
|
+
"gray": "#7B7C8D",
|
|
8
|
+
"darkgray": "#67686C",
|
|
9
|
+
"black": "#202125",
|
|
10
|
+
"red": "#FF606A",
|
|
11
|
+
"coral": "#FF7069",
|
|
12
|
+
"orange": "#FF876A",
|
|
13
|
+
"tangerine": "#FF9962",
|
|
14
|
+
"gold": "#FFAF60",
|
|
15
|
+
"yellow": "#FFD260",
|
|
16
|
+
"green": "#7EE787",
|
|
17
|
+
"teal": "#50EAAF",
|
|
18
|
+
"cyan": "#3EE6DE",
|
|
19
|
+
"ice": "#77EFEF",
|
|
20
|
+
"lightblue": "#60AAFF",
|
|
21
|
+
"blue": "#8085FF",
|
|
22
|
+
"lavender": "#9B7DFF",
|
|
23
|
+
"purple": "#AD68FF",
|
|
24
|
+
"magenta": "#C860FF",
|
|
25
|
+
"pink": "#EE60BB",
|
|
26
|
+
"rose": "#FF6090",
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
30
|
class CHARS:
|
|
33
31
|
|
|
34
32
|
# CODE TO SIGNAL, ALL CHARACTERS ARE ALLOWED
|
|
35
|
-
all =
|
|
33
|
+
all = "<*allowed>"
|
|
36
34
|
|
|
37
35
|
# DIGIT SETS
|
|
38
|
-
digits =
|
|
39
|
-
float_digits =
|
|
40
|
-
hex_digits =
|
|
36
|
+
digits = "0123456789"
|
|
37
|
+
float_digits = digits + "."
|
|
38
|
+
hex_digits = digits + "#abcdefABCDEF"
|
|
41
39
|
|
|
42
40
|
# LETTER CATEGORIES
|
|
43
|
-
lowercase =
|
|
44
|
-
lowercase_extended =
|
|
45
|
-
uppercase =
|
|
46
|
-
uppercase_extended =
|
|
41
|
+
lowercase = "abcdefghijklmnopqrstuvwxyz"
|
|
42
|
+
lowercase_extended = lowercase + "äëïöüÿàèìòùáéíóúýâêîôûãñõåæç"
|
|
43
|
+
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
44
|
+
uppercase_extended = uppercase + "ÄËÏÖÜÀÈÌÒÙÁÉÍÓÚÝÂÊÎÔÛÃÑÕÅÆÇß"
|
|
47
45
|
|
|
48
46
|
# COMBINED LETTER SETS
|
|
49
|
-
letters =
|
|
50
|
-
letters_extended =
|
|
47
|
+
letters = lowercase + uppercase
|
|
48
|
+
letters_extended = lowercase_extended + uppercase_extended
|
|
51
49
|
|
|
52
50
|
# ASCII sets
|
|
53
|
-
special_ascii =
|
|
54
|
-
special_ascii_extended =
|
|
55
|
-
standard_ascii =
|
|
56
|
-
full_ascii =
|
|
57
|
-
|
|
58
|
-
|
|
51
|
+
special_ascii = " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
|
52
|
+
special_ascii_extended = special_ascii + "ø£Ø×ƒªº¿®¬½¼¡«»░▒▓│┤©╣║╗╝¢¥┐└┴┬├─┼╚╔╩╦╠═╬¤ðÐı┘┌█▄¦▀µþÞ¯´≡±‗¾¶§÷¸°¨·¹³²■ "
|
|
53
|
+
standard_ascii = special_ascii + digits + letters
|
|
54
|
+
full_ascii = special_ascii_extended + digits + letters_extended
|
|
59
55
|
|
|
60
56
|
|
|
61
57
|
class ANSI:
|
|
62
58
|
|
|
63
59
|
global CHAR, START, SEP, END
|
|
64
60
|
|
|
65
|
-
CHAR
|
|
66
|
-
START = start =
|
|
67
|
-
SEP
|
|
68
|
-
END
|
|
61
|
+
CHAR = char = "\x1b"
|
|
62
|
+
START = start = "["
|
|
63
|
+
SEP = sep = ";"
|
|
64
|
+
END = end = "m"
|
|
65
|
+
modifier = {"lighten": "+l", "darken": "-d"}
|
|
69
66
|
|
|
70
|
-
def seq(parts:int = 1) -> str:
|
|
67
|
+
def seq(parts: int = 1) -> str:
|
|
71
68
|
"""Generate an ANSI sequence with `parts` amount of placeholders."""
|
|
72
|
-
return CHAR + START + SEP.join([
|
|
69
|
+
return CHAR + START + SEP.join(["{}" for _ in range(parts)]) + END
|
|
73
70
|
|
|
74
|
-
seq_color:str = CHAR + START +
|
|
75
|
-
seq_bg_color:str = CHAR + START +
|
|
71
|
+
seq_color: str = CHAR + START + "38" + SEP + "2" + SEP + "{}" + SEP + "{}" + SEP + "{}" + END
|
|
72
|
+
seq_bg_color: str = CHAR + START + "48" + SEP + "2" + SEP + "{}" + SEP + "{}" + SEP + "{}" + END
|
|
76
73
|
|
|
77
|
-
color_map:list[str] = [
|
|
74
|
+
color_map: list[str] = [
|
|
78
75
|
########### DEFAULT CONSOLE COLOR NAMES ############
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
76
|
+
"black",
|
|
77
|
+
"red",
|
|
78
|
+
"green",
|
|
79
|
+
"yellow",
|
|
80
|
+
"blue",
|
|
81
|
+
"magenta",
|
|
82
|
+
"cyan",
|
|
83
|
+
"white",
|
|
87
84
|
]
|
|
88
85
|
|
|
89
|
-
codes_map:dict[str|tuple[str], int] = {
|
|
86
|
+
codes_map: dict[str | tuple[str], int] = {
|
|
90
87
|
###################### RESETS ######################
|
|
91
|
-
|
|
92
|
-
(
|
|
93
|
-
(
|
|
94
|
-
(
|
|
95
|
-
(
|
|
96
|
-
(
|
|
97
|
-
(
|
|
98
|
-
(
|
|
99
|
-
(
|
|
100
|
-
(
|
|
101
|
-
(
|
|
88
|
+
"_": 0,
|
|
89
|
+
("_bold", "_b"): 22,
|
|
90
|
+
("_dim", "_d"): 22,
|
|
91
|
+
("_italic", "_i"): 23,
|
|
92
|
+
("_underline", "_u"): 24,
|
|
93
|
+
("_double-underline", "_du"): 24,
|
|
94
|
+
("_inverse", "_invert", "_in"): 27,
|
|
95
|
+
("_hidden", "_hide", "_h"): 28,
|
|
96
|
+
("_strikethrough", "_s"): 29,
|
|
97
|
+
("_color", "_c"): 39,
|
|
98
|
+
("_background", "_bg"): 49,
|
|
102
99
|
################### TEXT FORMATS ###################
|
|
103
|
-
(
|
|
104
|
-
(
|
|
105
|
-
(
|
|
106
|
-
(
|
|
107
|
-
(
|
|
108
|
-
(
|
|
109
|
-
(
|
|
110
|
-
(
|
|
100
|
+
("bold", "b"): 1,
|
|
101
|
+
("dim", "d"): 2,
|
|
102
|
+
("italic", "i"): 3,
|
|
103
|
+
("underline", "u"): 4,
|
|
104
|
+
("inverse", "invert", "in"): 7,
|
|
105
|
+
("hidden", "hide", "h"): 8,
|
|
106
|
+
("strikethrough", "s"): 9,
|
|
107
|
+
("double-underline", "du"): 21,
|
|
111
108
|
############## DEFAULT CONSOLE COLORS ##############
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
109
|
+
"black": 30,
|
|
110
|
+
"red": 31,
|
|
111
|
+
"green": 32,
|
|
112
|
+
"yellow": 33,
|
|
113
|
+
"blue": 34,
|
|
114
|
+
"magenta": 35,
|
|
115
|
+
"cyan": 36,
|
|
116
|
+
"white": 37,
|
|
120
117
|
########## BRIGHT DEFAULT CONSOLE COLORS ###########
|
|
121
|
-
(
|
|
122
|
-
(
|
|
123
|
-
(
|
|
124
|
-
(
|
|
125
|
-
(
|
|
126
|
-
(
|
|
127
|
-
(
|
|
128
|
-
(
|
|
118
|
+
("bright:black", "br:black"): 90,
|
|
119
|
+
("bright:red", "br:red"): 91,
|
|
120
|
+
("bright:green", "br:green"): 92,
|
|
121
|
+
("bright:yellow", "br:yellow"): 93,
|
|
122
|
+
("bright:blue", "br:blue"): 94,
|
|
123
|
+
("bright:magenta", "br:magenta"): 95,
|
|
124
|
+
("bright:cyan", "br:cyan"): 96,
|
|
125
|
+
("bright:white", "br:white"): 97,
|
|
129
126
|
######## DEFAULT CONSOLE BACKGROUND COLORS #########
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
127
|
+
"bg:black": 40,
|
|
128
|
+
"bg:red": 41,
|
|
129
|
+
"bg:green": 42,
|
|
130
|
+
"bg:yellow": 43,
|
|
131
|
+
"bg:blue": 44,
|
|
132
|
+
"bg:magenta": 45,
|
|
133
|
+
"bg:cyan": 46,
|
|
134
|
+
"bg:white": 47,
|
|
138
135
|
##### BRIGHT DEFAULT CONSOLE BACKGROUND COLORS #####
|
|
139
|
-
(
|
|
140
|
-
(
|
|
141
|
-
(
|
|
142
|
-
(
|
|
143
|
-
(
|
|
144
|
-
(
|
|
145
|
-
(
|
|
146
|
-
(
|
|
136
|
+
("bg:bright:black", "bg:br:black"): 100,
|
|
137
|
+
("bg:bright:red", "bg:br:red"): 101,
|
|
138
|
+
("bg:bright:green", "bg:br:green"): 102,
|
|
139
|
+
("bg:bright:yellow", "bg:br:yellow"): 103,
|
|
140
|
+
("bg:bright:blue", "bg:br:blue"): 104,
|
|
141
|
+
("bg:bright:magenta", "bg:br:magenta"): 105,
|
|
142
|
+
("bg:bright:cyan", "bg:br:cyan"): 106,
|
|
143
|
+
("bg:bright:white", "bg:br:white"): 107,
|
|
147
144
|
}
|
xulbux/xx_code.py
CHANGED
|
@@ -1,28 +1,20 @@
|
|
|
1
|
-
from .xx_string import
|
|
2
|
-
from .xx_regex import
|
|
3
|
-
from .xx_data import
|
|
1
|
+
from .xx_string import String
|
|
2
|
+
from .xx_regex import Regex
|
|
3
|
+
from .xx_data import Data
|
|
4
4
|
|
|
5
5
|
import regex as _rx
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
8
|
class Code:
|
|
11
9
|
|
|
12
10
|
@staticmethod
|
|
13
|
-
def
|
|
14
|
-
"""Replaces all special space characters with normal spaces.<br>
|
|
15
|
-
Also replaces tab characters with `tab_spaces` spaces."""
|
|
16
|
-
return string.replace('\t', ' ' * tab_spaces).replace('\u2000', ' ').replace('\u2001', ' ').replace('\u2002', ' ').replace('\u2003', ' ').replace('\u2004', ' ').replace('\u2005', ' ').replace('\u2006', ' ').replace('\u2007', ' ').replace('\u2008', ' ').replace('\u2009', ' ').replace('\u200A', ' ')
|
|
17
|
-
|
|
18
|
-
@staticmethod
|
|
19
|
-
def add_indent(code:str, indent:int) -> str:
|
|
11
|
+
def add_indent(code: str, indent: int) -> str:
|
|
20
12
|
"""Adds `indent` spaces at the beginning of each line."""
|
|
21
|
-
indented_lines = [
|
|
22
|
-
return
|
|
13
|
+
indented_lines = [" " * indent + line for line in code.splitlines()]
|
|
14
|
+
return "\n".join(indented_lines)
|
|
23
15
|
|
|
24
16
|
@staticmethod
|
|
25
|
-
def get_tab_spaces(code:str) -> int:
|
|
17
|
+
def get_tab_spaces(code: str) -> int:
|
|
26
18
|
"""Will try to get the amount of spaces used for indentation."""
|
|
27
19
|
code_lines = String.get_string_lines(code, remove_empty_lines=True)
|
|
28
20
|
indents = [len(line) - len(line.lstrip()) for line in code_lines]
|
|
@@ -30,42 +22,50 @@ class Code:
|
|
|
30
22
|
return min(non_zero_indents) if non_zero_indents else 0
|
|
31
23
|
|
|
32
24
|
@staticmethod
|
|
33
|
-
def change_tab_size(code:str, new_tab_size:int, remove_empty_lines:bool = False) -> str:
|
|
25
|
+
def change_tab_size(code: str, new_tab_size: int, remove_empty_lines: bool = False) -> str:
|
|
34
26
|
"""Replaces all tabs with `new_tab_size` spaces.<br>
|
|
35
|
-
If `remove_empty_lines` is `True`, empty lines will be removed in the process.
|
|
27
|
+
If `remove_empty_lines` is `True`, empty lines will be removed in the process.
|
|
28
|
+
"""
|
|
36
29
|
code_lines = String.get_string_lines(code, remove_empty_lines=True)
|
|
37
30
|
lines = code_lines if remove_empty_lines else String.get_string_lines(code)
|
|
38
31
|
tab_spaces = Code.get_tab_spaces(code)
|
|
39
32
|
if (tab_spaces == new_tab_size) or tab_spaces == 0:
|
|
40
33
|
if remove_empty_lines:
|
|
41
|
-
return
|
|
34
|
+
return "\n".join(code_lines)
|
|
42
35
|
return code
|
|
43
36
|
result = []
|
|
44
37
|
for line in lines:
|
|
45
38
|
stripped = line.lstrip()
|
|
46
39
|
indent_level = (len(line) - len(stripped)) // tab_spaces
|
|
47
|
-
new_indent =
|
|
40
|
+
new_indent = " " * (indent_level * new_tab_size)
|
|
48
41
|
result.append(new_indent + stripped)
|
|
49
|
-
return
|
|
42
|
+
return "\n".join(result)
|
|
50
43
|
|
|
51
44
|
@staticmethod
|
|
52
|
-
def get_func_calls(code:str) -> list:
|
|
45
|
+
def get_func_calls(code: str) -> list:
|
|
53
46
|
"""Will try to get all function calls and return them as a list."""
|
|
54
|
-
funcs
|
|
47
|
+
funcs = _rx.findall(r"(?i)" + Regex.func_call(), code)
|
|
48
|
+
nested_func_calls = []
|
|
55
49
|
for _, func_attrs in funcs:
|
|
56
|
-
nested_calls = _rx.findall(r
|
|
50
|
+
nested_calls = _rx.findall(r"(?i)" + Regex.func_call(), func_attrs)
|
|
57
51
|
if nested_calls:
|
|
58
52
|
nested_func_calls.extend(nested_calls)
|
|
59
53
|
return Data.remove_duplicates(funcs + nested_func_calls)
|
|
60
54
|
|
|
61
55
|
@staticmethod
|
|
62
|
-
def is_js(code:str, funcs:list = [
|
|
56
|
+
def is_js(code: str, funcs: list = ["__", "$t", "$lang"]) -> bool:
|
|
63
57
|
"""Will check if the code is likely to be JavaScript."""
|
|
64
|
-
funcs =
|
|
65
|
-
js_pattern = _rx.compile(
|
|
58
|
+
funcs = "|".join(funcs)
|
|
59
|
+
js_pattern = _rx.compile(
|
|
60
|
+
Regex.outside_strings(
|
|
61
|
+
r"""^(?:
|
|
66
62
|
(\$[\w_]+)\s* # JQUERY-STYLE VARIABLES
|
|
67
63
|
|(\$[\w_]+\s*\() # JQUERY-STYLE FUNCTION CALLS
|
|
68
|
-
|((
|
|
64
|
+
|(("""
|
|
65
|
+
+ funcs
|
|
66
|
+
+ r")"
|
|
67
|
+
+ Regex.brackets("()")
|
|
68
|
+
+ r"""\s*) # PREDEFINED FUNCTION CALLS
|
|
69
69
|
|(\bfunction\s*\() # FUNCTION DECLARATIONS
|
|
70
70
|
|(\b(var|let|const)\s+[\w_]+\s*=) # VARIABLE DECLARATIONS
|
|
71
71
|
|(\b(if|for|while|switch)\s*\() # CONTROL STRUCTURES
|
|
@@ -98,5 +98,8 @@ class Code:
|
|
|
98
98
|
|(\bdelete\b) # DELETE OPERATOR
|
|
99
99
|
|(\btypeof\b) # TYPEOF OPERATOR
|
|
100
100
|
|(\bvoid\b) # VOID OPERATOR
|
|
101
|
-
)[\s\S]*$
|
|
101
|
+
)[\s\S]*$"""
|
|
102
|
+
),
|
|
103
|
+
_rx.VERBOSE | _rx.IGNORECASE,
|
|
104
|
+
)
|
|
102
105
|
return bool(js_pattern.fullmatch(code))
|