Skip to content

Commit

Permalink
included support for the flat format
Browse files Browse the repository at this point in the history
  • Loading branch information
aFarchi committed Apr 29, 2021
1 parent e35d5f5 commit 008b450
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.1.0 (2021-04-29)
=====
- Added the support for the 'flat' format.

1.0.2 (2021-04-21)
=====
- Moved to github.
Expand Down
4 changes: 2 additions & 2 deletions anaconda/treeconfigparser/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{% set name = "treeconfigparser" %}
{% set version = "1.0.2" %}
{% set version = "1.1.0" %}

package:
name: "{{ name|lower }}"
version: "{{ version }}"

source:
url: "https://pypi.io/packages/source/{{ name[0] }}/{{ name }}/{{ name }}-{{ version }}.tar.gz"
sha256: 9471326dfb3c76fb78f835f856e7f5cfed5f7c93f6737068380be1e4a43308c9
sha256: 58d90d89ce49ea9569016483c8e32a6523798db9bf943a7853863e77f978afdc

build:
number: 0
Expand Down
136 changes: 119 additions & 17 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,25 @@ <h2 id="main-usage">Main Usage</h2>
<pre><code>&gt;&gt;&gt; config = fromfile(file_name)
</code></pre>
<h2 id="supported-formats">Supported Formats</h2>
<p>Currently, two formats are supported for configuration files:
the 'py' format and the 'cpp' format. The following option hierarchy:</p>
<p>Currently, three formats are supported for configuration files:
the 'py' format, the 'flat' format, and the 'cpp' format. The
'py' and 'cpp' formats support an arbitrary hierarchy depth,
while the 'flat' format only supports one level of hierarchy. </p>
<p>The following option hierarchy:</p>
<pre><code>section1
|
- option1 = val1
section2
|
- option2 = val2
</code></pre>
<p>can be constructed with this file under the 'flat' format:</p>
<pre><code>[section1]
option1 = val1
[section2]
option2 = val2
</code></pre>
<p>The following option hierarchy:</p>
<pre><code>section1
|
- option1 = val1
Expand All @@ -58,11 +75,12 @@ <h2 id="supported-formats">Supported Formats</h2>
[subsection12]
option3 = val3
</code></pre>
<p>and with this file under the 'cpp' format:</p>
<p>with this file under the 'cpp' format:</p>
<pre><code>section1.option1 = val1
section1.subsection11.option2 = val2
section1.subsection12.option3 = val3
</code></pre>
<p>and cannot be constructed under the 'flat' format. </p>
<p>Comments can be included in the configuration file using a dedicated
character (usually '#'). Everything which is on the right of this
comment character is ignored by the parser.</p>
Expand Down Expand Up @@ -483,7 +501,7 @@ <h2 id="programmatic-usage">Programmatic Usage</h2>
----------
file_name : str
The name of the file to parse.
convention : {&#39;py&#39;, &#39;cpp&#39;}, optional
convention : {&#39;py&#39;, &#39;flat&#39;, &#39;cpp&#39;}, optional
The format of the file.
comment_char : str, optional
The character used to delimit comments in the file.
Expand Down Expand Up @@ -511,11 +529,14 @@ <h2 id="programmatic-usage">Programmatic Usage</h2>
raise IndentationError(line)
return keylist[:depth]

def extract_tree_node(keylist, line, indentation):
def extract_tree_node_py(keylist, line, indentation):
key = line.strip()[1:-1]
keylist = update_keylist(keylist, line.find(key) - 1, indentation)
return keylist + [key]

def extract_tree_node_flat(line):
return line.strip()[1:-1]

def extract_tree_leaf_py(keylist, line, indentation):
if &#39;=&#39; in line:
split = line.split(&#39;=&#39;, 1)
Expand All @@ -528,6 +549,17 @@ <h2 id="programmatic-usage">Programmatic Usage</h2>
self.set(keylist + [key], value, update_tree=True)
return keylist

def extract_tree_leaf_flat(section, line):
if &#39;=&#39; in line:
split = line.split(&#39;=&#39;, 1)
key = split[0].strip()
value = split[1].strip()
else:
key = line.strip()
value = &#39;&#39;
self.set([section, key], value, update_tree=True)
return section

def extract_tree_leaf_cpp(line):
if &#39;=&#39; in line:
split = line.split(&#39;=&#39;, 1)
Expand All @@ -544,9 +576,17 @@ <h2 id="programmatic-usage">Programmatic Usage</h2>
if not line or line.isspace():
return keylist
if is_tree_node(line):
return extract_tree_node(keylist, line, indentation)
return extract_tree_node_py(keylist, line, indentation)
return extract_tree_leaf_py(keylist, line, indentation)

def read_line_flat(line, section, comment_char):
line = line.split(comment_char)[0]
if not line or line.isspace():
return section
if is_tree_node(line):
return extract_tree_node_flat(line)
return extract_tree_leaf_flat(section, line)

def read_line_cpp(line, comment_char):
line = line.split(comment_char)[0].strip()
if line and not line.isspace():
Expand Down Expand Up @@ -588,6 +628,10 @@ <h2 id="programmatic-usage">Programmatic Usage</h2>
for line in lines:
keylist = read_line_py(line, keylist, comment_char,
indentation)
elif convention == &#39;flat&#39;:
section = None
for line in lines:
section = read_line_flat(line, section, comment_char)
elif convention == &#39;cpp&#39;:
for line in lines:
read_line_cpp(line, comment_char)
Expand All @@ -608,7 +652,7 @@ <h2 id="programmatic-usage">Programmatic Usage</h2>
----------
file_name : str
The name of the file to write.
convention : {&#39;py&#39;, &#39;cpp&#39;}, optional
convention : {&#39;py&#39;, &#39;flat&#39;, &#39;cpp&#39;}, optional
The format of the file.
indentation : int, optional
The indentation used to add suboptions or
Expand Down Expand Up @@ -651,6 +695,8 @@ <h2 id="programmatic-usage">Programmatic Usage</h2>
# write lines
if convention == &#39;py&#39;:
lines = write_tree_py(self.tree, 0, indentation)
elif convention == &#39;flat&#39;:
lines = write_tree_py(self.tree, 0, 0)
elif convention == &#39;cpp&#39;:
lines = write_tree_cpp(self.tree, [])
Path(file_name).write_lines(lines)
Expand Down Expand Up @@ -1557,7 +1603,7 @@ <h2 id="attributes">Attributes</h2>
----------
file_name : str
The name of the file to parse.
convention : {&#39;py&#39;, &#39;cpp&#39;}, optional
convention : {&#39;py&#39;, &#39;flat&#39;, &#39;cpp&#39;}, optional
The format of the file.
comment_char : str, optional
The character used to delimit comments in the file.
Expand Down Expand Up @@ -1585,11 +1631,14 @@ <h2 id="attributes">Attributes</h2>
raise IndentationError(line)
return keylist[:depth]

def extract_tree_node(keylist, line, indentation):
def extract_tree_node_py(keylist, line, indentation):
key = line.strip()[1:-1]
keylist = update_keylist(keylist, line.find(key) - 1, indentation)
return keylist + [key]

def extract_tree_node_flat(line):
return line.strip()[1:-1]

def extract_tree_leaf_py(keylist, line, indentation):
if &#39;=&#39; in line:
split = line.split(&#39;=&#39;, 1)
Expand All @@ -1602,6 +1651,17 @@ <h2 id="attributes">Attributes</h2>
self.set(keylist + [key], value, update_tree=True)
return keylist

def extract_tree_leaf_flat(section, line):
if &#39;=&#39; in line:
split = line.split(&#39;=&#39;, 1)
key = split[0].strip()
value = split[1].strip()
else:
key = line.strip()
value = &#39;&#39;
self.set([section, key], value, update_tree=True)
return section

def extract_tree_leaf_cpp(line):
if &#39;=&#39; in line:
split = line.split(&#39;=&#39;, 1)
Expand All @@ -1618,9 +1678,17 @@ <h2 id="attributes">Attributes</h2>
if not line or line.isspace():
return keylist
if is_tree_node(line):
return extract_tree_node(keylist, line, indentation)
return extract_tree_node_py(keylist, line, indentation)
return extract_tree_leaf_py(keylist, line, indentation)

def read_line_flat(line, section, comment_char):
line = line.split(comment_char)[0]
if not line or line.isspace():
return section
if is_tree_node(line):
return extract_tree_node_flat(line)
return extract_tree_leaf_flat(section, line)

def read_line_cpp(line, comment_char):
line = line.split(comment_char)[0].strip()
if line and not line.isspace():
Expand Down Expand Up @@ -1662,6 +1730,10 @@ <h2 id="attributes">Attributes</h2>
for line in lines:
keylist = read_line_py(line, keylist, comment_char,
indentation)
elif convention == &#39;flat&#39;:
section = None
for line in lines:
section = read_line_flat(line, section, comment_char)
elif convention == &#39;cpp&#39;:
for line in lines:
read_line_cpp(line, comment_char)
Expand All @@ -1682,7 +1754,7 @@ <h2 id="attributes">Attributes</h2>
----------
file_name : str
The name of the file to write.
convention : {&#39;py&#39;, &#39;cpp&#39;}, optional
convention : {&#39;py&#39;, &#39;flat&#39;, &#39;cpp&#39;}, optional
The format of the file.
indentation : int, optional
The indentation used to add suboptions or
Expand Down Expand Up @@ -1725,6 +1797,8 @@ <h2 id="attributes">Attributes</h2>
# write lines
if convention == &#39;py&#39;:
lines = write_tree_py(self.tree, 0, indentation)
elif convention == &#39;flat&#39;:
lines = write_tree_py(self.tree, 0, 0)
elif convention == &#39;cpp&#39;:
lines = write_tree_cpp(self.tree, [])
Path(file_name).write_lines(lines)</code></pre>
Expand Down Expand Up @@ -2046,7 +2120,7 @@ <h2 id="parameters">Parameters</h2>
<dl>
<dt><strong><code>file_name</code></strong> :&ensp;<code>str</code></dt>
<dd>The name of the file to parse.</dd>
<dt><strong><code>convention</code></strong> :&ensp;<code>{'py', 'cpp'}</code>, optional</dt>
<dt><strong><code>convention</code></strong> :&ensp;<code>{'py', 'flat', 'cpp'}</code>, optional</dt>
<dd>The format of the file.</dd>
<dt><strong><code>comment_char</code></strong> :&ensp;<code>str</code>, optional</dt>
<dd>The character used to delimit comments in the file.</dd>
Expand Down Expand Up @@ -2081,7 +2155,7 @@ <h2 id="raises">Raises</h2>
----------
file_name : str
The name of the file to parse.
convention : {&#39;py&#39;, &#39;cpp&#39;}, optional
convention : {&#39;py&#39;, &#39;flat&#39;, &#39;cpp&#39;}, optional
The format of the file.
comment_char : str, optional
The character used to delimit comments in the file.
Expand Down Expand Up @@ -2109,11 +2183,14 @@ <h2 id="raises">Raises</h2>
raise IndentationError(line)
return keylist[:depth]

def extract_tree_node(keylist, line, indentation):
def extract_tree_node_py(keylist, line, indentation):
key = line.strip()[1:-1]
keylist = update_keylist(keylist, line.find(key) - 1, indentation)
return keylist + [key]

def extract_tree_node_flat(line):
return line.strip()[1:-1]

def extract_tree_leaf_py(keylist, line, indentation):
if &#39;=&#39; in line:
split = line.split(&#39;=&#39;, 1)
Expand All @@ -2126,6 +2203,17 @@ <h2 id="raises">Raises</h2>
self.set(keylist + [key], value, update_tree=True)
return keylist

def extract_tree_leaf_flat(section, line):
if &#39;=&#39; in line:
split = line.split(&#39;=&#39;, 1)
key = split[0].strip()
value = split[1].strip()
else:
key = line.strip()
value = &#39;&#39;
self.set([section, key], value, update_tree=True)
return section

def extract_tree_leaf_cpp(line):
if &#39;=&#39; in line:
split = line.split(&#39;=&#39;, 1)
Expand All @@ -2142,9 +2230,17 @@ <h2 id="raises">Raises</h2>
if not line or line.isspace():
return keylist
if is_tree_node(line):
return extract_tree_node(keylist, line, indentation)
return extract_tree_node_py(keylist, line, indentation)
return extract_tree_leaf_py(keylist, line, indentation)

def read_line_flat(line, section, comment_char):
line = line.split(comment_char)[0]
if not line or line.isspace():
return section
if is_tree_node(line):
return extract_tree_node_flat(line)
return extract_tree_leaf_flat(section, line)

def read_line_cpp(line, comment_char):
line = line.split(comment_char)[0].strip()
if line and not line.isspace():
Expand Down Expand Up @@ -2186,6 +2282,10 @@ <h2 id="raises">Raises</h2>
for line in lines:
keylist = read_line_py(line, keylist, comment_char,
indentation)
elif convention == &#39;flat&#39;:
section = None
for line in lines:
section = read_line_flat(line, section, comment_char)
elif convention == &#39;cpp&#39;:
for line in lines:
read_line_cpp(line, comment_char)
Expand Down Expand Up @@ -2474,7 +2574,7 @@ <h2 id="parameters">Parameters</h2>
<dl>
<dt><strong><code>file_name</code></strong> :&ensp;<code>str</code></dt>
<dd>The name of the file to write.</dd>
<dt><strong><code>convention</code></strong> :&ensp;<code>{'py', 'cpp'}</code>, optional</dt>
<dt><strong><code>convention</code></strong> :&ensp;<code>{'py', 'flat', 'cpp'}</code>, optional</dt>
<dd>The format of the file.</dd>
<dt><strong><code>indentation</code></strong> :&ensp;<code>int</code>, optional</dt>
<dd>The indentation used to add suboptions or
Expand All @@ -2494,7 +2594,7 @@ <h2 id="parameters">Parameters</h2>
----------
file_name : str
The name of the file to write.
convention : {&#39;py&#39;, &#39;cpp&#39;}, optional
convention : {&#39;py&#39;, &#39;flat&#39;, &#39;cpp&#39;}, optional
The format of the file.
indentation : int, optional
The indentation used to add suboptions or
Expand Down Expand Up @@ -2537,6 +2637,8 @@ <h2 id="parameters">Parameters</h2>
# write lines
if convention == &#39;py&#39;:
lines = write_tree_py(self.tree, 0, indentation)
elif convention == &#39;flat&#39;:
lines = write_tree_py(self.tree, 0, 0)
elif convention == &#39;cpp&#39;:
lines = write_tree_cpp(self.tree, [])
Path(file_name).write_lines(lines)</code></pre>
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[metadata]
license_files = LICENSE
name = treeconfigparser
version = 1.0.2
version = 1.1.0
author = Alban Farchi
author_email = [email protected]
description = Custom configuration parser based on a tree
Expand Down
Loading

0 comments on commit 008b450

Please sign in to comment.