Skip to content

Commit 5176a7a

Browse files
tests
1 parent 9db0be6 commit 5176a7a

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed

lib/dialoget.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
"""
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys
2+
sys.path.append('../../')
3+
from data.env import GITHUB_API_URL
4+
from src.create_repo_on_org_github import create_repo_on_org_github
5+
from data.prompt import *
6+
#from lib.dialoget import *
7+
#from data.map import *
8+
9+
10+
11+
12+
# print(FromEnv)
13+
# api_token="API token"
14+
# repo_name="Repository"
15+
# org_name="GitHub Organization"
16+
#f'Connect to the github API {GITHUB_API_URL} with {api_token}'
17+
#f'Create a {repo_name} on {org_name}'
18+
result = create_repo_on_org_github(repo_name, org_name, api_token, GITHUB_API_URL)
19+
# print(result)
20+
print(result == 200)
21+
22+
# f'Connect to {Github,github_url,GITHUB_API_URL} by {"API token",api_token,GITHUB_API_TOKEN}'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
sys.path.append('../../')
3+
from src.update_repo_on_github import update_repo_on_github
4+
from data.map import *
5+
#from lib.dialoget import *
6+
7+
8+
9+
#print(FromEnv)
10+
#api_token="API token"
11+
#repo_name="Repository"
12+
#org_name="GitHub Organization"
13+
f'Update a {repo_name} on {org_name}'
14+
f'Set a {description}'
15+
f'Set a {domain}'
16+
result = update_repo_on_github(api_token, org_name, repo_name, description, domain)
17+
#print(result)
18+
print(result == 200)
19+
20+
21+
# f'Connect to {Github,github_url,GITHUB_API_URL} by {"API token",api_token,GITHUB_API_TOKEN}'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
sys.path.append('../../')
3+
from data.env import GITHUB_API_URL
4+
from src.update_repo_on_github2 import update_repo_on_github2
5+
from data.prompt import *
6+
#from data.map import *
7+
8+
#print(FromEnv)
9+
#api_token="API token"
10+
#repo_name="Repository"
11+
#org_name="GitHub Organization"
12+
f'Connect to the github API {GITHUB_API_URL} with {api_token}'
13+
f'Update a {repo_name} on {org_name}'
14+
f'with a {description}'
15+
f'on the {domain}'
16+
result = update_repo_on_github2(api_token, org_name, repo_name, description, domain, GITHUB_API_URL)
17+
#print(result)
18+
print(result == 200)
19+
20+
21+
# f'Connect to {Github,github_url,GITHUB_API_URL} by {"API token",api_token,GITHUB_API_TOKEN}'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
sys.path.append('../')
3+
#from data.env import GITHUB_API_URL
4+
from src.update_repo_on_github2 import update_repo_on_github2
5+
from data.prompt import *
6+
#from data.map import *
7+
8+
from behave import given, when, then
9+
10+
@given('a user has navigated to the login page')
11+
def step_impl(context):
12+
context.browser.visit(context.config.base_url)
13+
14+
@when('the user enters a correct username and password')
15+
def step_impl(context):
16+
context.browser.fill_form_with_correct_credentials()
17+
18+
@then('they are redirected to their dashboard')
19+
def step_impl(context):
20+
assert context.browser.on_dashboard()
21+
22+
@then('they see a welcome message')
23+
def step_impl(context):
24+
assert context.browser.shows_welcome_message()
25+
26+
#print(FromEnv)
27+
#api_token="API token"
28+
#repo_name="Repository"
29+
#org_name="GitHub Organization"
30+
f'Connect to the github API {GITHUB_API_URL} with {api_token}'
31+
f'Update a {repo_name} on {org_name}'
32+
f'with a {description}'
33+
f'on the {domain}'
34+
result = update_repo_on_github2(api_token, org_name, repo_name, description, domain, GITHUB_API_URL)
35+
#print(result)
36+
print(result == 200)
37+
38+
39+
# f'Connect to {Github,github_url,GITHUB_API_URL} by {"API token",api_token,GITHUB_API_TOKEN}'

0 commit comments

Comments
 (0)