PyKubeGrader 0.2.19__tar.gz → 0.2.21__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. {pykubegrader-0.2.19/src/PyKubeGrader.egg-info → pykubegrader-0.2.21}/PKG-INFO +1 -1
  2. {pykubegrader-0.2.19 → pykubegrader-0.2.21/src/PyKubeGrader.egg-info}/PKG-INFO +1 -1
  3. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/build/api_notebook_builder.py +6 -0
  4. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/build/build_folder.py +57 -14
  5. pykubegrader-0.2.21/src/pykubegrader/tokens/validate_token.py +138 -0
  6. pykubegrader-0.2.19/src/pykubegrader/tokens/validate_token.py +0 -71
  7. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/.coveragerc +0 -0
  8. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/.github/workflows/main.yml +0 -0
  9. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/.gitignore +0 -0
  10. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/.readthedocs.yml +0 -0
  11. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/AUTHORS.rst +0 -0
  12. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/CHANGELOG.rst +0 -0
  13. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/CONTRIBUTING.rst +0 -0
  14. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/LICENSE.txt +0 -0
  15. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/README.rst +0 -0
  16. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/Makefile +0 -0
  17. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/_static/Drexel_blue_Logo_square_Dark.png +0 -0
  18. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/_static/Drexel_blue_Logo_square_Light.png +0 -0
  19. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/_static/custom.css +0 -0
  20. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/authors.rst +0 -0
  21. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/changelog.rst +0 -0
  22. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/conf.py +0 -0
  23. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/contributing.rst +0 -0
  24. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/index.rst +0 -0
  25. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/license.rst +0 -0
  26. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/readme.rst +0 -0
  27. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/docs/requirements.txt +0 -0
  28. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/examples/.responses.json +0 -0
  29. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/examples/true_false.ipynb +0 -0
  30. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/pyproject.toml +0 -0
  31. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/setup.cfg +0 -0
  32. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/setup.py +0 -0
  33. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/PyKubeGrader.egg-info/SOURCES.txt +0 -0
  34. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/PyKubeGrader.egg-info/dependency_links.txt +0 -0
  35. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/PyKubeGrader.egg-info/entry_points.txt +0 -0
  36. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/PyKubeGrader.egg-info/not-zip-safe +0 -0
  37. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/PyKubeGrader.egg-info/requires.txt +0 -0
  38. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/PyKubeGrader.egg-info/top_level.txt +0 -0
  39. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/__init__.py +0 -0
  40. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/build/__init__.py +0 -0
  41. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/build/clean_folder.py +0 -0
  42. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/graders/__init__.py +0 -0
  43. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/graders/late_assignments.py +0 -0
  44. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/initialize.py +0 -0
  45. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/log_parser/__init__.py +0 -0
  46. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/log_parser/parse.ipynb +0 -0
  47. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/log_parser/parse.py +0 -0
  48. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/submit/submit_assignment.py +0 -0
  49. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/telemetry.py +0 -0
  50. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/tokens/tokens.py +0 -0
  51. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/utils.py +0 -0
  52. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/validate.py +0 -0
  53. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets/__init__.py +0 -0
  54. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets/multiple_choice.py +0 -0
  55. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets/reading_question.py +0 -0
  56. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets/select_many.py +0 -0
  57. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets/student_info.py +0 -0
  58. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets/style.py +0 -0
  59. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets/true_false.py +0 -0
  60. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets/types_question.py +0 -0
  61. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets_base/__init__.py +0 -0
  62. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets_base/multi_select.py +0 -0
  63. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets_base/reading.py +0 -0
  64. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/src/pykubegrader/widgets_base/select.py +0 -0
  65. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/tests/conftest.py +0 -0
  66. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/tests/import_test.py +0 -0
  67. {pykubegrader-0.2.19 → pykubegrader-0.2.21}/tox.ini +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PyKubeGrader
3
- Version: 0.2.19
3
+ Version: 0.2.21
4
4
  Summary: Add a short description here!
5
5
  Home-page: https://github.com/pyscaffold/pyscaffold/
6
6
  Author: jagar2
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PyKubeGrader
3
- Version: 0.2.19
3
+ Version: 0.2.21
4
4
  Summary: Add a short description here!
5
5
  Home-page: https://github.com/pyscaffold/pyscaffold/
6
6
  Author: jagar2
@@ -14,6 +14,7 @@ class FastAPINotebookBuilder:
14
14
  notebook_path: str
15
15
  temp_notebook: Optional[str] = None
16
16
  assignment_tag: Optional[str] = ""
17
+ require_key: Optional[bool] = False
17
18
 
18
19
  def __post_init__(self):
19
20
  self.root_path, self.filename = FastAPINotebookBuilder.get_filename_and_root(
@@ -119,6 +120,11 @@ class FastAPINotebookBuilder:
119
120
  f"os.environ['TOTAL_POINTS_FREE_RESPONSE'] = str({self.total_points})\n"
120
121
  )
