diff --git a/catalog.py b/catalog.py index 1274087..12ce4c1 100644 --- a/catalog.py +++ b/catalog.py @@ -1,8 +1,12 @@ import io +import os import tarfile import package +FILES = 'files' +PACKAGE = 'desc' + class Catalog: def __init__(self, binary): @@ -12,6 +16,17 @@ class Catalog: def load(self): f = io.BytesIO(self.binary) archive = tarfile.open(fileobj=f) + m = {} + packages = {} for member in archive.getmembers(): - print(member) + directory, *file = member.name.split(os.sep) + if file: + d = m[directory] + d[file[0]] = archive.extractfile(member).read() + if len(d) == 2: + p = package.Package(d[PACKAGE], d[FILES]) + packages[p.name] = p + else: + m[directory] = {} archive.close() + self.packages = packages diff --git a/package.py b/package.py index d85967f..2500fa9 100644 --- a/package.py +++ b/package.py @@ -1,3 +1,23 @@ +import os + +CHARSET = 'u8' +KEY = '%' +SEPARATOR = f'{os.linesep}{os.linesep}' + + class Package: - def __init__(self): - pass + def __init__(self, package, files): + p = package.decode(CHARSET).strip() + f = files.decode(CHARSET).strip() + text = f'{p}{SEPARATOR}{f}' + for item in text.split(SEPARATOR): + line, *lines = item.split(os.linesep) + key = line.split(KEY)[1].lower() + if len(lines) == 1: + value = lines[0] + else: + value = lines + setattr(self, key, value) + print() + print(key) + print(value)