diff --git a/cal_tools/cal_tools/tools.py b/cal_tools/cal_tools/tools.py index c48c8e26df080f38772e85e22957b8e348e5d7bf..0590c6fb4b49c94328894ce6859890a21ed232cf 100644 --- a/cal_tools/cal_tools/tools.py +++ b/cal_tools/cal_tools/tools.py @@ -1,16 +1,26 @@ from collections import OrderedDict +import datetime from glob import glob from importlib.machinery import SourceFileLoader -from os import chdir, listdir, makedirs, path, remove, stat +import json +from os import chdir, environ, listdir, makedirs, path, remove, stat, from os.path import isdir, isfile, splitext from queue import Queue +import re from shutil import copy, copytree, move, rmtree from subprocess import CalledProcessError, check_call, check_output from textwrap import dedent from time import sleep +from urllib.parse import urljoin +import ipykernel +from notebook.notebookapp import list_running_servers +import numpy as np +import requests import tabulate from jinja2 import Template + + from xfel_calibrate import settings @@ -364,7 +374,7 @@ def map_modules_from_files(filelist, file_inset, quadrants, modules_per_quad): def gain_map_files(in_folder, runs, sequences, file_inset, quadrants, - modules_per_quad): + mods_per_quad): total_sequences = 0 total_file_size = 0 gain_mapped_files = OrderedDict() @@ -387,36 +397,13 @@ def gain_map_files(in_folder, runs, sequences, file_inset, quadrants, mapped_files, mod_ids, seq, fs = map_modules_from_files(file_list, file_inset, quadrants, - modules_per_quad) + mods_per_quad) total_sequences += seq total_file_size += fs gain_mapped_files[gain] = mapped_files return gain_mapped_files, total_sequences, total_file_size / 1e9 -# from https://github.com/jupyter/notebook/issues/1000 -import json -import os.path -import re -import ipykernel -import requests - -try: # Python 3 - from urllib.parse import urljoin -except ImportError: # Python 2 - from urlparse import urljoin - -try: # Python 3 - from notebook.notebookapp import list_running_servers -except ImportError: # Python 2 - import warnings - from IPython.utils.shimmodule import ShimWarning - - with warnings.catch_warnings(): - warnings.simplefilter("ignore", category=ShimWarning) - from IPython.html.notebookapp import list_running_servers - - def get_notebook_name(): """ Return the full path of the jupyter notebook. @@ -433,26 +420,26 @@ def get_notebook_name(): relative_path = nn['notebook']['path'] return relative_path except: - return os.environ.get("CAL_NOTEBOOK_NAME", "Unknown Notebook") + return environ.get("CAL_NOTEBOOK_NAME", "Unknown Notebook") def get_dir_creation_date(directory, run): - import os - import datetime - creation_time = os.stat("{}/r{:04d}".format(directory, run)).st_ctime - creation_time = datetime.datetime.fromtimestamp(creation_time) - return creation_time - + """ + Return modification time of [directory]/[run]04 -def get_dir_creation_date(directory, run): - import os - import datetime - creation_time = os.stat("{}/r{:04d}".format(directory, run)).st_mtime + :param directory: path to directory which contains runs + :param run: run number + :return: (datetime) modification time + """ + creation_time = stat("{}/r{:04d}".format(directory, run)).st_mtime creation_time = datetime.datetime.fromtimestamp(creation_time) return creation_time def get_random_db_interface(cal_db_interface): + """ + Return interface to calibration DB with random (with given range) port + """ if "#" in cal_db_interface: prot, serv, ran = cal_db_interface.split(":") r1, r2 = ran.split("#") @@ -493,8 +480,8 @@ def get_from_db(device, constant, condition, empty_constant, while ntries > 0: this_interface = get_random_db_interface(cal_db_interface) try: - metadata.retrieve(this_interface, timeout=6000000, - meta_only=True) + metadata.retrieve(this_interface, timeout=timeout, + meta_only=meta_only) break except Exception as e: if verbosity > 0: @@ -510,7 +497,7 @@ def get_from_db(device, constant, condition, empty_constant, try: metadata.retrieve(this_interface, when=creation_time.isoformat(), - timeout=6000000, meta_only=meta_only) + timeout=timeout, meta_only=meta_only) break except Exception as e: if verbosity > 0: