pawnlib.output package

from pawnlib.output import *

Submodules

pawnlib.output.color_print module

from pawnlib.output import color_print
colored(text, color=None, on_color=None, attrs=None)[source]

Colorize text.

Available text colors:

red, green, yellow, blue, magenta, cyan, white.

Available text highlights:

on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.

Available _ATTRIBUTES:

bold, dark, underline, blink, reverse, concealed.

Example

colored(‘Hello, World!’, ‘red’, ‘on_grey’, [‘blue’, ‘blink’]) colored(‘Hello, World!’, ‘green’)

cprint(text, color=None, on_color=None, attrs=None, **kwargs)[source]

Print colorize text. It accepts arguments of print function.

Parameters:
Returns:

Example

cprint("message", "red")  # >>  message
class bcolors[source]

Bases: object

HEADER = '\x1b[95m'
OKBLUE = '\x1b[94m'
OKGREEN = '\x1b[92m'
GREEN = '\x1b[32;40m'
CYAN = '\x1b[96m'
WARNING = '\x1b[93m'
FAIL = '\x1b[91m'
RESET = '\x1b[0m'
ENDC = '\x1b[0m'
BOLD = '\x1b[1m'
ITALIC = '\x1b[1;3m'
UNDERLINE = '\x1b[4m'
WHITE = '\x1b[97m'
DARK_GREY = '\x1b[38;5;243m'
LIGHT_GREY = '\x1b[37m'
class PrintRichTable(title='', data=None, columns=None, remove_columns=None, with_idx=True, call_value_func=<class 'str'>, call_desc_func=None, columns_options=None, display_output=True, no_wrap=False, overflow='fold', justify='left', **kwargs)[source]

Bases: object

Print a table using a rich.table module.

Parameters:
  • title (str, optional) – Title of table |default| ''

  • data (Union[dict, list, None], optional) – Data of table |default| None

  • columns (Optional[list], optional) – Columns of table. Print only column parameter values. |default| None

  • with_idx (bool, optional) – Print the row count. |default| True

  • call_value_func (optional) – The row value must be a string. If you want to perform other tasks, please add the function name. |default| <class 'str'>

  • call_desc_func (optional) – Invoke a function that describes the value. |default| None

  • display_output (optional) – Determines whether to print output (default: True) |default| True

  • justify (optional) – Alignment value of the console. (default: left) |default| 'left'

Example

from pawnlib.output.color_print import PrintRichTable

data = [
    {
        "address":         "1x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08",
        "value":         399999999999999966445568,
    },
    {
        "address":         "2x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08",
        "value":         399999999999999966445568,
    },
    {
        "address":         "3x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08",
        "value":         399999999999999966445568,
    }
]

PrintRichTable(title="RichTable", data=data)


#                                    RichTable
# ┏━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┓
# ┃ idx ┃ address                                    ┃ value                    ┃
# ┡━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━┩
# │ 0   │ 1x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08 │ 399999999999999966445568 │
# ├─────┼────────────────────────────────────────────┼──────────────────────────┤
# │ 1   │ 2x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08 │ 399999999999999966445568 │
# ├─────┼────────────────────────────────────────────┼──────────────────────────┤
# │ 2   │ 3x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08 │ 399999999999999966445568 │
# └─────┴────────────────────────────────────────────┴──────────────────────────┘
#
# PrintRichTable(title="RichTable", data=data, columns=["address"])
#
#                               RichTable
# ┏━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
# ┃ idx ┃ address                                    ┃
# ┡━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
# │ 0   │ 1x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08 │
# ├─────┼────────────────────────────────────────────┤
# │ 1   │ 2x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08 │
# ├─────┼────────────────────────────────────────────┤
# │ 2   │ 3x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08 │
# └─────┴────────────────────────────────────────────┘
class TablePrinter(fmt=[], sep='  ', ul='-')[source]

Bases: object

Print a list of dicts as a table

Parameters:
  • fmt (optional) –

    list of tuple(heading, key, width) |default| []

    heading: str, column label

    key: dictionary key to value to print

    width: int, column width in chars

  • sep (optional) – string, separation between columns |default| '  '

  • ul (optional) – string, character to underline column label, or None for no underlining |default| '-'

Example

from pawnlib import output

data = [
    {
        "address":         "1x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08",
        "value":         399999999999999966445568,
    },
    {
        "address":         "2x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08",
        "value":         399999999999999966445568,
    },
    {
        "address":         "3x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08",
        "value":         399999999999999966445568,
    },

]
fmt = [
    ('address',       'address',          10),
    ('value',       'value',          15)
]
cprint("Print Table", "white")
print(output.TablePrinter(fmt=fmt)(data))
print(output.TablePrinter()(data))

 address         value
----------  ---------------
1x038bd14d  399999999999999
2x038bd14d  399999999999999
3x038bd14d  399999999999999
__init__(fmt=[], sep='  ', ul='-')[source]
Parameters:
  • fmt (optional) –

    list of tuple(heading, key, width) |default| []

    heading: str, column label

    key: dictionary key to value to print

    width: int, column width in chars

  • sep (optional) – string, separation between columns |default| '  '

  • ul (optional) – string, character to underline column label, or None for no underlining |default| '-'

Example

from pawnlib import output

data = [
    {
        "address":         "1x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08",
        "value":         399999999999999966445568,
    },
    {
        "address":         "2x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08",
        "value":         399999999999999966445568,
    },
    {
        "address":         "3x038bd14d5ce28a4ac713c21e89f0e6ca5f107f08",
        "value":         399999999999999966445568,
    },

]
fmt = [
    ('address',       'address',          10),
    ('value',       'value',          15)
]
cprint("Print Table", "white")
print(output.TablePrinter(fmt=fmt)(data))
print(output.TablePrinter()(data))

 address         value
----------  ---------------
1x038bd14d  399999999999999
2x038bd14d  399999999999999
3x038bd14d  399999999999999
row(data, head=False)[source]
get_unique_columns()[source]
get_bcolors(text, color, bold=False, underline=False, width=None)[source]

Returns the color from the bcolors object.

Parameters:
Returns:

colored_input(message, password=False, color='WHITE')[source]
get_colorful_object(v)[source]
dump(obj, nested_level=0, output=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, hex_to_int=False, debug=True, _is_list=False, _last_key=None, is_compact=False)[source]

Prints a variable (obj) for debugging in a structured way. - dict: recursively prints nested key/value pairs - list: recursively prints elements - scalar: prints with optional transforms (transform_dict, hex->int, etc.)

debug_print(text, color='green', on_color=None, attrs=None, view_time=True, **kwargs)[source]

Print colorize text.

It accepts arguments of print function.

classdump(obj)[source]

For debugging, print the properties of the class.

kvPrint(key, value)[source]

print the {key: value} format.

Parameters:
  • key

  • value

Returns:

print_json(obj, syntax=True, line_indent='', rich_syntax=True, style='material', **kwargs)[source]

Print a JSON object with optional syntax highlighting and indentation.

Parameters:
  • obj – The JSON object to print.

  • syntax (optional) – Whether to use syntax highlighting (default: True). |default| True

  • line_indent (optional) – The indentation for each line (default: “”). |default| ''

  • style (optional) – Style for syntax highlighting (default: “material”) |default| 'material'

  • kwargs – Additional keyword arguments for json.dumps().

Example

data = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

print_json(data)
# >> {
# >>     "name": "John",
# >>     "age": 30,
# >>     "city": "New York"
# >> }

print_json(data, syntax=False)
# >> {"name": "John", "age": 30, "city": "New York"}
pretty_json(obj, syntax=True, rich_syntax=False, style='one-dark', line_indent='', **kwargs)[source]

Return a prettified JSON string with optional syntax highlighting.

Parameters:
  • obj – JSON object to prettify

  • syntax (optional) – If True, apply syntax highlighting (default: True) |default| True

  • rich_syntax (optional) – If True, use rich library for syntax highlighting (default: False) |default| False

  • style (optional) – Style name for syntax highlighting (default: “one-dark”) |default| 'one-dark'

  • line_indent (optional) – Custom line indentation (default: “”) |default| ''

  • kwargs – Additional keyword arguments for json.dumps

Example

data = {"name": "John", "age": 30, "city": "New York"}
print(pretty_json(data))
# {
#     "name": "John",
#     "age": 30,
#     "city": "New York"
# }

print(pretty_json(data, syntax=False))
# {"name": "John", "age": 30, "city": "New York"}
debug_logging(message, dump_message=None)[source]

print debug_logging

Parameters:
  • message

  • dump_message (optional) – |default| None

Returns:

Example

from pawnlib import output

output.debug_logging("message")

