Esempio n. 1
0
    def load(self, *, config_fd: TextIO = None) -> None:
        config = ""
        if config_fd:
            config = config_fd.read()
        else:
            # Local configurations (per project) are supposed to be static.
            # That's why it's only checked for 'loading' and never written to.
            # Essentially, all authentication-related changes, like
            # login/logout or macaroon-refresh, will not be persisted for the
            # next runs.
            file_path = ""
            if os.path.exists(LOCAL_CONFIG_FILENAME):
                file_path = LOCAL_CONFIG_FILENAME

                # FIXME: We don't know this for sure when loading the config.
                # Need a better separation of concerns.
                logger.warn(
                    "Using local configuration ({!r}), changes will not be "
                    "persisted.".format(file_path)
                )
            else:
                file_path = BaseDirectory.load_first_config(
                    "snapcraft", "snapcraft.cfg"
                )
            if file_path and os.path.exists(file_path):
                with open(file_path, "r") as f:
                    config = f.read()

        if config:
            _load_potentially_base64_config(self.parser, config)
Esempio n. 2
0
def load(fp: TextIO, *args, **kwargs) -> Braindump:
    """
    Deserialize ``fp`` (a ``.read()``-supporting file-like object containing a Braindump document) to a Python object.

    [extended_summary]

    :param fp: [description]
    :type fp: TextIO
    :return: [description]
    :rtype: Dict
    """
    return loads(fp.read())
Esempio n. 3
0
def unlock_account_with_passwordfile(account_manager: AccountManager,
                                     address_hex: AddressHex,
                                     password_file: TextIO) -> PrivateKey:
    password = password_file.read().strip("\r\n")

    try:
        return account_manager.get_privkey(address_hex, password.strip())
    except ValueError:
        click.secho(
            f"Incorrect password for {address_hex} in file. Aborting ...",
            fg="red")
        sys.exit(1)
Esempio n. 4
0
def load_lammps_eam_parameters(file: TextIO) -> Tuple[Callable[[Array], Array],
                                                      Callable[[Array], Array],
                                                      Callable[[Array], Array],
                                                      float]:
  """Reads EAM parameters from a LAMMPS file and returns relevant spline fits.

  This function reads single-element EAM potential fit parameters from a file
  in DYNAMO funcl format. In summary, the file contains:
  Line 1-3: comments,
  Line 4: Number of elements and the element type,
  Line 5: The number of charge values that the embedding energy is evaluated
  on (num_drho), interval between the charge values (drho), the number of
  distances the pairwise energy and the charge density is evaluated on (num_dr),
  the interval between these distances (dr), and the cutoff distance (cutoff).
  The lines that come after are the embedding function evaluated on num_drho
  charge values, charge function evaluated at num_dr distance values, and
  pairwise energy evaluated at num_dr distance values. Note that the pairwise
  energy is multiplied by distance (in units of eV x Angstroms). For more
  details of the DYNAMO file format, see:
  https://sites.google.com/a/ncsu.edu/cjobrien/tutorials-and-guides/eam
  Args:
    f: File handle for the EAM parameters text file.

  Returns:
    charge_fn: A function that takes an ndarray of shape [n, m] of distances
      between particles and returns a matrix of charge contributions.
    embedding_fn: Function that takes an ndarray of shape [n] of charges and
      returns an ndarray of shape [n] of the energy cost of embedding an atom
      into the charge.
    pairwise_fn: A function that takes an ndarray of shape [n, m] of distances
      and returns an ndarray of shape [n, m] of pairwise energies.
    cutoff: Cutoff distance for the embedding_fn and pairwise_fn.
  """
  raw_text = file.read().split('\n')
  if 'setfl' not in raw_text[0]:
    raise ValueError('File format is incorrect, expected LAMMPS setfl format.')
  temp_params = raw_text[4].split()
  num_drho, num_dr = int(temp_params[0]), int(temp_params[2])
  drho, dr, cutoff = float(temp_params[1]), float(temp_params[3]), float(
      temp_params[4])
  data = np.array(map(float, raw_text[6:-1]))
  embedding_fn = interpolate.spline(data[:num_drho], drho)
  charge_fn = interpolate.spline(data[num_drho:num_drho + num_dr], dr)
  # LAMMPS EAM parameters file lists pairwise energies after multiplying by
  # distance, in units of eV*Angstrom. We divide the energy by distance below,
  distances = np.arange(num_dr) * dr
  # Prevent dividing by zero at zero distance, which will not
  # affect the calculation
  distances = np.where(distances == 0, f32(0.001), distances)
  pairwise_fn = interpolate.spline(
      data[num_dr + num_drho:num_drho + f32(2) * num_dr] / distances,
      dr)
  return charge_fn, embedding_fn, pairwise_fn, cutoff
