rahavard 0.0.49__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 rahavard might be problematic. Click here for more details.
- rahavard/__init__.py +41 -0
- rahavard/utils.py +627 -0
- rahavard-0.0.49.dist-info/LICENSE +21 -0
- rahavard-0.0.49.dist-info/METADATA +28 -0
- rahavard-0.0.49.dist-info/RECORD +7 -0
- rahavard-0.0.49.dist-info/WHEEL +5 -0
- rahavard-0.0.49.dist-info/top_level.txt +1 -0
rahavard/__init__.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from rahavard.utils import (
|
|
2
|
+
DU_CMD,
|
|
3
|
+
HMS_REGEX,
|
|
4
|
+
JALALI_FORMAT,
|
|
5
|
+
YMD_REGEX,
|
|
6
|
+
|
|
7
|
+
contains_ymd,
|
|
8
|
+
is_ymd,
|
|
9
|
+
starts_with_ymdhms,
|
|
10
|
+
|
|
11
|
+
calculate_offset,
|
|
12
|
+
comes_from_htmx,
|
|
13
|
+
convert_byte,
|
|
14
|
+
convert_millisecond,
|
|
15
|
+
convert_second,
|
|
16
|
+
convert_string_True_False_None_0,
|
|
17
|
+
convert_timestamp_to_jalali,
|
|
18
|
+
convert_to_jalali,
|
|
19
|
+
convert_to_second,
|
|
20
|
+
create_id_for_htmx_indicator,
|
|
21
|
+
get_date_time_live,
|
|
22
|
+
get_list_of_files,
|
|
23
|
+
get_percent,
|
|
24
|
+
intcomma_persian,
|
|
25
|
+
persianize,
|
|
26
|
+
short_uuid,
|
|
27
|
+
sort_dict,
|
|
28
|
+
to_tilda,
|
|
29
|
+
|
|
30
|
+
make_active,
|
|
31
|
+
make_inactive,
|
|
32
|
+
|
|
33
|
+
abort,
|
|
34
|
+
add_yearmonthday_force,
|
|
35
|
+
colorize,
|
|
36
|
+
get_command,
|
|
37
|
+
get_command_log_file,
|
|
38
|
+
is_allowed,
|
|
39
|
+
keyboard_interrupt_handler,
|
|
40
|
+
log,
|
|
41
|
+
)
|
rahavard/utils.py
ADDED
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
from django.conf import settings
|
|
2
|
+
from django.contrib import admin
|
|
3
|
+
from django.core.management.base import CommandError
|
|
4
|
+
from django.http import HttpResponse
|
|
5
|
+
|
|
6
|
+
from datetime import datetime
|
|
7
|
+
from math import floor, log as math_log, pow as math_pow
|
|
8
|
+
from os import path, listdir, getenv
|
|
9
|
+
from re import match, sub
|
|
10
|
+
from uuid import uuid4
|
|
11
|
+
|
|
12
|
+
from convert_numbers import english_to_persian
|
|
13
|
+
from jdatetime import datetime as jdt
|
|
14
|
+
from natsort import natsorted
|
|
15
|
+
|
|
16
|
+
import jdatetime
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
JALALI_FORMAT = '%A %H %M %S %d %m %Y'
|
|
20
|
+
|
|
21
|
+
YMD_REGEX = r'[0-9]{4}-[0-9]{2}-[0-9]{2}'
|
|
22
|
+
HMS_REGEX = r'[0-9]{2}:[0-9]{2}:[0-9]{2}'
|
|
23
|
+
|
|
24
|
+
if not settings.DEBUG:
|
|
25
|
+
DU_CMD = 'du -h -s -x -A'
|
|
26
|
+
else:
|
|
27
|
+
DU_CMD = 'du -h -s -x'
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def contains_ymd(string):
|
|
31
|
+
'''
|
|
32
|
+
returns match for:
|
|
33
|
+
.../general/2024-06-02/...
|
|
34
|
+
.../2024-06-02/...
|
|
35
|
+
'''
|
|
36
|
+
return match(f'^.*{YMD_REGEX}.*$', string)
|
|
37
|
+
|
|
38
|
+
def is_ymd(string):
|
|
39
|
+
'''
|
|
40
|
+
returns match for 2024-06-02
|
|
41
|
+
'''
|
|
42
|
+
return match(f'^{YMD_REGEX}$', string)
|
|
43
|
+
|
|
44
|
+
def starts_with_ymdhms(string):
|
|
45
|
+
'''
|
|
46
|
+
returns match for: 2024-09-08 16:14:30 SOME (user/notice) ...
|
|
47
|
+
returns None for: Some-DC (auth/info) [sth] Exiting on signal...
|
|
48
|
+
'''
|
|
49
|
+
return match(f'^{YMD_REGEX} {HMS_REGEX} ', string)
|
|
50
|
+
|
|
51
|
+
## ---------------------------------
|
|
52
|
+
|
|
53
|
+
def calculate_offset(page_number, limit_to_show):
|
|
54
|
+
return (page_number - 1) * limit_to_show ## 0/25/etc.
|
|
55
|
+
|
|
56
|
+
def comes_from_htmx(request):
|
|
57
|
+
return 'HX-Request' in request.headers
|
|
58
|
+
|
|
59
|
+
## https://stackoverflow.com/questions/5194057/better-way-to-convert-file-sizes-in-python
|
|
60
|
+
def convert_byte(size_in_bytes, to_persian=False):
|
|
61
|
+
if (size_in_bytes == 0):
|
|
62
|
+
if to_persian:
|
|
63
|
+
return '. بایت'
|
|
64
|
+
return '0B'
|
|
65
|
+
|
|
66
|
+
if to_persian:
|
|
67
|
+
suff = [
|
|
68
|
+
'بایت', 'کیلوبایت', 'مگابایت', 'گیگابایت', 'ترابایت',
|
|
69
|
+
'پتابایت', 'اگزابایت', 'زتابایت', 'یوتابایت',
|
|
70
|
+
]
|
|
71
|
+
else:
|
|
72
|
+
suff = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
73
|
+
i = int(floor(math_log(size_in_bytes, 1024)))
|
|
74
|
+
p = math_pow(1024, i)
|
|
75
|
+
conv = f'{float(size_in_bytes / p):.1f}'
|
|
76
|
+
|
|
77
|
+
## remove trailing .0 or .00
|
|
78
|
+
reg = r'^[0-9]+\.0+$'
|
|
79
|
+
if match(reg, conv):
|
|
80
|
+
conv = sub(r'\.0+$', '', conv)
|
|
81
|
+
|
|
82
|
+
if to_persian:
|
|
83
|
+
return f'{persianize(conv)} {suff[i]}'
|
|
84
|
+
|
|
85
|
+
return f'{conv}{suff[i]}'
|
|
86
|
+
|
|
87
|
+
def convert_millisecond(ms, verbose=True):
|
|
88
|
+
return convert_second(
|
|
89
|
+
ms / 1000, ## milliseconds -> seconds
|
|
90
|
+
verbose=verbose,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
def convert_second(seconds, verbose=True):
|
|
94
|
+
## used float instead of int
|
|
95
|
+
## to prevent turning 0.56 into 0
|
|
96
|
+
seconds = float(seconds)
|
|
97
|
+
|
|
98
|
+
if seconds == 0:
|
|
99
|
+
if verbose:
|
|
100
|
+
return '0'
|
|
101
|
+
else:
|
|
102
|
+
return '0:00'
|
|
103
|
+
|
|
104
|
+
if seconds < 1:
|
|
105
|
+
if verbose:
|
|
106
|
+
return '~0'
|
|
107
|
+
else:
|
|
108
|
+
return '~0:00'
|
|
109
|
+
|
|
110
|
+
ss = f'{int(seconds % 60):02}'
|
|
111
|
+
mi = f'{int(seconds / 60 % 60):02}'
|
|
112
|
+
hh = f'{int(seconds / 3600 % 24):02}'
|
|
113
|
+
dd = f'{int(seconds / 3600 / 24 % 30):02}'
|
|
114
|
+
mo = f'{int(seconds / 3600 / 24 / 30 % 12):02}'
|
|
115
|
+
yy = f'{int(seconds / 3600 / 24 / 30 / 12):02}'
|
|
116
|
+
|
|
117
|
+
if yy == '00' and mo == '00' and dd == '00':
|
|
118
|
+
if verbose: result = f'{hh} hrs, {mi} mins and {ss} secs'
|
|
119
|
+
else: result = f'{hh}:{mi}:{ss}'
|
|
120
|
+
elif yy == '00' and mo == '00':
|
|
121
|
+
if verbose: result = f'{dd} days, {hh} hrs and {mi} mins'
|
|
122
|
+
else: result = f'{dd}:{hh}:{mi}:{ss}'
|
|
123
|
+
elif yy == '00':
|
|
124
|
+
if verbose: result = f'{mo} months, {dd} days and {hh} hrs'
|
|
125
|
+
else: result = f'{mo}:{dd}:{hh}:{mi}:{ss}'
|
|
126
|
+
else:
|
|
127
|
+
if verbose: result = f'{yy} years, {mo} months and {dd} days'
|
|
128
|
+
else: result = f'{yy}:{mo}:{dd}:{hh}:{mi}:{ss}'
|
|
129
|
+
|
|
130
|
+
if verbose:
|
|
131
|
+
## remove items whose values are 00, and adjust comma and 'and'
|
|
132
|
+
result = sub(r'00 [a-z]+s, ', r'', result)
|
|
133
|
+
result = sub(r'00 [a-z]+s and ', r'', result)
|
|
134
|
+
result = sub(r'00 [a-z]+s$', r'', result)
|
|
135
|
+
result = sub(r', ([0-9][0-9] [a-z]+s )', r' and \1', result)
|
|
136
|
+
result = sub(r'and 00 [a-z]+s ', r'', result)
|
|
137
|
+
result = sub(r' and $', r'', result)
|
|
138
|
+
result = sub(r', ([0-9][0-9] [a-z]+)$', r' and \1', result)
|
|
139
|
+
result = sub(r' and ([0-9][0-9] [a-z]+) and', r', \1 and', result)
|
|
140
|
+
result = sub(r', +$', r'', result)
|
|
141
|
+
result = sub(r', ([0-9][0-9] [a-z]+s)$', r' and \1', result)
|
|
142
|
+
|
|
143
|
+
## remove plural s when value is 01
|
|
144
|
+
result = sub(r'(01 [a-z]+)s ', r'\1 ', result)
|
|
145
|
+
result = sub(r'(01 [a-z]+)s, ', r'\1, ', result)
|
|
146
|
+
result = sub(r'(01 [a-z]+)s$', r'\1', result)
|
|
147
|
+
|
|
148
|
+
result = sub(r', 0([0-9])', r', \1', result) ## ..., 01 hour, ... -> ..., 1 hour, ...
|
|
149
|
+
result = sub(r'and 0([0-9])', r'and \1', result) ## ... and 05 hrs ... -> ... and 5 hrs ... (this seems to be a bug in the original function)
|
|
150
|
+
|
|
151
|
+
else:
|
|
152
|
+
result = sub(r'^0+:0([0-9]):', r'\1:', result) ## 0:00:12 -> 0:12
|
|
153
|
+
## 0:08:12 -> 8:12
|
|
154
|
+
result = sub(r'^0+:([1-9])([0-9]):', r'\1\2:', result) ## 0:10:12 -> 10:12
|
|
155
|
+
|
|
156
|
+
## 02 days, ... -> 2 days, ...
|
|
157
|
+
## 01:23 -> 1:23
|
|
158
|
+
result = sub(r'^0([0-9])', r'\1', result)
|
|
159
|
+
|
|
160
|
+
return result
|
|
161
|
+
|
|
162
|
+
def convert_string_True_False_None_0(item):
|
|
163
|
+
'''
|
|
164
|
+
'True' -> True
|
|
165
|
+
'False' -> False
|
|
166
|
+
'None' -> None
|
|
167
|
+
'0' -> 0
|
|
168
|
+
'''
|
|
169
|
+
|
|
170
|
+
if item in ['True', 'False', 'None', '0']:
|
|
171
|
+
return {
|
|
172
|
+
'True': True,
|
|
173
|
+
'False': False,
|
|
174
|
+
'None': None,
|
|
175
|
+
'0': 0,
|
|
176
|
+
}.get(item)
|
|
177
|
+
return item
|
|
178
|
+
|
|
179
|
+
def convert_timestamp_to_jalali(tmstmp=None):
|
|
180
|
+
'''
|
|
181
|
+
takes timestamp, e.g. 1682598113
|
|
182
|
+
and converts it to jalali in this format: چهارشنبه ۰۷:۰۶:۳۳ ۳۰-/۰۱/۱۴۰۲
|
|
183
|
+
'''
|
|
184
|
+
|
|
185
|
+
if not tmstmp:
|
|
186
|
+
return ''
|
|
187
|
+
|
|
188
|
+
jdatetime.set_locale('fa_IR')
|
|
189
|
+
|
|
190
|
+
jalali_object = jdt.fromtimestamp(int(tmstmp))
|
|
191
|
+
w, h, mi, s, d, mo, y = jalali_object.strftime(JALALI_FORMAT).split()
|
|
192
|
+
|
|
193
|
+
return f'{w} {english_to_persian(h)}:{english_to_persian(mi)}:{english_to_persian(s)} {english_to_persian(y)}/{english_to_persian(mo)}/{english_to_persian(d)}'
|
|
194
|
+
|
|
195
|
+
def convert_to_jalali(gregorian_object=None):
|
|
196
|
+
'''
|
|
197
|
+
takes gregorian object, like self.created
|
|
198
|
+
and converts it to jalali in this format: چهارشنبه ۰۷:۰۶:۳۳ ۳۰-/۰۱/۱۴۰۲
|
|
199
|
+
'''
|
|
200
|
+
|
|
201
|
+
if not gregorian_object:
|
|
202
|
+
return ''
|
|
203
|
+
|
|
204
|
+
jdatetime.set_locale('fa_IR')
|
|
205
|
+
|
|
206
|
+
timestamp = convert_to_second(gregorian_object)
|
|
207
|
+
|
|
208
|
+
jalali_object = jdt.fromtimestamp(timestamp)
|
|
209
|
+
w, h, mi, s, d, mo, y = jalali_object.strftime(JALALI_FORMAT).split()
|
|
210
|
+
|
|
211
|
+
return f'{w} {english_to_persian(h)}:{english_to_persian(mi)}:{english_to_persian(s)} {english_to_persian(y)}/{english_to_persian(mo)}/{english_to_persian(d)}'
|
|
212
|
+
|
|
213
|
+
def convert_to_second(date_obj):
|
|
214
|
+
return int(date_obj.timestamp()) ## 1698381096
|
|
215
|
+
|
|
216
|
+
def create_id_for_htmx_indicator(*args):
|
|
217
|
+
'''
|
|
218
|
+
by-date-source-ip-2024-06-30--htmx-indicator
|
|
219
|
+
OR
|
|
220
|
+
tops--htmx-indicator
|
|
221
|
+
'''
|
|
222
|
+
return sub(
|
|
223
|
+
'-{3,}',
|
|
224
|
+
'--',
|
|
225
|
+
f'{"-".join(args)}--htmx-indicator'
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
def get_date_time_live():
|
|
229
|
+
jdatetime.set_locale('fa_IR')
|
|
230
|
+
|
|
231
|
+
jdt_now = jdt.now()
|
|
232
|
+
|
|
233
|
+
_year = english_to_persian(jdt_now.strftime('%Y')) ## ۱۴۰۱
|
|
234
|
+
_day = english_to_persian(jdt_now.strftime('%d')) ## ۱۷
|
|
235
|
+
_month = english_to_persian(jdt_now.strftime('%m')) ## ۰۱
|
|
236
|
+
|
|
237
|
+
_hour = english_to_persian(jdt_now.strftime('%H'))
|
|
238
|
+
_min = english_to_persian(jdt_now.strftime('%M'))
|
|
239
|
+
# _sec = english_to_persian(jdt_now.strftime('%S'))
|
|
240
|
+
|
|
241
|
+
# _weekday = jdt_now.strftime('%A') ## چهارشنبه
|
|
242
|
+
|
|
243
|
+
return HttpResponse(f'{_year}/{_month}/{_day} {_hour}:{_min}')
|
|
244
|
+
|
|
245
|
+
def get_list_of_files(directory, extension):
|
|
246
|
+
if not path.exists(directory):
|
|
247
|
+
return []
|
|
248
|
+
return natsorted([
|
|
249
|
+
path.abspath(path.join(directory, _))
|
|
250
|
+
for _ in listdir(directory)
|
|
251
|
+
if all([
|
|
252
|
+
_.endswith((f'.{extension}')), ## NOTE do NOT .{extension} -> {extension}
|
|
253
|
+
path.isfile(f'{directory}/{_}'),
|
|
254
|
+
])
|
|
255
|
+
])
|
|
256
|
+
|
|
257
|
+
def get_percent(smaller_number, total_number, to_persian=False):
|
|
258
|
+
if smaller_number == 0 or total_number == 0:
|
|
259
|
+
if to_persian:
|
|
260
|
+
return '۰'
|
|
261
|
+
return '0'
|
|
262
|
+
|
|
263
|
+
_perc = (smaller_number * 100) / total_number
|
|
264
|
+
|
|
265
|
+
if int(_perc) == 0:
|
|
266
|
+
if to_persian:
|
|
267
|
+
return '~۰'
|
|
268
|
+
return '~0'
|
|
269
|
+
|
|
270
|
+
_perc = int(_perc * 10) / 10 ## 99.95232355216523 -> 99.9
|
|
271
|
+
## NOTE we didn't use f'{_perc:.1f}'
|
|
272
|
+
## because it turns 99.95232355216523 to 100.0
|
|
273
|
+
|
|
274
|
+
_perc = sub(r'\.0+$', '', str(_perc)) ## 97.0 -> 97
|
|
275
|
+
|
|
276
|
+
if to_persian:
|
|
277
|
+
return persianize(_perc)
|
|
278
|
+
|
|
279
|
+
return _perc
|
|
280
|
+
|
|
281
|
+
## inspired by:
|
|
282
|
+
## https://stackoverflow.com/questions/50319819/separate-thousands-while-typing-in-farsipersian
|
|
283
|
+
def intcomma_persian(num):
|
|
284
|
+
'''
|
|
285
|
+
۱۲۳۴۵۶۷۸ -> ۱۲،۳۴۵،۶۷۸
|
|
286
|
+
۱۲۳۴۵۶۷۸.۶۷ -> ۱۲،۳۴۵،۶۷۸.۶۷
|
|
287
|
+
۱۲۳۴۵۶۷۸/۶۷ -> ۱۲،۳۴۵،۶۷۸/۶۷
|
|
288
|
+
'''
|
|
289
|
+
|
|
290
|
+
commad = ''
|
|
291
|
+
left = ''
|
|
292
|
+
right = ''
|
|
293
|
+
is_float = False
|
|
294
|
+
|
|
295
|
+
## JUMP_1 is float
|
|
296
|
+
if match(r'^[۱۲۳۴۵۶۷۸۹۰]+\.[۱۲۳۴۵۶۷۸۹۰]+$', num):
|
|
297
|
+
left, right = num.split('.')
|
|
298
|
+
separator = '.'
|
|
299
|
+
is_float = True
|
|
300
|
+
|
|
301
|
+
## JUMP_1 is float
|
|
302
|
+
elif match(r'^[۱۲۳۴۵۶۷۸۹۰]+\/[۱۲۳۴۵۶۷۸۹۰]+$', num):
|
|
303
|
+
left, right = num.split('/')
|
|
304
|
+
separator = '/'
|
|
305
|
+
is_float = True
|
|
306
|
+
|
|
307
|
+
else:
|
|
308
|
+
left = num
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
for idx, char in enumerate(reversed(left), start=0):
|
|
312
|
+
if idx % 3 == 0 and idx > 0:
|
|
313
|
+
commad = char + '،' + commad
|
|
314
|
+
else:
|
|
315
|
+
commad = char + commad
|
|
316
|
+
|
|
317
|
+
if is_float:
|
|
318
|
+
commad = f'{commad}{separator}{right}'
|
|
319
|
+
|
|
320
|
+
return commad
|
|
321
|
+
|
|
322
|
+
def persianize(number):
|
|
323
|
+
number = str(number)
|
|
324
|
+
|
|
325
|
+
## JUMP_1 is float
|
|
326
|
+
if match(r'^[0-9]+\.[0-9]+$', number):
|
|
327
|
+
_left, _right = number.split('.')
|
|
328
|
+
if match('^0+$', _right):
|
|
329
|
+
return english_to_persian(_left)
|
|
330
|
+
return f'{english_to_persian(_left)}.{english_to_persian(_right[:2])}'
|
|
331
|
+
|
|
332
|
+
return english_to_persian(int(number))
|
|
333
|
+
|
|
334
|
+
def short_uuid():
|
|
335
|
+
_sample = uuid4()
|
|
336
|
+
return hex(int(_sample.time_low))[2:]
|
|
337
|
+
|
|
338
|
+
def sort_dict(dictionary, based_on, reverse):
|
|
339
|
+
if based_on == 'key':
|
|
340
|
+
return dict(natsorted(dictionary.items(), reverse=reverse))
|
|
341
|
+
|
|
342
|
+
if based_on == 'value':
|
|
343
|
+
return dict(natsorted(dictionary.items(), key=lambda item: item[1], reverse=reverse))
|
|
344
|
+
|
|
345
|
+
return dictionary
|
|
346
|
+
|
|
347
|
+
def to_tilda(text):
|
|
348
|
+
return sub(getenv('HOME'), '~', text)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
## vvv functions used in django admin.py -----------------
|
|
353
|
+
|
|
354
|
+
@admin.action(description='Make Active')
|
|
355
|
+
def make_active(modeladmin, request, queryset):
|
|
356
|
+
## https://stackoverflow.com/questions/67979442/how-do-i-find-the-class-that-relatedmanager-is-managing-when-the-queryset-is-emp
|
|
357
|
+
_caller = modeladmin.model.__name__ ## 'User'/'Router'/... (-> is str)
|
|
358
|
+
if _caller == 'User':
|
|
359
|
+
inactive_objects = queryset.filter(is_active=False)
|
|
360
|
+
else:
|
|
361
|
+
inactive_objects = queryset.filter(active=False)
|
|
362
|
+
|
|
363
|
+
count = inactive_objects.count()
|
|
364
|
+
|
|
365
|
+
if count:
|
|
366
|
+
if _caller == 'User':
|
|
367
|
+
inactive_objects.update(is_active=True)
|
|
368
|
+
else:
|
|
369
|
+
inactive_objects.update(active=True)
|
|
370
|
+
|
|
371
|
+
modeladmin.message_user(
|
|
372
|
+
request,
|
|
373
|
+
f'{count} made active'
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
@admin.action(description='Make Inactive')
|
|
377
|
+
def make_inactive(modeladmin, request, queryset):
|
|
378
|
+
## https://stackoverflow.com/questions/67979442/how-do-i-find-the-class-that-relatedmanager-is-managing-when-the-queryset-is-emp
|
|
379
|
+
_caller = modeladmin.model.__name__ ## 'User'/'Router'/... (-> is str)
|
|
380
|
+
if _caller == 'User':
|
|
381
|
+
active_objects = queryset.filter(is_active=True)
|
|
382
|
+
else:
|
|
383
|
+
active_objects = queryset.filter(active=True)
|
|
384
|
+
|
|
385
|
+
count = active_objects.count()
|
|
386
|
+
|
|
387
|
+
if count:
|
|
388
|
+
if _caller == 'User':
|
|
389
|
+
active_objects.update(is_active=False)
|
|
390
|
+
else:
|
|
391
|
+
active_objects.update(active=False)
|
|
392
|
+
|
|
393
|
+
modeladmin.message_user(
|
|
394
|
+
request,
|
|
395
|
+
f'{count} made inactive'
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
## vvv functions used in django custom commands -----------------
|
|
402
|
+
|
|
403
|
+
def abort(self, text=None):
|
|
404
|
+
print()
|
|
405
|
+
if text:
|
|
406
|
+
print(colorize(self, 'error', text))
|
|
407
|
+
print(colorize(self, 'error', 'aborting...'))
|
|
408
|
+
print()
|
|
409
|
+
|
|
410
|
+
def add_yearmonthday_force(parser, for_mysql=False):
|
|
411
|
+
## __DATABASE_YMD_PATTERN__
|
|
412
|
+
|
|
413
|
+
if for_mysql:
|
|
414
|
+
help_msg = 'year-month(s) in YYYY_MM format, e.g. 2024_12 or 2024_05 2024_07 2024_11'
|
|
415
|
+
else:
|
|
416
|
+
help_msg = 'year-month(s) in YYYY-MM format, e.g. 2024-12 or 2024-05 2024-07 2024-11'
|
|
417
|
+
parser.add_argument(
|
|
418
|
+
# '-x', ## JUMP_2 commented due to lack of a proper name for it
|
|
419
|
+
'--year-months',
|
|
420
|
+
default=[],
|
|
421
|
+
nargs='+', ## one or more
|
|
422
|
+
type=str,
|
|
423
|
+
help=help_msg,
|
|
424
|
+
)
|
|
425
|
+
if for_mysql:
|
|
426
|
+
help_msg = 'year-month-day(s) in YYYY_MM_DD format, e.g. 2024_12_03 or 2024_05_09 2024_07_29 2024_11_02'
|
|
427
|
+
else:
|
|
428
|
+
help_msg = 'year-month-day(s) in YYYY-MM-DD format, e.g. 2024-12-03 or 2024-05-09 2024-07-29 2024-11-02'
|
|
429
|
+
parser.add_argument(
|
|
430
|
+
# '-x', ## JUMP_2
|
|
431
|
+
'--year-month-days',
|
|
432
|
+
default=[],
|
|
433
|
+
nargs='+', ## one or more
|
|
434
|
+
type=str,
|
|
435
|
+
help=help_msg,
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
if for_mysql:
|
|
439
|
+
help_msg = 'start year-month in YYYY_MM format, e.g. 2024_10'
|
|
440
|
+
else:
|
|
441
|
+
help_msg = 'start year-month in YYYY-MM format, e.g. 2024-10'
|
|
442
|
+
parser.add_argument(
|
|
443
|
+
# '-x', ## JUMP_2
|
|
444
|
+
'--start-year-month',
|
|
445
|
+
default=None,
|
|
446
|
+
type=str,
|
|
447
|
+
help=help_msg,
|
|
448
|
+
)
|
|
449
|
+
if for_mysql:
|
|
450
|
+
help_msg = 'start year-month-day in YYYY_MM_DD format, e.g. 2024_10_30'
|
|
451
|
+
else:
|
|
452
|
+
help_msg = 'start year-month-day in YYYY-MM-DD format, e.g. 2024-10-30'
|
|
453
|
+
parser.add_argument(
|
|
454
|
+
# '-x', ## JUMP_2
|
|
455
|
+
'--start-year-month-day',
|
|
456
|
+
default=None,
|
|
457
|
+
type=str,
|
|
458
|
+
help=help_msg,
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
if for_mysql:
|
|
462
|
+
help_msg = 'end year-month in YYYY_MM format, e.g. 2024_12'
|
|
463
|
+
else:
|
|
464
|
+
help_msg = 'end year-month in YYYY-MM format, e.g. 2024-12'
|
|
465
|
+
parser.add_argument(
|
|
466
|
+
# '-x', ## JUMP_2
|
|
467
|
+
'--end-year-month',
|
|
468
|
+
default=None,
|
|
469
|
+
type=str,
|
|
470
|
+
help=help_msg,
|
|
471
|
+
)
|
|
472
|
+
if for_mysql:
|
|
473
|
+
help_msg = 'end year-month-day in YYYY_MM_DD format, e.g. 2024_12_15'
|
|
474
|
+
else:
|
|
475
|
+
help_msg = 'end year-month-day in YYYY-MM-DD format, e.g. 2024-12-15'
|
|
476
|
+
parser.add_argument(
|
|
477
|
+
# '-x', ## JUMP_2
|
|
478
|
+
'--end-year-month-day',
|
|
479
|
+
default=None,
|
|
480
|
+
type=str,
|
|
481
|
+
help=help_msg,
|
|
482
|
+
)
|
|
483
|
+
|
|
484
|
+
if for_mysql:
|
|
485
|
+
help_msg = 'force even if compressed'
|
|
486
|
+
else:
|
|
487
|
+
help_msg = 'force even if accomplished'
|
|
488
|
+
parser.add_argument(
|
|
489
|
+
'-f',
|
|
490
|
+
'--force',
|
|
491
|
+
default=False,
|
|
492
|
+
action='store_true',
|
|
493
|
+
help=help_msg,
|
|
494
|
+
)
|
|
495
|
+
|
|
496
|
+
def colorize(self, mode, text):
|
|
497
|
+
if mode == 'already_parsed': return self.style.SQL_COLTYPE(text) ## green
|
|
498
|
+
if mode == 'command': return self.style.HTTP_SERVER_ERROR(text) ## bold magenta
|
|
499
|
+
if mode == 'country_error': return self.style.NOTICE(text) ## red
|
|
500
|
+
if mode == 'country_success': return self.style.SQL_COLTYPE(text) ## green
|
|
501
|
+
if mode == 'country_warning': return self.style.SQL_KEYWORD(text) ## yellow
|
|
502
|
+
if mode == 'error': return self.style.ERROR(text) ## bold red
|
|
503
|
+
if mode == 'host_name': return self.style.HTTP_SUCCESS(text) ## white
|
|
504
|
+
if mode == 'invalid': return self.style.NOTICE(text) ## red
|
|
505
|
+
if mode == 'warning': return self.style.SQL_KEYWORD(text) ## yellow
|
|
506
|
+
if mode == 'ymdhms': return self.style.HTTP_NOT_MODIFIED(text) ## cyan
|
|
507
|
+
|
|
508
|
+
if mode in [
|
|
509
|
+
'dropping',
|
|
510
|
+
'removing',
|
|
511
|
+
]:
|
|
512
|
+
return self.style.SQL_KEYWORD(text) ## yellow
|
|
513
|
+
|
|
514
|
+
if mode in [
|
|
515
|
+
'copying',
|
|
516
|
+
'creating',
|
|
517
|
+
]:
|
|
518
|
+
return self.style.HTTP_INFO(text) ## bold white
|
|
519
|
+
|
|
520
|
+
if mode in [
|
|
521
|
+
'accomplished_in',
|
|
522
|
+
'compressed_in',
|
|
523
|
+
'done',
|
|
524
|
+
'dropped',
|
|
525
|
+
'fetched_in',
|
|
526
|
+
'parsed_in',
|
|
527
|
+
'removed',
|
|
528
|
+
'success',
|
|
529
|
+
'updated_in',
|
|
530
|
+
'wrote_in',
|
|
531
|
+
]:
|
|
532
|
+
return self.style.SUCCESS(text) ## bold green
|
|
533
|
+
|
|
534
|
+
return self.style.HTTP_SUCCESS(text) ## white
|
|
535
|
+
|
|
536
|
+
def get_command(full_path, drop_extention=True):
|
|
537
|
+
base = path.basename(full_path) ## parse_dns.py
|
|
538
|
+
|
|
539
|
+
if drop_extention:
|
|
540
|
+
root_base, _ = path.splitext(base) ## parse_dns
|
|
541
|
+
return root_base
|
|
542
|
+
|
|
543
|
+
return base
|
|
544
|
+
|
|
545
|
+
def get_command_log_file(command):
|
|
546
|
+
return f'{settings.PROJECT_LOGS_DIR}/{command}.log'
|
|
547
|
+
|
|
548
|
+
def is_allowed(cmd, only, exclude):
|
|
549
|
+
'''checks if cmd is in either of 'only' or 'exclude' lists'''
|
|
550
|
+
|
|
551
|
+
_allowed = True
|
|
552
|
+
|
|
553
|
+
## NOTE do NOT if -> elif
|
|
554
|
+
if only: _allowed = False
|
|
555
|
+
if only and cmd in only: _allowed = True
|
|
556
|
+
if exclude and cmd in exclude: _allowed = False
|
|
557
|
+
|
|
558
|
+
return _allowed
|
|
559
|
+
|
|
560
|
+
def keyboard_interrupt_handler(signal, frame):
|
|
561
|
+
print('\n')
|
|
562
|
+
raise CommandError(
|
|
563
|
+
f'command interrupted by user (signal: {signal})',
|
|
564
|
+
returncode=0,
|
|
565
|
+
)
|
|
566
|
+
|
|
567
|
+
def log(self, command, host_name, dest_file, msg, echo=True):
|
|
568
|
+
ymdhms = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
569
|
+
|
|
570
|
+
msg = to_tilda(msg)
|
|
571
|
+
|
|
572
|
+
if echo:
|
|
573
|
+
|
|
574
|
+
if 'accomplished in' in msg:
|
|
575
|
+
msg_ = colorize(self, 'accomplished_in', msg)
|
|
576
|
+
|
|
577
|
+
elif 'wrote in' in msg:
|
|
578
|
+
msg_ = colorize(self, 'wrote_in', msg)
|
|
579
|
+
|
|
580
|
+
elif 'parsed in' in msg:
|
|
581
|
+
msg_ = colorize(self, 'parsed_in', msg)
|
|
582
|
+
|
|
583
|
+
## in compressed_parsed.py
|
|
584
|
+
elif 'compressed in' in msg:
|
|
585
|
+
msg_ = colorize(self, 'compressed_in', msg)
|
|
586
|
+
|
|
587
|
+
## in update_snort.py
|
|
588
|
+
elif msg == 'done':
|
|
589
|
+
msg_ = colorize(self, 'done', msg)
|
|
590
|
+
|
|
591
|
+
## in fetch_malicious.py
|
|
592
|
+
elif 'fetched in' in msg:
|
|
593
|
+
msg_ = colorize(self, 'fetched_in', msg)
|
|
594
|
+
|
|
595
|
+
## in update_dns.py
|
|
596
|
+
elif 'updated in' in msg:
|
|
597
|
+
msg_ = colorize(self, 'updated_in', msg)
|
|
598
|
+
|
|
599
|
+
## in rotate.py
|
|
600
|
+
elif 'ERROR' in msg:
|
|
601
|
+
msg_ = colorize(self, 'error', msg)
|
|
602
|
+
|
|
603
|
+
## in rotate.py
|
|
604
|
+
elif 'WARNING' in msg:
|
|
605
|
+
msg_ = colorize(self, 'warning', msg)
|
|
606
|
+
|
|
607
|
+
## in rotate.py
|
|
608
|
+
elif 'removing' in msg:
|
|
609
|
+
msg_ = colorize(self, 'removing', msg)
|
|
610
|
+
|
|
611
|
+
## in rotate.py
|
|
612
|
+
elif 'creating' in msg:
|
|
613
|
+
msg_ = colorize(self, 'creating', msg)
|
|
614
|
+
|
|
615
|
+
elif 'dropping' in msg:
|
|
616
|
+
msg_ = colorize(self, 'dropping', msg)
|
|
617
|
+
|
|
618
|
+
elif 'copying' in msg:
|
|
619
|
+
msg_ = colorize(self, 'copying', msg)
|
|
620
|
+
|
|
621
|
+
else:
|
|
622
|
+
msg_ = msg
|
|
623
|
+
|
|
624
|
+
print(f"{colorize(self, 'host_name', host_name)} {colorize(self, 'command', command)} {colorize(self, 'ymdhms', ymdhms)} {msg_}")
|
|
625
|
+
|
|
626
|
+
with open(dest_file, 'a') as opened:
|
|
627
|
+
opened.write(f'{ymdhms} {msg}\n')
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Davoud Arsalani
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: rahavard
|
|
3
|
+
Version: 0.0.49
|
|
4
|
+
Summary: Re-Usable Utils
|
|
5
|
+
Home-page: https://github.com/davoudarsalani/rahavard
|
|
6
|
+
Author: Davoud Arsalani
|
|
7
|
+
Author-email: d_arsalani@yahoo.com
|
|
8
|
+
Keywords: python
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Framework :: Django
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Topic :: Utilities
|
|
15
|
+
Classifier: Operating System :: Unix
|
|
16
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
17
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: convert_numbers
|
|
21
|
+
Requires-Dist: django
|
|
22
|
+
Requires-Dist: jdatetime
|
|
23
|
+
Requires-Dist: natsort
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# rahavard
|
|
27
|
+
|
|
28
|
+
Under construction! Not ready for use yet! Currently experimenting and planning!
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
rahavard/__init__.py,sha256=iSR5QCXl4fNRaRYL1za1-nofP-12AXSXAH32Q2DxbUI,743
|
|
2
|
+
rahavard/utils.py,sha256=j5AEB3U2tyHRMITKgUVJ4ifW5Ur4qmobbs_N0x5baok,18927
|
|
3
|
+
rahavard-0.0.49.dist-info/LICENSE,sha256=z93awrj8VAbDtMKcWPFVcoJlePqvmvMjinBE28vFWDI,1072
|
|
4
|
+
rahavard-0.0.49.dist-info/METADATA,sha256=Roo1FidWBC4wUYUEVMoDbY-cvSBwHndPUlIfu0huPtU,854
|
|
5
|
+
rahavard-0.0.49.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
|
6
|
+
rahavard-0.0.49.dist-info/top_level.txt,sha256=JvvglMybHJfvF6ibHOWlLI9QkI32YZf4E8c86B36AhM,9
|
|
7
|
+
rahavard-0.0.49.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rahavard
|