Exemple #1
0
    def test_yaml_file_save(self):
        y = fantail.Fantail()
        y['a']['b']['c'] = 1
        y['d.e'] = '2'

        tf = tempfile.NamedTemporaryFile(delete=False)
        tf.close()
        fantail.yaml_file_save(y, tf.name)
        tf.close()

        z = fantail.yaml_file_loader(tf.name)
        self.assertEqual(y, z)
Exemple #2
0
    def load(self, madfile, sha1sum=None):
        """

        sha1sum is never used for sidecars

        """

        lg.debug("sidecar load: %s", madfile)

        if os.path.exists(madfile['madname']):
            lg.debug("loading madfile {0}".format(madfile['madname']))

            # note the mad file data is in stack[1] - 0 is transient
            madfile.mad.update(fantail.yaml_file_loader(madfile['madname']))
Exemple #3
0
    def load(self, madfile, sha1sum=None):
        """

        sha1sum is never used for sidecars

        """

        lg.debug("sidecar load: %s", madfile)

        if os.path.exists(madfile['madname']):
            lg.debug("loading madfile {0}".format(madfile['madname']))

            # note the mad file data is in stack[1] - 0 is transient
            madfile.mad.update(
                fantail.yaml_file_loader(madfile['madname']))
Exemple #4
0
def _get_recursive_dir_data(pth):

    global RECURSE_CACHE
    lg.debug("start recursive data load for {}".format(pth))

    pth = os.path.abspath(pth)

    if os.path.exists(pth) and not os.path.isdir(pth):
        here = os.path.dirname(pth)
    else:
        here = pth

    here = here.rstrip('/')
    conf = []

    # find existing directory configuration from the perspective
    # of the madfile
    last = here

    while True:

        if os.path.isdir(here):

            here_c = os.path.join(here, 'mad.config')
            if os.path.exists(here_c):
                conf.append(here_c)

        parent = os.path.dirname(here)

        if parent == '/':  # no config in the root - that would be evil!
            break
        last = here
        here = parent

    rv = fantail.Fantail()
    for c in conf[::-1]:
        lg.debug("read .config file: %s", c)
        fullname = os.path.expanduser(os.path.abspath(c))
        if fullname in RECURSE_CACHE:
            y = RECURSE_CACHE[fullname]
        else:
            #print('start load dir', fullname)
            y = fantail.yaml_file_loader(fullname)
            RECURSE_CACHE[fullname] = y
        rv.update(y)
    lg.debug(rv.pretty())
    return rv
Exemple #5
0
    def retrieve_template_file(self, template_file):
        self.data = fantail.yaml_file_loader(template_file)
        assert template_file.exists()

        for fref in 'template epilog prolog'.split():
            tpath = self.data.get(fref, '')
            if tpath.startswith('file://'):
                tpath = tpath.replace('file://', '')
                if not tpath.startswith('/'):
                    tpath = Path(template_file).dirname() / tpath

                with open(tpath) as FT:
                    self.data[fref] = FT.read()

        if self.transient:
            self._template_file = template_file
        else:
            lg.debug("Copying template_file to %s", self.template_file)
            self.save_template()
Exemple #6
0
    def retrieve_template_file(self, template_file):
        self.data = fantail.yaml_file_loader(template_file)
        assert template_file.exists()

        for fref in 'template epilog prolog'.split():
            tpath = self.data.get(fref, '')
            if tpath.startswith('file://'):
                tpath = tpath.replace('file://', '')
                if not tpath.startswith('/'):
                    tpath = Path(template_file).dirname() / tpath

                with open(tpath) as FT:
                    self.data[fref] = FT.read()

        if self.transient:
            self._template_file = template_file
        else:
            lg.debug("Copying template_file to %s", self.template_file)
            self.save_template()
Exemple #7
0
 def test_yaml_file_loader(self):
     y = fantail.yaml_file_loader(self.tf.name)
     self.assertEqual(y['a']['b1']['c2'], 'v2')
     self.assertEqual(y['b'], 'v5')
     z = fantail.dict_loader(test_dict)
     self.assertEqual(y, z)
