def command(args): import os import sys from sr.tools.environment import open_editor from sr.tools.inventory.inventory import find_top_level_dir # Check we're being run in the inventory repo gitdir = find_top_level_dir() if not gitdir: print("This command must be run in the inventory git repository.") sys.exit(2) for assetfn in args.assetfnames: assetname = assetfn[:assetfn.find("-sr")] assetcd = assetfn[assetfn.find("-sr") + 3:] # Check that a template for the asset exists templatefn = os.path.join(gitdir, ".meta", "parts", assetname) if not os.path.isfile(templatefn): print("A template for the asset \"%s\" could not be found. " "Skipping." % assetname) continue templatefile = open(templatefn) assetfile = open(assetfn, "w") for line in templatefile: assetfile.write(line.replace("[ASSET_CODE]", assetcd)) templatefile.close() assetfile.close() if args.start_editor: open_editor(assetfn)
def command(args): import sys from sr.tools.inventory import assetcode from sr.tools.inventory.inventory import get_inventory from sr.tools.environment import open_editor inv = get_inventory() parts = [] for c in args.part_code: code = assetcode.normalise(c) try: assetcode.code_to_num(code) except: print("Error: %s is an invalid code." % code, file=sys.stderr) sys.exit(1) try: part = inv.root.parts[code] except KeyError: print("Error: There is no part with code %s." % code, file=sys.stderr) sys.exit(1) parts.append(part) for part in parts: open_editor(part.path)
def command(args): import argparse import os import yaml from sr.tools.environment import open_editor from sr.tools.inventory.inventory import get_inventory from sr.tools.cli import inv_new_asset dirname = args.dirname inventory = get_inventory() gitdir = inventory.root_path # Find out if there's a template for the 'info' file templatefn = os.path.join(gitdir, ".meta", "assemblies", dirname) if not os.path.isfile(templatefn): templatefn = os.path.join(gitdir, ".meta", "assemblies", "default") userno = inventory.current_user_number assetcd = inventory.get_next_asset_code(userno) groupname = "%s-sr%s" % (dirname, assetcd) if os.path.isdir(dirname): os.rename(dirname, groupname) else: os.mkdir(groupname) print("Created new assembly with name \"%s\"" % groupname) # Copy the group template to the group dir # Insert the asset code into the file while we're at it templatefile = open(templatefn) assetfile = open(os.path.join(groupname, "info"), "w") for line in templatefile: assetfile.write(line.replace("[ASSET_CODE]", assetcd)) templatefile.close() assetfile.close() if args.start_editor: open_editor(os.path.join(groupname, "info")) if args.create_all: assy_data = yaml.load(open(templatefn)) if "elements" in assy_data: os.chdir(groupname) for element in assy_data["elements"]: other_args = argparse.Namespace() other_args.assetname = element other_args.start_editor = args.start_editor inv_new_asset.command(other_args)
def command(args): import os from sr.tools.environment import open_editor from sr.tools.inventory.inventory import get_inventory assetname = args.assetname inventory = get_inventory() gitdir = inventory.root_path # Check that a template for the new asset exists templatefn = os.path.join(gitdir, ".meta", "parts", assetname) if not os.path.isfile(templatefn): print('A template for the asset "{}" could not be found. ' 'The default template will be used.'.format(assetname)) templatefn = os.path.join(gitdir, ".meta", "parts", "default") # Get the git name/email of the user userno = inventory.current_user_number assetcd = inventory.get_next_asset_code(userno) assetfn = "%s-sr%s" % (assetname, assetcd) print('Created new asset with name "{0}-\033[1msr{1}\033[0m"' .format(assetname, assetcd)) # Copy the template to the actual asset file # Insert the asset code into the file while we're at it templatefile = open(templatefn) assetfile = open(assetfn, "w") for line in templatefile: assetfile.write(line.replace("[ASSET_CODE]", assetcd)) templatefile.close() assetfile.close() if args.start_editor: open_editor(assetfn)