| 1 | package OPTIMADE::PropertyDefinitions; | ||
| 2 | |||
| 3 | # ABSTRACT: Top-level Property Definition class | ||
| 4 | our $VERSION = '0.1.0'; # VERSION | ||
| 5 | |||
| 6 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 2 | ▶ | use OPTIMADE::PropertyDefinitions::EntryType; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use YAML qw( LoadFile ); | ||
| 12 | |||
| 13 | sub new | ||
| 14 | { | ||
| 15 | my( $class, $path, $format ) = @_; | ||
| 16 | $path .= '/' unless $path =~ /\/$/; | ||
| 17 | $format = 'yaml' unless $format; | ||
| 18 | return bless { path => $path, format => $format }, $class; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub entry_type($) | ||
| 22 | { | ||
| 23 | my( $self, $entry_type ) = @_; | ||
| 24 | die "no such entry type '$entry_type'\n" unless $self->raw->{entrytypes}{$entry_type}; | ||
| 25 | return OPTIMADE::PropertyDefinitions::EntryType->new( $self, $entry_type ); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub entry_types() | ||
| 29 | { | ||
| 30 | my( $self ) = @_; | ||
| 31 | return map { OPTIMADE::PropertyDefinitions::EntryType->new( $self, $_ ) } | ||
| 32 | sort keys %{$self->raw->{entrytypes}}; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub path() { $_[0]->{path} } | ||
| 36 | |||
| 37 | sub raw() | ||
| 38 | { | ||
| 39 | my( $self ) = @_; | ||
| 40 | $self->{raw} = $self->_raw( 'standards/optimade' ) unless $self->{raw}; | ||
| 41 | return $self->{raw}; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _raw($) | ||
| 45 | { | ||
| 46 | my( $self, $path ) = @_; | ||
| 47 | if( $self->{format} eq 'json' ) { | ||
| 48 | open my $inp, '<', $self->path . $path . '.json'; | ||
| 49 | my $json = decode_json join '', <$inp>; | ||
| 50 | close $inp; | ||
| 51 | return $json; | ||
| 52 | } elsif( $self->{format} eq 'yaml' ) { | ||
| 53 | return $self->_resolve_inherits( LoadFile( $self->path . $path . '.yaml' ) ); | ||
| 54 | } else { | ||
| 55 | die "no such format '$self->{format}'\n"; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _resolve_inherits($$) | ||
| 60 | { | ||
| 61 | my( $self, $yaml ) = @_; | ||
| 62 | |||
| 63 | if( exists $yaml->{'$$inherit'} ) { | ||
| 64 | my $parent = $self->_raw( '..' . $yaml->{'$$inherit'} ); | ||
| 65 | $yaml = { %$parent, %$yaml }; | ||
| 66 | } | ||
| 67 | |||
| 68 | for my $key (keys %$yaml) { | ||
| 69 | next unless ref $yaml->{$key} eq 'HASH'; | ||
| 70 | $yaml->{$key} = $self->_resolve_inherits( $yaml->{$key} ); | ||
| 71 | } | ||
| 72 | |||
| 73 | return $yaml; | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||