Utils¶
The helpers/utils subpackage provides utility classes with static methods for common operations including date/time
handling, string manipulation, file I/O, JWT tokens, passwords, TOTP, Keycloak integration, Prometheus metrics, and
application-level utilities.
Base Utils¶
Base utility class providing foundational helpers shared across other utility classes.
archipy.helpers.utils.base_utils.BaseUtils ¶
Bases: ErrorUtils, DatetimeUtils, PasswordUtils, JWTUtils, TOTPUtils, FileUtils, StringUtils
A utility class that combines multiple utility functionalities into a single class.
This class inherits from various utility classes to provide a centralized place for common utility methods.
Source code in archipy/helpers/utils/base_utils.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
archipy.helpers.utils.base_utils.BaseUtils.arabic_vowel_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_akoolad_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_be_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_pe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_te_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_se_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_jim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_che_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_he_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_khe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_dal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_zal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_re_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_ze_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_zhe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_sin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_shin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_sad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_zad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_ta_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_za_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_eyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_gheyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_fe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_ghaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_kaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_gaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_lam_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_mim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_nun_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_vav_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_ha_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_ye_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table1
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table2
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table3
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table4
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table5
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table6
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table7
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table8
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table9
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table10
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table11
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table12
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table13
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.character_refinement_patterns
class-attribute
instance-attribute
¶
character_refinement_patterns: list = compile_patterns(
[(" +", " "), ("\\n\\n+", "\n"), (" ?\\.\\.\\.", " …")]
)
archipy.helpers.utils.base_utils.BaseUtils.punctuation_after
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_before
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_spacing_patterns
class-attribute
instance-attribute
¶
punctuation_spacing_patterns = compile_patterns(
[
(f" ([{punctuation_after}])", "\\1"),
(f"([{punctuation_before}]) ", "\\1"),
(
f"([{punctuation_after[:3]}])([^ {punctuation_after}"
+ "\\d])",
"\\1 \\2",
),
(
f"([{punctuation_after[3:]}])([^ {punctuation_after}])",
"\\1 \\2",
),
(
f"([^ {punctuation_before}])([{punctuation_before}])",
"\\1 \\2",
),
]
)
archipy.helpers.utils.base_utils.BaseUtils.number_zero_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_one_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_two_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_three_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_four_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_five_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_six_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_seven_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_eight_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_nine_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_persian_marks_to_space_translate_table
class-attribute
instance-attribute
¶
punctuation_persian_marks_to_space_translate_table = (
maketrans(fromkeys(".:!،؛؟»])}«[({-ـ٪!'\"#+/", " "))
)
archipy.helpers.utils.base_utils.BaseUtils.sanitize_iranian_landline_or_phone_number
staticmethod
¶
Sanitizes an Iranian landline or mobile phone number by removing non-numeric characters and standardizing the format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
landline_or_phone_number
|
str
|
The phone number to sanitize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The sanitized phone number in a standardized format. |
Source code in archipy/helpers/utils/base_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_iranian_phone_number
classmethod
¶
Validates an Iranian mobile phone number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phone_number
|
str
|
The phone number to validate. |
required |
Raises:
| Type | Description |
|---|---|
InvalidPhoneNumberError
|
If the phone number is invalid. |
Source code in archipy/helpers/utils/base_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_iranian_landline_number
classmethod
¶
Validates an Iranian landline number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
landline_number
|
str
|
The landline number to validate. |
required |
Raises:
| Type | Description |
|---|---|
InvalidLandlineNumberError
|
If the landline number is invalid. |
Source code in archipy/helpers/utils/base_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_iranian_national_code_pattern
classmethod
¶
Validates an Iranian National ID number using the official algorithm.
To see how the algorithm works, see http://www.aliarash.com/article/codemeli/codemeli.htm
The algorithm works by: 1. Checking if the ID is exactly 10 digits 2. Multiplying each digit (except the last) by its position weight 3. Summing these products 4. Calculating the remainder when divided by 11 5. Comparing the check digit based on specific rules
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
national_code
|
str
|
A string containing the national ID to validate. |
required |
Raises:
| Type | Description |
|---|---|
InvalidNationalCodeError
|
If the ID is invalid due to length or checksum. |
Source code in archipy/helpers/utils/base_utils.py
archipy.helpers.utils.base_utils.BaseUtils.remove_arabic_vowels
classmethod
¶
Removes Arabic vowels (tashkeel) from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Arabic vowels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Arabic vowels removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.normalize_persian_chars
classmethod
¶
Normalizes Persian characters to their standard forms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Persian characters. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Persian characters normalized. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.normalize_punctuation
classmethod
¶
Normalizes punctuation marks in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing punctuation marks. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with punctuation marks normalized. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.normalize_numbers
classmethod
¶
Normalizes numbers in the text to English format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with numbers normalized to English format. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.clean_spacing
classmethod
¶
Cleans up spacing issues in the text, such as non-breaking spaces and zero-width non-joiners.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text with spacing issues. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with spacing cleaned up. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.normalize_punctuation_spacing
classmethod
¶
Applies proper spacing around punctuation marks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text with punctuation spacing issues. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with proper spacing around punctuation marks. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.remove_punctuation_marks
classmethod
¶
Removes punctuation marks from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing punctuation marks. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with punctuation marks removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.mask_urls
classmethod
¶
Masks URLs in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing URLs. |
required |
mask
|
str | None
|
The mask to replace URLs with. Defaults to "MASK_URL". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with URLs masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.mask_emails
classmethod
¶
Masks email addresses in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing email addresses. |
required |
mask
|
str | None
|
The mask to replace emails with. Defaults to "MASK_EMAIL". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with email addresses masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.mask_phones
classmethod
¶
Masks phone numbers in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing phone numbers. |
required |
mask
|
str | None
|
The mask to replace phone numbers with. Defaults to "MASK_PHONE". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with phone numbers masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.convert_english_number_to_persian
classmethod
¶
Converts English numbers to Persian numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing English numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with English numbers converted to Persian numbers. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.convert_numbers_to_english
classmethod
¶
Converts Persian/Arabic numbers to English numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Persian/Arabic numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Persian/Arabic numbers converted to English numbers. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.convert_add_3digit_delimiter
classmethod
¶
Adds thousand separators to numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
The number to format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted number with thousand separators. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.remove_emoji
classmethod
¶
Removes emoji characters from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing emojis. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with emojis removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.replace_currencies_with_mask
classmethod
¶
Masks currency symbols and amounts in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing currency symbols and amounts. |
required |
mask
|
str | None
|
The mask to replace currencies with. Defaults to "MASK_CURRENCIES". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with currency symbols and amounts masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.replace_numbers_with_mask
classmethod
¶
Masks numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing numbers. |
required |
mask
|
str | None
|
The mask to replace numbers with. Defaults to "MASK_NUMBERS". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with numbers masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.is_string_none_or_empty
classmethod
¶
Checks if a string is None or empty (after stripping whitespace).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.normalize_persian_text
classmethod
¶
normalize_persian_text(
text: str,
*,
remove_vowels: bool = True,
normalize_punctuation: bool = True,
normalize_numbers: bool = True,
normalize_persian_chars: bool = True,
mask_urls: bool = False,
mask_emails: bool = False,
mask_phones: bool = False,
mask_currencies: bool = False,
mask_all_numbers: bool = False,
remove_emojis: bool = False,
url_mask: str | None = None,
email_mask: str | None = None,
phone_mask: str | None = None,
currency_mask: str | None = None,
number_mask: str | None = None,
clean_spacing: bool = True,
remove_punctuation: bool = False,
normalize_punctuation_spacing: bool = False,
) -> str
Normalizes Persian text with configurable options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text to normalize. |
required |
remove_vowels
|
bool
|
Whether to remove Arabic vowels. Defaults to |
True
|
normalize_punctuation
|
bool
|
Whether to normalize punctuation marks. Defaults to |
True
|
normalize_numbers
|
bool
|
Whether to normalize numbers to English format. Defaults to |
True
|
normalize_persian_chars
|
bool
|
Whether to normalize Persian characters. Defaults to |
True
|
mask_urls
|
bool
|
Whether to mask URLs. Defaults to |
False
|
mask_emails
|
bool
|
Whether to mask email addresses. Defaults to |
False
|
mask_phones
|
bool
|
Whether to mask phone numbers. Defaults to |
False
|
mask_currencies
|
bool
|
Whether to mask currency symbols and amounts. Defaults to |
False
|
mask_all_numbers
|
bool
|
Whether to mask all numbers. Defaults to |
False
|
remove_emojis
|
bool
|
Whether to remove emojis. Defaults to |
False
|
url_mask
|
str | None
|
The mask to replace URLs with. Defaults to |
None
|
email_mask
|
str | None
|
The mask to replace email addresses with. Defaults to |
None
|
phone_mask
|
str | None
|
The mask to replace phone numbers with. Defaults to |
None
|
currency_mask
|
str | None
|
The mask to replace currency symbols and amounts with. Defaults to |
None
|
number_mask
|
str | None
|
The mask to replace numbers with. Defaults to |
None
|
clean_spacing
|
bool
|
Whether to clean up spacing issues. Defaults to |
True
|
remove_punctuation
|
bool
|
Whether to remove punctuation marks. Defaults to |
False
|
normalize_punctuation_spacing
|
bool
|
Whether to apply proper spacing around punctuation marks. Defaults to |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The normalized text. |
Source code in archipy/helpers/utils/string_utils.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |
archipy.helpers.utils.base_utils.BaseUtils.snake_to_camel_case
classmethod
¶
Converts snake_case to camelCase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text in snake_case format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text converted to camelCase format. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.camel_to_snake_case
classmethod
¶
Converts camelCase to snake_case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text in camelCase format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text converted to snake_case format. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.create_secure_link
classmethod
¶
create_secure_link(
path: str,
minutes: int | None = None,
file_config: FileConfig | None = None,
) -> str
Creates a secure link with expiration for file access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The file path to create a secure link for. |
required |
minutes
|
int | None
|
Number of minutes until link expiration. Defaults to the config's |
None
|
file_config
|
FileConfig | None
|
Optional file configuration object. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A secure link with a hash and expiration timestamp. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the |
OutOfRangeError
|
If |
Source code in archipy/helpers/utils/file_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_file_name
classmethod
¶
Validates a file name based on allowed extensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
The file name to validate. |
required |
file_config
|
FileConfig | None
|
Optional file configuration object. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If |
Source code in archipy/helpers/utils/file_utils.py
archipy.helpers.utils.base_utils.BaseUtils.generate_totp
classmethod
¶
Generates a TOTP code using the configured hash algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret
|
str | UUID
|
The secret key used to generate the TOTP code. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[str, datetime]
|
A tuple containing the generated TOTP code and its expiration time. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the secret is invalid or empty. |
Source code in archipy/helpers/utils/totp_utils.py
archipy.helpers.utils.base_utils.BaseUtils.verify_totp
classmethod
¶
Verifies a TOTP code against the provided secret.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret
|
str | UUID
|
The secret key used to generate the TOTP code. |
required |
totp_code
|
str
|
The TOTP code to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the secret is invalid or empty. |
InvalidTokenError
|
If the TOTP code format is invalid. |
Source code in archipy/helpers/utils/totp_utils.py
archipy.helpers.utils.base_utils.BaseUtils.generate_secret_key_for_totp
staticmethod
¶
Generates a random secret key for TOTP initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A base32-encoded secret key for TOTP initialization. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the TOTP_SECRET_KEY is not configured. |
InternalError
|
If there is an error generating the secret key. |
Source code in archipy/helpers/utils/totp_utils.py
archipy.helpers.utils.base_utils.BaseUtils.create_token
classmethod
¶
create_token(
data: dict[str, Any],
expires_in: int,
additional_claims: dict[str, Any] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates a JWT token with enhanced security features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Base claims data to include in the token. |
required |
expires_in
|
int
|
Token expiration time in seconds. |
required |
additional_claims
|
dict[str, Any] | None
|
Optional additional claims to include in the token. |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded JWT token. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is empty or expiration is invalid |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.create_access_token
classmethod
¶
create_access_token(
user_uuid: UUID,
additional_claims: dict[str, Any] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates an access token for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_uuid
|
UUID
|
The user's UUID to include in the token. |
required |
additional_claims
|
dict[str, Any] | None
|
Optional additional claims to include in the token. |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded access token. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.create_refresh_token
classmethod
¶
create_refresh_token(
user_uuid: UUID,
additional_claims: dict[str, Any] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates a refresh token for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_uuid
|
UUID
|
The user's UUID to include in the token. |
required |
additional_claims
|
dict[str, Any] | None
|
Optional additional claims to include in the token. |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded refresh token. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.decode_token
classmethod
¶
decode_token(
token: str,
verify_type: str | None = None,
auth_config: AuthConfig | None = None,
) -> dict[str, Any]
Decodes and verifies a JWT token with enhanced security checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The JWT token to decode. |
required |
verify_type
|
str | None
|
Optional token type to verify (e.g., "access" or "refresh"). |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: The decoded token payload. |
Raises:
| Type | Description |
|---|---|
TokenExpiredError
|
If the token has expired. |
InvalidTokenError
|
If the token is invalid (e.g., invalid signature, audience, issuer, or type). |
Source code in archipy/helpers/utils/jwt_utils.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
archipy.helpers.utils.base_utils.BaseUtils.verify_access_token
classmethod
¶
Verifies an access token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The access token to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: The decoded access token payload. |
Raises:
| Type | Description |
|---|---|
InvalidTokenException
|
If the token is invalid or not an access token. |
TokenExpiredException
|
If the token has expired. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.verify_refresh_token
classmethod
¶
Verifies a refresh token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The refresh token to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: The decoded refresh token payload. |
Raises:
| Type | Description |
|---|---|
InvalidTokenException
|
If the token is invalid or not a refresh token. |
TokenExpiredException
|
If the token has expired. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.extract_user_uuid
staticmethod
¶
Extracts the user UUID from the token payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict[str, Any]
|
The decoded token payload. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
UUID |
UUID
|
The user's UUID. |
Raises:
| Type | Description |
|---|---|
InvalidTokenException
|
If the user identifier is invalid or missing. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_token_expiry
classmethod
¶
Gets the token expiry timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The JWT token. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The token expiry timestamp in seconds. |
Raises:
| Type | Description |
|---|---|
InvalidTokenException
|
If the token is invalid. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.hash_password
staticmethod
¶
Hashes a password using PBKDF2 with SHA256.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to hash. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A base64-encoded string containing the salt and hash in the format "salt:hash". |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.base_utils.BaseUtils.verify_password
staticmethod
¶
verify_password(
password: str,
stored_password: str,
auth_config: AuthConfig | None = None,
) -> bool
Verifies a password against a stored hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to verify. |
required |
stored_password
|
str
|
The stored password hash to compare against. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the password matches the stored hash, False otherwise. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_password
staticmethod
¶
Validates a password against the password policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to validate. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidPasswordError
|
If the password does not meet the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.base_utils.BaseUtils.generate_password
staticmethod
¶
Generates a random password that meets the policy requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A randomly generated password that meets the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_password_history
classmethod
¶
validate_password_history(
new_password: str,
password_history: list[str],
auth_config: AuthConfig | None = None,
lang: LanguageType | None = None,
) -> None
Validates a new password against the password history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_password
|
str
|
The new password to validate. |
required |
password_history
|
list[str]
|
A list of previous password hashes. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
lang
|
LanguageType
|
The language to use for error messages. Defaults to Persian. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidPasswordError
|
If the new password has been used recently or does not meet the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.base_utils.BaseUtils.convert_to_jalali
staticmethod
¶
Converts a Gregorian date to a Jalali (Persian) date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_date
|
date
|
The Gregorian date to convert. |
required |
Returns:
| Type | Description |
|---|---|
date
|
jdatetime.date: The corresponding Jalali date. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.is_holiday_in_iran
classmethod
¶
Determines if the target date is a holiday in Iran.
This method leverages caching and an external API to check if the given date is a holiday.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_date
|
date
|
The date to check for holiday status. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the date is a holiday, False otherwise. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.ensure_timezone_aware
classmethod
¶
Ensures a datetime object is timezone-aware, converting it to UTC if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
datetime
|
The datetime object to make timezone-aware. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The timezone-aware datetime object. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.daterange
classmethod
¶
Generates a range of dates from start_date to end_date, exclusive of end_date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime
|
The start date of the range. |
required |
end_date
|
datetime
|
The end date of the range. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
date |
Generator[date]
|
Each date in the range. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_string_datetime_from_datetime
classmethod
¶
Converts a datetime object to a formatted string. Default format is ISO 8601.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
datetime
|
The datetime object to format. |
required |
format_
|
str | None
|
The format string. If None, uses ISO 8601. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.standardize_string_datetime
classmethod
¶
Standardizes a datetime string to the default format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_string
|
str
|
The datetime string to standardize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The standardized datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_datetime_from_string_datetime
classmethod
¶
Parses a string to a datetime object using the given format, or ISO 8601 by default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_string
|
str
|
The datetime string to parse. |
required |
format_
|
str | None
|
The format string. If None, uses ISO 8601. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The parsed datetime object with UTC timezone. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_string_datetime_now
classmethod
¶
Gets the current datetime as a formatted string. Default format is ISO 8601.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_datetime_now
classmethod
¶
Gets the current local datetime.
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The current local datetime. |
archipy.helpers.utils.base_utils.BaseUtils.get_datetime_utc_now
classmethod
¶
Gets the current UTC datetime.
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The current UTC datetime. |
archipy.helpers.utils.base_utils.BaseUtils.get_epoch_time_now
classmethod
¶
Gets the current time in seconds since the epoch.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The current epoch time. |
archipy.helpers.utils.base_utils.BaseUtils.get_datetime_before_given_datetime_or_now
classmethod
¶
get_datetime_before_given_datetime_or_now(
weeks: int = 0,
days: int = 0,
hours: int = 0,
minutes: int = 0,
seconds: int = 0,
datetime_given: datetime | None = None,
) -> datetime
Subtracts time from a given datetime or the current datetime if not specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weeks
|
int
|
The number of weeks to subtract. |
0
|
days
|
int
|
The number of days to subtract. |
0
|
hours
|
int
|
The number of hours to subtract. |
0
|
minutes
|
int
|
The number of minutes to subtract. |
0
|
seconds
|
int
|
The number of seconds to subtract. |
0
|
datetime_given
|
datetime | None
|
The datetime to subtract from. If None, uses the current datetime. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The resulting datetime after subtraction. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_datetime_after_given_datetime_or_now
classmethod
¶
get_datetime_after_given_datetime_or_now(
weeks: int = 0,
days: int = 0,
hours: int = 0,
minutes: int = 0,
seconds: int = 0,
datetime_given: datetime | None = None,
) -> datetime
Adds time to a given datetime or the current datetime if not specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weeks
|
int
|
The number of weeks to add. |
0
|
days
|
int
|
The number of days to add. |
0
|
hours
|
int
|
The number of hours to add. |
0
|
minutes
|
int
|
The number of minutes to add. |
0
|
seconds
|
int
|
The number of seconds to add. |
0
|
datetime_given
|
datetime | None
|
The datetime to add to. If None, uses the current datetime. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The resulting datetime after addition. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.format_validation_errors
staticmethod
¶
format_validation_errors(
validation_error: ValidationError,
*,
include_type: bool = False,
) -> list[dict[str, str]]
Formats Pydantic validation errors into a structured format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validation_error
|
ValidationError
|
The validation error to format. |
required |
include_type
|
bool
|
Whether to include the error type in the output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
list[dict[str, str]]: A list of formatted validation error details. |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.base_utils.BaseUtils.capture_exception
staticmethod
¶
Captures an exception and reports it to configured external services.
This method logs the exception locally and optionally reports it to Sentry and Elastic APM, depending on the configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseException
|
The exception to capture and report. |
required |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.base_utils.BaseUtils.async_handle_fastapi_exception
async
staticmethod
¶
async_handle_fastapi_exception(
_request: RequestProtocol, exception: BaseError
) -> JSONResponseProtocol
Handles a FastAPI exception and returns a JSON response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_request
|
Request
|
The incoming FastAPI request. |
required |
exception
|
BaseError
|
The exception to handle. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
JSONResponse |
JSONResponseProtocol
|
A JSON response containing the exception details. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If FastAPI is not available. |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.base_utils.BaseUtils.handle_grpc_exception
staticmethod
¶
Handles a gRPC exception and returns a tuple of status code and message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseError
|
The exception to handle. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, str]
|
tuple[int, str]: A tuple containing the gRPC status code and error message. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If gRPC is not available. |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_fastapi_exception_responses
staticmethod
¶
Generates OpenAPI response documentation for the given errors.
This method creates OpenAPI-compatible response schemas for FastAPI errors, including validation errors and custom errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exceptions
|
list[type[BaseError]]
|
A list of exception types to generate responses for. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, dict[str, Any]]
|
dict[int, dict[str, Any]]: A dictionary mapping HTTP status codes to their corresponding response schemas. |
Source code in archipy/helpers/utils/error_utils.py
options: show_root_toc_entry: false heading_level: 3
App Utils¶
Application-level utilities for runtime environment inspection and process management.
archipy.helpers.utils.app_utils.CreateGrpcServerType
module-attribute
¶
archipy.helpers.utils.app_utils.create_grpc_server
module-attribute
¶
archipy.helpers.utils.app_utils.FastAPIExceptionHandler ¶
Handles various types of errors and converts them to appropriate JSON responses.
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIExceptionHandler.create_error_response
staticmethod
¶
Creates a standardized error response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseError
|
The exception to be converted into a response. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
JSONResponse |
JSONResponse
|
A JSON response containing the exception details. |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIExceptionHandler.custom_exception_handler
async
staticmethod
¶
Handles custom errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming request. |
required |
exception
|
BaseError
|
The custom exception to handle. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
JSONResponse |
JSONResponse
|
A JSON response containing the exception details. |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIExceptionHandler.generic_exception_handler
async
staticmethod
¶
Handles generic errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming request. |
required |
exception
|
Exception
|
The generic exception to handle. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
JSONResponse |
JSONResponse
|
A JSON response containing the exception details. |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIExceptionHandler.validation_exception_handler
async
staticmethod
¶
Handles validation errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming request. |
required |
exception
|
ValidationError
|
The validation exception to handle. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
JSONResponse |
JSONResponse
|
A JSON response containing the validation error details. |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils ¶
Utility class for FastAPI configuration and setup.
Source code in archipy/helpers/utils/app_utils.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
archipy.helpers.utils.app_utils.FastAPIUtils.custom_generate_unique_id
staticmethod
¶
Generates a unique ID for API routes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
route
|
APIRoute
|
The route for which to generate a unique ID. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A unique ID for the route. |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils.setup_sentry
staticmethod
¶
Initializes Sentry configuration if enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
The configuration object containing Sentry settings. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils.setup_cors
staticmethod
¶
Configures CORS middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
config
|
BaseConfig
|
The configuration object containing CORS settings. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils.setup_elastic_apm
staticmethod
¶
Configures Elastic APM if enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
config
|
BaseConfig
|
The configuration object containing Elastic APM settings. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils.setup_metric_interceptor
staticmethod
¶
Configures metric interceptor for FastAPI if Prometheus is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
config
|
BaseConfig
|
The configuration object containing Prometheus settings. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils.setup_exception_handlers
staticmethod
¶
Configures exception handlers for the FastAPI application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AsyncGrpcAPIUtils ¶
async grpc api utilities.
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AsyncGrpcAPIUtils.setup_trace_interceptor
staticmethod
¶
Configures trace interceptor for gRPC server if tracing is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
The configuration object containing tracing settings. |
required |
interceptors
|
List
|
List of gRPC interceptors to add the trace interceptor to. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AsyncGrpcAPIUtils.setup_metric_interceptor
staticmethod
¶
Configures metric interceptor for gRPC server if Prometheus is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
The configuration object containing Prometheus settings. |
required |
interceptors
|
List
|
List of gRPC interceptors to add the metric interceptor to. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.GrpcAPIUtils ¶
grpc api utilities.
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.GrpcAPIUtils.setup_trace_interceptor
staticmethod
¶
Configures trace interceptor for gRPC server if tracing is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
The configuration object containing tracing settings. |
required |
interceptors
|
List
|
List of gRPC interceptors to add the trace interceptor to. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.GrpcAPIUtils.setup_metric_interceptor
staticmethod
¶
Configures metric interceptor for gRPC server if Prometheus is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
The configuration object containing Prometheus settings. |
required |
interceptors
|
List
|
List of gRPC interceptors to add the metric interceptor to. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AppUtils ¶
Utility class for creating and configuring FastAPI applications.
Source code in archipy/helpers/utils/app_utils.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
archipy.helpers.utils.app_utils.AppUtils.create_fastapi_app
classmethod
¶
create_fastapi_app(
config: BaseConfig | None = None,
*,
configure_exception_handlers: bool = True,
include_common_responses: bool = True,
lifespan: Callable[..., AbstractAsyncContextManager]
| None = None,
) -> FastAPI
Create and configure a FastAPI application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig | None
|
Custom configuration. If not provided, uses global config. |
None
|
configure_exception_handlers
|
bool
|
Whether to configure exception handlers. Defaults to True. |
True
|
include_common_responses
|
bool
|
Whether to configure common response definitions for all endpoints. Defaults to True. |
True
|
lifespan
|
Callable[..., AbstractAsyncContextManager] | None
|
Custom lifespan context manager for the app. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
FastAPI |
FastAPI
|
The configured FastAPI application instance. |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AppUtils.create_async_grpc_app
classmethod
¶
create_async_grpc_app(
config: BaseConfig,
customized_interceptors: set[Any] | None = None,
compression: Compression | None = None,
) -> GrpcAioServer
Create and configure an async gRPC application.
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AppUtils.create_grpc_app
classmethod
¶
create_grpc_app(
config: BaseConfig,
customized_interceptors: set[Any] | None = None,
compression: Compression | None = None,
) -> grpc.Server
Create and configure an async gRPC application.
Source code in archipy/helpers/utils/app_utils.py
options: show_root_toc_entry: false heading_level: 3
Datetime Utils¶
Utilities for timezone-aware date and time operations with microsecond precision.
archipy.helpers.utils.datetime_utils.DatetimeUtils ¶
A utility class for handling date and time operations, including conversions, caching, and API integrations.
This class provides methods for working with both Gregorian and Jalali (Persian) calendars, as well as utility functions for timezone-aware datetime objects, date ranges, and string formatting.
Source code in archipy/helpers/utils/datetime_utils.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
archipy.helpers.utils.datetime_utils.DatetimeUtils.convert_to_jalali
staticmethod
¶
Converts a Gregorian date to a Jalali (Persian) date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_date
|
date
|
The Gregorian date to convert. |
required |
Returns:
| Type | Description |
|---|---|
date
|
jdatetime.date: The corresponding Jalali date. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.is_holiday_in_iran
classmethod
¶
Determines if the target date is a holiday in Iran.
This method leverages caching and an external API to check if the given date is a holiday.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_date
|
date
|
The date to check for holiday status. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the date is a holiday, False otherwise. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.ensure_timezone_aware
classmethod
¶
Ensures a datetime object is timezone-aware, converting it to UTC if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
datetime
|
The datetime object to make timezone-aware. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The timezone-aware datetime object. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.daterange
classmethod
¶
Generates a range of dates from start_date to end_date, exclusive of end_date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime
|
The start date of the range. |
required |
end_date
|
datetime
|
The end date of the range. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
date |
Generator[date]
|
Each date in the range. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_string_datetime_from_datetime
classmethod
¶
Converts a datetime object to a formatted string. Default format is ISO 8601.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
datetime
|
The datetime object to format. |
required |
format_
|
str | None
|
The format string. If None, uses ISO 8601. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.standardize_string_datetime
classmethod
¶
Standardizes a datetime string to the default format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_string
|
str
|
The datetime string to standardize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The standardized datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_datetime_from_string_datetime
classmethod
¶
Parses a string to a datetime object using the given format, or ISO 8601 by default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_string
|
str
|
The datetime string to parse. |
required |
format_
|
str | None
|
The format string. If None, uses ISO 8601. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The parsed datetime object with UTC timezone. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_string_datetime_now
classmethod
¶
Gets the current datetime as a formatted string. Default format is ISO 8601.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_datetime_now
classmethod
¶
Gets the current local datetime.
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The current local datetime. |
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_datetime_utc_now
classmethod
¶
Gets the current UTC datetime.
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The current UTC datetime. |
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_epoch_time_now
classmethod
¶
Gets the current time in seconds since the epoch.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The current epoch time. |
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_datetime_before_given_datetime_or_now
classmethod
¶
get_datetime_before_given_datetime_or_now(
weeks: int = 0,
days: int = 0,
hours: int = 0,
minutes: int = 0,
seconds: int = 0,
datetime_given: datetime | None = None,
) -> datetime
Subtracts time from a given datetime or the current datetime if not specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weeks
|
int
|
The number of weeks to subtract. |
0
|
days
|
int
|
The number of days to subtract. |
0
|
hours
|
int
|
The number of hours to subtract. |
0
|
minutes
|
int
|
The number of minutes to subtract. |
0
|
seconds
|
int
|
The number of seconds to subtract. |
0
|
datetime_given
|
datetime | None
|
The datetime to subtract from. If None, uses the current datetime. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The resulting datetime after subtraction. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_datetime_after_given_datetime_or_now
classmethod
¶
get_datetime_after_given_datetime_or_now(
weeks: int = 0,
days: int = 0,
hours: int = 0,
minutes: int = 0,
seconds: int = 0,
datetime_given: datetime | None = None,
) -> datetime
Adds time to a given datetime or the current datetime if not specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weeks
|
int
|
The number of weeks to add. |
0
|
days
|
int
|
The number of days to add. |
0
|
hours
|
int
|
The number of hours to add. |
0
|
minutes
|
int
|
The number of minutes to add. |
0
|
seconds
|
int
|
The number of seconds to add. |
0
|
datetime_given
|
datetime | None
|
The datetime to add to. If None, uses the current datetime. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The resulting datetime after addition. |
Source code in archipy/helpers/utils/datetime_utils.py
options: show_root_toc_entry: false heading_level: 3
String Utils¶
Utilities for string manipulation including slugification, truncation, random string generation, and HTML sanitization.
archipy.helpers.utils.string_utils.StringUtils ¶
Bases: StringUtilsConstants
String utilities for text normalization, cleaning, and masking.
This class provides methods for handling Persian and Arabic text, including normalization, punctuation cleaning, number conversion, and masking of sensitive information like URLs, emails, and phone numbers.
Source code in archipy/helpers/utils/string_utils.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
archipy.helpers.utils.string_utils.StringUtils.arabic_vowel_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_akoolad_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_be_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_pe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_te_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_se_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_jim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_che_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_he_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_khe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_dal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_zal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_re_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_ze_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_zhe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_sin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_shin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_sad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_zad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_ta_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_za_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_eyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_gheyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_fe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_ghaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_kaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_gaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_lam_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_mim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_nun_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_vav_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_ha_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_ye_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table1
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table2
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table3
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table4
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table5
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table6
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table7
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table8
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table9
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table10
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table11
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table12
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table13
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.character_refinement_patterns
class-attribute
instance-attribute
¶
character_refinement_patterns: list = compile_patterns(
[(" +", " "), ("\\n\\n+", "\n"), (" ?\\.\\.\\.", " …")]
)
archipy.helpers.utils.string_utils.StringUtils.punctuation_after
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_before
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_spacing_patterns
class-attribute
instance-attribute
¶
punctuation_spacing_patterns = compile_patterns(
[
(f" ([{punctuation_after}])", "\\1"),
(f"([{punctuation_before}]) ", "\\1"),
(
f"([{punctuation_after[:3]}])([^ {punctuation_after}"
+ "\\d])",
"\\1 \\2",
),
(
f"([{punctuation_after[3:]}])([^ {punctuation_after}])",
"\\1 \\2",
),
(
f"([^ {punctuation_before}])([{punctuation_before}])",
"\\1 \\2",
),
]
)
archipy.helpers.utils.string_utils.StringUtils.number_zero_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_one_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_two_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_three_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_four_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_five_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_six_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_seven_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_eight_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_nine_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_persian_marks_to_space_translate_table
class-attribute
instance-attribute
¶
punctuation_persian_marks_to_space_translate_table = (
maketrans(fromkeys(".:!،؛؟»])}«[({-ـ٪!'\"#+/", " "))
)
archipy.helpers.utils.string_utils.StringUtils.remove_arabic_vowels
classmethod
¶
Removes Arabic vowels (tashkeel) from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Arabic vowels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Arabic vowels removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.normalize_persian_chars
classmethod
¶
Normalizes Persian characters to their standard forms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Persian characters. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Persian characters normalized. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.normalize_punctuation
classmethod
¶
Normalizes punctuation marks in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing punctuation marks. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with punctuation marks normalized. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.normalize_numbers
classmethod
¶
Normalizes numbers in the text to English format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with numbers normalized to English format. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.clean_spacing
classmethod
¶
Cleans up spacing issues in the text, such as non-breaking spaces and zero-width non-joiners.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text with spacing issues. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with spacing cleaned up. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.normalize_punctuation_spacing
classmethod
¶
Applies proper spacing around punctuation marks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text with punctuation spacing issues. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with proper spacing around punctuation marks. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.remove_punctuation_marks
classmethod
¶
Removes punctuation marks from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing punctuation marks. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with punctuation marks removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.mask_urls
classmethod
¶
Masks URLs in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing URLs. |
required |
mask
|
str | None
|
The mask to replace URLs with. Defaults to "MASK_URL". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with URLs masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.mask_emails
classmethod
¶
Masks email addresses in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing email addresses. |
required |
mask
|
str | None
|
The mask to replace emails with. Defaults to "MASK_EMAIL". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with email addresses masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.mask_phones
classmethod
¶
Masks phone numbers in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing phone numbers. |
required |
mask
|
str | None
|
The mask to replace phone numbers with. Defaults to "MASK_PHONE". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with phone numbers masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.convert_english_number_to_persian
classmethod
¶
Converts English numbers to Persian numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing English numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with English numbers converted to Persian numbers. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.convert_numbers_to_english
classmethod
¶
Converts Persian/Arabic numbers to English numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Persian/Arabic numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Persian/Arabic numbers converted to English numbers. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.convert_add_3digit_delimiter
classmethod
¶
Adds thousand separators to numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
The number to format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted number with thousand separators. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.remove_emoji
classmethod
¶
Removes emoji characters from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing emojis. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with emojis removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.replace_currencies_with_mask
classmethod
¶
Masks currency symbols and amounts in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing currency symbols and amounts. |
required |
mask
|
str | None
|
The mask to replace currencies with. Defaults to "MASK_CURRENCIES". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with currency symbols and amounts masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.replace_numbers_with_mask
classmethod
¶
Masks numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing numbers. |
required |
mask
|
str | None
|
The mask to replace numbers with. Defaults to "MASK_NUMBERS". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with numbers masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.is_string_none_or_empty
classmethod
¶
Checks if a string is None or empty (after stripping whitespace).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.normalize_persian_text
classmethod
¶
normalize_persian_text(
text: str,
*,
remove_vowels: bool = True,
normalize_punctuation: bool = True,
normalize_numbers: bool = True,
normalize_persian_chars: bool = True,
mask_urls: bool = False,
mask_emails: bool = False,
mask_phones: bool = False,
mask_currencies: bool = False,
mask_all_numbers: bool = False,
remove_emojis: bool = False,
url_mask: str | None = None,
email_mask: str | None = None,
phone_mask: str | None = None,
currency_mask: str | None = None,
number_mask: str | None = None,
clean_spacing: bool = True,
remove_punctuation: bool = False,
normalize_punctuation_spacing: bool = False,
) -> str
Normalizes Persian text with configurable options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text to normalize. |
required |
remove_vowels
|
bool
|
Whether to remove Arabic vowels. Defaults to |
True
|
normalize_punctuation
|
bool
|
Whether to normalize punctuation marks. Defaults to |
True
|
normalize_numbers
|
bool
|
Whether to normalize numbers to English format. Defaults to |
True
|
normalize_persian_chars
|
bool
|
Whether to normalize Persian characters. Defaults to |
True
|
mask_urls
|
bool
|
Whether to mask URLs. Defaults to |
False
|
mask_emails
|
bool
|
Whether to mask email addresses. Defaults to |
False
|
mask_phones
|
bool
|
Whether to mask phone numbers. Defaults to |
False
|
mask_currencies
|
bool
|
Whether to mask currency symbols and amounts. Defaults to |
False
|
mask_all_numbers
|
bool
|
Whether to mask all numbers. Defaults to |
False
|
remove_emojis
|
bool
|
Whether to remove emojis. Defaults to |
False
|
url_mask
|
str | None
|
The mask to replace URLs with. Defaults to |
None
|
email_mask
|
str | None
|
The mask to replace email addresses with. Defaults to |
None
|
phone_mask
|
str | None
|
The mask to replace phone numbers with. Defaults to |
None
|
currency_mask
|
str | None
|
The mask to replace currency symbols and amounts with. Defaults to |
None
|
number_mask
|
str | None
|
The mask to replace numbers with. Defaults to |
None
|
clean_spacing
|
bool
|
Whether to clean up spacing issues. Defaults to |
True
|
remove_punctuation
|
bool
|
Whether to remove punctuation marks. Defaults to |
False
|
normalize_punctuation_spacing
|
bool
|
Whether to apply proper spacing around punctuation marks. Defaults to |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The normalized text. |
Source code in archipy/helpers/utils/string_utils.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |
archipy.helpers.utils.string_utils.StringUtils.snake_to_camel_case
classmethod
¶
Converts snake_case to camelCase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text in snake_case format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text converted to camelCase format. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.camel_to_snake_case
classmethod
¶
Converts camelCase to snake_case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text in camelCase format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text converted to snake_case format. |
Source code in archipy/helpers/utils/string_utils.py
options: show_root_toc_entry: false heading_level: 3
String Utils Constants¶
Constants used by string_utils for character sets, patterns, and limits.
archipy.helpers.utils.string_utils_constants.StringUtilsConstants ¶
Constants for string utility operations including translation tables and regex patterns.
Source code in archipy/helpers/utils/string_utils_constants.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.arabic_vowel_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_akoolad_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_be_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_pe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_te_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_se_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_jim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_che_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_he_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_khe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_dal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_zal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_re_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_ze_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_zhe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_sin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_shin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_sad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_zad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_ta_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_za_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_eyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_gheyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_fe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_ghaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_kaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_gaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_lam_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_mim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_nun_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_vav_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_ha_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_ye_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table1
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table2
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table3
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table4
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table5
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table6
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table7
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table8
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table9
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table10
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table11
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table12
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table13
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.character_refinement_patterns
class-attribute
instance-attribute
¶
character_refinement_patterns: list = compile_patterns(
[(" +", " "), ("\\n\\n+", "\n"), (" ?\\.\\.\\.", " …")]
)
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_after
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_before
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_spacing_patterns
class-attribute
instance-attribute
¶
punctuation_spacing_patterns = compile_patterns(
[
(f" ([{punctuation_after}])", "\\1"),
(f"([{punctuation_before}]) ", "\\1"),
(
f"([{punctuation_after[:3]}])([^ {punctuation_after}"
+ "\\d])",
"\\1 \\2",
),
(
f"([{punctuation_after[3:]}])([^ {punctuation_after}])",
"\\1 \\2",
),
(
f"([^ {punctuation_before}])([{punctuation_before}])",
"\\1 \\2",
),
]
)
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_zero_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_one_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_two_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_three_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_four_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_five_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_six_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_seven_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_eight_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_nine_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_persian_marks_to_space_translate_table
class-attribute
instance-attribute
¶
punctuation_persian_marks_to_space_translate_table = (
maketrans(fromkeys(".:!،؛؟»])}«[({-ـ٪!'\"#+/", " "))
)
archipy.helpers.utils.string_utils_constants.compile_patterns ¶
Compile regex patterns with their replacement strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
list[tuple[str, str]]
|
List of tuples containing (pattern, replacement) pairs. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[Pattern[str], str]]
|
List of tuples containing (compiled_pattern, replacement) pairs. |
Source code in archipy/helpers/utils/string_utils_constants.py
options: show_root_toc_entry: false heading_level: 3
File Utils¶
Utilities for file operations including reading, writing, hashing, and type validation.
archipy.helpers.utils.file_utils.FileUtils ¶
A utility class for handling file-related operations, such as creating secure links and validating file names.
Source code in archipy/helpers/utils/file_utils.py
archipy.helpers.utils.file_utils.FileUtils.create_secure_link
classmethod
¶
create_secure_link(
path: str,
minutes: int | None = None,
file_config: FileConfig | None = None,
) -> str
Creates a secure link with expiration for file access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The file path to create a secure link for. |
required |
minutes
|
int | None
|
Number of minutes until link expiration. Defaults to the config's |
None
|
file_config
|
FileConfig | None
|
Optional file configuration object. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A secure link with a hash and expiration timestamp. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the |
OutOfRangeError
|
If |
Source code in archipy/helpers/utils/file_utils.py
archipy.helpers.utils.file_utils.FileUtils.validate_file_name
classmethod
¶
Validates a file name based on allowed extensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
The file name to validate. |
required |
file_config
|
FileConfig | None
|
Optional file configuration object. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If |
Source code in archipy/helpers/utils/file_utils.py
options: show_root_toc_entry: false heading_level: 3
Error Utils¶
Utilities for error formatting, context enrichment, and error chain inspection.
archipy.helpers.utils.error_utils.RequestProtocol ¶
archipy.helpers.utils.error_utils.JSONResponseProtocol ¶
archipy.helpers.utils.error_utils.ErrorUtils ¶
A utility class for handling errors, including capturing, reporting, and generating responses.
Source code in archipy/helpers/utils/error_utils.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
archipy.helpers.utils.error_utils.ErrorUtils.format_validation_errors
staticmethod
¶
format_validation_errors(
validation_error: ValidationError,
*,
include_type: bool = False,
) -> list[dict[str, str]]
Formats Pydantic validation errors into a structured format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validation_error
|
ValidationError
|
The validation error to format. |
required |
include_type
|
bool
|
Whether to include the error type in the output. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
list[dict[str, str]]: A list of formatted validation error details. |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.error_utils.ErrorUtils.capture_exception
staticmethod
¶
Captures an exception and reports it to configured external services.
This method logs the exception locally and optionally reports it to Sentry and Elastic APM, depending on the configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseException
|
The exception to capture and report. |
required |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.error_utils.ErrorUtils.async_handle_fastapi_exception
async
staticmethod
¶
async_handle_fastapi_exception(
_request: RequestProtocol, exception: BaseError
) -> JSONResponseProtocol
Handles a FastAPI exception and returns a JSON response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_request
|
Request
|
The incoming FastAPI request. |
required |
exception
|
BaseError
|
The exception to handle. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
JSONResponse |
JSONResponseProtocol
|
A JSON response containing the exception details. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If FastAPI is not available. |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.error_utils.ErrorUtils.handle_grpc_exception
staticmethod
¶
Handles a gRPC exception and returns a tuple of status code and message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseError
|
The exception to handle. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, str]
|
tuple[int, str]: A tuple containing the gRPC status code and error message. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If gRPC is not available. |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.error_utils.ErrorUtils.get_fastapi_exception_responses
staticmethod
¶
Generates OpenAPI response documentation for the given errors.
This method creates OpenAPI-compatible response schemas for FastAPI errors, including validation errors and custom errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exceptions
|
list[type[BaseError]]
|
A list of exception types to generate responses for. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, dict[str, Any]]
|
dict[int, dict[str, Any]]: A dictionary mapping HTTP status codes to their corresponding response schemas. |
Source code in archipy/helpers/utils/error_utils.py
options: show_root_toc_entry: false heading_level: 3
JWT Utils¶
Utilities for JWT generation, verification, and decoding with configurable signing algorithms and expiration.
Utility module for JWT token operations with enhanced security and datetime handling.
This module provides a robust JWT handling implementation with support for access and refresh tokens, cryptographic security, token validation, and comprehensive error handling.
archipy.helpers.utils.jwt_utils.JWTUtils ¶
Utility class for JWT token operations with enhanced security and datetime handling.
Source code in archipy/helpers/utils/jwt_utils.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
archipy.helpers.utils.jwt_utils.JWTUtils.create_token
classmethod
¶
create_token(
data: dict[str, Any],
expires_in: int,
additional_claims: dict[str, Any] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates a JWT token with enhanced security features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Base claims data to include in the token. |
required |
expires_in
|
int
|
Token expiration time in seconds. |
required |
additional_claims
|
dict[str, Any] | None
|
Optional additional claims to include in the token. |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded JWT token. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is empty or expiration is invalid |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.create_access_token
classmethod
¶
create_access_token(
user_uuid: UUID,
additional_claims: dict[str, Any] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates an access token for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_uuid
|
UUID
|
The user's UUID to include in the token. |
required |
additional_claims
|
dict[str, Any] | None
|
Optional additional claims to include in the token. |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded access token. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.create_refresh_token
classmethod
¶
create_refresh_token(
user_uuid: UUID,
additional_claims: dict[str, Any] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates a refresh token for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_uuid
|
UUID
|
The user's UUID to include in the token. |
required |
additional_claims
|
dict[str, Any] | None
|
Optional additional claims to include in the token. |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded refresh token. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.decode_token
classmethod
¶
decode_token(
token: str,
verify_type: str | None = None,
auth_config: AuthConfig | None = None,
) -> dict[str, Any]
Decodes and verifies a JWT token with enhanced security checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The JWT token to decode. |
required |
verify_type
|
str | None
|
Optional token type to verify (e.g., "access" or "refresh"). |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: The decoded token payload. |
Raises:
| Type | Description |
|---|---|
TokenExpiredError
|
If the token has expired. |
InvalidTokenError
|
If the token is invalid (e.g., invalid signature, audience, issuer, or type). |
Source code in archipy/helpers/utils/jwt_utils.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
archipy.helpers.utils.jwt_utils.JWTUtils.verify_access_token
classmethod
¶
Verifies an access token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The access token to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: The decoded access token payload. |
Raises:
| Type | Description |
|---|---|
InvalidTokenException
|
If the token is invalid or not an access token. |
TokenExpiredException
|
If the token has expired. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.verify_refresh_token
classmethod
¶
Verifies a refresh token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The refresh token to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: The decoded refresh token payload. |
Raises:
| Type | Description |
|---|---|
InvalidTokenException
|
If the token is invalid or not a refresh token. |
TokenExpiredException
|
If the token has expired. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.extract_user_uuid
staticmethod
¶
Extracts the user UUID from the token payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict[str, Any]
|
The decoded token payload. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
UUID |
UUID
|
The user's UUID. |
Raises:
| Type | Description |
|---|---|
InvalidTokenException
|
If the user identifier is invalid or missing. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.get_token_expiry
classmethod
¶
Gets the token expiry timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The JWT token. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The token expiry timestamp in seconds. |
Raises:
| Type | Description |
|---|---|
InvalidTokenException
|
If the token is invalid. |
Source code in archipy/helpers/utils/jwt_utils.py
options: show_root_toc_entry: false heading_level: 3
Password Utils¶
Utilities for secure password hashing, verification, generation, and strength validation with timing-attack protection.
archipy.helpers.utils.password_utils.PasswordUtils ¶
A utility class for handling password-related operations, such as hashing, verification, and validation.
Source code in archipy/helpers/utils/password_utils.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
archipy.helpers.utils.password_utils.PasswordUtils.hash_password
staticmethod
¶
Hashes a password using PBKDF2 with SHA256.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to hash. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A base64-encoded string containing the salt and hash in the format "salt:hash". |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.password_utils.PasswordUtils.verify_password
staticmethod
¶
verify_password(
password: str,
stored_password: str,
auth_config: AuthConfig | None = None,
) -> bool
Verifies a password against a stored hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to verify. |
required |
stored_password
|
str
|
The stored password hash to compare against. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the password matches the stored hash, False otherwise. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.password_utils.PasswordUtils.validate_password
staticmethod
¶
Validates a password against the password policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to validate. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidPasswordError
|
If the password does not meet the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.password_utils.PasswordUtils.generate_password
staticmethod
¶
Generates a random password that meets the policy requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A randomly generated password that meets the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.password_utils.PasswordUtils.validate_password_history
classmethod
¶
validate_password_history(
new_password: str,
password_history: list[str],
auth_config: AuthConfig | None = None,
lang: LanguageType | None = None,
) -> None
Validates a new password against the password history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_password
|
str
|
The new password to validate. |
required |
password_history
|
list[str]
|
A list of previous password hashes. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
lang
|
LanguageType
|
The language to use for error messages. Defaults to Persian. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidPasswordError
|
If the new password has been used recently or does not meet the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
options: show_root_toc_entry: false heading_level: 3
TOTP Utils¶
Utilities for TOTP (Time-based One-Time Password) generation, verification, and QR code URI construction.
Utility module for TOTP (Time-based One-Time Password) operations.
This module provides functionality for generating and verifying TOTP codes that are commonly used for multi-factor authentication.
archipy.helpers.utils.totp_utils.TOTPUtils ¶
Utility class for TOTP (Time-based One-Time Password) operations.
This class provides methods for generating and verifying TOTP codes, as well as generating secure secret keys for TOTP initialization.
Uses the following configuration parameters from AuthConfig: - TOTP_SECRET_KEY: Master secret key for generating TOTP secrets - TOTP_HASH_ALGORITHM: Hash algorithm used for TOTP generation (default: SHA1) - TOTP_LENGTH: Number of digits in generated TOTP codes - TOTP_TIME_STEP: Time step in seconds between TOTP code changes - TOTP_EXPIRES_IN: TOTP validity period in seconds - TOTP_VERIFICATION_WINDOW: Number of time steps to check before/after - SALT_LENGTH: Length of random bytes for secure key generation
Source code in archipy/helpers/utils/totp_utils.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
archipy.helpers.utils.totp_utils.TOTPUtils.generate_totp
classmethod
¶
Generates a TOTP code using the configured hash algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret
|
str | UUID
|
The secret key used to generate the TOTP code. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[str, datetime]
|
A tuple containing the generated TOTP code and its expiration time. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the secret is invalid or empty. |
Source code in archipy/helpers/utils/totp_utils.py
archipy.helpers.utils.totp_utils.TOTPUtils.verify_totp
classmethod
¶
Verifies a TOTP code against the provided secret.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret
|
str | UUID
|
The secret key used to generate the TOTP code. |
required |
totp_code
|
str
|
The TOTP code to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the secret is invalid or empty. |
InvalidTokenError
|
If the TOTP code format is invalid. |
Source code in archipy/helpers/utils/totp_utils.py
archipy.helpers.utils.totp_utils.TOTPUtils.generate_secret_key_for_totp
staticmethod
¶
Generates a random secret key for TOTP initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A base32-encoded secret key for TOTP initialization. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the TOTP_SECRET_KEY is not configured. |
InternalError
|
If there is an error generating the secret key. |
Source code in archipy/helpers/utils/totp_utils.py
options: show_root_toc_entry: false heading_level: 3
Keycloak Utils¶
Utilities for Keycloak token acquisition, validation, user info retrieval, and role checking.
archipy.helpers.utils.keycloak_utils.GrpcContextType
module-attribute
¶
archipy.helpers.utils.keycloak_utils.AsyncGrpcContextType
module-attribute
¶
archipy.helpers.utils.keycloak_utils.security
module-attribute
¶
archipy.helpers.utils.keycloak_utils.AuthContext ¶
Bases: BaseModel
Authentication context passed to business logic.
Source code in archipy/helpers/utils/keycloak_utils.py
archipy.helpers.utils.keycloak_utils.AuthContext.raw_user_info
instance-attribute
¶
archipy.helpers.utils.keycloak_utils.AuthContextManager ¶
Manager for handling auth context in gRPC services.
Source code in archipy/helpers/utils/keycloak_utils.py
archipy.helpers.utils.keycloak_utils.KeycloakUtils ¶
Utility class for Keycloak authentication and authorization in FastAPI applications.
Source code in archipy/helpers/utils/keycloak_utils.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | |
archipy.helpers.utils.keycloak_utils.KeycloakUtils.fastapi_auth
classmethod
¶
fastapi_auth(
resource_type_param: str | None = None,
resource_type: str | None = None,
required_roles: frozenset[str] | None = None,
all_roles_required: bool = False,
required_permissions: tuple[tuple[str, str], ...]
| None = None,
admin_roles: frozenset[str] | None = None,
lang: LanguageType = DEFAULT_LANG,
) -> Callable
FastAPI decorator for Keycloak authentication and resource-based authorization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type_param
|
str | None
|
The parameter name in the path (e.g., 'user_uuid', 'employee_uuid') |
None
|
resource_type
|
str | None
|
The type of resource being accessed (e.g., 'users', 'employees') |
None
|
required_roles
|
frozenset[str] | None
|
Set of role names that the user must have |
None
|
all_roles_required
|
bool
|
If True, user must have all specified roles; if False, any role is sufficient |
False
|
required_permissions
|
tuple[tuple[str, str], ...] | None
|
List of (resource, scope) tuples to check |
None
|
admin_roles
|
frozenset[str] | None
|
Set of roles that grant administrative access to all resources |
None
|
lang
|
LanguageType
|
Language for error messages |
DEFAULT_LANG
|
Raises: UnauthenticatedError: If no valid Authorization header is provided InvalidTokenError: If token is invalid TokenExpiredError: If token is expired PermissionDeniedError: If user lacks required roles, permissions, or resource access InvalidArgumentError: If resource_type_param is missing when resource_type is provided
Source code in archipy/helpers/utils/keycloak_utils.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
archipy.helpers.utils.keycloak_utils.KeycloakUtils.async_fastapi_auth
classmethod
¶
async_fastapi_auth(
resource_type_param: str | None = None,
resource_type: str | None = None,
required_roles: frozenset[str] | None = None,
all_roles_required: bool = False,
required_permissions: tuple[tuple[str, str], ...]
| None = None,
admin_roles: frozenset[str] | None = None,
lang: LanguageType = DEFAULT_LANG,
) -> Callable
FastAPI async decorator for Keycloak authentication and resource-based authorization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type_param
|
str | None
|
The parameter name in the path (e.g., 'user_uuid', 'employee_uuid') |
None
|
resource_type
|
str | None
|
The type of resource being accessed (e.g., 'users', 'employees') |
None
|
required_roles
|
frozenset[str] | None
|
Set of role names that the user must have |
None
|
all_roles_required
|
bool
|
If True, user must have all specified roles; if False, any role is sufficient |
False
|
required_permissions
|
tuple[tuple[str, str], ...] | None
|
List of (resource, scope) tuples to check |
None
|
admin_roles
|
frozenset[str] | None
|
Set of roles that grant administrative access to all resources |
None
|
lang
|
LanguageType
|
Language for error messages |
DEFAULT_LANG
|
Raises: UnauthenticatedError: If no valid Authorization header is provided InvalidTokenError: If token is invalid TokenExpiredError: If token is expired PermissionDeniedError: If user lacks required roles, permissions, or resource access InvalidArgumentError: If resource_type_param is missing when resource_type is provided
Source code in archipy/helpers/utils/keycloak_utils.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
archipy.helpers.utils.keycloak_utils.KeycloakUtils.grpc_auth
classmethod
¶
grpc_auth(
required_roles: frozenset[str] | None = None,
all_roles_required: bool = False,
required_permissions: tuple[tuple[str, str], ...]
| None = None,
resource_attribute_name: str | None = None,
admin_roles: frozenset[str] | None = None,
lang: LanguageType = DEFAULT_LANG,
) -> Callable[[Callable], Callable]
Synchronous gRPC decorator for authentication and authorization.
This decorator handles: 1. Token validation 2. Role/permission checking 3. Passing auth context to business logic
Resource ownership is handled in the business logic layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
required_roles
|
frozenset[str] | None
|
Set of roles, user must have at least one (or all if all_roles_required=True) |
None
|
all_roles_required
|
bool
|
If True, user must have all required_roles; if False, any one role is sufficient |
False
|
required_permissions
|
tuple[tuple[str, str], ...] | None
|
Tuple of (resource, scope) pairs that must be satisfied |
None
|
resource_attribute_name
|
str | None
|
Attribute name to extract resource UUID from context for ownership checking |
None
|
admin_roles
|
frozenset[str] | None
|
Set of admin roles that bypass resource ownership checks |
None
|
lang
|
LanguageType
|
Language for error messages |
DEFAULT_LANG
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable], Callable]
|
Decorated function with authentication and authorization |
Source code in archipy/helpers/utils/keycloak_utils.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
archipy.helpers.utils.keycloak_utils.KeycloakUtils.async_grpc_auth
classmethod
¶
async_grpc_auth(
required_roles: frozenset[str] | None = None,
all_roles_required: bool = False,
required_permissions: tuple[tuple[str, str], ...]
| None = None,
resource_attribute_name: str | None = None,
admin_roles: frozenset[str] | None = None,
lang: LanguageType = DEFAULT_LANG,
) -> Callable[[Callable], Callable]
Simplified gRPC decorator for authentication and authorization.
This decorator handles: 1. Token validation 2. Role/permission checking 3. Passing auth context to business logic
Resource ownership is handled in the business logic layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
required_roles
|
frozenset[str] | None
|
Set of roles, user must have at least one (or all if all_roles_required=True) |
None
|
all_roles_required
|
bool
|
If True, user must have all required_roles; if False, any one role is sufficient |
False
|
required_permissions
|
tuple[tuple[str, str], ...] | None
|
Tuple of (resource, scope) pairs that must be satisfied |
None
|
resource_attribute_name
|
str | None
|
Attribute name to extract resource UUID from context for ownership checking |
None
|
admin_roles
|
frozenset[str] | None
|
Set of admin roles that bypass resource ownership checks |
None
|
lang
|
LanguageType
|
Language for error messages |
DEFAULT_LANG
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable], Callable]
|
Decorated function with authentication and authorization |
Source code in archipy/helpers/utils/keycloak_utils.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | |
options: show_root_toc_entry: false heading_level: 3
Prometheus Utils¶
Utilities for registering and exposing Prometheus metrics within ArchiPy applications.
Prometheus utilities for managing Prometheus HTTP server and metrics.
This module provides utilities for Prometheus metrics server management that can be shared across FastAPI, gRPC, and Temporal adapters.
archipy.helpers.utils.prometheus_utils.is_prometheus_server_running ¶
Check if Prometheus server is already running on the specified port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
int
|
The port number to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if server is running, False otherwise. |
Example
Source code in archipy/helpers/utils/prometheus_utils.py
archipy.helpers.utils.prometheus_utils.start_prometheus_server_if_needed ¶
Start Prometheus HTTP server if not already running on the specified port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
int
|
The port number for the Prometheus metrics endpoint. |
required |
Example
Source code in archipy/helpers/utils/prometheus_utils.py
options: show_root_toc_entry: false heading_level: 3