Skip to content

Commit

Permalink
adding support for getting deps
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed Mar 14, 2022
1 parent f5bbfc5 commit 649f1c2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 16 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,19 @@ And from the command line:
$ citelang package pypi requests
```

Or with a version:
### Dependencies

You can ask to see package dependencies:


```bash
$ citelang deps pypi requests
```

If you don't provide a version, the latest will be used (retrieved from the package).

```bash
$ citelang package pypi requests@2.27.1
$ citelang deps pypi requests@2.27.1
```


Expand Down Expand Up @@ -173,7 +182,7 @@ Are you sure you want to clear the cache? yes

## TODO

- add support for version
- add support for a citation or credit tree
- citelang needs tests
- create documentation, settings table

Expand Down
26 changes: 16 additions & 10 deletions citelang/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,24 @@ def get_parser():
"--clear", help="clear the cache", default=False, action="store_true"
)

# Get a package
# Get a package or dependencies
pkg = subparsers.add_parser(
"package",
description="list package managers available to derive citations from.",
)
pkg.add_argument("package", help="package manager and name to parse", nargs=2)
pkg.add_argument(
"--json",
dest="json",
help="print output to json.",
default=False,
action="store_true",
)
pkg.add_argument("--outfile", "-o", help="Save to an output json file.")
deps = subparsers.add_parser("deps", description="list dependencies for a package.")
for command in [pkg, deps]:
command.add_argument(
"package", help="package manager and name to parse", nargs=2
)
command.add_argument(
"--json",
dest="json",
help="print output to json.",
default=False,
action="store_true",
)
command.add_argument("--outfile", "-o", help="Save to an output json file.")

# Local shell with client loaded
shell = subparsers.add_parser(
Expand Down Expand Up @@ -185,6 +189,8 @@ def help(return_code=0):

if args.command == "cache":
from .cache import main
elif args.command == "deps":
from .deps import main
elif args.command == "config":
from .config import main
elif args.command == "shell":
Expand Down
22 changes: 22 additions & 0 deletions citelang/client/deps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
__author__ = "Vanessa Sochat"
__copyright__ = "Copyright 2022, Vanessa Sochat"
__license__ = "MPL 2.0"

from citelang.main import Client


def main(args, parser, extra, subparser):

cli = Client(quiet=args.quiet, settings_file=args.settings_file)
result = cli.dependencies(
name=args.package[1], manager=args.package[0], use_cache=not args.no_cache
)

if args.json and not args.outfile:
result.print_json()

elif args.outfile:
result.save(args.outfile)

else:
result.table()
1 change: 0 additions & 1 deletion citelang/client/listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def main(args, parser, extra, subparser):
cli = Client(quiet=args.quiet, settings_file=args.settings_file)
result = cli.package_managers(use_cache=not args.no_cache)

# Default limit is 25, unless --all provided
if args.json and not args.outfile:
result.print_json()

Expand Down
30 changes: 29 additions & 1 deletion citelang/main/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,39 @@ def package_managers(self, use_cache=True):
"""
return self.get_endpoint("package_managers", use_cache=use_cache)

def dependencies(self, manager, name, use_cache=True):
"""
Get dependencies for a package. If no version, use latest.
"""
# First try getting version from package name
version = None
if "@" in name:
name, version = name.split("@", 1)

if not version:
package = self.package(manager, name, use_cache)
if "versions" in package.data and package.data["versions"]:
version = package.data["versions"][-1]["number"]
if not version:
logger.exit(
f"Cannot automatically derive version, please provide {name}@<version>"
)
return self.get_endpoint(
"dependencies",
use_cache=use_cache,
manager=manager,
package_name=name,
version=version,
)

def package(self, manager, name, use_cache=True):
"""
Lookup a package in a specific package manager
"""
# TODO need another endpoint / thing for version
if "@" in name:
logger.warning("This function does not require a package version.")
name, _ = name.split("@", 1)

# Ensure we know the manager before
# Store package in cache based on manager and name
cache_name = f"package/{manager}/{name}"
Expand Down
29 changes: 28 additions & 1 deletion citelang/main/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__license__ = "MPL 2.0"

import citelang.defaults as defaults
from datetime import datetime
import sys

# Registered endpoints (populated on init)
Expand Down Expand Up @@ -78,6 +79,18 @@ class PackageManagers(Endpoint):
skips = ["color"]


class Dependencies(Endpoint):
name = "dependencies"
path = "/api/{manager}/{package_name}/{version}/dependencies"
format_url = ["manager", "package_name", "version"]
emoji = "arrow"
skips = ["normalized_licenses"]
dont_truncate = ["project_name"]

def table_data(self, data):
return data.get("dependencies")


class Package(Endpoint):
name = "package"
path = "/api/{manager}/{package_name}"
Expand All @@ -90,6 +103,20 @@ class Package(Endpoint):
"original_license",
]

def order(self, data):
"""
Order versions by published at
"""
versions = data.get("versions", [])
if versions:
data["versions"] = sorted(
versions,
key=lambda x: datetime.strptime(
x["published_at"].split("T", 1)[0], "%Y-%m-%d"
),
)
return data

def table_data(self, data):
# package should return a list of versions
return data.get("versions")
Expand All @@ -99,7 +126,7 @@ def title(self):
return "Package " + self.params.get("package_name", "")


for endpoint in [PackageManagers, Package]:
for endpoint in [PackageManagers, Package, Dependencies]:
registry_names.append(endpoint.name)
registry[endpoint.name] = endpoint

Expand Down

0 comments on commit 649f1c2

Please sign in to comment.