Esempio n. 5
0
def lex(input: TextIO) -> Iterator[Command]:
    input.seek(0)
    whitespace = re.compile('\s+')

    parsing_item    = False
    parsing_command = False
    command, args   = '', []
    while True:
        char = input.read(1)
        if char == '':
            break
        if whitespace.match(char):
            if parsing_item:
                assert(parsing_command)
                args.append('')
            parsing_item = False
        elif char == '(':
            if parsing_command:
                raise RuntimeError('Nested command')
            args.append('')
            parsing_command = True
            parsing_item    = False
        elif char == ')':
            if not parsing_command:
                raise RuntimeError('Unexpected ")"')
            if args[-1] == '':
                args = args[:-1]
            yield command, args
            command, args   = '', []
            parsing_item    = False
            parsing_command = False
        elif char == '#':
            while char != '\n':
                char = input.read(1)
        else:
            if parsing_command:
                args[-1] += char
            else:
                command += char
            parsing_item = True
Esempio n. 6
0
def template(
    client_obj: client.VaultClientBase, template: TextIO, output: TextIO
) -> None:
    """
    Render the given template and insert secrets in it.

    Rendering is done with jinja2. A vault() function is exposed that
    recieves a path and outputs the secret at this path.

    If template is -, standard input will be read.
    """
    result = client_obj.render_template(template.read())
    output.write(result)
def add_podsetting_cli(env: str, team: str, debug: bool, podsetting: TextIO) -> None:
    """Deploy a new podsetting to the K8s cluster."""
    if debug:
        enable_debug(format=DEBUG_LOGGING_FORMAT)
    _logger.debug("env: %s", env)
    _logger.debug("team: %s", team)
    podsetting_str = podsetting.read()
    _logger.debug("podsetting: %s", podsetting_str)
    _logger.debug("debug: %s", debug)
    try:
        build_podsetting(env_name=env, team_name=team, podsetting=podsetting_str, debug=debug)
    except ImportError:
        raise click.ClickException('The "utils" submodule is required to use "run" commands')
Esempio n. 8
0
def _reqs_from_pkg_el(pkg_el: TextIO) -> str:
    """Pull the requirements out of a -pkg.el file.
    >>> import io
    >>> _reqs_from_pkg_el(io.StringIO(
    ...   '''(define-package "x" "1.2" "A pkg." '(a (b "31.5")))'''))
    '( a ( b "31.5" ) )'
    """
    # TODO: fails if EXTRA-PROPERTIES args were given to #'define-package
    reqs = pkg_el.read()
    reqs = ' '.join(_tokenize_expression(reqs)[5:-1])
    reqs = reqs[reqs.find("' (") + 2:]
    reqs = reqs[:reqs.find(') )') + 3]
    return reqs
Esempio n. 9
0
    def load(cls,
             bibliography_file: TextIO,
             parser: BibTexParser = None) -> Bibliography:
        """Load bibliography from file.

        Parameters
        ----------
        bibliography_file : file-like
            Bibliography file to read. Should have a ``read`` method that returns a string.
        parser : bibtexparser.bparser.BibTexParser [Optional]
            Custom BibTexParser parser used to load the bibliography
        """
        return cls(bibliography_file.read())
Esempio n. 10
0
def generate(schema_file: TextIO, output_path: IO) -> None:
    interop_schema = avro.schema.parse(schema_file.read())
    datum_writer = avro.io.DatumWriter()
    output = ((codec, gen_data(codec, datum_writer, interop_schema)) for codec in CODECS_TO_VALIDATE)
    if output_path.isatty():
        json.dump({codec: base64.b64encode(data).decode() for codec, data in output}, output_path)
        return
    for codec, data in output:
        if codec == NULL_CODEC:
            output_path.write(data)
            continue
        base, ext = os.path.splitext(output_path.name)
        Path(f"{base}_{codec}{ext}").write_bytes(data)
