コード例 #1
0
ファイル: processMEG.py プロジェクト: leadingin/childrenmeg
 def run_catch_fail(self, function, *args, autotries=2, failedon=None):
     """
     This function will run the function provided, and catch unexpected error codes. It will automatically try the
     function again for the number of provided autotries, then wait for the user to respond.
     """
     while True:
         try:
             stdout = function(*args)
             if self.verbose:
                 print(stdout)
                 break
         except ProcessExecutionError as e:
             if autotries != 0:
                 autotries -= 1
                 time.sleep(60)
                 continue
             if failedon is not None:
                 print('Failed on: ' + failedon)
             print('STDOUT: ', e.stdout)
             with colors.red:
                 print('STDERR: ', e.stderr)
                 print('ERRNO: ', e.errno)
             if terminal.ask('Try again'):
                 continue
             if terminal.ask('Skip?'):
                 break
             print('Exiting...')
             exit(-1)
コード例 #2
0
 def test_ask(self):
     with captured_stdout("\n") as stream:
         self.assertTrue(ask("Do you like cats?", default = True))
     self.assertEqual(stream.getvalue(), "Do you like cats? [Y/n] ")
     with captured_stdout("\nyes") as stream:
         self.assertTrue(ask("Do you like cats?"))
     self.assertEqual(stream.getvalue(), "Do you like cats? (y/n) Invalid response, please try again\nDo you like cats? (y/n) ")
コード例 #3
0
ファイル: test_terminal.py プロジェクト: AndydeCleyre/plumbum
    def test_ask(self, capsys):
        with send_stdin("\n"):
            assert ask("Do you like cats?", default = True)
        assert capsys.readouterr()[0] == "Do you like cats? [Y/n] "

        with send_stdin("\nyes"):
            assert ask("Do you like cats?")
        assert capsys.readouterr()[0] == "Do you like cats? (y/n) Invalid response, please try again\nDo you like cats? (y/n) "
コード例 #4
0
ファイル: test_terminal.py プロジェクト: zhuweijava/plumbum
    def test_ask(self, capsys):
        with send_stdin("\n"):
            assert ask("Do you like cats?", default=True)
        assert capsys.readouterr()[0] == "Do you like cats? [Y/n] "

        with send_stdin("\nyes"):
            assert ask("Do you like cats?")
        assert capsys.readouterr(
        )[0] == "Do you like cats? (y/n) Invalid response, please try again\nDo you like cats? (y/n) "
コード例 #5
0
ファイル: create_infiles.py プロジェクト: ManiOrgrim/Rikitake
def ask_for_data():
    """This function asks the user the values of parameters, number of integration steps,
    initial conditions and simulation_ID. Then creates a '.temp_for_create_infiles.txt' file to store them.
    This file will be used by other functions to read the data"""
    while (True):
        in_data = terminal.prompt(
            "Type the data in the form 'mu k N_steps x1_0 x2_0 y1_0 simulation_identifier"
        )
        split_values = in_data.split()
        if (len(split_values) != 7):
            print(colors.yellow | "You did not give 7 values. Try again.")
            continue
        try:
            mu = float(in_data.split()[0])
            k = float(in_data.split()[1])
            N_steps = int(in_data.split()[2])
            if (N_steps < 20000):
                print(
                    colors.yellow |
                    "The number of integration steps may be too small. Convergence of Lyapunov exponents is not granted."
                )
            x1_0 = float(in_data.split()[3])
            x2_0 = float(in_data.split()[4])
            y1_0 = float(in_data.split()[5])
            simulation_ID = in_data.split()[6]
        except ValueError:
            print(
                colors.yellow |
                "Some of the parameter values you gave are not numbers. Please try again."
            )
            continue
        if (0 < mu < 10**2 and 1e-2 < k < 1e+2):
            pass
        else:
            go_on1 = terminal.ask(
                colors.yellow |
                """The values of mu or k you choose may cause overflow issues.
                        It is recommended that 0< mu< 10^2 and 10^-2< k < 10^2.
                        Continue anyway?""")
            if (go_on1):
                break
            else:
                continue
        if ('.' not in simulation_ID):
            break
        else:
            go_on2 = terminal.ask(
                colors.yellow |
                """The presence of '.' character in simulation ID may cause issues when saving images. Continue anyway?"""
            )
            if (go_on2):
                break
    outfile = open(".temp_for_create_infiles.txt", 'w+')
    outfile.write(in_data)
    outfile.close()
