Skip to content

Commit

Permalink
New command "mpf init" to initialize a project folder
Browse files Browse the repository at this point in the history
  • Loading branch information
avanwinkle committed Jul 10, 2024
1 parent b619883 commit 2af5478
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
69 changes: 69 additions & 0 deletions mpf/commands/init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Command to initialize an MPF project folder."""

import os
import pathlib
import shutil

SUBCOMMAND = True
INITIAL_MODES = ("attract", "base")

class Command:

"""Initializes an MPF project folder."""

__slots__ = ("path", )

def __init__(self, argv, path):
"""Generate config and mode folders."""
# If a path is provided, use it
if len(argv) > 1:
path = os.path.join(path, argv[1])
self.path = path

print(f"Initializing MPF project at path {self.path}")

try:
os.stat(self.path)
except FileNotFoundError:
os.makedirs(self.path)

self.generate_config_file()
self.generate_mode_folder()

print("MPF project initialization complete!")

def generate_config_file(self):
# Create the config folder
config_dir = os.path.join(self.path, "config")
try:
os.stat(config_dir)
except FileNotFoundError:
os.mkdir(config_dir)

config_file = os.path.join(config_dir, "config.yaml")
try:
os.stat(config_file)
except FileNotFoundError:
base_config_file = os.path.join(pathlib.Path(__file__).parent.absolute(), "templates/config.yaml")
shutil.copy2(base_config_file, config_file)

def generate_mode_folder(self):
mode_dir = os.path.join(self.path, "modes")
try:
os.stat(mode_dir)
except FileNotFoundError:
os.mkdir(mode_dir)


for mode in INITIAL_MODES:
mode_path = os.path.join(mode_dir, mode, "config")
try:
os.stat(mode_path)
except FileNotFoundError:
os.makedirs(mode_path)
mode_config = os.path.join(mode_path, f"{mode}.yaml")
try:
os.stat(mode_config)
except FileNotFoundError:
mode_config_template = os.path.join(pathlib.Path(__file__).parent.absolute(), f"templates/{mode}.yaml")
shutil.copy2(mode_config_template, mode_config)
7 changes: 7 additions & 0 deletions mpf/commands/templates/attract.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#config_version=6

# Attract mode that starts when the machine boots up, stops
# whenever a game is started, and re-starts after the game ends.

slide_player:
mode_attract_started: attract
9 changes: 9 additions & 0 deletions mpf/commands/templates/base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#config_version=6

# Base mode that starts at the beginning of each ball
# and stops at the end of each ball.

mode:
start_events: ball_starting
stop_events: ball_will_end
priority: 100
12 changes: 12 additions & 0 deletions mpf/commands/templates/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#config_version=6

## MPF Main Machine Config File
#
# This file defines your system-wide properties like switches,
# drivers, ball devices, hardware platforms, and more. You can
# include everything here, or create sub-config files to be
# imported and merged with this one.

modes:
- attract
- base

0 comments on commit 2af5478

Please sign in to comment.