def _get_default_file_path(): try: if os.path.isdir(fo.getUserConfigDir()) and not os.path.isdir(CONFIG_DEFAULT_DIR): os.makedirs(CONFIG_DEFAULT_DIR) except OSError: sys.stderr.write("AI Config Error: could not create path %s\n" % CONFIG_DEFAULT_DIR) return fo.getUserConfigDir() return CONFIG_DEFAULT_DIR
def _get_default_file_path(): try: if os.path.isdir(fo.getUserConfigDir() ) and not os.path.isdir(CONFIG_DEFAULT_DIR): os.makedirs(CONFIG_DEFAULT_DIR) except OSError: sys.stderr.write("AI Config Error: could not create path %s\n" % CONFIG_DEFAULT_DIR) return fo.getUserConfigDir() return CONFIG_DEFAULT_DIR
def _parse_options(): # get defaults; check if don't already exist and can write default_file = _get_option_file() if os.path.exists(default_file): config = SafeConfigParser() config.read([default_file]) else: try: config = _create_default_config_file(default_file) except IOError: sys.stderr.write("AI Config: default file is not present and not writable at location %s\n" % default_file) config = _create_default_config_file(os.path.join(fo.getUserConfigDir(), CONFIG_DEFAULT_FILE)) option_file = _get_option_file() # if not os.path.exists(option_path): # raise Exception('Error, option path "%s" does not exists.' % option_path) # read the defaults and then the specified config path if option_file: config_files = [option_file] configs_read = config.read(config_files) print "AI Config read config file(s): %s" % configs_read if len(configs_read) != len(config_files): sys.stderr.write("AI Config Error; could NOT read config file(s): %s\n" % list(set(config_files).difference(configs_read))) for section in config.sections(): sectioned_options.setdefault(section, odict()) for k, v in config.items(section): flat_options[k] = v sectioned_options[section][k] = v
def _parse_options(): # get defaults; check if don't already exist and can write default_file = _get_option_file() if os.path.exists(default_file): config = SafeConfigParser() config.read([default_file]) else: try: config = _create_default_config_file(default_file) except IOError: sys.stderr.write( "AI Config: default file is not present and not writable at location %s\n" % default_file) config = _create_default_config_file( os.path.join(fo.getUserConfigDir(), CONFIG_DEFAULT_FILE)) option_file = _get_option_file() # if not os.path.exists(option_path): # raise Exception('Error, option path "%s" does not exists.' % option_path) # read the defaults and then the specified config path if option_file: config_files = [option_file] configs_read = config.read(config_files) print "AI Config read config file(s): %s" % configs_read if len(configs_read) != len(config_files): sys.stderr.write( "AI Config Error; could NOT read config file(s): %s\n" % list(set(config_files).difference(configs_read))) for section in config.sections(): sectioned_options.setdefault(section, odict()) for k, v in config.items(section): flat_options[k] = v sectioned_options[section][k] = v
these methods in turn activate other portions of the python AI code.""" from logging import debug, info, error, fatal from common.configure_logging import redirect_logging_to_freeorion_logger # Logging is redirected before other imports so that import errors appear in log files. redirect_logging_to_freeorion_logger() import sys import random import freeOrionAIInterface as fo # interface used to interact with FreeOrion AI client # pylint: disable=import-error from common.option_tools import parse_config, get_option_dict, check_bool parse_config(fo.getOptionsDBOptionStr("ai-config"), fo.getUserConfigDir()) from freeorion_tools import patch_interface patch_interface() import ColonisationAI import ExplorationAI import DiplomaticCorp import FleetUtilsAI import InvasionAI import MilitaryAI import PlanetUtilsAI import PriorityAI import ProductionAI import ResearchAI import ResourcesAI
"""The FreeOrionAI module contains the methods which can be made by the C AIInterface; these methods in turn activate other portions of the python AI code.""" import pickle # Python object serialization library import sys import random # IMPORTANT! this import also execute python code to update freeOrionAIInterface interface, # removing this import will brake AI in unexpected way. import freeOrionAIInterface as fo # interface used to interact with FreeOrion AI client # pylint: disable=import-error from common.option_tools import parse_config parse_config(fo.getAIConfigStr(), fo.getUserConfigDir()) from freeorion_tools import patch_interface patch_interface() import AIstate import ColonisationAI import ExplorationAI import DiplomaticCorp import FleetUtilsAI import InvasionAI import MilitaryAI import PlanetUtilsAI import PriorityAI import ProductionAI import ResearchAI import ResourcesAI from freeorion_tools import UserStringList, chat_on_error, print_error, UserString, handle_debug_chat, Timer, init_handlers from common.listeners import listener
import os from ConfigParser import SafeConfigParser from collections import OrderedDict as odict import sys try: import freeOrionAIInterface as fo # pylint: disable=import-error except ImportError: sys.stderr.write("Executing outside of FreeOrion.\n") raise AI_SUB_DIR = 'AI' DEFAULT_SUB_DIR = os.path.join(AI_SUB_DIR, 'default') CONFIG_DEFAULT_DIR = os.path.join(fo.getUserConfigDir(), DEFAULT_SUB_DIR) CONFIG_DEFAULT_FILE = 'config.ini' # CONFIG KEYS TIMER_SECTION = 'Timers' TIMERS_USE_TIMERS = 'timers_to_log' TIMERS_TO_FILE = 'timers_dump' TIMERS_DUMP_FOLDER = 'timers_dump_folder' HANDLERS = 'handlers' flat_options = odict() sectioned_options = odict() def check_bool(option): return str(option).lower() in ["1", "on", "true", "yes"]
"""The FreeOrionAI module contains the methods which can be made by the C AIInterface; these methods in turn activate other portions of the python AI code.""" import pickle # Python object serialization library import sys import random from common import configure_logging import freeOrionAIInterface as fo # interface used to interact with FreeOrion AI client # pylint: disable=import-error from common.option_tools import parse_config parse_config(fo.getAIConfigStr(), fo.getUserConfigDir()) from freeorion_tools import patch_interface patch_interface() import AIstate import ColonisationAI import ExplorationAI import DiplomaticCorp import FleetUtilsAI import InvasionAI import MilitaryAI import PlanetUtilsAI import PriorityAI import ProductionAI import ResearchAI import ResourcesAI import TechsListsAI from freeorion_tools import UserStringList, chat_on_error, print_error, UserString, handle_debug_chat, Timer, init_handlers from common.listeners import listener