Skip to content

Commit

Permalink
fix: Handle execution from pip install and as script correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jimisola committed Feb 5, 2024
1 parent e15783c commit 588480f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
22 changes: 13 additions & 9 deletions src/maven_artifact/main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#!/bin/env python

import argparse

import os
import sys
import textwrap
from importlib.metadata import version
from maven_artifact.artifact import Artifact

try:
from maven_artifact.utils import Utils
except ImportError:
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
if __package__ is None:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from maven_artifact.utils import Utils

Utils.is_installed_package = False

import argparse
import textwrap

from maven_artifact.artifact import Artifact
from maven_artifact.utils import Utils
from maven_artifact.requestor import RequestException
from maven_artifact.downloader import Downloader

Expand Down Expand Up @@ -57,13 +59,15 @@ def _split_lines(self, text, width):


class MainCommand:

def _get_arguments(self):
parser = argparse.ArgumentParser(formatter_class=WrappedNewlineFormatter, epilog=__epilog__)

parser.add_argument(
"-V",
"--version",
action="version",
version=version("maven-artifact"),
version=f"{Utils.get_version()}",
)

parser.add_argument(
Expand Down
5 changes: 2 additions & 3 deletions src/maven_artifact/requestor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import base64
import requests
from importlib.metadata import version

from maven_artifact.utils import Utils

Expand All @@ -16,12 +15,12 @@ def __init__(
username=None,
password=None,
token=None,
user_agent=f"Maven Artifact Downloader/{version('maven-artifact')}",
user_agent=f"Maven Artifact Downloader/{Utils.get_version()}",
):
self.user_agent = user_agent
self.username = username
self.password = password
self.token = token
self.user_agent = user_agent

def request(self, url, onFail, onSuccess=None, method: str = "get", **kwargs):
headers = {"User-Agent": self.user_agent}
Expand Down
7 changes: 7 additions & 0 deletions src/maven_artifact/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import base64
from importlib.metadata import version


class Utils:
is_installed_package = True

@staticmethod
def is_base64(sb):
try:
Expand All @@ -15,3 +18,7 @@ def is_base64(sb):
return base64.b64encode(base64.b64decode(sb_bytes)) == sb_bytes
except Exception:
return False

@staticmethod
def get_version():
return f"{version('maven-artifact')}" if Utils.is_installed_package else "non-pip-package"

0 comments on commit 588480f

Please sign in to comment.