コード例 #6
0
ファイル: __main__.py プロジェクト: ManiOrgrim/Rikitake
    def there_is_infile(self):
        """Asks the user if she wants to replace currently existing 'input_values.txt' file."""

        answer = terminal.ask(
            "Do you want to create a new 'input_values.txt' file?")
        if (answer):
            answer2 = terminal.ask(
                colors.yellow |
                "This process will overwrite DEFINITEVELY 'input_values.txt'. Do you still want to proceed?"
            )
            if (answer2):
                self.create_infile()
コード例 #7
0
ファイル: init.py プロジェクト: katiakweb/docky
 def _generate_dev_docker_compose_file(self):
     for service in TEMPLATE_SERVICE:
         generate = ask("Do you want to generate docker-compose.yml "
                        "automatically for %s" % service,
                        default=True)
         if generate:
             return GenerateComposeFile(service).generate()
コード例 #8
0
ファイル: main.py プロジェクト: cjolowicz/copier
    def _solve_render_conflict(self, dst_relpath: Path):
        """Properly solve render conflicts.

        It can ask the user if running in interactive mode.
        """
        assert not dst_relpath.is_absolute()
        printf(
            "conflict",
            dst_relpath,
            style=Style.DANGER,
            quiet=self.quiet,
            file_=sys.stderr,
        )
        if self.match_skip(dst_relpath):
            printf(
                "skip",
                dst_relpath,
                style=Style.OK,
                quiet=self.quiet,
                file_=sys.stderr,
            )
            return False
        if self.overwrite or dst_relpath == self.answers_relpath:
            printf(
                "overwrite",
                dst_relpath,
                style=Style.WARNING,
                quiet=self.quiet,
                file_=sys.stderr,
            )
            return True
        return bool(ask(f" Overwrite {dst_relpath}?", default=True))
コード例 #9
0
ファイル: main.py プロジェクト: PierrickBrun/voodoo
 def _init_env(self, *args, **kwargs):
     self.env = self.parent.force_env or self.parent.env
     config_path = '.'.join([self.env, DOCKER_COMPOSE_PATH])
     if self.env == 'dev':
         self.config_path = config_path
     elif local.path(config_path).is_file():
         self.config_path = config_path
     elif local.path(DOCKER_COMPOSE_PATH).is_file():
         self.config_path = DOCKER_COMPOSE_PATH
     else:
         raise_error(
             "There is not %s.%s or %s file, please add one"
             % (self.env, DOCKER_COMPOSE_PATH, DOCKER_COMPOSE_PATH))
     self.main_service = self._get_main_service()
     if self.env == 'dev':
         if not local.path(self.config_path).isfile():
             generate = ask(
                 "There is not dev.docker-compose.yml file.\n"
                 "Do you want to generate one automatically",
                 default=True)
             if generate:
                 self.run_hook(GenerateDevComposeFile)
             else:
                 raise_error("No dev.docker-compose.yml file, abort!")
     self.compose = compose['-f', self.config_path]
コード例 #10
0
ファイル: rawr.py プロジェクト: AndydeCleyre/rawr
    def main(self, *search_terms):
        try:
            results = search(search_terms, adult=self.adult)
        except KeyboardInterrupt:
            results = None
        if not results:
            raise NoResults

        with suppress(CommandNotFound):
            print(
                f"{local['df']('-h', '-P', '.').splitlines()[-1].split()[3]} available"
                | yellow)
        uri = choose_result(results)
        if not uri:
            return
        try:
            clip(uri)
        except CommandNotFound:
            print(uri | blue)
        else:
            print("Magnet URI copied to clipboard" | green)

        show_connection()
        try:
            aria2c = local['aria2c']
        except CommandNotFound:
            print(
                "If I'd found the 'aria2c' command, I'd have offered to launch it for you."
                | yellow)
        else:
            if ask("Begin download with aria2" | magenta, True):
                # try:
                aria2c['--seed-time=0', uri] & FG
