1
0
mirror of https://github.com/Foltik/dotfiles synced 2024-11-28 05:27:06 -05:00
dotfiles/scripts/install/parse.py

35 lines
948 B
Python
Raw Normal View History

2018-10-15 18:21:55 -04:00
#!/usr/bin/python
2018-10-16 22:02:11 -04:00
import re
2018-10-15 18:21:55 -04:00
class Package:
def __init__(self, line):
self.type = line[0]
self.name = line[1]
self.script = line[2] if len(line) == 3 else None
2018-10-16 22:02:11 -04:00
@staticmethod
def is_package_line(line):
return re.match(r"^[^*]+", line[0])
2018-10-15 18:21:55 -04:00
class Category:
def __init__(self, line):
self.name = ' '.join(line[1:])
self.packages = []
2018-10-16 22:02:11 -04:00
@staticmethod
def is_category_line(line):
return re.match(r"^\*+", line[0])
def split(lines):
return list(map(lambda line: line.rstrip().split(' '), lines))
2018-10-15 18:21:55 -04:00
def parse_package_listing(file):
f = open(file, 'r')
categories = []
category = None
2018-10-16 22:02:11 -04:00
for line in split(f.readlines()):
if Category.is_category_line(line):
2018-10-15 18:21:55 -04:00
category = Category(line)
categories.append(category)
2018-10-16 22:02:11 -04:00
elif Package.is_package_line(line):
2018-10-15 18:21:55 -04:00
category.packages.append(Package(line))
return categories