[2022-07-25 16:35:15.105][DBG][/Users/jinwoo/work/python_prj/pawnlib/examples/asyncio/./run_async.py main(33)] : message
print_progress_bar(iteration, total, prefix='', suffix='', decimals=1, bar_length=100, overlay=True)[source]

Print progress bar

Parameters:
Returns:

Example

from pawnlib import output

for i in range(1, 100):
    time.sleep(0.05)
    output.print_progress_bar(i, total=100, prefix="start", suffix="suffix")

# >> start |\#\#\#\#\#\#\#\| 100.0% suffix
json_compact_dumps(data, indent=4, monkey_patch=True)[source]
class NoIndent(value)[source]

Bases: object

Value wrapper.

class NoListIndentEncoder(*args, **kwargs)[source]

Bases: JSONEncoder

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an RecursionError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (’, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.

If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

iterencode(o, _one_shot=False)[source]

Encode the given object and yield each string representation as available.

For example:

for chunk in JSONEncoder().iterencode(bigobject):
    mysocket.write(chunk)
class ProgressTime(**kwargs)[source]

Bases: Progress

print_syntax(data, name='json', indent=4, style='material', oneline_list=True, line_indent='', rich=True, **kwargs)[source]

Print the syntax of the data.

Parameters:
  • data – The data to print.

  • name (optional) – The name of the syntax. Default is “json”. |default| 'json'

  • indent (optional) – The number of spaces for indentation. Default is 4. |default| 4

  • style (optional) – The style of the syntax. Default is “material”. |default| 'material'

  • oneline_list (optional) – Whether to print the list in one line. Default is True. |default| True

  • line_indent (optional) – The indentation for each line. Default is ‘’. |default| ''

  • rich (optional) – Whether to use rich for printing. Default is True. |default| True

  • kwargs – Other keyword arguments.

Example

data = {"name": "John", "age": 30, "city": "New York"}
print_syntax(data)
# >> {
# >>     "name": "John",
# >>     "age": 30,
# >>     "city": "New York"
# >> }

print_syntax(data, name="xml", style="monokai", rich=False)
# >> <name>John</name>
# >> <age>30</age>
# >> <city>New York</city>
syntax_highlight(data, name='json', indent=4, style='material', oneline_list=True, line_indent='', rich=False, word_wrap=True, format_config=None, **kwargs)[source]

Syntax highlighting function with support for class instance representation instead of serialization.

Parameters:
  • data – The data to be highlighted.

  • name (optional) – The name of the lexer to use for highlighting. |default| 'json'

  • indent (optional) – The number of spaces to use for indentation. |default| 4

  • style (optional) – The style to use for highlighting. |default| 'material'

  • oneline_list (optional) – Whether to compact lists into one line. |default| True

  • line_indent (optional) – The string to use for line indentation. |default| ''

  • rich (optional) – Whether to use rich text formatting. |default| False

  • word_wrap (optional) – Whether to enable word wrapping. |default| True

  • format_config (optional) – Configuration for formatting. |default| None

Returns:

The highlighted code as a string.

print_here()[source]
print_frames(frame_list)[source]
get_debug_here_info()[source]

Get debug information from the previous frame.

This function uses the inspect module to get information about the previous frame, including the filename, line number, function name, lines of context, and index.

Returns:

A dictionary containing the debug information.

Return type:

dict

get_variable_name_list(var=None)[source]

Retrieve the name of var.

Parameters:

var (optional) – variable to get name from |default| None

Returns:

name of var

Example

a = 5
get_variable_name_list(a)
# >> ['a']
get_variable_name(var=None)[source]

Retrieve the name of the variable.

Parameters:

var (Any, optional) – variable to get the name from, defaults to None |default| None

Returns:

name of the variable

Return type:

str

Example

a = 10
get_variable_name(a)
# >> 'a'
dict_clean(data)[source]

Clean the dictionary data.

This function iterates over the items in the dictionary. If the value is an instance of CaseInsensitiveDict, it converts it to a regular dictionary. If the value is None, it converts it to an empty string.

Parameters:

data (dict) – The dictionary to clean.

Returns:

The cleaned dictionary.

Return type:

dict

Example

data = {"key1": "value1", "key2": None, "key3": CaseInsensitiveDict({"subkey": "subvalue"})}
dict_clean(data)
# >> {"key1": "value1", "key2": "", "key3": {"subkey": "subvalue"}}
list_clean(data)[source]

Clean the list data.

This function iterates over the items in the list. If the value is None, it converts it to an empty string.

Parameters:

data (list) – The list to clean.

Returns:

The cleaned list.

Return type:

list

Example

data = [None, 'hello', None, 'world']
list_clean(data)
# >> ['', 'hello', '', 'world']
data_clean(data)[source]

Clean the data.

This function checks the type of the data. If the data is a dictionary, it cleans it using the dict_clean function. If the data is a list, it cleans it using the list_clean function.

Parameters:

data (Any) – The data to clean.

Returns:

The cleaned data.

Return type:

Any

Example

data = {'name': ' John ', 'age': ' 25 '}
clean_data = data_clean(data)
# >> {'name': 'John', 'age': '25'}

data = [' John ', ' 25 ']
clean_data = data_clean(data)
# >> ['John', '25']
count_nested_dict_len(d)[source]

Count the total number of keys in a nested dictionary.

Parameters:

d (dict) – Dictionary to count keys in.

Returns:

Total number of keys in the dictionary, including nested dictionaries.

Return type:

int

Example

nested_dict = {
    'a': 1,
    'b': {'c': 2, 'd': {'e': 3}},
    'f': {'g': 4}
}

count_nested_dict_len(nested_dict)
# >> 6

count_nested_dict_len({'x': {'y': {'z': {}}}})
# >> 3

count_nested_dict_len({})
# >> 0
get_var_name(var)[source]

Get the variable name from the call frame. This function uses the inspect and ast modules to get the variable name from the call frame.

Parameters:

var – The variable whose name is to be retrieved.

Returns:

The name of the variable as a string.

Example

my_var = 10
get_var_name(my_var)
# >> 'my_var'

class MyClass:
    def __init__(self):
        self.attr = 5

instance = MyClass()
get_var_name(instance.attr)
# >> 'instance.attr'
get_data_length(data)[source]

Get the length of the data. If the data is a dictionary, it calculates the nested dictionary length.

Parameters:

data – The input data which can be of any type.

Returns:

A string representing the length of the data.

Example

get_data_length([1, 2, 3])
# >> 'len=3'

get_data_length({'a': 1, 'b': {'c': 2}})
# >> 'dict_len=2'

get_data_length("hello")
# >> 'len=5'
print_var(data=None, title='', line_indent=' ', detail=True, title_align='center', **kwargs)[source]

Print the variable name and its value on the same line with optional details.

Parameters:
  • data (optional) – The variable to be printed. |default| None

  • title (str, optional) – Optional title for the output. |default| ''

  • line_indent (str, optional) – String used for indentation. |default| ' '

  • detail (bool, optional) – Whether to include detailed information. |default| True

  • title_align (Literal['left', 'center', 'right'], optional) – Alignment method for the title. Can be “left”, “center”, or “right”. Default is “center”. |default| 'center'

  • kwargs – Additional keyword arguments.

Example

dict_var = {"key": "value"}

print_var(dict_var)
print_var(data={"key": "value"}, title="Example Dict")
print_var(data=[1, 2, 3], title="Example List")
print_var(data=42, title="Example Int")
print_var(data="Hello, World!", title="Example String")
print_var2(data=None, title='', line_indent=' ', detail=True, title_align='center', **kwargs)[source]

Print variable content with type and length info for dict, list, class, or basic types.

Parameters:
  • data (optional) – Variable to be printed. |default| None

  • title (str, optional) – Optional title for the output. |default| ''

  • line_indent (str, optional) – String used for indentation. |default| ' '

  • detail (bool, optional) – Whether to include detailed information. |default| True

  • title_align (Literal['left', 'center', 'right'], optional) – Title alignment (left, center, or right). |default| 'center'

add_node(tree, key, value, detail)[source]

Recursively add nodes to the tree with syntax_highlight for dict and list.

get_type_length_info_style(value)[source]

Get the type and length information as a styled string based on the value’s type. This version provides a more intuitive output for type objects.

style_value(value)[source]

Apply different styles based on the value’s type or value.

create_kv_table(padding=0, key_ratio=2, value_ratio=7, overflow='fold')[source]
get_pretty_value(value, is_force_syntax=False)[source]
print_kv(key='', value='', symbol='░', separator=':', padding=1, key_ratio=1, value_ratio=7, is_force_syntax=True)[source]

Print a key-value pair with a symbol, padding, and value size information.

Parameters:
  • key (optional) – The key to print. Defaults to an empty string. |default| ''

  • value (optional) – The value associated with the key. Defaults to an empty string. |default| ''

  • symbol (optional) – A symbol to prepend to the key. Defaults to a star emoji. |default| '░'

  • separator (optional) – The separator between the key and the value. Defaults to a colon. |default| ':'

  • padding (optional) – The padding between columns. Defaults to 5. |default| 1

  • key_ratio (optional) – The ratio of the table width allocated for keys. Defaults to 1z. |default| 1

  • value_ratio (optional) – The ratio of the table width allocated for values. Defaults to 7. |default| 7

  • is_force_syntax (optional) – Force the use of Syntax rendering for dictionaries/lists. |default| True

Example

print_kv(key="Name", value="John Doe", symbol="👤")
# Output:
# 👤 Name   :   John Doe   str[bright_black](8)[/bright_black]

print_kv(key="Age", value=30, symbol="🔢", padding=3)
# Output:
# 🔢 Age : 30   int[bright_black](2)[/bright_black]
print_grid(data=None, title='', symbol='░', separator=':', padding=0, key_ratio=1, value_ratio=7, key_prefix='', key_postfix='', value_prefix='', value_postfix='', is_value_type=True)[source]

Print a grid layout with a title and optional padding and edge padding.

Parameters:
  • data (Optional[dict], optional) – The data to display in the grid. Default is None. |default| None

  • title (optional) – The title of the grid. Default is an empty string. |default| ''

  • symbol (optional) – The symbol used for the grid. Default is ‘░’. or 🔘★⭐️ ■ ▓ ▒ ░ |default| '░'

  • separator (optional) – The separator between the key and the value. Defaults to a colon. |default| ':'

  • padding (optional) – The padding around each cell. Default is 0. |default| 0

  • key_ratio (optional) – The ratio of the table width allocated for keys. Defaults to 1. |default| 1

  • value_ratio (optional) – The ratio of the table width allocated for values. Defaults to 7. |default| 7

  • key_prefix (optional) – A prefix to add to each key. Default is an empty string. |default| ''

  • key_postfix (optional) – A postfix to add to each key. Default is an empty string. |default| ''

  • value_prefix (optional) – A prefix to add to each value. Default is an empty string. |default| ''

  • value_postfix (optional) – A postfix to add to each value. Default is an empty string. |default| ''

  • is_value_type (optional) – Flag to indicate if value type should be displayed. |default| True

Example

data = {"Item 1": 123, "Item 2": 456}
print_grid(data, title="Inventory", symbol="■", padding=1, pad_edge=False)

# Output will display a grid with the title "Inventory", using '■' as the symbol,
# 1 padding around each cell, and no padding on the edge.

print_grid(data, title="Inventory", symbol="■", padding=1, key_prefix="[", key_postfix="]", value_prefix="(", value_postfix=")")
print_aligned_text(left_text, right_text, filler='.')[source]

Print left and right text aligned with filler in between.

Parameters:
  • left_text – The text to be aligned on the left side.

  • right_text – The text to be aligned on the right side.

  • filler (optional) – The character used to fill the space between left and right text. Default is ‘.’. |default| '.'

Example

print_aligned_text("Left Text", "Right Text")
# Left Text...........................................Right Text

print_aligned_text("Chapter 1", "Page 10", filler='*')
# Chapter 1********************************************Page 10
align_text(left_text='', right_text='', filler='.', offset=2)[source]

Aligns text to the left and right with a filler in between.

Parameters:
  • left_text (str, optional) – The text to align to the left. |default| ''

  • right_text (str, optional) – The text to align to the right. |default| ''

  • filler (str, optional) – The character to use as filler between the left and right text. |default| '.'

  • offset (int, optional) – The number of spaces to offset the right text from the right edge. |default| 2

Example

align_text('Hello', 'World', filler='-', offset=3)
# >> 'Hello ---------------------------- World'

align_text('Left', 'Right', filler='*', offset=5)
# >> 'Left ************************** Right'
disable_exception_traceback()[source]

All traceback information is suppressed and only the exception type and value are printed

exception NoTraceBackException(msg)[source]

Bases: Exception

get_color_by_threshold(value, limit=100, unit='', thresholds=None, return_tuple=False)[source]

Determine the color based on the value and thresholds.

Parameters:
  • value – The value to be evaluated.

  • limit (optional) – The limit to compare the value against. Default is 100. |default| 100

  • unit (optional) – The unit to append to the value. Default is an empty string. |default| ''

  • thresholds (optional) – A dictionary defining the thresholds and their corresponding colors. The keys are the threshold values (as a fraction of the limit) and the values are the colors. Example: {0.9: “red”, 0.8: “orange1”, 0.7: “yellow”} Default is {0.9: “red”, 0.8: “orange1”, 0.7: “yellow”, 0.0: “white”}. |default| None

  • return_tuple (optional) – If True, returns a tuple (color, formatted_value). Default is False. |default| False

Returns:

If return_tuple is False, returns a formatted string in rich text format. If return_tuple is True, returns a tuple (color, formatted_value).

pawnlib.output.file module

from pawnlib.output import file
class NullByteRemover(file_paths, chunk_size=1048576, replace_with=None)[source]

Bases: object

A class to remove null bytes from files.

Parameters:
  • file_paths – List of file paths or a single file path.

  • chunk_size (optional) – Size of the chunks to read from the file (default is 1 MB). |default| 1048576

  • replace_with (optional) – Bytes to replace null bytes with (default is empty bytes). |default| None

Example

remover = NullByteRemover(["file1.txt", "file2.txt"])
remover.remove_null_bytes()

remover = NullByteRemover("single_file.txt", replace_with=b' ')
remover.remove_null_bytes()
__init__(file_paths, chunk_size=1048576, replace_with=None)[source]

A class to remove null bytes from files.

Parameters:
  • file_paths – List of file paths or a single file path.

  • chunk_size (optional) – Size of the chunks to read from the file (default is 1 MB). |default| 1048576

  • replace_with (optional) – Bytes to replace null bytes with (default is empty bytes). |default| None

Example

remover = NullByteRemover(["file1.txt", "file2.txt"])
remover.remove_null_bytes()

remover = NullByteRemover("single_file.txt", replace_with=b' ')
remover.remove_null_bytes()
run()[source]
check_null_bytes(file_path)[source]

Check if the file contains null bytes.

Parameters:

file_path – Path to the file to check.

Returns:

True if null bytes are found, otherwise False.

Example

has_null_bytes = remover.check_null_bytes("file1.txt")
print(has_null_bytes)  # >> True or False
remove_null_bytes()[source]

Remove null bytes from the specified files and create new files without null bytes.

Example

remover.remove_null_bytes()
print_report()[source]

Print a detailed report of the processing results after completion.

This includes information about each processed file such as size and null byte count.

class Tail(log_file_paths, filters, callback, async_mode=False, formatter=None, enable_logging=True, logger=None, verbose=0)[source]

Bases: object

Tail class for monitoring log files with support for both synchronous and asynchronous modes.

Parameters:
  • log_file_paths (Union[str, List[str]]) – Paths to the log files to monitor.

  • filters (List[str]) – List of regex patterns to filter log lines.

  • callback (Callable[[str], Union[None, asyncio.coroutine]]) – Function to call with processed log lines.

  • async_mode (bool) – Whether to operate in asynchronous mode. |default| False

  • formatter (Callable[[str], str]) – Function to format log lines before processing. |default| None

Example

from pawnlib.output.file import Tail

# Synchronous mode
tail = Tail(log_file_paths=["/var/log/app.log"],
             filters=["ERROR", "CRITICAL"],
             callback=process_log_line,
             async_mode=False)

tail.follow()

# Asynchronous mode
async def process_log_line_async(line: str):
    # Your async callback logic
    pass

tail = Tail(log_file_paths=["/var/log/app.log"],
             filters=["ERROR", "WARNING"],
             callback=process_log_line_async,
             async_mode=True)

tail.follow()
async follow_async()[source]

Asynchronous follow method

follow()[source]

Unified follow method for both async and sync

check_path(path, detailed=False)[source]

Check if the provided path is a file, a directory, or neither. Optionally return extended details about the path.

Parameters:
  • path (str) – The path to check.

  • detailed (bool) – If True, returns a dictionary with additional information about the path. If False, returns only the type as a string. |default| False

Returns:

If detailed=True, returns a dictionary with path type, existence, readability, and writability. If detailed=False, returns only the type of the path as a string (‘file’, ‘directory’, or ‘neither’).

Return type:

dict | str

Example usage:

>>> check_path("/path/to/file.txt")
'file'
>>> check_path("/path/to/file.txt", detailed=True)
{'type': 'file', 'exists': True, 'readable': True, 'writable': False}
>>> check_path("/path/to/directory")
'directory'
>>> check_path("/path/to/directory", detailed=True)
{'type': 'directory', 'exists': True, 'readable': True, 'writable': True}
>>> check_path("/path/to/unknown")
None
>>> check_path("/path/to/unknown", detailed=True)
{'type': None, 'exists': False, 'readable': False, 'writable': False}

Notes

  • detailed=False by default, returning only the type of the path as a string.

  • If detailed=True, additional details (existence, readability, writability) are provided in a dictionary.

check_file_overwrite(filename, answer=None, exit_on_deny=True)[source]

Checks the existence of a file.

Parameters:
  • filename – filename to check

  • answer (optional) – answer to the question |default| None

  • exit_on_deny (bool, optional) – exit the program if the answer is no |default| True

Return type:

bool

Returns:

Example

# touch sdsd

from pawnlib.output import file
file.check_file_overwrite(filename="sdsd")
# >>   File already exists => sdsd
# >>  Overwrite already existing 'sdsd' file? (y/n)
get_file_path(filename)[source]

This function is intended to return the file information

Parameters:

filename

Return type:

dict

Returns:

Example

from pawnlib.output import file
file.get_file_path("sample_file.txt")

# >>
   {
      dirname:
      file: sample_file.txt
      extension: txt
      filename: sample_file.txt
      full_path: /examples/sample_file.txt
   }
get_file_extension(file_path)[source]

Get the file extension from a file path.

Parameters:

file_path – The path of the file.

Returns:

The extension of the file.

Example

utils.get_file_extension("/home/user/document.txt")
# >> 'txt'

utils.get_file_extension("/home/user/.hiddenfile")
# >> ''

utils.get_file_extension("/home/user/.hiddenfile.txt")
# >> 'txt'
get_file_list(path='./', pattern='*', recursive=False)[source]

Get the list of files in the specified directory that match the given pattern.

Parameters:
  • path (optional) – The path of the directory to search. Default is current directory. |default| './'

  • pattern (optional) – The pattern to match. Default is “*”, which matches all files. |default| '*'

  • recursive (optional) – Whether to search subdirectories recursively. Default is False. |default| False

Returns:

A list of file paths that match the pattern.

Example

get_file_list(directory_path="./", pattern="*.py", recursive=True)
# >> ['./main.py', './utils/helper.py']

get_file_list(directory_path="./", pattern="*.txt", recursive=False)
# >> ['./README.txt']
get_parent_path(run_path='/home/docs/checkouts/readthedocs.org/user_builds/pawnlib/checkouts/latest/pawnlib/output/file.py')[source]

Returns the parent path

Parameters:

run_path (optional) – (str) Path of the file to get the parent path of. |default| '/home/docs/checkouts/readthedocs.org/user_builds/pawnlib/checkouts/latest/pawnlib/output/file.py'

Return type:

str

Returns:

(str) Parent path of the given file path.

Example

get_parent_path(__file__)
# >> '/path/to/parent/directory'
get_script_path(run_path=None)[source]

Returns the script directory.

Parameters:

run_path (optional) – (str) Path of the file to get the script directory of. |default| None

Returns:

(str) Script directory of the given file path.

Example

get_script_dir(__file__)
# >> '/path/to/script/directory'

get_script_dir()
# >> '/path/to/script/directory'
get_real_path(run_path='/home/docs/checkouts/readthedocs.org/user_builds/pawnlib/checkouts/latest/pawnlib/output/file.py')[source]

Returns the real path.

Parameters:

run_path (optional) – (str) Path of the file to get the real path of. |default| '/home/docs/checkouts/readthedocs.org/user_builds/pawnlib/checkouts/latest/pawnlib/output/file.py'

Returns:

(str) Real path of the given file path.

Example

get_real_path(__file__)
# >> '/path/to/real/directory'
get_abs_path(filename)[source]

Returns the absolute path.

Parameters:

filename – (str) Name of the file to get the absolute path of.

Return type:

str

Returns:

(str) Absolute path of the given file name.

Example

get_abs_path('example.txt')
# >> '/path/to/example.txt'
is_binary_file(filename)[source]

Check if the file is binary.

Parameters:

filename – (str) Name of the file to check.

Return type:

bool

Returns:

(bool) True if the file is binary, False otherwise.

Example

is_binary_file('example.txt')
# >> False
is_file(filename)[source]

Check if the file exists.

Parameters:

filename (str) – (str) Name of the file to check.

Return type:

bool

Returns:

(bool) True if the file exists, False otherwise.

Example

is_file('example.txt')
# >> True
is_directory(path)[source]

Check if the given path is a directory.

Parameters:

path – The path to check.

Returns:

True if the path is a directory, False otherwise.

Example

check.is_directory("/home/user")
# >> True

check.is_directory("/home/user/myfile.txt")
# >> False
is_json(json_file)[source]

Validate the JSON.

Parameters:

json_file (str) – (str) Name of the JSON file to validate.

Return type:

bool

Returns:

(bool) True if the JSON is valid, False otherwise.

Example

is_json('example.json')
# >> True
is_json_file(json_file)[source]

Validate the JSON.

Parameters:

json_file (str) – (str) Name of the JSON file to validate.

Return type:

bool

Returns:

(bool) True if the JSON is valid, False otherwise.

Example

is_json('example.json')
# >> True
open_json(filename, encoding='utf-8-sig')[source]

Read the JSON file.

Parameters:
  • filename (str) – str, the name of the JSON file to be read.

  • encoding (optional) – str, the encoding to use when opening the file. |default| 'utf-8-sig'

Returns:

dict, the contents of the JSON file as a dictionary.

Example

json_data = open_json("data.json")
# >> {'name': 'John', 'age': 30, 'city': 'New York'}
open_file(filename, encoding=None)[source]

Read the file.

Parameters:
  • filename (str) – str, the name of the file to be read.

  • encoding (optional) – str, the encoding to use when opening the file. |default| None

Returns:

str, the contents of the file as a string.

Example

file_contents = open_file("example.txt")
# >> 'This is an example file.\nIt contains some text.\n'
open_yaml_file(filename, encoding=None)[source]

Read the YAML file.

Parameters:
  • filename (str) – str, the name of the YAML file to be read.

  • encoding (optional) – str, the encoding to use when opening the file. |default| None

Returns:

dict, the contents of the YAML file as a dictionary.

Example

yaml_data = open_yaml_file("data.yaml")
# >> {'name': 'John', 'age': 30, 'city': 'New York'}
write_file(filename, data, option='w', permit='664')[source]

Write data to a file.

Parameters:
  • filename (str) – The name of the file to write.

  • data (Any) – The data to write to the file.

  • option (str, optional) – The write mode. Default is ‘w’. |default| 'w'

  • permit (str, optional) – The permission of the file. Default is ‘664’. |default| '664'

Example

write_file('test.txt', 'Hello, World!')
# >> 'Write file -> test.txt, 13'
Returns:

A string indicating the success or failure of the write operation.

write_json(filename, data, option='w', permit='664', force_write=True, json_default=None)[source]

Write JSON data to a file.

Parameters:
  • filename (str) – The name of the file to write.

  • data (Union[dict, list]) – The JSON data to write to the file.

  • option (str, optional) – The write mode. Default is ‘w’. |default| 'w'

  • permit (str, optional) – The permission of the file. Default is ‘664’. |default| '664'

  • force_write (bool, optional) – Whether to force the write operation. Default is True. |default| True

  • json_default (optional) – A function to convert non-serializable objects to a serializable format. Default is None. |default| None

Example

data = {'name': 'John', 'age': 30}
write_json('test.json', data)
# >> 'Write json file -> test.json, 23'
Returns:

A string indicating the success or failure of the write operation.

represent_ordereddict(dumper, data)[source]

Represents an OrderedDict for YAML.

Parameters:
  • dumper – A YAML dumper instance.

  • data – An OrderedDict instance.

Example

import yaml
from collections import OrderedDict

data = OrderedDict()
data['key1'] = 'value1'
data['key2'] = 'value2'

yaml.add_representer(OrderedDict, represent_ordereddict)
print(yaml.dump(data))

# >> "key1: value1\nkey2: value2\n"
write_yaml(filename, data, option='w', permit='664')[source]

Write YAML data to a file.

Parameters:
  • filename (str) – The name of the file to write.

  • data (Union[dict, list]) – The YAML data to write to the file.

  • option (str, optional) – The write mode. Default is ‘w’. |default| 'w'

  • permit (str, optional) – The permission of the file. Default is ‘664’. |default| '664'

Example

data = {'name': 'John', 'age': 30}
write_yaml('test.yaml', data)
# >> 'Write json file -> test.yaml, 23'
Returns:

A string indicating the success or failure of the write operation.