Exemplo n.º 1
0
    def put(self):
        """Update Project"""
        config = current_app.cea_config
        payload = api.payload

        if 'path' in payload:
            path = payload['path']
            if os.path.exists(path):
                config.project = path
                if 'scenario' not in payload:
                    config.scenario_name = ''
                    config.save()
                    return {'message': 'Project path changed'}
            else:
                abort(400, 'Path given does not exist')

        if 'scenario' in payload:
            scenario = payload['scenario']
            choices = list_scenario_names_for_project(config)
            if scenario in choices:
                config.scenario_name = scenario
                config.save()
                return {'message': 'Scenario changed'}
            else:
                abort(400, 'Scenario does not exist', choices=choices)
Exemplo n.º 2
0
    def put(self):
        """Update Project info in config"""
        config = current_app.cea_config
        project = api.payload.get('project')
        scenario_name = api.payload.get('scenario_name')

        if project and scenario_name:
            # Project path must exist but scenario does not have to
            if os.path.exists(project):
                config.project = project
                config.scenario_name = scenario_name
                config.save()
                return {
                    'message': 'Updated project info in config',
                    'project': project,
                    'scenario_name': scenario_name
                }
            else:
                abort(
                    400, 'project: "{project}" does not exist'.format(
                        project=project))
        else:
            abort(
                400,
                'Parameters not valid - project: {project}, scenario_name: {scenario_name}'
                .format(project=project, scenario_name=scenario_name))
def main(args=None):
    """
    :param List[str] args: the arguments to use - when not running from the command line, for testing.
    :return:
    """
    if args is None:
        args = sys.argv[1:]  # drop the script name from the arguments

    config = cea.config.Configuration()

    # handle arguments
    if not len(args) or args[0].lower() == '--help':
        print_help()
        sys.exit(1)
    script_name = args.pop(0)
    if script_name == "write":
        for fqname, value in cea.config.parse_command_line_args(args).items():
            parameter = config.get_parameter(fqname)
            parameter.set(parameter.decode(
                parameter.replace_references(value)))
    elif script_name == "read":
        for fqname in args:
            parameter = config.get_parameter(fqname)
            print("- {fqname} = {parameter_value}".format(
                fqname=fqname, parameter_value=parameter.get()))
            print("  (default: {default})".format(default=parameter.default))
    else:
        cea_script = cea.scripts.by_name(script_name, plugins=config.plugins)
        config.restrict_to(cea_script.parameters)
        config.apply_command_line_args(args, cea_script.parameters)
        cea_script.print_script_configuration(config, verb='Configuring')

    # save the updates to the configuration file (re-running the same tool will result in the
    # same parameters being set)
    config.save(cea.config.CEA_CONFIG)
Exemplo n.º 4
0
def main(config=None):
    """

    :param cea.config.Configuration config: the configuration file to use (instead of creating a new one)
    :return:
    """
    if not config:
        config = cea.config.Configuration()

    cli_config = get_cli_config()

    # handle arguments
    args = sys.argv[1:]  # drop the script name from the arguments
    if not len(args) or args[0].lower() == '--help':
        print_help()
        sys.exit(1)
    script_name = args.pop(0)
    option_list = cli_config.get('config', script_name).split()
    config.restrict_to(option_list)
    config.apply_command_line_args(args, option_list)

    # save the updates to the configuration file (re-running the same tool will result in the
    # same parameters being set)
    config.save(cea.config.CEA_CONFIG)
    print_script_configuration(config, script_name, option_list)
Exemplo n.º 5
0
 def post(self, tool_name):
     """Save the configuration for this tool to the configuration file"""
     config = cea.config.Configuration()
     for parameter in parameters_for_script(tool_name, config):
         payload = api.payload[parameter.name]
         print('%s: %s' % (parameter.name, payload))
         parameter.set(payload)
     config.save()
     return 'Success'
Exemplo n.º 6
0
def route_restore_defaults(script_name):
    """Restore the default configuration values for the CEA"""
    config = current_app.cea_config

    for parameter in parameters_for_script(script_name, config):
        if parameter.name != 'scenario':
            parameter.set(parameter.default)
    config.save()

    return redirect(url_for('tools_blueprint.route_tool', script_name=script_name))
