Cleanup, bugfixes

This commit is contained in:
Chris 2022-10-30 23:02:14 +01:00
parent c3e651440f
commit 8d6c1800b9
3 changed files with 42 additions and 12 deletions

View File

@ -3,11 +3,12 @@
foreach ([
__DIR__."/../vendor",
__DIR__."/../../../vendor"
__DIR__."/../../../vendor",
__DIR__."/vendor",
] as $dir) {
if (file_exists($dir."/autoload.php")) {
define("COMPOSER_VENDOR_PATH", $dir);
require_once $dir."/autoload.php";
echo $dir."\n";
}
}
@ -16,5 +17,13 @@ $datasetManager = new NoccyLabs\Dataset\DatasetManager();
$datasets = $datasetManager->getAvailableDatasets();
foreach ($datasets as $dataset) {
echo $dataset->getIdentifier()."\n";
echo $dataset->getIdentifier()." (".$dataset->getVersion().")\n";
$reader = $dataset->open();
$rows = 0;
$headers = null;
foreach ($reader as $row) {
if (!$headers) $headers = array_keys($row);
$rows++;
}
echo " ".$rows." rows\n - ".join("\n - ",$headers)."\n";
}

View File

@ -11,10 +11,13 @@ class Dataset
protected array $options;
public function __construct(string $identifier, array $options)
protected ?string $version;
public function __construct(string $identifier, array $options, ?string $version=null)
{
$this->identifier = $identifier;
$this->options = $options;
$this->version = $version;
}
public function getIdentifier(): string
@ -22,6 +25,11 @@ class Dataset
return $this->identifier;
}
public function getVersion(): ?string
{
return $this->version;
}
public function open(): ReaderInterface
{
$filename = $this->options['filename'];

View File

@ -11,9 +11,9 @@ namespace NoccyLabs\Dataset;
*/
class DatasetManager
{
private static array $packageVersions = [];
private static $datasets = [];
private static array $datasets = [];
public function __construct()
{
@ -47,6 +47,15 @@ class DatasetManager
$glob = glob($root."/*/*/dataset.json");
self::$packageVersions = [];
$fn = realpath($root."/composer/installed.php");
if (file_exists($fn)) {
$versions = include $fn;
foreach ($versions['versions'] as $name=>$version) {
self::$packageVersions[$name] = $version['version'];
}
}
foreach ($glob as $match) {
$path = dirname($match);
$package = basename(dirname($path))."/".basename($path);
@ -81,20 +90,24 @@ class DatasetManager
private function loadDatasets(array $datasets, ?string $prefix, string $package, string $path)
{
foreach ($datasets as $name=>$info) {
if (!array_key_exists('filename', $info)) {
$this->loadDatasets($info, ltrim($prefix . "." . $name, "."), $package, $path);
foreach ($datasets as $name=>$options) {
if (!array_key_exists('filename', $options)) {
$this->loadDatasets($options, ltrim($prefix . "." . $name, "."), $package, $path);
return;
}
$info['filename'] = $path . "/" . $info['filename'];
$options['filename'] = $path . "/" . $options['filename'];
$pn = sprintf("%s#%s", $package, ltrim($prefix.".".$name,"."));
$ds = new Dataset($pn, $info);
$pv = self::$packageVersions[$package]??null;
$ds = new Dataset($pn, $options, $pv);
$this->registerDataset($ds);
}
}
private function determineVendorPath(): ?string
{
if (defined("COMPOSER_VENDOR_PATH")) {
return COMPOSER_VENDOR_PATH;
}
if (file_exists(__DIR__."/../../../autoload.php")) {
// we are installed as a composer package
return dirname(__DIR__, 3);
@ -127,4 +140,4 @@ class DatasetManager
return self::$datasets[$identifier];
}
}
}