Esempio n. 11
0
    def encrypt(self, text: TextIO, squares: SquaresTuple,
                cipher: TextIO) -> None:
        """Encrypt the text using chars squares."""
        self.__set_squares(squares)

        while True:
            text_chunk = text.read(CHUNK_BYTES)
            if text_chunk == '':
                break

            cipher_chunk = self.__encrypt_chunk(text_chunk)

            cipher.write(cipher_chunk)
Esempio n. 12
0
    def decrypt(self, cipher: TextIO, squares: SquaresTuple,
                text: TextIO) -> None:
        """Decrypt the cipher using chars squares."""
        self.__set_squares(squares)

        while True:
            cipher_chunk = cipher.read(CHUNK_BYTES)
            if cipher_chunk == '':
                break

            text_chunk = self.__decrypt_chunk(cipher_chunk)

            text.write(text_chunk)
Esempio n. 13
0
def manual_solution(pattern: str, wordlist: TextIO) -> List[str]:
    """Not using regular expressions"""

    letters = [t for t in enumerate(pattern) if t[1] != '_']
    #letters = filter(lambda t: t[1] != '_', enumerate(pattern))
    wanted_len = len(pattern)
    words = []

    for word in wordlist.read().split():
        if len(word) == wanted_len and all(
            [word[i] == char for i, char in letters]):
            words.append(word)

    return words
Esempio n. 14
0
def handler(raw_rules_and_messages: TextIO) -> int:
    raw_rules, raw_messages = raw_rules_and_messages.read().split("\n\n")

    for raw_rule in raw_rules.strip().splitlines():
        index, rule = raw_rule.split(": ")
        RULES[int(index)] = rule.replace('"', "")

    rule_zero_matches = 0

    for raw_message in raw_messages.strip().splitlines():
        rule_zero_matches += (regex.compile(
            group_rule_by_index(0)).fullmatch(raw_message) is not None)

    return rule_zero_matches
Esempio n. 15
0
    def __init__(self: BayesGraph, file_object: TextIO):
        """Initialize a BayesGraph object from an input file in .uai format
    """
        graph_type = file_object.readline()
        if graph_type != "BAYES\n":
            raise Exception(
                "File does not contain a Bayes network in .uai format")
        num_vars = file_object.readline()
        self.cardinalities = [int(n) for n in file_object.readline().split()]
        num_factors = int(file_object.readline())
        # handle factors in preamble
        self.factors: List[List[int]] = []
        for i in range(num_factors):
            factor_description = [
                int(n) for n in file_object.readline().split()
            ]
            if factor_description[0] != len(factor_description) - 1:
                raise Exception(
                    "Number given for number of variables for factor does not match number of variables given for factor"
                )
            self.factors.append(factor_description[1:])
        # handle function tables
        ft_description = file_object.read().split()
        self.tables: List[Dict[Tuple, str]] = []
        i = 0
        # for each factor
        for factor_i in range(num_factors):
            table: Dict[Tuple, str] = {}
            num_entries = int(ft_description[i])
            i += 1
            # iterate through each assignment to local random variables
            assignment = [0 for i in self.factors[factor_i]]
            in_range = True
            while in_range:
                # and record that assignment with its probability in the map
                table[tuple(assignment)] = ft_description[i]
                i += 1
                # then update the assignment to the next iterand
                in_range = False
                for j in range(len(assignment) - 1, -1, -1):
                    if assignment[j] < self.cardinalities[
                            self.factors[factor_i][j]] - 1:
                        assignment[j] += 1
                        in_range = True
                        break
                    else:
                        assignment[j] = 0
            self.tables.append(table)

        self._memo_to_formula: Optional[Tuple[List[str], Formula]] = None
Esempio n. 16
0
 def _extract(self, station: str, source: TextIO) -> Optional[str]:
     """Returns report pulled from the saved file"""
     start, end = self._index_target(station)
     txt = source.read()
     txt = txt[txt.find(start):]
     txt = txt[:txt.find(end, 30)]
     lines = []
     for line in txt.split("\n"):
         if "CLIMO" not in line:
             line = line.strip()
         if not line:
             break
         lines.append(line)
     return "\n".join(lines) or None
