Exemplo n.º 1
0
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)
Exemplo n.º 2
0
def partcode_match(device):
    """
    Returns True if the udev Device has a valid SR partcode in its 'serial'
    attribute.
    """
    import re

    from sr.tools.inventory import assetcode

    if 'serial' not in device.attributes:
        return False

    ALPHA = "".join(assetcode.alphabet_lut)
    PARTCODE_RE = re.compile("sr([{0}]+)$".format(ALPHA), re.IGNORECASE)

    # does it look like a partcode?
    match = PARTCODE_RE.match(device.attributes['serial'])
    if not match:
        return False
    try:
        # is the partcode valid?
        assetcode.code_to_num(match.groups()[0])
        return True
    except:
        return False
Exemplo n.º 3
0
def partcode_match(device):
    """
    Returns True if the udev Device has a valid SR partcode in its 'serial'
    attribute.
    """
    import re

    from sr.tools.inventory import assetcode

    if 'serial' not in device.attributes:
        return False

    ALPHA = "".join(assetcode.alphabet_lut)
    PARTCODE_RE = re.compile("sr([{0}]+)$".format(ALPHA), re.IGNORECASE)

    # does it look like a partcode?
    match = PARTCODE_RE.match(device.attributes['serial'])
    if not match:
        return False
    try:
        # is the partcode valid?
        assetcode.code_to_num(match.groups()[0])
        return True
    except:
        return False
Exemplo n.º 4
0
def command(args):
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    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:
        print("# item -> parent")

        if hasattr(part.parent, "code"):
            print("%s -> %s" % (part.code, part.parent.code))

        elif hasattr(part.parent, "name"):
            print("%s -> dir(%s)" % (part.code, part.parent.name))
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
def command(args):
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    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:
        print("# item -> parent")

        if hasattr(part.parent, "code"):
            print("%s -> %s" % (part.code, part.parent.code))

        elif hasattr(part.parent, "name"):
            print("%s -> dir(%s)" % (part.code, part.parent.name))
Exemplo n.º 7
0
def command(args):
    import os
    import subprocess
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    inv = get_inventory()
    cwd = os.getcwd()

    parts = []
    for c in args.assetcodes:
        code = assetcode.normalise(c)

        try:
            assetcode.code_to_num(code)
        except:
            print("Error: {} is an invalid asset code.".format(c),
                  file=sys.stderr)
            sys.exit(1)

        try:
            part = inv.root.parts[code]
        except:
            print("Error: There is no part with code {}.".format(code),
                  file=sys.stderr)
            sys.exit(1)

        if part.parent.path == cwd:
            print("Warning: Part {} is already in {}.".format(code, cwd))
            continue

        if hasattr(part.parent, "code"):
            if args.assy:
                parts.append(part.parent)
            else:
                print("Warning: Part {} is in an assembly.".format(code),
                      "To move the assembly, use the -a switch.",
                      file=sys.stderr)
                parts.append(part)

        else:
            parts.append(part)

    if parts:
        paths = [x.path for x in parts]
        subprocess.check_call(['git', 'mv'] + paths + ['.'])
    else:
        print("Warning: No parts to move", file=sys.stderr)
Exemplo n.º 8
0
    def test_both_ways(self):
        uid = 10
        pid = 20

        num = assetcode.num_to_code(uid, pid)
        self.assertEqual(assetcode.code_to_num(num), (uid, pid))
Exemplo n.º 9
0
def command(args):
    import os
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    ASSET_CODE = 0
    PART_TYPE = 1

    COLOUR_RESET = "\033[0m"
    COLOUR_GREEN = "\033[1;32m"
    COLOUR_RED = "\033[1;31m"
    COLOUR_YELLOW = "\033[1;33m"

    inv = get_inventory()

    parts = []
    spec_type = ASSET_CODE
    for c in args.itemspecs:
        code = assetcode.normalise(c)

        try:
            assetcode.code_to_num(code)
            spec_type = ASSET_CODE
        except:
            if c in inv.root.types:
                spec_type = PART_TYPE
            else:
                print("Error: %s is an invalid asset code or part type." %
                      c, file=sys.stderr)
                sys.exit(1)

        if spec_type == ASSET_CODE:
            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)
        else:
            try:
                parts.extend(inv.root.types[c])
            except:
                print("Error: There is no part type %s." % c, file=sys.stderr)
                sys.exit(1)

    stat_colour = ""
    path = ""
    for part in parts:
        if args.relpath:
            path = os.path.relpath(part.path)
        else:
            path = part.path

        if args.asset_stat and hasattr(part, "condition"):
            if part.condition == "broken":
                stat_colour = COLOUR_RED
            elif part.condition == "unknown":
                stat_colour = COLOUR_YELLOW
            elif part.condition == "working":
                stat_colour = COLOUR_GREEN
            print(path, stat_colour, part.condition, COLOUR_RESET)
        else:
            print(path)