Exemplo n.º 7
0
def main(args=None):
    """
    :param List[str] args: the arguments to use - when not running from the command line, for testing.
    :return:
    """
    if args is None:
        args = sys.argv[1:]  # drop the script name from the arguments

    config = cea.config.Configuration()

    # handle arguments
    if not len(args) or args[0].lower() == '--help':
        print_help()
        sys.exit(1)
    script_name = args.pop(0)
    if script_name == "write":
        for fqname, value in cea.config.parse_command_line_args(args).items():
            parameter = config.get_parameter(fqname)
            parameter.set(parameter.decode(
                parameter.replace_references(value)))
    elif script_name == "read":
        for fqname in args:
            parameter = config.get_parameter(fqname)
            print("- {fqname} = {parameter_value}".format(
                fqname=fqname, parameter_value=parameter.get()))
            print("  (default: {default})".format(default=parameter.default))
    elif script_name == "add-plugins":
        plugins_fqname = "general:plugins"
        parameter = config.get_parameter(plugins_fqname)
        plugins = cea.config.parse_string_to_list(
            parameter.encode(parameter.get()))

        new_plugins = [p for p in args
                       if p not in plugins]  # filter existing plugins
        valid_plugins = [
            p for p in new_plugins if instantiate_plugin(p) is not None
        ]

        if valid_plugins:
            print(f"Adding valid plugins {','.join(valid_plugins)}")
            plugins.extend(valid_plugins)
            parameter.set(plugins)
        else:
            print('No plugins to add')

    else:
        cea_script = cea.scripts.by_name(script_name, plugins=config.plugins)
        config.restrict_to(cea_script.parameters)
        config.apply_command_line_args(args, cea_script.parameters)
        cea_script.print_script_configuration(config, verb='Configuring')

    # save the updates to the configuration file (re-running the same tool will result in the
    # same parameters being set)
    config.save(cea.config.CEA_CONFIG)
Exemplo n.º 8
0
    def post(self, tool_name):
        """Restore the default configuration values for the CEA"""
        config = cea.config.Configuration()
        default_config = cea.config.Configuration(
            config_file=cea.config.DEFAULT_CONFIG)

        for parameter in parameters_for_script(tool_name, config):
            if parameter.name != 'scenario':
                parameter.set(default_config.sections[
                    parameter.section.name].parameters[parameter.name].get())
        config.save()
        return 'Success'
Exemplo n.º 9
0
 def delete(self, scenario):
     """Delete scenario from project"""
     config = current_app.cea_config
     scenario_path = os.path.join(config.project, scenario)
     try:
         if config.scenario_name == scenario:
             config.scenario_name = ''
             config.save()
         shutil.rmtree(scenario_path)
         return {'scenarios': list_scenario_names_for_project(config)}
     except OSError:
         abort(400, 'Make sure that the scenario you are trying to delete is not open in any application. '
                    'Try and refresh the page again.')
def route_restore_defaults(script_name):
    """Restore the default configuration values for the CEA"""
    config = current_app.cea_config
    default_config = cea.config.Configuration(
        config_file=cea.config.DEFAULT_CONFIG)

    for parameter in parameters_for_script(script_name, config):
        parameter.set(default_config.sections[
            parameter.section.name].parameters[parameter.name].get())
    config.save()

    return redirect(
        url_for('tools_blueprint.route_tool', script_name=script_name))
Exemplo n.º 11
0
def main(config=None):
    """

    :param cea.config.Configuration config: the configuration file to use (instead of creating a new one)
    :return:
    """
    t0 = datetime.datetime.now()

    if not config:
        config = cea.config.Configuration()

    from cea import suppress_3rd_party_debug_loggers
    suppress_3rd_party_debug_loggers()

    # handle arguments
    args = sys.argv[1:]  # drop the script name from the arguments
    if not len(args):
        print_help(config, args[1:])
        sys.exit(1)
    if args[0].lower() == '--help':
        print_help(config, args[1:])
        sys.exit(0)
    if args[0].lower() == '--version':
        print('City Energy Analyst version %s' % cea.__version__)
        sys.exit(0)
    script_name = args.pop(0)
    cea_script = cea.scripts.by_name(script_name, config.plugins)
    config.restrict_to(cea_script.parameters)
    config.apply_command_line_args(args, cea_script.parameters)

    # save the updates to the configuration file (re-running the same tool will result in the
    # same parameters being set)
    config.save(cea.config.CEA_CONFIG)

    cea_script.print_script_configuration(config)
    if list(cea_script.missing_input_files(config)):
        cea_script.print_missing_input_files(config)
        sys.exit(cea.MissingInputDataException.rc)

    script_module = importlib.import_module(cea_script.module)
    try:
        script_module.main(config)
        print("Execution time: %.2fs" % (datetime.datetime.now() - t0).total_seconds())
    except cea.ConfigError as config_error:
        print('ERROR: %s' % config_error)
        sys.exit(config_error.rc)
    except cea.CustomDatabaseNotFound as error:
        print('ERROR: %s' % error)
        sys.exit(error.rc)
    except:
        raise
Exemplo n.º 12
0
 def get(self):
     config = current_app.cea_config
     name = os.path.basename(config.project)
     if not os.path.exists(config.project):
         abort(400, 'Project path does not exist')
     if not os.path.exists(config.scenario):
         config.scenario_name = ''
         config.save()
     return {
         'name': name,
         'path': config.project,
         'scenario': config.scenario_name,
         'scenarios': list_scenario_names_for_project(config)
     }
