Skip to content

Commit

Permalink
Merge pull request #393 from harunurhan/seminar-builder
Browse files Browse the repository at this point in the history
seminars: add builder
  • Loading branch information
harunurhan committed May 5, 2020
2 parents 086c8bb + a2c2499 commit 7301b5c
Show file tree
Hide file tree
Showing 7 changed files with 857 additions and 15 deletions.
1 change: 1 addition & 0 deletions inspire_schemas/builders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@
from .literature import LiteratureBuilder # noqa
from .references import ReferenceBuilder # noqa
from .signatures import SignatureBuilder # noqa
from .seminars import SeminarBuilder # noqa
3 changes: 2 additions & 1 deletion inspire_schemas/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ def _sourced_dict(self, source=None, **kwargs):
kwargs['source'] = source
elif self.source:
kwargs['source'] = self.source
return kwargs

return {key: value for key, value in kwargs.items() if value not in EMPTIES}
16 changes: 5 additions & 11 deletions inspire_schemas/builders/conferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ def add_alternative_title(self, title, subtitle=None, source=None):
title_entry = self._sourced_dict(
source,
title=title,
subtitle=subtitle
)
if subtitle is not None:
title_entry['subtitle'] = subtitle

self._append_to('alternative_titles', title_entry)

Expand Down Expand Up @@ -200,10 +199,7 @@ def add_keyword(self, value, schema=None, source=None):
schema (str): schema to which the keyword belongs.
source (str): source for the keyword.
"""
keyword_dict = self._sourced_dict(source, value=value)

if schema is not None:
keyword_dict['schema'] = schema
keyword_dict = self._sourced_dict(source, value=value, schema=schema)

self._append_to('keywords', keyword_dict)

Expand All @@ -228,9 +224,8 @@ def add_series(self, name, number=None):
name (str): name of the conference series.
number (int): number of the conference series.
"""
serie_object = self._sourced_dict(name=name)
if number:
serie_object['number'] = number
serie_object = self._sourced_dict(name=name, number=number)

self._append_to('series', serie_object)

@filter_empty_parameters
Expand All @@ -245,9 +240,8 @@ def add_title(self, title, subtitle=None, source=None):
title_entry = self._sourced_dict(
source,
title=title,
subtitle=subtitle,
)
if subtitle is not None:
title_entry['subtitle'] = subtitle

self._append_to('titles', title_entry)

Expand Down
269 changes: 269 additions & 0 deletions inspire_schemas/builders/seminars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
# -*- coding: utf-8 -*-
#
# This file is part of INSPIRE-SCHEMAS.
# Copyright (C) 2020 CERN.
#
# INSPIRE-SCHEMAS is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# INSPIRE-SCHEMAS is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with INSPIRE-SCHEMAS; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Conferences builder class and related code."""

from __future__ import absolute_import, division, print_function

import six

from inspire_utils.name import normalize_name
from inspire_schemas.builders.builder import RecordBuilder
from inspire_schemas.utils import (
filter_empty_parameters,
sanitize_html,
validate,
)
from inspire_utils.date import normalize_date


class SeminarBuilder(RecordBuilder):
"""Seminar record builder."""

_collections = ['Seminars']

@staticmethod
def _prepare_url(value, description=None):
"""Build url dict satysfying url.yml requirements
Args:
value (str): URL itself
description (str): URL description
"""
entry = {
'value': value
}
if description:
entry['description'] = description
return entry

def validate_record(self):
"""Validate the record in according to the hep schema."""
validate(self.record, 'seminars')

@filter_empty_parameters
def set_address(
self,
cities=None,
country_code=None,
latitude=None,
longitude=None,
place_name=None,
postal_address=None,
postal_code=None,
state=None,
):
"""
Args:
cities (list): list of strings containing cities.
country_code (str): string of length 2 representing the country.
latitude (float): latitude of the location.
longitude (float): longitude of the location.
place_name (str): name of the specific place where this is located.
postal_address (str): full postal address in original language.
postal_code (str): postal code of the location.
state (str): state or province of the location.
"""
address = {}

if cities:
address['cities'] = cities
if country_code:
address['country_code'] = country_code
if latitude:
address['latitude'] = latitude
if longitude:
address['longitude'] = longitude
if place_name:
address['place_name'] = place_name
if postal_address:
address['postal_address'] = postal_address
if postal_code:
address['postal_code'] = postal_code
if state:
address['state'] = state