コード例 #11
0
def overwrite_file(conf: ConfigData, dst_path: Path, rel_path: Path) -> bool:
    printf("conflict", rel_path, style=Style.DANGER, quiet=conf.quiet)
    if conf.force:
        return True
    if conf.skip:
        return False
    return bool(ask(f" Overwrite {dst_path}?", default=True))
コード例 #12
0
ファイル: main.py プロジェクト: Clear-ICT/voodoo
 def __init__(self, *args, **kwargs):
     super(VoodooSub, self).__init__(*args, **kwargs)
     if args and args[0] == 'voodoo new':
         return
     config_path = '.'.join([self.parent.env, DOCKER_COMPOSE_PATH])
     if self.parent.env == 'dev':
         self.config_path = config_path
     elif local.path(config_path).is_file():
         self.config_path = config_path
     elif local.path(DOCKER_COMPOSE_PATH).is_file():
         self.config_path = DOCKER_COMPOSE_PATH
     else:
         raise_error(
             "There is not %s.%s or %s file, please add one"
             % (self.parent.env, DOCKER_COMPOSE_PATH, DOCKER_COMPOSE_PATH))
     self.main_service = self._get_main_service()
     if self.parent.env == 'dev':
         if not local.path(self.config_path).isfile():
             generate = ask(
                 "There is not dev.docker-compose.yml file.\n"
                 "Do you want to generate one automatically",
                 default=True)
             if generate:
                 self.run_hook(GenerateDevComposeFile)
             else:
                 raise_error("No dev.docker-compose.yml file, abort!")
     self.compose = compose['-f', self.config_path]
コード例 #13
0
 def _generate_dev_docker_compose_file(self):
     print("There is not dev.docker-compose.yml file.\n")
     for service in TEMPLATE_SERVICE:
         generate = ask(
             "Do you want to generate one automatically for %s" % service,
             default=True)
         if generate:
             return GenerateComposeFile(service).generate()
     raise_error("No dev.docker-compose.yml file, abort!")
コード例 #14
0
ファイル: project.py プロジェクト: dwtcourses/docky
 def _generate_dev_docker_compose_file(self):
     generate = ask(
         "There is not dev.docker-compose.yml file.\n"
         "Do you want to generate one automatically",
         default=True)
     if generate:
         GenerateComposeFile(self.service).generate()
     else:
         raise_error("No dev.docker-compose.yml file, abort!")
コード例 #15
0
ファイル: user_data.py プロジェクト: yelizariev/copier
def query_user_data(
    questions_data: AnyByStrDict,
    answers_data: AnyByStrDict,
    ask_user: bool,
    envops: EnvOps,
) -> AnyByStrDict:
    """Query the user for questions given in the config file."""
    type_maps: Dict[str, Callable] = {
        "bool": bool,
        "float": float,
        "int": int,
        "json": json.loads,
        "str": str,
        "yaml": parse_yaml_string,
    }
    env = get_jinja_env(envops=envops)
    result = DEFAULT_DATA.copy()

    _render_value = partial(render_value, env=env, context=result)
    _render_choices = partial(render_choices, env=env, context=result)

    for question, details in questions_data.items():
        # Get default answer
        answer = default = answers_data.get(
            question, _render_value(details.get("default")))
        # Get question type; by default let YAML decide it
        type_name = _render_value(details.get("type", "yaml"))
        try:
            type_fn = type_maps[type_name]
        except KeyError:
            raise InvalidTypeError()
        if ask_user:
            # Generate message to ask the user
            emoji = "🕵️" if details.get("secret", False) else "🎤"
            message = f"\n{bold | question}? Format: {type_name}\n{emoji} "
            if details.get("help"):
                message = (
                    f"\n{info & italics | _render_value(details['help'])}{message}"
                )
            # Use the right method to ask
            if type_fn is bool:
                answer = ask(message, default)
            elif details.get("choices"):
                choices = _render_choices(details["choices"])
                answer = choose(message, choices, default)
            else:
                answer = prompt(message, type_fn, default)
        result[question] = cast_answer_type(answer, type_fn)

    for key in DEFAULT_DATA:
        del result[key]
    return result
コード例 #16
0
 def _ask_optional_service(self):
     """Container can be set as optional by adding the key
     "optional" and "description". This method will ask the user to
     use or not this optional container"""
     for name, config in self.config['services'].items():
         if config.get('optional'):
             install = ask("%s. Do you want to install it" %
                           config['description'],
                           default=False)
             if install:
                 # remove useless dockercompose key
                 del self.config['services'][name]['description']
                 del self.config['services'][name]['optional']
                 self.config['services']['odoo']['links'].append(name)
             else:
                 del self.config['services'][name]
コード例 #17
0
ファイル: main.py プロジェクト: nat-n/copier
def overwrite_file(conf: ConfigData, dst_path: Path, rel_path: Path) -> bool:
    """Handle the case when there's an update conflict.

    Arguments:
        conf: Configuration obtained with [`make_config`][copier.config.factory.make_config].
        dst_path: The destination file.
        rel_path: The new file.

    Returns:
        True if the overwrite was forced or the user answered yes,
        False if skipped by configuration or if the user answered no.
    """
    printf("conflict", rel_path, style=Style.DANGER, quiet=conf.quiet)
    if conf.force:
        return True
    if conf.skip:
        return False
    return bool(ask(f" Overwrite {dst_path}?", default=True))
コード例 #18
0
 def _ask_optional_service(self):
     """Container can be set as optional by adding the key
     "optional" and "description". This method will ask the user to
     use or not this optional container"""
     answer = {}
     for name, config in self.config['services'].copy().items():
         if config.get('optional'):
             option = config['optional']
             if not option in answer:
                 answer[option] = ask("%s. Do you want to install it" %
                                      option,
                                      default=False)
             if answer[option]:
                 # remove useless docker compose key
                 del self.config['services'][name]['optional']
                 if not 'links' in self.config['services'][self.service]:
                     self.config['services'][self.service]['links'] = []
                 self.config['services'][self.service]['links'].append(name)
             else:
                 del self.config['services'][name]
コード例 #19
0
ファイル: __main__.py プロジェクト: ManiOrgrim/Rikitake
    def main(self):
        """This function leads the exectution of the program."""
        if 'input_values.txt' in os.listdir(self.save_dir):
            self.there_is_infile()
        else:
            self.there_is_not_infile()
        if not self.dont_perform_integration:
            self.integration_completed = integrator.generate_data(
                self.save_dir, self.dt)
        if not self.dont_calculate_lyapunov:
            self.lyap_exp_calculated = lyapunov.lyapunov(self.save_dir)
        if not self.dont_generate_images:
            self.images_created = image_creator.image_creator(self.save_dir)
        if (self.integration_completed and self.images_created
                and self.verbose):
            print(colors.green | "Process completed.")
        if (self.verbose):
            print(colors.green | "These processes have been performed:")
            print(colors.green | "INTEGRATION:", self.integration_completed)
            print(colors.green | "LYAPUNOV EPONENT:", self.lyapunov)

            print(colors.green | "IMAGE GENERATION:", self.images_created)
        if (self.alert):
            """Alert script. If you wish to modify the alert act here.
            Change duration and/or frequency as you wish
            Or change directly the command os.system receives a sinput."""
            duration = 1  # seconds
            freq = 441  # Hz
            os.system('play -nq -t alsa synth {} sine {}'.format(
                duration, freq))
        if (terminal.ask("Run another simulation?")):
            print(
                "Please rename/save elsewhere the present 'input_values.txt' if you want to change it for this simulation."
            )
            self.main()
        else:
            sys.exit([0])
コード例 #20
0
ファイル: stream.py プロジェクト: bfg/dotfiles
#!/usr/bin/env python3

import sys

from requests import get
from plumbum import local, ProcessExecutionError
from plumbum.cli.terminal import ask
from plumbum.cmd import peerflix, xclip, mpv

vpn = local['~/Code/vpn.py']

uris = sys.argv[1:] or xclip('-sel', 'clip', '-o').splitlines()
for uri in uris:
    if uri.startswith('magnet:?xt=urn:btih:') or uri.endswith('.torrent'):
        r = get('https://api.duckduckgo.com', {'q': 'ip', 'format': 'json'})
        location = r.json()['Answer'].split('>')[1].split('<')[0]
        if ask(f"Your IP address indicates you're in {location}. Connect to VPN?", True):
            vpn['up', 'us'].run_fg()
        try:
            peerflix['-kdf', local.path('~/Downloads/peerflix'), uri, '--', '--no-resume-playback'].run_fg()
        except (ProcessExecutionError, KeyboardInterrupt) as e:
            print(e, file=sys.stderr)
        if ask(f"Disconnect from VPN?", True):
            vpn['down'].run_fg()
    else:
        mpv[uri].run_fg()
コード例 #21
0
ファイル: init.py プロジェクト: katiakweb/docky
 def _generate_env(self):
     generate = ask("Do you want to generate .env file ?", default=True)
     if generate:
         return GenerateEnvFile().generate()
コード例 #22
0
ファイル: processMEG.py プロジェクト: leadingin/childrenmeg
    def main(self):
        self.dest_dir = local.path(self.dest_dir)
        self.meg_dir = local.path(self.meg_dir)
        self.meg_conf = local.path(self.meg_conf)

        # Run some checks on the provided arguments
        if not self.meg_dir.is_file():
            print("Provided MEG source directory:")
            with colors.red:
                print(self.meg_dir)
            print("Does not exist")

        if not self.meg_conf.is_file():
            print("Provided config file:")
            with colors.red:
                print(self.meg_dir)
            print("Does not exist")

        if not self.dest_dir.is_dir():
            if terminal.ask(
                    "Destination directory: %s does not exist, create" %
                    str(self.dest_dir), True):
                self.dest_dir.mkdir()
            else:
                return -1

        if self.verbose:
            print("Verbose mode")
            print("Raw MEG found at:", self.meg_dir)
            print("Using config file: ", self.meg_conf)
            with colors.green:
                print("Outputting to: ", self.dest_dir)
            self.completed_subjects = find_completed(self.dest_dir).keys()
            print("Completed subjects: ", str(self.completed_subjects))
            if self.update:
                with colors.yellow:
                    print('WARNING: Overwriting existing data for subjects')
            if self.only_acoustic:
                print('Extracting acoustic data')

        if self.all_subjects or self.remaining_subjects:
            # determine remaining
            # FIXME hardcoded path is stupid
            toplevel = local.path(
                '/media/demetres/My Book/LizPang2015/Children')
            if not toplevel.exists():
                print('No subjects available.')
                return

            pattern = re.compile("^CC[0-9]+(-[0-9]+)?")

            self.subjects = []
            self.subjects += [
                subject.name for subject in toplevel
                if pattern.fullmatch(subject.name) is not None
            ]

            if self.remaining_subjects and self.update:
                print(
                    'Warning: Remaining and update specified, going through all...'
                )
            if self.all_subjects and not self.update:
                # easiest way to ensure that all get processed
                self.update = True

        if not self.update:
            self.subjects = [
                x for x in self.subjects if x not in self.completed_subjects
            ]
        if self.verbose:
            print('Subjects to process:', self.subjects)
        subjectsprogress = progress.ProgressAuto(self.subjects, body=True)
        subjectsprogress.start()
        # Go through each subject remaining
        for subject in self.subjects:
            with colors.green:
                print('Subject: ', subject)

            # First extract the data for opensmile processing, if not already done
            if preproc_csvs_dir.is_dir() and (len(preproc_csvs_dir.list()) > 0) and \
                    terminal.ask('Previously pre-processed data found, would you like to proceed ' +
                                         'presuming it is for subject ' + str(subject) +
                                                 ' (%d tests found)' % len(preproc_csvs_dir.list())):
                pass
            else:
                print(
                    'Preprocessing raw MEG for opensmile feature extraction...'
                )
                stdout = preprocess(subject, audioonly=self.only_acoustic)
                if self.verbose:
                    print(stdout[1])
                with colors.red:
                    if stdout[0] != 0 or 'FAILED' in stdout[
                            1] or 'Complete.' not in stdout[1]:
                        print('Failed to extract data for subject:', subject)
                        print('Skipping...')
                        continue
                with colors.green:
                    print("Completed outputting preprocessed csv files.")

            # Run extractions on all the preprocessed data
            for test in preproc_csvs_dir:
                # Ignore the absolute path for just the last directory title
                test_label = test.name.split('/')[-1]
                if self.verbose:
                    print("Test: ", test_label)

                directory = '/'.join([self.dest_dir, subject, test_label, ''])

                if not self.only_acoustic:
                    dest_meg_dir = directory + 'MEG/'
                    mkdir(dest_meg_dir)
                    temp_meg_dir = test.glob('MEG').pop()
                else:
                    temp_meg_dir = None

                if not self.only_meg or not self.only_wavelets:
                    dest_acoustic_dir = directory + 'Acoustic/'
                    mkdir(dest_acoustic_dir)
                    temp_acoustic_dir = test.glob('Acoustic').pop()
                else:
                    temp_acoustic_dir = None

                temp_location = local.path(preproc_csvs_dir + '/temp.csv')

                # MEG processing
                if temp_meg_dir is not None and temp_meg_dir.is_dir() and \
                        not (self.only_acoustic or self.only_wavelets):
                    print('Extracting MEG features...')
                    for epoch in progress.Progress(temp_meg_dir,
                                                   has_output=True):
                        if self.verbose:
                            print(time.strftime('%X %x %Z'))

                        # Check if the epoch has already been processed
                        if not self.update:
                            if epoch.name.split('.')[0] in [
                                    x.name.split('.')[0]
                                    for x in local.path(dest_meg_dir)
                            ]:
                                print("Already exists, skipping...")
                                continue

                        ProcessMEG.run_catch_fail(self,
                                                  opensmile_extract_features,
                                                  self.meg_conf,
                                                  epoch,
                                                  temp_location,
                                                  failedon=epoch)
                        ProcessMEG.run_catch_fail(
                            self,
                            mat_compress,
                            temp_location,
                            dest_meg_dir + epoch.name.split('.')[0] + '.mat',
                            failedon=epoch)
                        temp_location.delete()

                # Acoustic Processing
                if temp_acoustic_dir is not None and temp_acoustic_dir.is_dir() and \
                        not (self.only_meg or self.only_wavelets):
                    print('Extracting Audio features...')
                    for epoch in progress.Progress(temp_acoustic_dir,
                                                   has_output=True):
                        if self.verbose:
                            print(time.strftime('%X %x %Z'))

                        # Check if the epoch has already been processed
                        if not self.update:
                            if epoch.name.split('.')[0] in \
                                    [x.name.split('.')[0] for x in local.path(dest_acoustic_dir)]:
                                print("Already exists, skipping...")
                                continue

                        ProcessMEG.run_catch_fail(self,
                                                  opensmile_extract_features,
                                                  self.acoustic_conf,
                                                  epoch,
                                                  temp_location,
                                                  failedon=epoch)
                        ProcessMEG.run_catch_fail(self,
                                                  mat_compress,
                                                  temp_location,
                                                  dest_acoustic_dir +
                                                  epoch.name.split('.')[0] +
                                                  '.mat',
                                                  failedon=epoch)
                        temp_location.delete()

                # Wavelet Processing
                if temp_meg_dir is not None and temp_meg_dir.is_dir() and \
                        not (self.only_acoustic or self.only_meg):
                    print("Extracting wavelet coefficients...")
                    for epoch in progress.Progress(temp_meg_dir,
                                                   has_output=True):
                        if self.verbose:
                            print(time.strftime('%X %x %Z'))

                        outname = epoch.name.split('.')[0] + '_wavelets'

                        # Check if the epoch has already been processed
                        if not self.update:
                            if outname in [
                                    x.name.split('.')[0]
                                    for x in local.path(dest_meg_dir)
                            ]:
                                print("Already exists, skipping...")
                                continue

                        ProcessMEG.run_catch_fail(self,
                                                  extractwaveletcoef,
                                                  epoch,
                                                  dest_meg_dir + outname,
                                                  failedon=epoch)

                test.delete()

            # Increment
            with colors.green & colors.bold:
                subjectsprogress.increment()

        subjectsprogress.done()