121
122
 
123
+ if self.require_key:
124
+ first_cell_header.append(
125
+ "from pykubegrader.submit.submit_assignment import validate_token\nvalidate_token()\n"
126
+ )
127
+
122
128
  short_filename = self.filename.split(".")[0].replace("_temp", "")
123
129
  first_cell_header.extend(
124
130
  [
@@ -46,6 +46,7 @@ class NotebookProcessor:
46
46
  solutions_folder: str = field(init=False)
47
47
  verbose: bool = False
48
48
  log: bool = True
49
+ require_key: bool = False
49
50
 
50
51
  def __post_init__(self):
51
52
  """
@@ -559,11 +560,22 @@ class NotebookProcessor:
559
560
  "you should see your score."
560
561
  )
561
562
 
562
- # Define the Code cell
563
- code_cell = nbformat.v4.new_code_cell(
564
- "from pykubegrader.submit.submit_assignment import submit_assignment\n\n"
565
- f'submit_assignment("week{self.week_num}-{self.assignment_type}", "{os.path.basename(notebook_path).replace(".ipynb", "")}")'
566
- )
563
+ if self.require_key:
564
+ # Add an additional line for validate_token()
565
+ validate_token_line = "from pykubegrader.submit.submit_assignment import validate_token\nvalidate_token()\n"
566
+
567
+ # Define the Code cell
568
+ code_cell = nbformat.v4.new_code_cell(
569
+ f"{validate_token_line}\n\n" # Add the validate_token() line
570
+ "from pykubegrader.submit.submit_assignment import submit_assignment\n\n"
571
+ f'submit_assignment("week{self.week_num}-{self.assignment_type}", "{os.path.basename(notebook_path).replace(".ipynb", "")}")'
572
+ )
573
+ else:
574
+ # Define the Code cell without validate_token()
575
+ code_cell = nbformat.v4.new_code_cell(
576
+ "from pykubegrader.submit.submit_assignment import submit_assignment\n\n"
577
+ f'submit_assignment("week{self.week_num}-{self.assignment_type}", "{os.path.basename(notebook_path).replace(".ipynb", "")}")'
578
+ )
567
579
 
568
580
  # Make the code cell non-editable and non-deletable
569
581
  code_cell.metadata = {"editable": False, "deletable": False}
@@ -607,7 +619,9 @@ class NotebookProcessor:
607
619
  shutil.copy("./keys/.server_public_key.bin", server_public_key)
608
620
 
609
621
  out = FastAPINotebookBuilder(
610
- notebook_path=temp_notebook_path, assignment_tag=self.assignment_tag
622
+ notebook_path=temp_notebook_path,
623
+ assignment_tag=self.assignment_tag,
624
+ require_key=self.require_key,
611
625
  )
612
626
 
613
627
  debug_notebook = os.path.join(
@@ -632,7 +646,10 @@ class NotebookProcessor:
632
646
  )
633
647
 
634
648
  NotebookProcessor.add_initialization_code(
635
- student_notebook, self.week, self.assignment_type
649
+ student_notebook,
650
+ self.week,
651
+ self.assignment_type,
652
+ require_key=self.require_key,
636
653
  )
637
654
 
638
655
  NotebookProcessor.replace_temp_in_notebook(
@@ -659,7 +676,10 @@ class NotebookProcessor:
659
676
  return student_notebook, out.total_points
660
677
  else:
661
678
  NotebookProcessor.add_initialization_code(
662
- temp_notebook_path, self.week, self.assignment_type
679
+ temp_notebook_path,
680
+ self.week,
681
+ self.assignment_type,
682
+ require_key=self.require_key,
663
683
  )
664
684
  NotebookProcessor.replace_temp_no_otter(
665
685
  temp_notebook_path, temp_notebook_path
@@ -691,11 +711,19 @@ class NotebookProcessor:
691
711
  nbformat.write(notebook, f)
692
712
 
693
713
  @staticmethod
694
- def add_initialization_code(notebook_path, week, assignment_type):
714
+ def add_initialization_code(
715
+ notebook_path, week, assignment_type, require_key=False
716
+ ):
695
717
  # finds the first code cell
696
718
  index, cell = find_first_code_cell(notebook_path)
697
719
  cell = cell["source"]
698
- import_text = "from pykubegrader.initialize import initialize_assignment\n"
720
+ import_text = "# You must make sure to run all cells in sequence using shift + enter or you might encounter errors\n"
721
+ import_text += "from pykubegrader.initialize import initialize_assignment\n"
722
+ if require_key:
723
+ import_text += (
724
+ "from pykubegrader.token.validate_token import validate_token\n"
725
+ )
726
+ import_text += "validate_token('type the key provided by your TA here')\n"
699
727
  import_text += f'\nresponses = initialize_assignment("{os.path.splitext(os.path.basename(notebook_path))[0]}", "{week}", "{assignment_type}" )\n'
700
728
  cell = f"{import_text}\n" + cell
701
729
  replace_cell_source(notebook_path, index, cell)
@@ -1740,7 +1768,9 @@ def generate_mcq_file(data_dict, output_file="mc_questions.py"):
1740
1768
  )
1741
1769
  f.write(" def __init__(self):\n")
1742
1770
  f.write(" super().__init__(\n")
1743
- f.write(f' title=f"{q_value['question_text']}",\n')
1771
+ f.write(
1772
+ f' title=f"Question{q_value['question number']}: {q_value['title']}",\n'
1773
+ )
1744
1774
  f.write(" style=MCQ,\n")
1745
1775
  f.write(
1746
1776
  f" question_number={q_value['question number']},\n"
@@ -1811,7 +1841,9 @@ def generate_select_many_file(data_dict, output_file="select_many_questions.py")
1811
1841
  )
1812
1842
  f.write(" def __init__(self):\n")
1813
1843
  f.write(" super().__init__(\n")
1814
- f.write(f' title=f"{q_value['question_text']}",\n')
1844
+ f.write(
1845
+ f' title=f"Question{q_value['question number']}: {q_value['title']}",\n'
1846
+ )
1815
1847
  f.write(" style=MultiSelect,\n")
1816
1848
  f.write(
1817
1849
  f" question_number={q_value['question number']},\n"
@@ -1888,7 +1920,9 @@ def generate_tf_file(data_dict, output_file="tf_questions.py"):
1888
1920
  )
1889
1921
  f.write(" def __init__(self):\n")
1890
1922
  f.write(" super().__init__(\n")
1891
- f.write(f' title=f"{q_value['question_text']}",\n')
1923
+ f.write(
1924
+ f' title=f"Question{q_value['question number']}: {q_value['title']}",\n'
1925
+ )
1892
1926
  f.write(" style=TFStyle,\n")
1893
1927
  f.write(
1894
1928
  f" question_number={q_value['question number']},\n"
@@ -2060,9 +2094,18 @@ def main():
2060
2094
  default="Reading-Week-X",
2061
2095
  )
2062
2096
 
2097
+ parser.add_argument(
2098
+ "--require-key",
2099
+ type=bool,
2100
+ help="Require a key to be generated for the assignment",
2101
+ default=False,
2102
+ )
2103
+
2063
2104
  args = parser.parse_args()
2064
2105
  processor = NotebookProcessor(
2065
- root_folder=args.root_folder, assignment_tag=args.assignment_tag
2106
+ root_folder=args.root_folder,
2107
+ assignment_tag=args.assignment_tag,
2108
+ require_key=args.require_key,
2066
2109
  )
2067
2110
  processor.process_notebooks()
2068
2111
 
@@ -0,0 +1,138 @@
1
+ import os
2
+ import base64
3
+ import httpx
4
+ import asyncio
5
+ import nest_asyncio
6
+
7
+ # Apply nest_asyncio for environments like Jupyter
8
+ nest_asyncio.apply()
9
+
10
+
11
+ class TokenValidationError(Exception):
12
+ """
13
+ Custom exception raised when the token validation fails.
14
+ """
15
+
16
+ def __init__(self, message=None):
17
+ """
18
+ Initialize the exception with an optional message.
19
+
20
+ Args:
21
+ message (str, optional): The error message to display. Defaults to None.
22
+ """
23
+ super().__init__(message)
24
+
25
+
26
+ def get_credentials():
27
+ """
28
+ Fetch the username and password from environment variables.
29
+
30
+ Returns:
31
+ dict: A dictionary containing 'username' and 'password'.
32
+ """
33
+ username = os.getenv("user_name_student")
34
+ password = os.getenv("keys_student")
35
+ if not username or not password:
36
+ raise ValueError(
37
+ "Environment variables 'user_name_student' or 'keys_student' are not set."
38
+ )
39
+ return {"username": username, "password": password}
40
+
41
+
42
+ async def async_validate_token(token: str) -> None:
43
+ """
44
+ Asynchronously validate a token by making a GET request to the validation endpoint.
45
+
46
+ Args:
47
+ token (str): The token to validate.
48
+
49
+ Raises:
50
+ TokenValidationError: If the token is invalid or if there is an error in the validation process.
51
+
52
+ Returns:
53
+ None: If the token is valid, the function will pass silently.
54
+ """
55
+
56
+ if token is not None:
57
+ os.environ["TOKEN"] = token
58
+
59
+ if token is None:
60
+ token = os.getenv("TOKEN", None)
61
+
62
+ if token is None:
63
+ raise TokenValidationError("No token provided")
64
+
65
+ # Fetch the endpoint URL
66
+ base_url = os.getenv("DB_URL")
67
+ if not base_url:
68
+ raise ValueError("Environment variable 'DB_URL' is not set.")
69
+ endpoint = f"{base_url}validate-token/{token}"
70
+
71
+ # Get credentials
72
+ credentials = get_credentials()
73
+ username = credentials["username"]
74
+ password = credentials["password"]
75
+
76
+ # Encode credentials for Basic Authentication
77
+ auth_header = (
78
+ f"Basic {base64.b64encode(f'{username}:{password}'.encode()).decode()}"
79
+ )
80
+
81
+ # Make the GET request
82
+ async with httpx.AsyncClient() as client:
83
+ try:
84
+ response = await client.get(
85
+ endpoint, headers={"Authorization": auth_header}, timeout=10
86
+ )
87
+
88
+ if response.status_code == 200:
89
+ # If the response is 200, the token is valid
90
+ return # Pass silently
91
+ elif response.status_code == 404:
92
+ # If the response is 404, the token is invalid
93
+ detail = response.json().get("detail", "Token not found")
94
+ raise TokenValidationError(detail)
95
+ else:
96
+ # Handle unexpected status codes
97
+ raise TokenValidationError(
98
+ f"Unexpected response code: {response.status_code}"
99
+ )
100
+ except httpx.RequestError as e:
101
+ raise TokenValidationError(f"Request failed: {e}")
102
+ except Exception as e:
103
+ raise TokenValidationError(f"An unexpected error occurred: {e}")
104
+
105
+
106
+ def validate_token(token: str) -> None:
107
+ """
108
+ Synchronous wrapper for the `async_validate_token` function.
109
+
110
+ Args:
111
+ token (str): The token to validate.
112
+
113
+ Raises:
114
+ TokenValidationError: If the token is invalid or if there is an error in the validation process.
115
+
116
+ Returns:
117
+ None: If the token is valid, the function will pass silently.
118
+ """
119
+
120
+ # Get the current event loop or create one
121
+ try:
122
+ loop = asyncio.get_event_loop()
123
+ except RuntimeError:
124
+ loop = asyncio.new_event_loop()
125
+ asyncio.set_event_loop(loop)
126
+
127
+ # Run the async function in the event loop
128
+ loop.run_until_complete(async_validate_token(token))
129
+
130
+
131
+ # Example usage
132
+ if __name__ == "__main__":
133
+ token = "test"
134
+ try:
135
+ validate_token(token)
136
+ print("Token is valid.")
137
+ except TokenValidationError as e:
138
+ print(f"Token validation failed: {e}")
@@ -1,71 +0,0 @@
1
- import requests
2
- import os
3
-
4
- class TokenValidationError(Exception):
5
- """
6
- Custom exception raised when the token validation fails.
7
- """
8
-
9
- def __init__(self, message=None):
10
- """
11
- Initialize the exception with an optional message.
12
-
13
- Args:
14
- message (str, optional): The error message to display. Defaults to None.
15
- """
16
- super().__init__(message)
17
-
18
-
19
- def validate_token(token = None):
20
- """
21
- Validate a token by making a GET request to the validation endpoint.
22
-
23
- Args:
24
- token (str): The token to validate.
25
-
26
- Raises:
27
- TokenValidationError: If the token is invalid or if there is an error in the validation process.
28
-
29
- Returns:
30
- None: If the token is valid, the function will pass silently.
31
- """
32
- # sets the token in the environment variable
33
- if token is not None:
34
- os.environ["TOKEN"] = token
35
-
36
- if token is None:
37
- token = os.getenv("TOKEN", None)
38
-
39
- if token is None:
40
- raise TokenValidationError("No token provided")
41
-
42
- # Define the validation endpoint
43
- endpoint = f"https://engr-131-api.eastus.cloudapp.azure.com/validate-token/{token}"
44
-
45
- try:
46
- response = requests.get(endpoint, timeout=10)
47
-
48
- if response.status_code == 200:
49
- # If the response is 200, the token is valid
50
- return # Pass silently
51
- elif response.status_code == 404:
52
- # If the response is 404, the token is invalid
53
- raise TokenValidationError(response.json().get("detail", "Token not found"))
54
- else:
55
- # Handle unexpected status codes
56
- raise TokenValidationError(
57
- f"Unexpected response code: {response.status_code}"
58
- )
59
- except requests.RequestException as e:
60
- # Raise an exception for connection errors or timeout
61
- raise TokenValidationError(f"Request failed: {e}")
62
-
63
-
64
- # Example usage
65
- if __name__ == "__main__":
66
- token = "test"
67
- try:
68
- validate_token(token)
69
- print("Token is valid.")
70
- except TokenValidationError as e:
71
- print(f"Token validation failed: {e}")
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes