Skip to content Skip to sidebar Skip to footer

Unnecessary Quotation Of Subkey And Iteration Through Primary Key In Pyyaml Event Structure

I have the next code: import gnupg import re import textwrap from pprint import pprint import yaml from yaml.events import * class AppendableEvents: def __init__(self, path, eve

Solution 1:

First of all, please provide a minimal example when asking a question. I removed the gnupg stuff from your code because it is not relevant at all to your question. It is your responsibility to provide only relevant parts of your actual code so that people trying to help you do not need to dig through irrelevant lines.

Now, to solve your question: You simply need to construct the events you output appropriately. Here's a solution (without the gnupg stuff):

defExistingFile():
  withopen(creds) as sensitive_data:
    additions = [key("app2"), MappingStartEvent(None, None, True)]
    for line in sensitive_data:
      encrypted_value = re.sub(r'^( +?|[A-Za-z0-9]|[A-Za]|[0-9])+( +)?' + '=' + '( +)?', '', line, 1)
      line = re.sub(r'^( +)?|( +)?' + '=' + '.*', '', line.rstrip('\n'))
      additions.extend(
        [ScalarEvent(None, None, (True, False), line),
         ScalarEvent(None, None, (False, True), encrypted_value, style='|')])
    additions.append(MappingEndEvent())
    append_to_yaml(f1, f1_mod, [AppendableEvents(["global", "app1"], additions)])

As you can see, I construct the scalar event for the key differently from that for the value, so that the value is presented as literal scalar and the key as plain scalar (without quotes).

I also call append_to_yaml only once so that app2 is only created once.

Post a Comment for "Unnecessary Quotation Of Subkey And Iteration Through Primary Key In Pyyaml Event Structure"