self.record['address'] = address

@filter_empty_parameters
def add_contact(self, name=None, email=None, curated_relation=None, record=None):
"""Add a contact object to the list of ``contact_details``.
Args:
name (str): name of the contact.
email (str): email to the contact.
curated_relation (bool): mark if relation is curated [NOT REQUIRED]
record (dict): dictionary with ``$ref`` pointing to proper record.
If string, then will be converted to proper dict.
"""
self._append_to(
'contact_details',
name=name,
email=email,
curated_relation=curated_relation,
record=record,
)

@filter_empty_parameters
def add_speaker(self, name=None, record=None, affiliations=None, ids=None, curated_relation=None):
"""
Args:
name (str): name of the contact.
record (dict): dictionary with ``$ref`` pointing to proper record.
If string, then will be converted to proper dict.
affiliations (list): list of affiliations objects
"""
self._append_to(
'speakers',
name=normalize_name(name),
record=record,
affiliations=affiliations,
ids=ids,
curated_relation=curated_relation,
)

@filter_empty_parameters
def add_join_url(self, value, description=None):
"""Add url dict to ``urls`` list.
Args:
value (str): url itself.
description (str): description of the url.
"""
entry = self._prepare_url(value, description)
self._append_to('join_urls', entry)

@filter_empty_parameters
def add_inspire_categories(self, subject_terms, source=None):
"""Add inspire categories.
Args:
subject_terms (list): user categories for the current document.
source (str): source for the given categories.
"""
for category in subject_terms:
category_dict = self._sourced_dict(
source,
term=category,
)
self._append_to('inspire_categories', category_dict)

@filter_empty_parameters
def add_keyword(self, value, schema=None, source=None):
"""Add a keyword.
Args:
keyword (str): keyword to add.
schema (str): schema to which the keyword belongs.
source (str): source for the keyword.
"""
keyword_dict = self._sourced_dict(source, value=value, schema=schema)

self._append_to('keywords', keyword_dict)

@filter_empty_parameters
def add_public_note(self, value, source=None):
"""Add public note.
Args:
value (str): public note for the current article.
source (str): source for the given notes.
"""
self._append_to('public_notes', self._sourced_dict(
source,
value=value,
))

@filter_empty_parameters
def add_series(self, name, number=None):
"""Add conference series.
Args:
name (str): name of the conference series.
number (int): number of the conference series.
"""
serie_object = self._sourced_dict(name=name, number=number)
self._append_to('series', serie_object)

@filter_empty_parameters
def set_title(self, title, subtitle=None, source=None):
"""Sets the title of this seminar.
Args:
title (str): title for the current document.
subtitle (str): subtitle for the current document.
source (str): source for the given title.
"""
title_dict = self._sourced_dict(
source,
title=title,
subtitle=subtitle
)
self.record['title'] = title_dict

@filter_empty_parameters
def add_url(self, value, description=None):
"""Add url dict to ``urls`` list.
Args:
value (str): url itself.
description (str): description of the url.
"""
entry = self._prepare_url(value, description)
self._append_to('urls', entry)

def set_end_datetime(self, date=None):
"""
Args:
date (str)
"""
if date is not None:
self.record['end_datetime'] = date

def set_start_datetime(self, date=None):
"""
Args:
date (str)
"""
if date is not None:
self.record['start_datetime'] = date

def set_abstract(self, value, source=None):
"""
Args:
value (str): the description to set.
source (str): source of the description.
"""
self.record['abstract'] = self._sourced_dict(
source=source,
value=sanitize_html(value)
)

def set_timezone(self, timezone=None):
"""
Args:
date (str)
"""
if timezone is not None:
self.record['timezone'] = timezone
4 changes: 2 additions & 2 deletions tests/integration/fixtures/seminars_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
"value": "HEPNAMES-5578139057"
}
],
"name": "n,#hs",
"name": "Urhan, Harun",
"record": {
"$ref": "http://I9I80ju"
}
Expand All @@ -244,7 +244,7 @@
"value": "HEPNAMES-591413785"
}
],
"name": "O/N>_=j,ZR(uIs{",
"name": "Urhan, Ahmet",
"record": {
"$ref": "http://Z2S"
}
Expand Down
Loading

0 comments on commit 7301b5c

Please sign in to comment.