Esempio n. 17
0
def load(fp: TextIO) -> StepFile:
    """ Load STEP-file (ISO 10303-21:2002) from text stream.

    A special encoding form characters > 126 is applied in STEP-Files, therefore an encoding setting at opening files
    is not necessary, reading as ``'ascii'`` works fine. Decoding of this special characters will be applied.
    ISO 10303-21:2016 files are not supported yet, they are ``'UTF-8'`` encoded, this encoding can be used also
    for older STEP-files, because code points < 127 are equal to ``'ISO-8859-1'`` or default ``'ascii'`` encoding.

    Args:
        fp: STEP-file content as text stream yielding unicode strings

    """
    content = fp.read()
    return loads(content)
Esempio n. 18
0
def handler(raw_output_joltages: TextIO) -> int:
    ADAPTER_OUTPUT_JOLTAGES.append(0)
    for raw_output_joltage in raw_output_joltages.read().splitlines():
        ADAPTER_OUTPUT_JOLTAGES.append(int(raw_output_joltage))

    counter: Dict[int, int] = defaultdict(int)
    counter[0] += 1

    for adapter_joltage in sorted(ADAPTER_OUTPUT_JOLTAGES):
        counter[adapter_joltage] += counter.get(adapter_joltage - 1, 0)
        counter[adapter_joltage] += counter.get(adapter_joltage - 2, 0)
        counter[adapter_joltage] += counter.get(adapter_joltage - 3, 0)

    return counter[max(ADAPTER_OUTPUT_JOLTAGES)]
Esempio n. 19
0
def _retrieve_last_line_of_file(f: typing.TextIO, read_chunk_size: int = 100) -> str:
    """ Retrieve the last line of the file.

    From: https://stackoverflow.com/a/7167316/12907985

    Args:
        f: File-like object.
        read_chunk_size: Size of step in bytes to read backwards into the file. Default: 100.

    Returns:
        Last line of file, assuming it's found.
    """
    last_line = ""
    while True:
        # We grab chunks from the end of the file towards the beginning until we
        # get a new line
        # However, we need to be more careful because seeking directly from the end
        # of a text file is apparently undefined behavior. To address this, we
        # follow the approach from here: https://stackoverflow.com/a/51131242/12907985
        # First, move to the end of the file.
        f.seek(0, os.SEEK_END)
        # Then, just back from the current position (based on SEEK_SET and tell()
        # of the current position).
        f.seek(f.tell() - len(last_line) - read_chunk_size, os.SEEK_SET)
        # NOTE: This chunk isn't necessarily going back read_chunk_size characters, but
        #       rather just some number of bytes. Depending on the encoding, it may be.
        #       In any case, we don't care - we're just looking for the last line.
        chunk = f.read(read_chunk_size)

        if not chunk:
            # The whole file is one big line
            return last_line

        if not last_line and chunk.endswith('\n'):
            # Ignore the trailing newline at the end of the file (but include it
            # in the output).
            last_line = '\n'
            chunk = chunk[:-1]

        nl_pos = chunk.rfind('\n')
        # What's being searched for will have to be modified if you are searching
        # files with non-unix line endings.

        last_line = chunk[nl_pos + 1:] + last_line

        if nl_pos == -1:
            # The whole chunk is part of the last line.
            continue

        return last_line
Esempio n. 20
0
    def parse(self, mfp: TextIO, names: Optional[List[str]] = None) -> None:
        """Parse an object model text stream.

           :param mfp: object model file-like object
           :param names: the device names to parse, or an empty list to
                         parse all devices.
        """
        objmod = json_loads(mfp.read())
        devmaps: List[Tuple[str, Dict[str, OMMemoryRegion]]] = []
        interrupts: List[Tuple[str, List[OMInterrupt]]] = []
        devkinds = {}
        for path in self._find_node_of_type(objmod, 'OMCore'):
            core, hartid = self._parse_core(path)
            self._cores[hartid] = core
        xlens = set()
        for core in self._cores.values():
            xlens.add(core.xlen)
        if len(xlens) != 1:
            raise ValueError(f'Incoherent xlen in platfornm: {xlens}')
        self._xlen = xlens.pop()
        for path in self._find_node_of_type(objmod, 'OMDevice'):
            if path.embedded:
                # for now, ignore sub devices
                continue
            try:
                dev = self._parse_device(path, names or [])
            except DiscardedItemError as exc:
                print(exc, file=stderr)
                continue
            if not dev:
                continue
            devname, mmaps, ints, devices = dev
            if devname not in devkinds:
                devkinds[devname] = 0
            else:
                devkinds[devname] += 1
            name = f'{devname}{devkinds[devname]}'
            if mmaps:
                devmaps.append((name, mmaps))
            if ints:
                interrupts.append((name, ints))
            # to be reworked
            if len(devices) == 1:
                self._devices[name] = list(devices.values())[0]
            else:
                for subname, device in devices.items():
                    self._devices[f'{name}_{subname}'] = device
        self._memorymap = self._merge_memory_regions(devmaps)
        self._intmap = self._merge_interrupts(interrupts)
Esempio n. 21
0
File: base.py Progetto: std0/pycrypt
    def decrypt(self, cipher: TextIO, key: str, text: TextIO) -> None:
        """Decrypt the cipher using the key."""
        super().decrypt(cipher, key, text)

        while True:
            cipher_hex = cipher.read(HEX_LENGTH)
            if cipher_hex == '':
                break

            cipher_int = hex_to_int(cipher_hex)
            char_int_allowed(cipher_int)

            text_int = self._process(cipher_int)

            text.write(chr(text_int))
Esempio n. 22
0
File: base.py Progetto: std0/pycrypt
    def encrypt(self, text: TextIO, key: str, cipher: TextIO) -> None:
        """Encrypt the text using the key."""
        super().encrypt(text, key, cipher)

        while True:
            text_char = text.read(1)
            if text_char == '':
                break

            text_int = ord(text_char)
            char_int_allowed(text_int)

            cipher_int = self._process(text_int)

            cipher.write(int_to_hex(cipher_int))
Esempio n. 23
0
File: cli.py Progetto: std0/pycrypt
def key_cipher_command(obj: dict, key_file: TextIO, cipher: KeyCipher) -> None:
    """Encrypt/decrypt text with given cipher using the key from
    KEY_FILE. If KEY_FILE is not provided, a prompt will appear."""
    if obj['is_encrypt'] is True:
        action = cipher.encrypt
    else:
        action = cipher.decrypt

    if key_file is None:
        key = click.prompt("Enter key")
    else:
        key = key_file.read(cipher.key_bytes)
        print("Key loaded from {0}".format(key_file.name))

    action(obj['input_stream'], key, obj['output_stream'])
Esempio n. 24
0
    def parse_dict_formatted_file(f: TextIO):
        from src.sliding_block_puzzle import Puzzle
        puzzle = eval(f.read())

        heuristic = puzzle[PuzzleInputParser.HEURISTIC_LABEL]
        row = puzzle[PuzzleInputParser.ROW_LABEL]
        column = puzzle[PuzzleInputParser.COLUMN_LABEL]
        blocks = puzzle[PuzzleInputParser.BLOCKS_LABEL]
        initial_state = puzzle[PuzzleInputParser.INITIAL_STATE_LABEL]
        final_states = puzzle[PuzzleInputParser.FINAL_STATES_LABEL]

        PuzzleInputParser._validate_inputs(row, column, initial_state,
                                           final_states)
        return Puzzle(heuristic, row, column, blocks, initial_state,
                      final_states)
Esempio n. 25
0
def parse_legacy(file: TextIO):
    data = file.read()
    commands = data.split('\n\n')
    for command in commands:
        com, *args = command.split('\n')
        if com.lower() == 'word':
            src_word, dest_words, langs = args
            src_lang, dest_lang = langs.split('->')
            for word in dest_words.split(';'):
                commit_word(src_word, word, (src_lang, dest_lang))
        if com.lower() == 'func':
            src_func, dest_func, pos, langs = args
            src_lang, dest_lang = langs.split('->')
            pos = [tuple(p.split('->')) for p in pos.split(';')]
            commit_func(src_func, dest_func, pos, (src_lang, dest_lang))
Esempio n. 26
0
def process_file(reader: TextIO) -> None:
    """Read and print the data from reader , which must start with a single
    description line, then a sequence of lines beginning with '#', then a sequence of data.

    >>> infile = StringIO('Example\\n#Comment\\nLine 1\\nLine 2\\n')
    >>> process_file(infile)
    Line 1
    Line 2
    """

    #Find and print the first piece of data
    line = skip_header(reader).strip()
    print(line)

    print(reader.read())
Esempio n. 27
0
def handler(raw_document: TextIO) -> int:
    raw_rules, _, raw_nearby_tickets = raw_document.read().split("\n\n")

    rules = parse_rules(raw_rules)
    tickets = parse_tickets(raw_nearby_tickets.strip())

    ticket_scanning_error_rate = 0

    for ticket in tickets:
        for value in ticket:
            if value not in rules:
                ticket_scanning_error_rate += value
                break

    return ticket_scanning_error_rate
