Skip to content

Commit 701213e

Browse files
committed
Add support for passing pathlib.Path objects to DockerfileParser(path=)
Add new attribute 'dockerfile' to the DockerfileParser class which exposes the path to the dockerfile as a PathLib object Add new property 'dockerfile_path' to the DockerfileParser class which exposes the 'dockerfile' attribute as a string for backwards compatibility Signed-off-by: Ethan Paul <ethan.paul@portalinstruments.com>
1 parent 3a4360f commit 701213e

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

dockerfile_parse/parser.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import json
1111
import logging
12-
import os
1312
import re
13+
from pathlib import Path
1414
from contextlib import contextmanager
1515
from shlex import quote
1616

@@ -100,18 +100,19 @@ def __init__(self, path=None,
100100
"""
101101

102102
self.fileobj = fileobj
103+
self.dockerfile = None
103104

104105
if self.fileobj is not None:
105106
if path is not None:
106107
raise ValueError("Parameters path and fileobj cannot be used together.")
107108
else:
108109
self.fileobj.seek(0)
109110
else:
110-
path = path or '.'
111-
if path.endswith(DOCKERFILE_FILENAME):
112-
self.dockerfile_path = path
111+
path = Path(path) or Path.cwd()
112+
if path.name.endswith(DOCKERFILE_FILENAME):
113+
self.dockerfile = path
113114
else:
114-
self.dockerfile_path = os.path.join(path, DOCKERFILE_FILENAME)
115+
self.dockerfile = path / DOCKERFILE_FILENAME
115116

116117
self.cache_content = cache_content
117118
self.cached_content = '' # unicode string
@@ -149,9 +150,20 @@ def _open_dockerfile(self, mode):
149150
yield self.fileobj
150151
self.fileobj.seek(0)
151152
else:
152-
with open(self.dockerfile_path, mode) as dockerfile:
153+
with self.dockerfile.open(mode) as dockerfile:
153154
yield dockerfile
154155

156+
@property
157+
def dockerfile_path(self):
158+
"""
159+
:return: Path to the Dockerfile as a string, or None if using fileobj
160+
"""
161+
return str(self.dockerfile) if self.dockerfile else None
162+
163+
@dockerfile_path.setter
164+
def dockerfile_path(self, value):
165+
self.dockerfile = Path(value) if value else None
166+
155167
@property
156168
def lines(self):
157169
"""

0 commit comments

Comments
 (0)