Script to get a photo of what's going on in Blaseball and copy it to the clipboard. (IP Webcam hook)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

44 lines
1.2 KiB

import re
import os
def attempt_import_config() -> dict:
"""
Attempt to import the config file
If missing, attempt to recover by copying config.sample.py
"""
# Attempt normal import
try:
from config import config
except ModuleNotFoundError as e:
config_error = e
else:
return config
# If missing, copy config from config.sample.py to config.py and try again
try:
# Get text from sample config file, but remove comment at top saying to
# rename to config.py
with open('config.sample.py') as sample:
text = ''.join(re.split(r'\n(config)', sample.read(), re.MULTILINE)[-2:])
# Create new config file
with open('config.py', 'w') as new_config:
print(text, file=new_config)
# Import new file
from config import config
except:
# Delete any malformed config that may have been generated
try:
os.remove('config.py')
except FileNotFoundError:
pass
else:
return config
# Raise *original* exception saying config.py is missing
raise config_error
config = attempt_import_config()