sudo-soldier commited on
Commit
3a2ee82
·
verified ·
1 Parent(s): cff5e12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -13
app.py CHANGED
@@ -11,19 +11,53 @@ EXTRACTED_FOLDER = "nes" # Directory to serve extracted ROMs
11
  # Ensure extraction directory exists
12
  os.makedirs(EXTRACTED_FOLDER, exist_ok=True)
13
 
14
- # Function to extract ROMs and nested zips
15
- def extract_roms():
16
- if os.path.exists(ZIP_FILE):
17
- with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:
18
- for file in zip_ref.namelist():
19
- if not file.startswith("__MACOSX/"):
20
- zip_ref.extract(file, EXTRACTED_FOLDER)
21
- extracted_path = os.path.join(EXTRACTED_FOLDER, file)
22
- print(f"Extracted: {extracted_path}") # Debug output
23
- if file.endswith(".zip"):
24
- extract_nested_zip(extracted_path)
25
- else:
26
- print("Error: `nes.zip` is missing. Upload it to the root directory.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Function to extract nested zip files (ROM zips)
29
  def extract_nested_zip(nested_zip_path):
 
11
  # Ensure extraction directory exists
12
  os.makedirs(EXTRACTED_FOLDER, exist_ok=True)
13
 
14
+ #def extract_roms():
15
+ """
16
+ Extract ROM files from a zip archive into an extracted folder.
17
+
18
+ Raises:
19
+ FileNotFoundError: If the zip file is missing or not found in the current directory.
20
+ PermissionError: When attempting to write data somewhere without sufficient permissions.
21
+
22
+ Returns:
23
+ list: A list of paths for extracted ROMs.
24
+ """
25
+ try:
26
+ if os.path.exists(ZIP_FILE):
27
+ with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:
28
+ for file in zip_ref.namelist():
29
+ # Ignore special files that do not contain actual data
30
+ if not file.startswith("__MACOSX/"):
31
+ zip_ref.extract(file, EXTRACTED_FOLDER)
32
+ extracted_path = os.path.join(EXTRACTED_FOLDER, file)
33
+ print(f"Extracted: {extracted_path}")
34
+ else:
35
+ raise FileNotFoundError("Error: `nes.zip` is missing. Please upload it to the root directory.")
36
+
37
+ except PermissionError as e:
38
+ # Handle permission errors gracefully
39
+ print(f"Permission error encountered while extracting ROMs - {e}")
40
+
41
+ return [str(extracted_path) for extracted_path in os.listdir(EXTRACTED_FOLDER)]
42
+
43
+ def list_roms():
44
+ """
45
+ List all available ROM files and construct a dropdown menu with the file paths.
46
+
47
+ Returns:
48
+ list: A list of paths to extracted ROMs, which are used as options for Gradio's dropdown input.
49
+
50
+ Raises:
51
+ FileNotFoundError: If the directory containing extracted files does not exist or if `nes.zip` is missing.
52
+ """
53
+ try:
54
+ # Check that a directory exists where our data will be stored and download from 'nes' zip archive
55
+ rom_files = extract_roms()
56
+
57
+ except FileNotFoundError as e:
58
+ print(f"Error: ROM folder has been removed. The program should not continue with this action - {e}")
59
+
60
+ return [str(rom) for rom in rom_files]
61
 
62
  # Function to extract nested zip files (ROM zips)
63
  def extract_nested_zip(nested_zip_path):