v50 Steam/Premium information for editors
  • v50 information can now be added to pages in the main namespace. v0.47 information can still be found in the DF2014 namespace. See here for more details on the new versioning policy.
  • Use this page to report any issues related to the migration.
This notice may be cached—the current version can be found here.

User:Button/BAMM

From Dwarf Fortress Wiki
Jump to navigation Jump to search

About

Features

Planned Features

Usage Instructions

Download

You can download the script from GitHub.

Python 3

Configuration

Button's Workspace

Sometimes I'm on a computer I can't put a git client on.

_is_logging_initialized = False

# TODO Allow file to be passed in, or possibly even a dict?
def load_run_config():
	"""Load config information from the default file.
	
	Also initializes loggers if they haven't been already.
	"""
    print("Loading run configuration...")
    global runconfig
    runconfig_file = open(runconfig, 'r')
    global properties
    for line in runconfig_file:
        uncommented = line.strip().split('#')[0]
        props = uncommented.strip().split('=')
        if len(props) == 0 or (len(props) == 1 and len(props[0]) == 0):
            continue
        elif len(props) != 2:
            print('Line "', line, '" in ', runconfig,
                  ' is improperly configured. Please format properties thus: \
                  "propertyname=value" (without quotes).')
        elif not _property_has_format_error(props[0], props[1]):
            set_property(props[0], props[1])
        else:
            print ('Line "', line, '" in', runconfig,
                   'is improperly configured. Please format properties thus: \
                   "propertyname=value" (without quotes).')

    runconfig_file.close()

	initialize_logging()
	userlog.info("**********")
    modderslog.info("**********")
    userlog.info("Run configuration loaded.")
	userlog.debug("Properties:")
    for propname in properties.keys():
        userlog.debug("Property %s:", propname)
        for item in properties[propname]:
            userlog.debug("\t%s", item)


# TODO implement parameters with defaults (and update docstring)
def initialize_logging()
	"""Initialize loggers config.userlog and config.modderslog
	
	Will not double-initialize.
	"""
	global _is_logging_initialized
	
	if _is_logging_initialized:
		return
	else:
		_is_logging_initialized = True

    # Logging
    fmt = logging.Formatter('%(message)s')

    userhandler = logging.FileHandler(properties[USERSLOG][1])
    userhandler.setFormatter(fmt)
    userlog.addHandler(userhandler)

    if properties[DEBUG][1]:
        userlog.setLevel(logging.DEBUG)
    else:
        userlog.setLevel(logging.INFO)

    modderhandler = logging.FileHandler(properties[MODDERSLOG][1])
    modderhandler.setFormatter(fmt)
    modderslog.addHandler(modderhandler)
    modderslog.setLevel(logging.INFO)


def _property_has_format_error(propkey, value):
	"""Returns True if the property is formatted incorrectly.
	
	* propkey is the "name" of the property, and is expected to be one of the CONFIG.X module variables declared up above.
	* value is the value you wish to check for compatibility with the property in question.
	
	Returns True if:
	
	* The property key is not recognized
	* The property's type is IS_BOOL and the value is not 'True' or 'False
	* The property's type is IS_DIR and the value is an existing (non-directory) file
	* The property's type is IS_FILE and the value is an existing directory.
	
	Otherwise, returns False.
	"""