From a706494a5467e149ae8b1d9bff6f306e77134f09 Mon Sep 17 00:00:00 2001 From: David Meyer Date: Wed, 21 Aug 2024 13:12:07 -0400 Subject: [PATCH 1/7] Configure `labscript-profile-create` to compile the default connection table. This will allow BLACS to run immediately using dummy devices, without manual compilation required from the user. Co-authored-by: NMFolks <70551431+NMFolks@users.noreply.github.com> --- labscript_profile/create.py | 59 +++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/labscript_profile/create.py b/labscript_profile/create.py index cf91b53..bc8deca 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,8 @@ def make_shared_secret(directory): raise RuntimeError("Could not parse output of zprocess.makesecret") -def make_labconfig_file(): +def make_labconfig_file(apparatus_name): + source_path = os.path.join(LABSCRIPT_SUITE_PROFILE, 'labconfig', 'example.ini') target_path = default_labconfig_path() if os.path.exists(target_path): @@ -47,16 +49,58 @@ 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: + config.set('DEFAULT', 'apparatus_name', apparatus_name) with open(target_path, 'w') as f: config.write(f) +def compile_connection_table(): + + 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 = config['paths']['connection_table_py'] + # path to the connection_table.h5 destination + output_h5_path = 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) def 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', + ) + + args = parser.parse_args() + src = Path(DEFAULT_PROFILE_CONTENTS) dest = Path(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 +115,13 @@ def create_profile(): else: shutil.copy2(src_file, dest_file) - make_labconfig_file() + make_labconfig_file(args.apparatus_name) + + # rename apparatus directories + if args.apparatus_name is not None: + for path in dest.glob('**/example_apparatus/'): + new_path = Path(str(path).replace('example_apparatus', args.apparatus_name)) + path.rename(new_path) + + # compile the initial example connection table + compile_connection_table() \ No newline at end of file From 2d9f23d911358cd457428222d5f9b99f95797154 Mon Sep 17 00:00:00 2001 From: David Meyer Date: Wed, 21 Aug 2024 13:24:45 -0400 Subject: [PATCH 2/7] Make connection table compilation an optional flag. Default is to not compile, maintaining older behavior. --- labscript_profile/create.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/labscript_profile/create.py b/labscript_profile/create.py index bc8deca..020deb2 100644 --- a/labscript_profile/create.py +++ b/labscript_profile/create.py @@ -91,9 +91,13 @@ def create_profile(): ) parser.add_argument('-n', '--apparatus_name', - type = str, - help = 'Sets the apparatus_name in the labconfig file. Defaults to example_apparatus', + type=str, + help='Sets the apparatus_name in the labconfig file. Defaults to example_apparatus', ) + parser.add_argument('-c', '--compile', + type=bool, + help='Enables compilation of the default example connection table', + default=False) args = parser.parse_args() @@ -123,5 +127,6 @@ def create_profile(): new_path = Path(str(path).replace('example_apparatus', args.apparatus_name)) path.rename(new_path) - # compile the initial example connection table - compile_connection_table() \ No newline at end of file + if args.compile: + # compile the initial example connection table + compile_connection_table() \ No newline at end of file From 00a21984b6e7dcfd664e7a17c4d6599a9d4c79cf Mon Sep 17 00:00:00 2001 From: David Meyer Date: Thu, 3 Oct 2024 15:03:59 -0400 Subject: [PATCH 3/7] Make compile flag a toggle. --- labscript_profile/create.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/labscript_profile/create.py b/labscript_profile/create.py index 020deb2..4c9b6de 100644 --- a/labscript_profile/create.py +++ b/labscript_profile/create.py @@ -95,7 +95,7 @@ def create_profile(): help='Sets the apparatus_name in the labconfig file. Defaults to example_apparatus', ) parser.add_argument('-c', '--compile', - type=bool, + action='store_true', help='Enables compilation of the default example connection table', default=False) From c9eac49ae37550e2754ba1e70572b5c6c897197a Mon Sep 17 00:00:00 2001 From: David Meyer Date: Thu, 3 Oct 2024 15:05:13 -0400 Subject: [PATCH 4/7] Add some feedback to `labscript-profile-create` to track progress and output directories. --- labscript_profile/create.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/labscript_profile/create.py b/labscript_profile/create.py index 4c9b6de..4f2a0c5 100644 --- a/labscript_profile/create.py +++ b/labscript_profile/create.py @@ -50,6 +50,7 @@ def make_labconfig_file(apparatus_name): ) 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: @@ -82,6 +83,7 @@ def dummy_callback(success): run_file = output_h5_path, stream_port = None, done_callback = dummy_callback) + print(f'\tOutput written to {output_h5_path}') def create_profile(): @@ -103,6 +105,7 @@ 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 # sub-directories we want to copy into it: os.makedirs(dest, exist_ok=True) @@ -119,14 +122,16 @@ def create_profile(): else: shutil.copy2(src_file, dest_file) + print('Writing labconfig file') make_labconfig_file(args.apparatus_name) - + # rename apparatus directories if args.apparatus_name is not None: + print('\tRenaming apparatus directories') for path in dest.glob('**/example_apparatus/'): new_path = Path(str(path).replace('example_apparatus', args.apparatus_name)) path.rename(new_path) if args.compile: - # compile the initial example connection table + print('Compiling the example connection table') compile_connection_table() \ No newline at end of file From a9927c87c543eaaa986777830ae7bda1e1a19b28 Mon Sep 17 00:00:00 2001 From: David Meyer Date: Tue, 14 Jan 2025 15:53:43 -0500 Subject: [PATCH 5/7] Ensure $HOME gets expanded correctly during compilation on unix Co-authored-by: NMFolks <70551431+NMFolks@users.noreply.github.com> --- labscript_profile/create.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/labscript_profile/create.py b/labscript_profile/create.py index 4f2a0c5..b526071 100644 --- a/labscript_profile/create.py +++ b/labscript_profile/create.py @@ -68,9 +68,9 @@ def compile_connection_table(): config.read(default_labconfig_path()) # The path to the user's connection_table.py script - script_path = config['paths']['connection_table_py'] + script_path = os.path.expandvars(config['paths']['connection_table_py']) # path to the connection_table.h5 destination - output_h5_path = config['paths']['connection_table_h5'] + 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 From be08cee368610c04a68e2466e130b3cfefd3eeb6 Mon Sep 17 00:00:00 2001 From: David Meyer Date: Wed, 15 Jan 2025 10:00:31 -0500 Subject: [PATCH 6/7] Add basic docstrings to labscript-profile submodule --- labscript_profile/create.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/labscript_profile/create.py b/labscript_profile/create.py index b526071..9ccf4e1 100644 --- a/labscript_profile/create.py +++ b/labscript_profile/create.py @@ -22,7 +22,14 @@ def make_shared_secret(directory): raise RuntimeError("Could not parse output of zprocess.makesecret") -def make_labconfig_file(apparatus_name): +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() @@ -57,6 +64,10 @@ def make_labconfig_file(apparatus_name): 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 @@ -86,6 +97,8 @@ def dummy_callback(success): print(f'\tOutput written to {output_h5_path}') def create_profile(): + """Function that defines the labscript-profile-create command + """ # capture CMD arguments parser = argparse.ArgumentParser(prog='labscript-profile-create', From c9a5e1366156b2fa96dcb00a30dca75b4e65058c Mon Sep 17 00:00:00 2001 From: David Meyer Date: Fri, 17 Jan 2025 09:58:19 -0500 Subject: [PATCH 7/7] Separate labscript-profile-create into a separate CLI interface and the actual creation function `create_profile`, which is automatically called when labscript_utils is imported and the profile does not already exist. Separation ensures that argparse doesn't consume erroneous CLI arguments just because a command incidentally imports labscript_utils (like on RTD). --- labscript_profile/create.py | 27 ++++++++++++++++++++++----- pyproject.toml | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/labscript_profile/create.py b/labscript_profile/create.py index 9ccf4e1..38a6773 100644 --- a/labscript_profile/create.py +++ b/labscript_profile/create.py @@ -96,8 +96,10 @@ def dummy_callback(success): done_callback = dummy_callback) print(f'\tOutput written to {output_h5_path}') -def create_profile(): +def create_profile_cli(): """Function that defines the labscript-profile-create command + + Parses CLI arguments and calls :func:`~.create_profile`. """ # capture CMD arguments @@ -116,6 +118,21 @@ def create_profile(): 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. + """ + src = Path(DEFAULT_PROFILE_CONTENTS) dest = Path(LABSCRIPT_SUITE_PROFILE) print(f'Creating labscript profile at {LABSCRIPT_SUITE_PROFILE}') @@ -136,15 +153,15 @@ def create_profile(): shutil.copy2(src_file, dest_file) print('Writing labconfig file') - make_labconfig_file(args.apparatus_name) + make_labconfig_file(apparatus_name) # rename apparatus directories - if args.apparatus_name is not None: + 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', args.apparatus_name)) + new_path = Path(str(path).replace('example_apparatus', apparatus_name)) path.rename(new_path) - if args.compile: + 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