Esempio n. 28
0
def run_csv2rdf(csv_filename: str, metadata_filename: str, csv_io: TextIO, metadata_io: TextIO):
    client = docker.from_env()
    csv2rdf = client.containers.create(
        'gsscogs/csv2rdf',
        command=f'csv2rdf -m annotated -o /tmp/output.ttl -t /tmp/{csv_filename} -u /tmp/{metadata_filename}'
    )
    archive = BytesIO()
    metadata_io.seek(0, SEEK_END)
    metadata_size = metadata_io.tell()
    metadata_io.seek(0)
    csv_io.seek(0, SEEK_END)
    csv_size = csv_io.tell()
    csv_io.seek(0)
    with TarFile(fileobj=archive, mode='w') as t:
        tis = TarInfo(str(metadata_filename))
        tis.size = metadata_size
        tis.mtime = time.time()
        t.addfile(tis, BytesIO(metadata_io.read().encode('utf-8')))
        tic = TarInfo(str(csv_filename))
        tic.size = csv_size
        tic.mtime = time.time()
        t.addfile(tic, BytesIO(csv_io.read().encode('utf-8')))
    archive.seek(0)
    csv2rdf.put_archive('/tmp/', archive)
    csv2rdf.start()
    response = csv2rdf.wait()
    sys.stdout.write(csv2rdf.logs().decode('utf-8'))
    assert_equal(response['StatusCode'], 0)
    output_stream, output_stat = csv2rdf.get_archive('/tmp/output.ttl')
    output_archive = BytesIO()
    for line in output_stream:
        output_archive.write(line)
    output_archive.seek(0)
    with TarFile(fileobj=output_archive, mode='r') as t:
        output_ttl = t.extractfile('output.ttl')
        return output_ttl.read()
Esempio n. 29
0
def binary_search(fh: TextIO, width: int, state_width: int, linecount: int, state_to_find: str) -> str:
    """
    Args:
        fh: the file to search
        width: the width of a line in the file
        state_width: the width of the state portion of the line
        linecount: the number of lines in the file
        state_to_find: the state we are looking for

    Returns:
        the value for state_to_find
    """
    first = 0
    last = linecount - 1

    while first <= last:
        midpoint = int((first + last) / 2)
        fh.seek(midpoint * width)

        # Only read the 'state' part of the line (for speed)
        b_state = fh.read(state_width)

        if state_to_find < b_state:
            last = midpoint - 1

        # If this is the line we are looking for, then read the entire line
        elif state_to_find == b_state:
            fh.seek(midpoint * width)
            line = fh.read(width)
            (_, value) = line.rstrip().split(":")
            return value

        else:
            first = midpoint + 1

    return None
Esempio n. 30
0
def handler(boot_code: TextIO) -> int:
    template_boot_code: List[Tuple[str, str]] = []

    # Load the boot code into a data structure that can be more
    # easily navigated.
    for inst in boot_code.read().splitlines():
        operation, argument = inst.split(" ")
        template_boot_code.append((operation, argument))

    # Copy the template boot code for each instruction where
    # the operator is nop or jmp.
    for index in range(len(template_boot_code)):
        boot_code_copy = copy.deepcopy(template_boot_code)

        if boot_code_copy[index][0] == "nop":
            boot_code_copy[index] = ("jmp", boot_code_copy[index][1])
        elif boot_code_copy[index][0] == "jmp":
            boot_code_copy[index] = ("nop", boot_code_copy[index][1])
        else:
            BOOT_CODE[index] = []
            continue

        BOOT_CODE[index] = boot_code_copy

    # Go through each index and evaluate all of the instructions
    # with the single corrupt instruction replacement to determine
    # if it completes successfully.
    for index in range(len(template_boot_code)):
        if not BOOT_CODE[index]:
            continue

        while True:
            instruction = BOOT_CODE[index][CODE_INDEX[index]]
            execute_instruction(instruction, index)

            # If we have seen the instruction at this index before,
            # then we should break out of the infinite loop.
            if CODE_INDEX[index] in BOOT_CODE_CACHE[index]:
                break
            BOOT_CODE_CACHE[index].append(CODE_INDEX[index])

            # Ensure that we return the accumulator if it ends up
            # being the last instruction before attempting to loop
            # again.
            if CODE_INDEX[index] == len(template_boot_code):
                return ACCUMULATOR[index]

    return ACCUMULATOR[index]
Esempio n. 31
0
def main(args: Sequence[str] = sys.argv[1:],
         stdin: typ.TextIO = sys.stdin) -> int:
    # pylint: disable=dangerous-default-value; we don't modify it, I promise.
    if "-h" in args or "--help" in args or not args:
        print(main.__doc__)
        return 0

    if "--test" in args:
        input_data = "Hello, 世界!!!!iasdf1234567890!!!!"
        block = _cli_encode(input_data)
        msg = _cli_decode(block)
        assert msg == input_data
        print("ok")
        return 0

    if "--profile" in args:
        import io
        import pstats
        import cProfile

        # init lookup tables
        input_data = "Hello, 世界!!!!iasdf1234567890!!!!"
        _cli_encode(input_data)

        pr = cProfile.Profile()
        pr.enable()
        _cli_encode(input_data)
        pr.disable()
        s = io.StringIO()
        ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
        ps.print_stats()
        print(s.getvalue())
        return 0

    input_data = stdin.read()

    if "-e" in args or "--encode" in args:
        block_b16 = _cli_encode(input_data)
        sys.stdout.write(block_b16)
        return 0
    elif "-d" in args or "--decode" in args:
        msg = _cli_decode(input_data)
        sys.stdout.write(msg)
        return 0
    else:
        sys.stderr.write("Invalid arguments\n")
        sys.stderr.write(CLI_HELP)
        return 1
Esempio n. 32
0
def _prepare_graph_struct(name: Optional[str], graph: TextIO, hosts: List[str], graph_format: str) -> dict:
    if graph_format == 'raw':
        return json.load(graph)
    assert name and hosts, 'Only raw graph format can not set hosts and name'
    result = GraphStruct()
    result.graph_name = name
    result.clusters.from_json({'I': hosts})
    if graph_format == 'script':
        task = ExtendedTaskStruct()
        task.task_name = 'main'
        task.hosts.append('I')
        task.task_struct.executor.name = 'shell'
        executor_cfg = ShellExecutorConfig()
        executor_cfg.shell_script = graph.read()
        task.task_struct.executor.config = executor_cfg.to_json()
        result.tasks.from_json([task.to_json()])
    elif graph_format == 'makefile':
        raise NotImplementedError()
    return result.to_json()
Esempio n. 33
0
    def map_input(self, data: typing.TextIO) -> libioc.Config.Data.Data:
        """Parse and normalize JSON data."""
        try:
            content = data.read().strip()
        except (FileNotFoundError, PermissionError) as e:
            raise libioc.errors.JailConfigError(
                message=str(e),
                logger=self.logger
            )

        if content == "":
            return libioc.Config.Data.Data()

        try:
            result = json.loads(content)  # type: typing.Dict[str, typing.Any]
            return libioc.Config.Data.Data(result)
        except json.decoder.JSONDecodeError as e:
            raise libioc.errors.JailConfigError(
                message=str(e),
                logger=self.logger
            )
Esempio n. 34
0
def lex(input: TextIO) -> Iterator[Command]:
    whitespace = re.compile('\s+')

    parsing_item    = False
    parsing_command = False
    command, args   = '', []
    while True:
        char = input.read(1)
        if char is '':
            break
        if whitespace.match(char):
            if parsing_item:
                if parsing_command:
                    args.append('')
                else:
                    command += char
            parsing_item = False
        elif char == '(':
            if parsing_command:
                raise RuntimeError('Nested command')
            args.append('')
            parsing_command = True
            parsing_item    = False
        elif char == ')':
            if not parsing_command:
                raise RuntimeError('Unexpected ")"')
            if args[-1] == '':
                args = args[:-1]
            yield command, args
            command, args   = '', []
            parsing_item    = False
            parsing_command = False
        else:
            if parsing_command:
                args[-1] += char
            else:
                command += char
            parsing_item = True
Esempio n. 35
0
 def map_input(self, data: typing.TextIO) -> typing.Dict[str, typing.Any]:
     """Normalize data read from the UCL file."""
     import ucl
     result = ucl.load(data.read())  # type: typing.Dict[str, typing.Any]
     result["legacy"] = True
     return result
Esempio n. 36
0
 def _read_file_handle(self, f: typing.TextIO) -> None:
     self.parse_lines(f.read())