Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle execution from pip install and as script correctly #33

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"