/
Flipper
/
Marginal
Обзор
Документация
Войти
/
Flipper
/
Marginal
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
master
src/utils.py
149 строк
5 KB
Alex
Marginal v1.0.0 RELEASE
11 янв 2026, 18:02
11 янв 2026, 18:02
5e1eff8
Код
Авторство
О чём код?
import sys import os import platform import subprocess def getBasePath() -> str: """ Get the base path of the application, handling both development and frozen (PyInstaller) environments. Returns: str -> Absolute path to the application base directory. Note: - When frozen (PyInstaller), returns directory of the executable. - When in development, returns parent directory of this file's location. """ if getattr(sys, 'frozen', False): return os.path.dirname(sys.executable) else: return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def getBakedPath() -> str: """ Get the path to PyInstaller temporary bundle directory (_MEIPASS) when running from a frozen executable. Returns: str -> Path to temporary bundle or base path if not frozen. """ try: # PyInstaller creates a temp folder and stores path in _MEIPASS return sys._MEIPASS except AttributeError: return getBasePath() def generateFullPath(base: str = getBasePath(), *args: str) -> str: """ Generate a full path by joining base path with additional path components. Args: base (str): Base directory path (defaults to application base path). *args (str): Additional path components to join. Returns: str -> Full joined path. """ return os.path.join(base, *args) def openFolder(folderPath: str): """ Open a folder in the system's file explorer (cross-platform). Args: folderPath (str): Path to folder (relative to base path). Returns: None """ path = generateFullPath(getBasePath(), folderPath) try: if os.path.exists(path): if os.name == 'nt': # Windows os.startfile(path) elif os.name == 'posix': # macOS or Linux if platform.system() == 'Darwin': # macOS subprocess.Popen(['open', path]) else: # Linux subprocess.Popen(['xdg-open', path]) except Exception as e: print(f"Failed to open folder: {e}") def listDir(folderPath: str,stripExt: bool = False,pattern: list[str] = None, recursive: bool = False,fullPath: bool = False) -> list[str]: """ List files in a directory with various filtering and formatting options. Args: folderPath (str): Directory path to scan. stripExt (bool): If True, remove file extensions from results. pattern (list[str]): Filter files by extension patterns (e.g., ["*.txt", "*.py"]). recursive (bool): If True, include files from subdirectories. fullPath (bool): If True, return full absolute paths; otherwise return relative paths. Returns: list[str] -> List of file paths or names matching the criteria. Note: Pattern matching is case-insensitive and supports wildcards (e.g., "*.txt"). """ try: if not os.path.isdir(folderPath): return [] files = [] if recursive: # Walk through directory tree recursively for root, dirs, filenames in os.walk(folderPath): for filename in filenames: # Apply pattern filtering if specified if pattern: # Check if any pattern matches (case-insensitive) matchesPattern = any( filename.lower().endswith( p.replace("*", "").lower() ) for p in pattern ) if not matchesPattern: continue if fullPath: filepath = os.path.join(root, filename) else: # Get relative path from the base folderPath relPath = os.path.relpath(root, folderPath) if relPath == ".": filepath = filename else: filepath = os.path.join(relPath, filename) files.append(filepath) else: # Only process top-level directory for item in os.listdir(folderPath): itemPath = os.path.join(folderPath, item) if os.path.isfile(itemPath): # Apply pattern filtering if specified if pattern: matchesPattern = any( item.lower().endswith( p.replace("*", "").lower() ) for p in pattern ) if not matchesPattern: continue files.append(item if not fullPath else itemPath) # Strip file extensions if requested if stripExt: files = [os.path.splitext(f)[0] for f in files] return files except Exception as e: return []