Example #1
0
def results_home(request):
    from Results.csv_generator import SUMMARY_FILE_NAME
    path_ex = workspace_path(scenario_filename() +"/*.csv")
    start = workspace_path()
    context = {'supplemental_files': [os.path.relpath(file_path, start=start) for file_path in glob(path_ex)]}
    summary_path = os.path.join(scenario_filename(), SUMMARY_FILE_NAME)
    try:
        context['supplemental_files'].remove(summary_path)
    #             context['supplemental_files'] = [file for file in context['supplemental_files'] if not file.endswith(SUMMARY_FILE_NAME)] # filter out summary.csv
    except ValueError: pass
    context['summary_file_name'] = summary_path

    if os.path.exists(map_zip_file()):
        context['supplemental_files'].append(os.path.relpath(map_zip_file(), start=start))
    # TODO: value dict file sizes
    if DailyControls.objects.all().count() > 0:
        context['summary'] = Results.summary.summarize_results()
        context['iterations'] = len(list_of_iterations())
        context['population_eta'] = Unit.objects.count() / 650  # estimate slow map calc in matplotlib
        try:
            v = ResultsVersion.objects.get()
            context['version_number'] = '.'.join([v.versionMajor, v.versionMinor, v.versionRelease])
        except:  # more specific exceptions kept leaking through
            pass
    return render(request, 'Results/SimulationProgress.html', context)
Example #2
0
 def make_population_map_file(self):
     if not os.path.exists(workspace_path(scenario_filename())):
         os.makedirs(workspace_path(scenario_filename()))
     print("Calculating a new Population Map")
     fig = population_results_map()
     FigureCanvas(fig).print_png(self.path)
     thumbnail(self.path, self.thumb_path, scale=0.1923)
     print("Finished Population Map")
Example #3
0
def new_scenario(request=None, new_name=None):
    copy_blank_to_session()

    update_db_version()
    if new_name:
        try:
            scenario_filename(new_name, check_duplicates=True)
        except: pass  # validation may kick it back in which case they'll need to rename it in a file browser
    return redirect('/setup/Scenario/1/')
Example #4
0
def import_legacy_scenario(param_path, popul_path, new_name=None):
    if new_name:
        names_without_extensions = new_name
    else:
        names_without_extensions = tuple(
            os.path.splitext(os.path.basename(x))[0] for x in [param_path, popul_path])  # stupid generators...
        names_without_extensions = '%s with %s' % names_without_extensions

    import_naadsm_xml(popul_path, param_path)  # puts all the data in activeSession
    scenario_filename(names_without_extensions, check_duplicates=True)
    return save_scenario(None)  # This will overwrite a file of the same name without prompting
Example #5
0
def open_scenario(request, target, wrap_target=True):
    if wrap_target:
        target = workspace_path(target)
    print("Copying ", target, "to", db_path(), ". This could take several minutes...")
    close_old_connections()
    shutil.copy(target, db_path(name='scenario_db'))
    scenario_filename(os.path.basename(target))
    print('Sessions overwritten with ', target)
    update_db_version()
    unsaved_changes(False)  # File is now in sync
    SmSession.objects.all().update(iteration_text = '', simulation_has_started=outputs_exist())  # This is also reset from delete_all_outputs
    # else:
    #     print('File does not exist')
    return redirect('/setup/Scenario/1/')
Example #6
0
def population_thumbnail_png(request, second_try=False):
    path = workspace_path(scenario_filename() + '/population_map.png')
    thumb_path = workspace_path(scenario_filename() + '/population_thumbnail.png')
    try:
        with open(thumb_path, "rb") as f:
            return HttpResponse(f.read(), content_type="image/png")
    except IOError:
        if os.path.exists(path):
            if second_try:
               sleep(1) 
            thumbnail(path, thumb_path, scale=0.1923)  # create the thumbnail
            return population_thumbnail_png(request, second_try=True)
        else:
            sleep(5)
            return population_thumbnail_png(request, second_try=False)
Example #7
0
    def run(self):
        if self.testing:
            for database in settings.DATABASES:
                settings.DATABASES[database]['NAME'] = settings.DATABASES[database]['TEST']['NAME'] if 'TEST' in settings.DATABASES[database] else settings.DATABASES[database]['TEST_NAME']

        location = workspace_path(scenario_filename() + '/' + SUMMARY_FILE_NAME)  # Note: scenario_filename uses the database
        headers, data = self.get_summary_data_table()

        create_csv_file(location, headers, data)
Example #8
0
def copy_file(request, target, destination):
    if target.replace('.sqlite3', '') == scenario_filename():  # copying the active scenario
        return save_scenario(request)
    if not destination.endswith('.sqlite3'):
        destination += ".sqlite3"
    print("Copying", target, "to", destination, ". This could take several minutes...")
    shutil.copy(workspace_path(target), workspace_path(destination))
    print("Done copying", target)
    return redirect('/')
Example #9
0
def delete_supplemental_folder():
    scenario_folder = scenario_filename()
    if scenario_folder != '':
        try:
            shutil.rmtree(workspace_path(scenario_folder))
        except:
            pass  # because the folder doesn't exist (which is fine)

        from django.db import connections
        connections['scenario_db'].cursor().execute('VACUUM')
