Arrcttacsrks commited on
Commit
6cb9bde
·
verified ·
1 Parent(s): 0fae916

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -4
app.py CHANGED
@@ -4,6 +4,11 @@ import tempfile
4
  import os
5
  import time
6
  import sys
 
 
 
 
 
7
 
8
  # Check for required dependencies
9
  try:
@@ -14,6 +19,40 @@ except ImportError as e:
14
  print("Please install requirements: pip install gradio skidl")
15
  sys.exit(1)
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def cleanup_old_files():
18
  """Clean up temporary files older than 1 hour."""
19
  temp_dir = tempfile.gettempdir()
@@ -33,6 +72,9 @@ def generate_circuit(skidl_code):
33
  reset() # Reset SKiDL state
34
  tmp_path = None
35
  try:
 
 
 
36
  # Validate code syntax
37
  try:
38
  compile(skidl_code, '<string>', 'exec')
@@ -74,15 +116,23 @@ def generate_circuit(skidl_code):
74
 
75
  # Gradio UI
76
  with gr.Blocks() as demo:
77
- gr.Markdown("# SKiDL Circuit Builder (No KiCad Needed)")
 
 
 
 
 
78
 
79
- # Example code for placeholder
80
  example_code = """
81
  from skidl import *
 
 
 
82
  vcc = Vcc()
83
  gnd = Gnd()
84
- r1 = Part('R', 'R', value='1k')
85
- c1 = Part('C', 'C', value='1uF')
86
  r1[1] & c1 & gnd
87
  r1[2] & vcc
88
  """
 
4
  import os
5
  import time
6
  import sys
7
+ import logging
8
+
9
+ # Set up logging
10
+ logging.basicConfig(level=logging.INFO)
11
+ logger = logging.getLogger(__name__)
12
 
13
  # Check for required dependencies
14
  try:
 
19
  print("Please install requirements: pip install gradio skidl")
20
  sys.exit(1)
21
 
22
+ # Check KiCad symbol directory environment variables
23
+ def check_kicad_env():
24
+ """Check if KiCad symbol directory environment variables are set."""
25
+ kicad_vars = [
26
+ 'KICAD_SYMBOL_DIR',
27
+ 'KICAD6_SYMBOL_DIR',
28
+ 'KICAD7_SYMBOL_DIR',
29
+ 'KICAD8_SYMBOL_DIR'
30
+ ]
31
+ missing = [var for var in kicad_vars if not os.environ.get(var)]
32
+ if missing:
33
+ logger.warning(
34
+ f"Missing KiCad environment variables: {', '.join(missing)}. "
35
+ "Set these to access default KiCad symbol libraries."
36
+ )
37
+ return False
38
+ return True
39
+
40
+ # Set default symbol library path if not set
41
+ def set_default_symbol_lib():
42
+ """Set a fallback symbol library path if environment variables are missing."""
43
+ if not check_kicad_env():
44
+ # Use a generic library or custom path (modify as needed)
45
+ default_lib_path = os.path.expanduser("~/kicad_symbols") # Example path
46
+ if os.path.exists(default_lib_path):
47
+ os.environ['KICAD_SYMBOL_DIR'] = default_lib_path
48
+ lib_search_paths.append(default_lib_path)
49
+ logger.info(f"Set default symbol library path: {default_lib_path}")
50
+ else:
51
+ logger.warning(
52
+ f"Default symbol library path {default_lib_path} not found. "
53
+ "Parts may not be resolved without valid libraries."
54
+ )
55
+
56
  def cleanup_old_files():
57
  """Clean up temporary files older than 1 hour."""
58
  temp_dir = tempfile.gettempdir()
 
72
  reset() # Reset SKiDL state
73
  tmp_path = None
74
  try:
75
+ # Set default symbol library if needed
76
+ set_default_symbol_lib()
77
+
78
  # Validate code syntax
79
  try:
80
  compile(skidl_code, '<string>', 'exec')
 
116
 
117
  # Gradio UI
118
  with gr.Blocks() as demo:
119
+ gr.Markdown(
120
+ """
121
+ # SKiDL Circuit Builder (No KiCad Needed)
122
+ **Note**: Ensure KiCad symbol libraries are accessible by setting `KICAD_SYMBOL_DIR` or other relevant environment variables.
123
+ """
124
+ )
125
 
126
+ # Example code with explicit library (Device library for basic components)
127
  example_code = """
128
  from skidl import *
129
+ # Use Device library for basic components
130
+ lib_search_paths.append('.') # Ensure local libraries are searchable
131
+ set_default_lib('Device') # Use built-in Device library
132
  vcc = Vcc()
133
  gnd = Gnd()
134
+ r1 = Part('Device', 'R', value='1k') # Explicitly use Device library
135
+ c1 = Part('Device', 'C', value='1uF')
136
  r1[1] & c1 & gnd
137
  r1[2] & vcc
138
  """