Exemple #8
0
def madset(app, args):
    """
    Set a key/value for one or more files.

    Use this command to set a key value pair for one or more files.

    This command can take the following forms::

        mad set project test genome.fasta
        ls *.fasta | mad set project test
        find . -size +10k | mad set project test

    """

    key = args.key
    val = args.value

    if args.prompt or args.editor:
        if not args.value is None:
            # when asking for a prompt - the next item on sys.argv
            # is assumed to be a file, and needs to be pushed
            # into args.file
            args.file = [args.value] + args.file

    madfiles = []

    # gather all madfiles for later parsing
    use_stdin = not (args.prompt or args.editor)
    if args.dir:
        for m in get_filenames(args, use_stdin, allow_dirs=True):
            if not os.path.isdir(m):
                continue

            fn = os.path.join(m, 'mad.config')
            mf = fantail.Fantail()
            if os.path.exists(fn):
                mf = fantail.yaml_file_loader(fn)
            mf._mad_dir_name = m
            mf._mad_file_name = fn
            madfiles.append(mf)
    else:
        for m in get_all_mad_files(app, args, use_stdin):
            madfiles.append(m)

    # check if mad needs to show a prompt or editor
    if val is None and not (args.prompt or args.editor):
        args.prompt = True

    # show prompt or editor
    if args.prompt or args.editor:
        # get a value from the user

        default = ''
        # Show a prompt asking for a value
        data = madfiles[0]
        default = madfiles[0].get(key, "")

        if args.prompt:
            sys.stdin = open('/dev/tty')
            val = mad2.ui.askUser(key, default, data)
            sys.stdin = sys.__stdin__

        elif args.editor:
            editor = os.environ.get('EDITOR', 'vim')
            tmp_file = tempfile.NamedTemporaryFile('wb', delete=False)

            # write default value to the tmp file
            if default:
                tmp_file.write(default + "\n")
            else:
                tmp_file.write("\n")
            tmp_file.close()

            tty = open('/dev/tty')

            subprocess.call('{} {}'.format(editor, tmp_file.name),
                            stdin=tty, shell=True)
            sys.stdin = sys.__stdin__

            # read value back in
            with open(tmp_file.name, 'r') as F:
                # removing trailing space
                val = F.read().rstrip()
            # remove tmp file
            os.unlink(tmp_file.name)

    # process key & val
    key, val, list_mode = _getkeyval(app, key, val, args.force)
    lg.info('set %s to %s', key, val)
    if list_mode:
        lg.info("List modus")

    # Now process madfiles
    lg.debug("processing %d files" % len(madfiles))

    for madfile in madfiles:
        if list_mode:
            if not key in madfile:
                oldval = []
            else:
                oldval = madfile.get(key)
                if not isinstance(oldval, list):
                    oldval = [oldval]
            if args.dir:
                madfile[key] = oldval + [val]
            else:
                madfile.mad[key] = oldval + [val]
        else:
            #not listmode
            if args.dir:
                madfile[key] = val
            else:
                madfile.mad[key] = val

        if args.echo:
            if args.dir:
                print((madfile._mad_dir_name))
            else:
                print((madfile['filename']))

        if args.dir:
            fantail.yaml_file_save(madfile, madfile._mad_file_name)
        else:
            madfile.save()
