Last active
January 15, 2017 03:33
-
-
Save joshland/e89347f88d128a971768c2a711dfa5dc to your computer and use it in GitHub Desktop.
i needed a tool to identify non-wrapping columns from text commands with variable fixed witdth output. (e.g. lsblk)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
__author__ = 'Joshua M. Schmidlkofer <joshland@protonmail.com>' | |
__credits__ = ["Joshua M. Schmidlkofer"] | |
__status__ = "Development" | |
__url__ = "https://gist.github.com/joshland/e89347f88d128a971768c2a711dfa5dc" | |
from collections import OrderedDict | |
import re | |
def findfields(header): | |
""" | |
guess at variable fixed-width columns, based upon header. | |
orientation is either 'left' or 'right' | |
""" | |
fields = OrderedDict() | |
colName = None | |
colStart = -1 | |
colEnd = -1 | |
index = 0 | |
while 1: | |
s = re.search(r'\w+', header[index:]) | |
if s: # we found something | |
start, end = s.span() ## relative start/stop | |
sub = header[index:] ## relative stringlet | |
if colName: ## if we have an old col, save it now | |
colEnd = index + start | |
fields[colName] = (colStart, colEnd) | |
pass | |
colName = sub[start:end] ## new column name | |
colStart = index + start ## set new start | |
colEnd = -1 ## reset the end | |
if end + index == len(header): | |
## We reached the end | |
fields[colName] = (colStart, -1) | |
break | |
index = index + end | |
else: | |
print("hmm Somethign went wrong") | |
break | |
continue | |
return fields |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment