diff --git a/labscript_profile/create.py b/labscript_profile/create.py index cf91b53..38a6773 100644 --- a/labscript_profile/create.py +++ b/labscript_profile/create.py @@ -5,6 +5,7 @@ from pathlib import Path from subprocess import check_output from labscript_profile import LABSCRIPT_SUITE_PROFILE, default_labconfig_path +import argparse _here = os.path.dirname(os.path.abspath(__file__)) DEFAULT_PROFILE_CONTENTS = os.path.join(_here, 'default_profile') @@ -21,7 +22,15 @@ def make_shared_secret(directory): raise RuntimeError("Could not parse output of zprocess.makesecret") -def make_labconfig_file(): +def make_labconfig_file(apparatus_name = None): + """Create labconfig file from template + + Parameters + ---------- + apparatus_name: str, optional + Overrides the default apparatus name with the provided one if not None + """ + source_path = os.path.join(LABSCRIPT_SUITE_PROFILE, 'labconfig', 'example.ini') target_path = default_labconfig_path() if os.path.exists(target_path): @@ -47,16 +56,88 @@ def make_labconfig_file(): '%(labscript_suite)s', shared_secret.relative_to(LABSCRIPT_SUITE_PROFILE) ) config.set('security', 'shared_secret', str(shared_secret_entry)) + if apparatus_name is not None: + print(f'\tSetting apparatus name to \'{apparatus_name}\'') + config.set('DEFAULT', 'apparatus_name', apparatus_name) with open(target_path, 'w') as f: config.write(f) +def compile_connection_table(): + """Compile the connection table defined in the labconfig file + + The output is placed in the location defined by the labconfig file. + """ + + try: + import runmanager + except ImportError: + # if runmanager doesn't import, skip compilation + return + + config = configparser.ConfigParser(defaults = {'labscript_suite': str(LABSCRIPT_SUITE_PROFILE)}) + config.read(default_labconfig_path()) + + # The path to the user's connection_table.py script + script_path = os.path.expandvars(config['paths']['connection_table_py']) + # path to the connection_table.h5 destination + output_h5_path = os.path.expandvars(config['paths']['connection_table_h5']) + # create output directory, if needed + Path(output_h5_path).parent.mkdir(parents=True, exist_ok=True) + # compile the h5 file + runmanager.new_globals_file(output_h5_path) + + def dummy_callback(success): + pass + + runmanager.compile_labscript_async(labscript_file = script_path, + run_file = output_h5_path, + stream_port = None, + done_callback = dummy_callback) + print(f'\tOutput written to {output_h5_path}') + +def create_profile_cli(): + """Function that defines the labscript-profile-create command + + Parses CLI arguments and calls :func:`~.create_profile`. + """ + + # capture CMD arguments + parser = argparse.ArgumentParser(prog='labscript-profile-create', + description='Initialises a default labscript profile' + ) + + parser.add_argument('-n', '--apparatus_name', + type=str, + help='Sets the apparatus_name in the labconfig file. Defaults to example_apparatus', + ) + parser.add_argument('-c', '--compile', + action='store_true', + help='Enables compilation of the default example connection table', + default=False) + + args = parser.parse_args() + + create_profile(args.apparatus_name, args.compile) + +def create_profile(apparatus_name = None, compile_table = False): + """Function that creates a labscript config profile from the default config + + Parameters + ---------- + appratus_name: str, optional + apparatus_name to define in the config. + If None, defaults to example_apparatus (set in default config file) + compile_table: bool, optional + Whether to compile to example connection table defined by the default config file + Default is False. + """ -def create_profile(): src = Path(DEFAULT_PROFILE_CONTENTS) dest = Path(LABSCRIPT_SUITE_PROFILE) + print(f'Creating labscript profile at {LABSCRIPT_SUITE_PROFILE}') # Profile directory may exist already, but we will error if it contains any of the - # files or directories we want to copy into it: + # sub-directories we want to copy into it: os.makedirs(dest, exist_ok=True) # Preferable to raise errors if anything exists before copying anything, rather than # do a partial copy before hitting an error: @@ -71,4 +152,16 @@ def create_profile(): else: shutil.copy2(src_file, dest_file) - make_labconfig_file() + print('Writing labconfig file') + make_labconfig_file(apparatus_name) + + # rename apparatus directories + if apparatus_name is not None: + print('\tRenaming apparatus directories') + for path in dest.glob('**/example_apparatus/'): + new_path = Path(str(path).replace('example_apparatus', apparatus_name)) + path.rename(new_path) + + if compile_table: + print('Compiling the example connection table') + compile_connection_table() \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 3863a26..707944c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -69,4 +69,4 @@ docs = [ ] [project.scripts] -labscript-profile-create = "labscript_profile.create:create_profile" +labscript-profile-create = "labscript_profile.create:create_profile_cli" pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy