[iniparse] r140 committed - added the missing new iniparse/utils.py

0 views
Skip to first unread message

inip...@googlecode.com

unread,
May 8, 2010, 6:49:11 AM5/8/10
to iniparse...@googlegroups.com
Revision: 140
Author: tim.lauridsen
Date: Sat May 8 03:48:34 2010
Log: added the missing new iniparse/utils.py
http://code.google.com/p/iniparse/source/detail?r=140

Added:
/trunk/iniparse/utils.py

=======================================
--- /dev/null
+++ /trunk/iniparse/utils.py Sat May 8 03:48:34 2010
@@ -0,0 +1,47 @@
+import compat
+from ini import LineContainer, EmptyLine
+
+def tidy(cfg):
+ """Clean up blank lines.
+
+ This functions makes the configuration look clean and
+ handwritten - consecutive empty lines and empty lines at
+ the start of the file are removed, and one is guaranteed
+ to be at the end of the file.
+ """
+
+ if isinstance(cfg, compat.RawConfigParser):
+ cfg = cfg.data
+ cont = cfg._data.contents
+ i = 1
+ while i < len(cont):
+ if isinstance(cont[i], LineContainer):
+ tidy_section(cont[i])
+ i += 1
+ elif (isinstance(cont[i-1], EmptyLine) and
+ isinstance(cont[i], EmptyLine)):
+ del cont[i]
+ else:
+ i += 1
+
+ # Remove empty first line
+ if cont and isinstance(cont[0], EmptyLine):
+ del cont[0]
+
+ # Ensure a last line
+ if cont and not isinstance(cont[-1], EmptyLine):
+ cont.append(EmptyLine())
+
+def tidy_section(lc):
+ cont = lc.contents
+ i = 1
+ while i < len(cont):
+ if (isinstance(cont[i-1], EmptyLine) and
+ isinstance(cont[i], EmptyLine)):
+ del cont[i]
+ else:
+ i += 1
+
+ # Remove empty first line
+ if len(cont) > 1 and isinstance(cont[1], EmptyLine):
+ del cont[1]

--
To post to this group, send email to iniparse...@googlegroups.com
To unsubscribe from this group, send email to iniparse-commi...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/iniparse-commits?hl=en
Reply all
Reply to author
Forward
0 new messages