Dictionary Assigning multiple lists from input file from character
<p>I am trying to parse and then edit the text that lives between <code>{</code> and a <code>}</code>. The information is different(in between the curly braces) so I would need to be able to have these results separated in some type of buffer to test the /test and /example for each one.</p>

<p><strong>Information in file</strong></p>

<pre><code>server {
    listen 80;
    listen 443 ssl;
    server_name example.com;
    rewrite /test redirectexample.com/test permanent;
    rewrite /  http://redirectedexample.com permanent;
}

server {
    listen 80;
    listen 443 ssl;
    server_name test.com;
    rewrite /example testing.com/example permanent;
    rewrite /  http://www.testing.com permanent;
}
</code></pre>

<p><strong>Script</strong></p>

<pre><code>import.os
InputFile= open('/tmp/redirect.conf', 'r')
dict = {}

file =InputFile.readlines()

parsing = False
for line in file:
    if line.startswith("}"):
        parsing = False
    if parsing:
        # Run some command here        
    if line.startswith("server {"):
        parsing = True
</code></pre>

<p>I am not sure where to start calling the dictionary as I want to somehow call everything between the first <code>server {</code> and <code>}</code>. The final goal is to be able to run a curl to test the example.com/test redirection and test.com/example redirect get the output from this and diff that to what is currently in our configuration files. The purpose of this is to audit these prior to moving them to avoid keeping configurations that are no longer needed. I had trouble finding info on this, if there is a document somewhere I need to RTFM then please feel free to drop a link.</p>

<p><strong>Edit</strong></p>

<p>Things looking for script to do:</p>

<ol>
<li><p>Take nginx server block's and grab value(s) of "server_name"</p></li>
<li><p>Grab value of rewrite /test /example &lt;-- actual string is random</p></li>
<li><p>Test both of these value's with <code>curl -w "%{url_effective}\n" -I -L
-s -S $m -o /dev/null</code></p></li>
<li><p>Then diff the curl output to whats rewritten.</p></li>
</ol>

<p>Take note that I don't need to parse example.com/example as the /example redirection is in a different server block.</p>

<p>Hope this makes more since. There are thousands of server blocks hence the reason for this.</p>

<p>Here's one method that splits the data into nested lists and then constructs a dict. I'm largely guessing at the desired data structure. Disclaimer: there is likely a more efficient way of doing this with regex, and this method isn't ideal if reading the entire file into memory is going to be an issue.</p>

<pre><code>import itertools


def isplit(iterable, splitters):  # thank you http://stackoverflow.com/a/4322780/6085135
    return [list(g) for k,g in itertools.groupby(iterable,lambda x:x in splitters) if not k]


with open('example.txt', 'r') as f:
    data = f.read()

# split the data into lists
data = data.replace('{', '').replace('}', '').replace(';', ' ;')
data = data.split()
data = isplit(data, ('server',))

# construct dict from sublists
servers = {}
for block in data:
    block = isplit(block, (';',))
    server = {}
    for item in block:
        key, value = item[0], ' '.join(item[1:])
        if key not in server.keys():
            server[key] = value
        else:
            if isinstance(server[key], list):
                server[key] = server[key].append(value)
            else:
                server[key] = [server[key], value]
    server_name = server.pop('server_name')
    servers.update({server_name: server})

print(servers)
</code></pre>

<p>Output:</p>

<pre><code>{
  'test.com': 
    {
      'listen': ['80', '443 ssl'], 
      'rewrite': ['/example testing.com/example permanent', '/ http://www.testing.com permanent']
    }, 
  'example.com': 
    {
      'listen': ['80', '443 ssl'], 
      'rewrite': ['/test redirectexample.com/test permanent', '/ http://redirectedexample.com permanent']
    }
} 
</code></pre>

python dictionary