Example #10
0
def population_zoom_png(request=None):
    path = workspace_path(scenario_filename() + '/population_map.png')
    thumb_path = workspace_path(scenario_filename() + '/population_thumbnail.png')
    try:
        with open(path, "rb") as img_file:
            return HttpResponse(img_file.read(), content_type='image/png')
    except IOError:
        save_image = is_simulation_stopped()  # we want to check this before reading the stats, this is in motion
        if not save_image:  # in order to avoid database locked Issue #150
            return population_png(request, 58.5, 52)
        else:

            if any(thread.name == 'population_map_thread' for thread in threading.enumerate()):
                print("Waiting on a Population Map")
                sleep(5)
            else:
                PopulationWorker(path, thumb_path).start()
                sleep(.5)
            return population_zoom_png(request)
Example #11
0
def zip_map_directory_if_it_exists():
    dir_to_zip = workspace_path(scenario_filename() + "/Map")
    if os.path.exists(dir_to_zip) and supplemental_folder_has_contents(subfolder='/Map'):
        zipname = map_zip_file()
        dir_to_zip_len = len(dir_to_zip.rstrip(os.sep)) + 1
        with zipfile.ZipFile(zipname, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
            for dirname, subdirs, files in os.walk(dir_to_zip):
                for filename in files:
                    path = os.path.join(dirname, filename)
                    entry = path[dir_to_zip_len:]
                    zf.write(path, entry)
    else:
        print("Folder is empty: ", dir_to_zip)
Example #12
0
def adsm_context(request):
    context = {}
    if not request.is_ajax() and request.path and request.path != '/' and '/LoadingScreen/' not in request.path:
        session = SmSession.objects.get()
        version = session.update_available
        context = {'filename': scenario_filename(),  # context in either mode
                   'unsaved_changes': unsaved_changes(),
                   'url': request.path,
                   'active_link': '/'.join(re.split('\W+', request.path)[2:]),
                   'dev_version': __version__,
                   'update_version': version if version and version != 'False' and version != '0' else '',
                   'workspace_path': workspace_path(),
                   'db_files': (file_list(".sqlite3")),
                   'show_help_text': session.show_help_text
        }

    return context
Example #13
0
def summary_csv(request):
    class HttpResponseAccepted(HttpResponse):
        status_code = 202

    if request.method == "GET":
        if SmSession.objects.get().calculating_summary_csv:
            return HttpResponseAccepted()
        elif not DailyControls.objects.all().count() or is_simulation_running():
            return HttpResponseBadRequest()
        elif not os.path.isfile(workspace_path(scenario_filename() + '/' + SUMMARY_FILE_NAME)):
            return HttpResponseNotFound()
        else:
            return HttpResponse()
    if request.method == "POST":
        csv_generator = SummaryCSVGenerator()
        csv_generator.start()  # starts a new thread
        return HttpResponseAccepted()
    else:
        return HttpResponseNotAllowed(permitted_methods=['GET', 'POST'])
Example #14
0
def map_zip_file():
    """This is a file named after the scenario in the folder that's also named after the scenario."""
    return workspace_path(scenario_filename() + '/' + scenario_filename() + " Map Output.zip")
Example #15
0
    from django.core import management

    from ADSMSettings.utils import db_path, workspace_path, scenario_filename
    from ADSMSettings.views import new_scenario, save_scenario
    from ADSMSettings.xml2sqlite import import_naadsm_xml
    from ScenarioCreator.models import DirectSpread


    if len(sys.argv) >= 4:  # single command line invocation
        print("""Usage: python3.4 ./xml2sqlite.py export_pop.xml parameters.xml debug.sqlite3 [--workspace]
        Include the flag '--workspace' as the fourth argument if you want to save the scenario to the ADSM Workspace""")
        shutil.copy(db_path(), workspace_path('activeSession.bak'))
        shutil.copy(db_path('default'), workspace_path('settings.bak'))
        
        popul_path = sys.argv[1]
        param_path = sys.argv[2]
        scenario_path = workspace_path(sys.argv[3]) if len(sys.argv) > 4 and sys.argv[4] == '--workspace' else sys.argv[3]
        scenario_name = os.path.basename(scenario_path)
        new_scenario()
        import_naadsm_xml(popul_path, param_path, saveIterationOutputsForUnits=False)
        scenario_filename(scenario_name, check_duplicates=True)
        save_scenario()
        count_model_entries()

        shutil.move(workspace_path(scenario_name), scenario_path)
        
        shutil.copy(workspace_path('activeSession.bak'), db_path())
        shutil.copy(workspace_path('settings.bak'), db_path('default'))

Example #16
-3
def save_scenario(request=None):
    """Save to the existing session of a new file name if target is provided
    """
    if request is not None and 'filename' in request.POST:
        target = request.POST['filename']
    else:
        target = scenario_filename()
    target = strip_tags(target)
    full_path = workspace_path(target) + ('.sqlite3' if not target.endswith('.sqlite3') else '')
    try:
        if '\\' in target or '/' in target:  # this validation has to be outside of scenario_filename in order for open_test_scenario to work
            raise ValueError("Slashes are not allowed: " + target)
        scenario_filename(target)
        print('Copying database to', target)

        shutil.copy(db_path(), full_path)
        unsaved_changes(False)  # File is now in sync
        print('Done Copying database to', full_path)
    except (IOError, AssertionError, ValueError) as err:
        if request is not None:
            save_error = 'Failed to save filename:' + str(err)
            print('Encountered an error while copying file', full_path)
            return render(request, 'ScenarioName.html', {"failure_message": save_error})

    if request is not None and request.is_ajax():
        return render(request, 'ScenarioName.html', {"success_message": "File saved to " + target,
                                                     "filename": scenario_filename(),
                                                     'unsaved_changes': unsaved_changes()})
    else:
        return redirect('/setup/Scenario/1/')