0

This is the current code and I am trying to wrap my head around an alternative:

Presently we have:

URL_PREFIX = "http://ourrepo:8081/artifactory"
pattern = re.compile(r'^.*-(ngwebui|nodeservice).*$')
if pattern.match(artifact):
    return URL_PREFIX + "/npm-local/region/%s/-/region/%s-%s" % (artifact, artifact, version)
else:
    return URL_PREFIX + "/libs-releases-local/org/region/%s/%s/%s-%s" % (artifact, version, artifact, version)

What I want to do is incorporate another type called "dockerservice" into this with a URL_PREFIX value via of

URL_PREFIX + "/docker-dev-local/%s-%s" % (artifact, artifact, version)

What would be the simplest way to retain the last catch all with an if ngwebui|nodeservice URL_PREFIX, dockerservice URL_PREFIX, else URL_PREFIX:?

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

1

A straight forward way to do it would be to use a second condition:

URL_PREFIX = "http://ourrepo:8081/artifactory"
nodePattern = re.compile(r'^.*-(ngwebui|nodeservice).*$')
dockerPattern = re.compile(r'patternForDocker')

if nodePattern.match(artifact):
    return URL_PREFIX + "/npm-local/region/%s/-/region/%s-%s" % (artifact, artifact, version)
elif dockerPattern.match(artifact):
    return URL_PREFIX + "docker/path/..."
else:
    return URL_PREFIX + "/libs-releases-local/org/region/%s/%s/%s-%s" % (artifact, version, artifact, version)

But a more scalable way to do it would be to create a map of pattern and path:

URL_PREFIX = "http://ourrepo:8081/artifactory"
PATHS = {
  '^.*-(ngwebui|nodeservice).*$': "/npm-local/region/%s/-/region/%s-%s" % (artifact, artifact, version),
  '^patternForDocker$': "docker/path/...",
  # other pairs
}

for pattern, path in PATHS.items():
  compiled = re.compile(pattern)
  if compiled.match(artifact):
    return URL_PREFIX + path

return URL_PREFIX + "/libs-releases-local/org/region/%s/%s/%s-%s" % (artifact, version, artifact, version)
martineau
  • 119,623
  • 25
  • 170
  • 301
Aziz Sonawalla
  • 2,482
  • 1
  • 5
  • 6