Exemple #9
0
def madset(app, args):
    """
    Set a key/value for one or more files.

    Use this command to set a key value pair for one or more files.

    This command can take the following forms::

        mad set project test genome.fasta
        ls *.fasta | mad set project test
        find . -size +10k | mad set project test

    """

    key = args.key
    val = args.value

    if args.prompt or args.editor:
        if not args.value is None:
            # when asking for a prompt - the next item on sys.argv
            # is assumed to be a file, and needs to be pushed
            # into args.file
            args.file = [args.value] + args.file

    madfiles = []

    # gather all madfiles for later parsing
    use_stdin = not (args.prompt or args.editor)
    if args.dir:
        for m in get_filenames(args, use_stdin, allow_dirs=True):
            if not os.path.isdir(m):
                continue

            fn = os.path.join(m, 'mad.config')
            mf = fantail.Fantail()
            if os.path.exists(fn):
                mf = fantail.yaml_file_loader(fn)
            mf._mad_dir_name = m
            mf._mad_file_name = fn
            madfiles.append(mf)
    else:
        for m in get_all_mad_files(app, args, use_stdin):
            madfiles.append(m)

    # check if mad needs to show a prompt or editor
    if val is None and not (args.prompt or args.editor):
        args.prompt = True

    # show prompt or editor
    if args.prompt or args.editor:
        # get a value from the user

        default = ''
        # Show a prompt asking for a value
        data = madfiles[0]
        default = madfiles[0].get(key, "")

        if args.prompt:
            sys.stdin = open('/dev/tty')
            val = mad2.ui.askUser(key, default, data)
            sys.stdin = sys.__stdin__

        elif args.editor:
            editor = os.environ.get('EDITOR', 'vim')
            tmp_file = tempfile.NamedTemporaryFile('wb', delete=False)

            # write default value to the tmp file
            if default:
                tmp_file.write(default + "\n")
            else:
                tmp_file.write("\n")
            tmp_file.close()

            tty = open('/dev/tty')

            subprocess.call('{} {}'.format(editor, tmp_file.name),
                            stdin=tty,
                            shell=True)
            sys.stdin = sys.__stdin__

            # read value back in
            with open(tmp_file.name, 'r') as F:
                # removing trailing space
                val = F.read().rstrip()
            # remove tmp file
            os.unlink(tmp_file.name)

    # process key & val
    key, val, list_mode = _getkeyval(app, key, val, args.force)
    lg.info('set %s to %s', key, val)
    if list_mode:
        lg.info("List modus")

    # Now process madfiles
    lg.debug("processing %d files" % len(madfiles))

    for madfile in madfiles:
        if list_mode:
            if not key in madfile:
                oldval = []
            else:
                oldval = madfile.get(key)
                if not isinstance(oldval, list):
                    oldval = [oldval]
            if args.dir:
                madfile[key] = oldval + [val]
            else:
                madfile.mad[key] = oldval + [val]
        else:
            #not listmode
            if args.dir:
                madfile[key] = val
            else:
                madfile.mad[key] = val

        if args.echo:
            if args.dir:
                print((madfile._mad_dir_name))
            else:
                print((madfile['filename']))

        if args.dir:
            fantail.yaml_file_save(madfile, madfile._mad_file_name)
        else:
            madfile.save()
Exemple #10
0
 def load_template(self):
     """
     Load the local template.
     """
     self.data = fantail.yaml_file_loader(self.template_file)
Exemple #11
0
def _get_recursive_dir_data(pth):

    global RECURSE_CACHE
    lg.debug("start recursive data load for {}".format(pth))

    pth = os.path.abspath(pth)

    if os.path.exists(pth) and not os.path.isdir(pth):
        here = os.path.dirname(pth)
    else:
        here = pth

    here = here.rstrip('/')
    conf = []

    # find existing directory configuration from the perspective
    # of the madfile
    last = here

    while True:

        if os.path.isdir(here):

            here_c = os.path.join(here, 'mad.config')
            if os.path.exists(here_c):
                conf.append(here_c)

        parent = os.path.dirname(here)

        if parent == '/':  # no config in the root - that would be evil!
            break
        last = here
        here = parent

    # now again from the current directory
    here = os.getcwd().rstrip('/')
    last = here
    cwdconf = []
    while True:
        assert(os.path.isdir(here))

        here_c = os.path.join(here, 'mad.config')
        if os.path.exists(here_c):
            if here_c in conf:
                break  # overlap with tree from the madfile's location
            else:
                cwdconf.append(here_c)
        parent = os.path.dirname(here)
        if parent == '/':  # no config in the root - that would be evil!
            break
        last = here
        here = parent

    conf = cwdconf + conf
    # load (or get from cache)

    rv = fantail.Fantail()
    for c in conf[::-1]:
        fullname = os.path.expanduser(os.path.abspath(c))
        if fullname in RECURSE_CACHE:
            y = RECURSE_CACHE[fullname]
        else:
            #print('start load dir', fullname)
            y = fantail.yaml_file_loader(fullname)
            RECURSE_CACHE[fullname] = y
        rv.update(y)
    return rv
Exemple #12
0
 def load_template(self):
     """
     Load the local template.
     """
     self.data = fantail.yaml_file_loader(self.template_file)
Exemple #13
0
def get_local_config_file(name):
    fn = get_local_config_filename(name)
    if os.path.exists(fn):
        return fantail.yaml_file_loader(fn)
    else:
        return fantail.Fantail()