1+ import sys
2+ sys .path .append ('../' )
3+ import os
4+ #from functools import wraps
5+ import functools
6+ import csv
7+ from datetime import datetime
8+
9+
10+ # root path to the project
11+ #ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
12+ ROOT_PATH = ''
13+ LOG_FILE = 'log.csv'
14+ ERROR_FILE = 'error.csv'
15+ LOG_PATH = os .path .join (ROOT_PATH , LOG_FILE )
16+ ERROR_PATH = os .path .join (ROOT_PATH , ERROR_FILE )
17+
18+ def dialoget (template , logs_path = '' ):
19+ USER_ERROR = os .path .abspath ( os .path .join (logs_path ,ERROR_PATH ) )
20+ USER_LOGS = os .path .abspath ( os .path .join (logs_path ,LOG_PATH ) )
21+ def decorator (func ):
22+ @functools .wraps (func )
23+ def wrapper (* args , ** kwargs ):
24+ # Get function argument names
25+ arg_names = func .__code__ .co_varnames [:func .__code__ .co_argcount ]
26+
27+ # Create a context dictionary that maps argument names to values
28+ context = dict (zip (arg_names , args ))
29+ context .update (kwargs )
30+
31+ # Check for undefined variables and log errors
32+ missing_vars = [name for name in arg_names if context .get (name ) is None ]
33+ if missing_vars :
34+ error_message = f"Warning: The following variables are undefined: { ', ' .join (missing_vars )} "
35+ log_to_csv (USER_ERROR , {'timestamp' : datetime .now (), 'error' : error_message })
36+ return None
37+
38+ # Try to replace placeholders in the template with actual argument values
39+ try :
40+ filled_template = template .format (** context )
41+ except KeyError as e :
42+ error_message = f"Error: in function '{ func .__name__ } ', variable { e } is not defined"
43+ log_to_csv (USER_ERROR , {'timestamp' : datetime .now (), 'error' : error_message })
44+ return None
45+
46+ # Log the filled template string
47+ log_to_csv (USER_LOGS , {'timestamp' : datetime .now (), 'message' : filled_template })
48+
49+ # Call the original function
50+ return func (* args , ** kwargs )
51+
52+ return wrapper
53+ return decorator
54+
55+ def log_to_csv (file_name , log_dict ):
56+ # Write log message or error to a CSV file
57+ with open (file_name , mode = 'a' , newline = '' ) as file :
58+ writer = csv .DictWriter (file , fieldnames = log_dict .keys ())
59+ if file .tell () == 0 : # Write header only if the file is empty
60+ writer .writeheader ()
61+ writer .writerow (log_dict )
62+
63+
64+ """
65+ def dialoget(template):
66+ def decorator(func):
67+ @functools.wraps(func)
68+ def wrapper(*args, **kwargs):
69+ # Assuming the function returns a dictionary of replacements
70+ replacements = func(*args, **kwargs)
71+
72+ # Replace placeholders in the template with actual values
73+ filled_template = template.format(**replacements)
74+
75+ # Print or use the filled template string
76+ print(filled_template) # or you can return it, if needed
77+
78+ # Return the result of the function, if it's necessary.
79+ return replacements
80+
81+ return wrapper
82+
83+ return decorator
84+
85+
86+
87+ # This is the decorator factory that accepts arguments
88+ def dialoget2(sentence="Sentence"):
89+ # This is the actual decorator
90+ def decorator(func):
91+ call_count = 0
92+
93+ @functools.wraps(func)
94+ def wrapper(*args, **kwargs):
95+ nonlocal call_count
96+ call_count += 1
97+ print(f"The sentence: {sentence} {func.__name__} has been called {call_count} times")
98+ return func(*args, **kwargs)
99+
100+ return wrapper
101+ return decorator
102+
103+
104+
105+
106+ # Usage example with decorator arguments
107+
108+ def nfunc(func):
109+ # This will hold the number of times the function has been called
110+ call_count = 0
111+
112+ @wraps(func) # Use this to preserve the original function's metadata
113+ def wrapper(*args, **kwargs):
114+ nonlocal call_count
115+ call_count += 1
116+ print(f"Function {func.__name__} has been called {call_count} times")
117+ return func(*args, **kwargs)
118+
119+ return wrapper
120+
121+ """
0 commit comments