Exemplo n.º 13
0
 def put(self, scenario):
     """Update scenario"""
     config = current_app.cea_config
     scenario_path = os.path.join(config.project, scenario)
     new_scenario_name = api.payload.get('name')
     try:
         if new_scenario_name is not None:
             new_path = os.path.join(config.project, new_scenario_name)
             os.rename(scenario_path, new_path)
             if config.scenario_name == scenario:
                 config.scenario_name = new_scenario_name
                 config.save()
             return {'name': new_scenario_name}
     except OSError:
         abort(400, 'Make sure that the scenario you are trying to rename is not open in any application. '
                    'Try and refresh the page again.')
Exemplo n.º 14
0
 def put(self, scenario):
     """Update scenario"""
     config = current_app.cea_config
     scenario_path = os.path.join(config.project, scenario)
     payload = api.payload
     try:
         if 'name' in payload:
             new_path = os.path.join(config.project, payload['name'])
             os.rename(scenario_path, new_path)
             if config.scenario_name == scenario:
                 config.scenario_name = payload['name']
                 config.save()
             return {'name': payload['name']}
     except WindowsError:
         abort(
             400,
             'Make sure that the scenario you are trying to rename is not open in any application. '
             'Try and refresh the page again.')
Exemplo n.º 15
0
    def post(self):
        """Create new project"""
        config = current_app.cea_config
        payload = api.payload

        if 'path' in payload:
            path = payload['path']
            name = payload['name']
            project_path = os.path.join(path, name)
            try:
                os.makedirs(project_path)
            except OSError as e:
                abort(400, str(e))

            config.project = project_path
            config.scenario_name = ''
            config.save()

            return {'message': 'Project created at {}'.format(project_path)}
Exemplo n.º 16
0
def main(config=None):
    """

    :param cea.config.Configuration config: the configuration file to use (instead of creating a new one)
    :return:
    """
    t0 = datetime.datetime.now()
    if not config:
        config = cea.config.Configuration()

    # handle arguments
    args = sys.argv[1:]  # drop the script name from the arguments
    if not len(args) or args[0].lower() == '--help':
        print_help(config, args[1:])
        sys.exit(1)
    script_name = args.pop(0)
    cea_script = cea.scripts.by_name(script_name, plugins=config.plugins)
    if not 'doc' in cea_script.interfaces:
        print('Invalid script for cea-doc')
        print_valid_script_names(config.plugins)
        sys.exit(ScriptNotFoundException.rc)
    config.restrict_to(cea_script.parameters)
    config.apply_command_line_args(args, cea_script.parameters)

    # save the updates to the configuration file (re-running the same tool will result in the
    # same parameters being set)
    config.save(cea.config.CEA_CONFIG)

    script_module = importlib.import_module(cea_script.module)
    try:
        script_module.main(config)
        print("Execution time: %.2fs" %
              (datetime.datetime.now() - t0).total_seconds())
    except cea.ConfigError as config_error:
        print('ERROR: %s' % config_error)
        sys.exit(config_error.rc)
    except cea.CustomDatabaseNotFound as error:
        print('ERROR: %s' % error)
        sys.exit(error.rc)
    except:
        raise
Exemplo n.º 17
0
def main(config=None):
    """

    :param cea.config.Configuration config: the configuration file to use (instead of creating a new one)
    :return:
    """
    if not config:
        config = cea.config.Configuration()

    # handle arguments
    args = sys.argv[1:]  # drop the script name from the arguments
    if not len(args) or args[0].lower() == '--help':
        print_help()
        sys.exit(1)
    script_name = args.pop(0)
    cea_script = cea.scripts.by_name(script_name)
    config.restrict_to(cea_script.parameters)
    config.apply_command_line_args(args, cea_script.parameters)

    # save the updates to the configuration file (re-running the same tool will result in the
    # same parameters being set)
    config.save(cea.config.CEA_CONFIG)
    cea_script.print_script_configuration(config, verb='Configuring')
Exemplo n.º 18
0
    def post(self):
        config = cea.config.Configuration()
        payload = api.payload

        if 'path' in payload:
            path = payload['path']
            if os.path.exists(path):
                config.project = path
                if 'scenario' not in payload:
                    config.scenario_name = ''
                    config.save()
                    return {'message': 'Project path changed'}
            else:
                abort(400, 'Path given does not exist')

        if 'scenario' in payload:
            scenario = payload['scenario']
            choices = config.get_parameter('general:scenario-name')._choices
            if scenario in choices:
                config.scenario_name = scenario
                config.save()
                return {'message': 'Scenario changed'}
            else:
                abort(400, 'Scenario does not exist', choices=choices)