Fix major stuff/moved stuff to docker_parser.py

This commit is contained in:
--global 2022-12-15 09:20:24 -06:00
parent aa12747469
commit 0ecf09e35b
3 changed files with 42 additions and 39 deletions

View file

@ -33,4 +33,6 @@ Notes:
## Status
- `composer.py` and `re_compose` are finished
- Haven't started on `basic_management.py`
- Redoing stuff as a new class `docker_parser.py`
- ps() is done
- container_info() still a WIP

View file

@ -3,44 +3,7 @@ from subprocess import getoutput
# Goals: Loop: Select container (or exit), then menu to do stuff to container (start, stop, rm, list info, go back to main menu)
def docker_info():
"""
Gets info about all the Docker containers
:return: Nested dict of containers info
"""
raw_info = getoutput('docker ps')
# Header: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces)
header = raw_info[:raw_info.find('\n')+1]
header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'), \
'COMMAND': header.find('COMMAND'), 'CREATED': header.find('CREATED'), 'STATUS': header.find('STATUS'), \
'PORTS': header.find('PORTS'), 'NAMES': header.find('NAMES')}
# Remove header (example above)
raw_info = raw_info[raw_info.find('\n')+1:]
info = {}
# Find container names
containers = []
for line in raw_info.split('\n'):
containers.append(line.strip()[line.strip().rfind(' ')+1:])
# Fill in info
for i in range(len(containers)):
info[containers[i]] = {}
header_indices_keys = list(header_indices)
for j in range(len(header_indices_keys)): # TODO: Fix
start_i = header_indices[header_indices_keys[j]]
if j+1 != len(header_indices):
end_i = header_indices[header_indices_keys[j+1]]
else:
end_i = len(header)
info[containers[i]][header_indices[header_indices_keys[j]]] = \
raw_info.split('\n')[i][start_i:end_i].strip()
return info
if '\n' not in getoutput('docker ps'):
print('No containers running')
exit()
docker_info()
ps()

View file

@ -5,6 +5,44 @@ class NoContainersError(Exception):
pass
class DockerParser:
def ps(raw_info='docker ps'):
"""
Gets info about all running Docker containers from docker ps
:return: Nested dict of containers info
"""
if '\n' not in raw_info:
raise(NoContainersError('A Docker container is required to run this program. Please create a docker container and try again.'))
raw_info = getoutput('docker ps')
# Header: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces)
header = raw_info[:raw_info.find('\n')+1]
header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'),
'COMMAND': header.find('COMMAND'), 'CREATED': header.find('CREATED'), 'STATUS': header.find('STATUS'),
'PORTS': header.find('PORTS'), 'NAMES': header.find('NAMES')}
# Remove header (example above)
raw_info = raw_info[raw_info.find('\n')+1:]
info = {}
# Find container names
containers = []
for line in raw_info.split('\n'):
containers.append(line.strip()[line.strip().rfind(' ')+1:])
# Fill in info
for i in range(len(containers)):
info[containers[i]] = {}
header_indices_keys = list(header_indices)
for j in range(len(header_indices_keys)): # TODO: Fix
start_i = header_indices[header_indices_keys[j]]
if j+1 != len(header_indices):
end_i = header_indices[header_indices_keys[j+1]]
else:
end_i = len(header)
info[containers[i]][header_indices[header_indices_keys[j]]] = \
raw_info.split('\n')[i][start_i:end_i].strip()
return info
def containers_info(raw_info=getoutput('docker container list')):
"""
Gets info about all the Docker containers