"""Definitions of the selector(s) """ from gluon import current from gluon.validators import (IS_IN_DB, IS_LENGTH, IS_INT_IN_RANGE, IS_IN_SET, IS_MATCH) from .harvest_tools import DRY_RUN from .model_harvester import CONTROLLERS, STORES from pydal import Field FORMATS = ["bib", "html", "odt", "pdf", "tex"] MODE_CHANGE_STATUS = "change status" MODE_LOAD_IN_DB = "load in the database" MONTH_SUBMIT = "month of submission" YEAR_PUBLISH = "year of publication" YEAR_SUBMIT = "year of submission" class Selector(object): """Create virtual tables used to generate selector interfaces. """ @staticmethod def define_tables(virtdb, db, T): """Define tables use for selectors. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): database connection T (gluon.languages.translator): language translator """ Selector.authors_selector(virtdb, db, T) Selector.check_selector(virtdb, db, T) Selector.edit_insert_selector(virtdb, db, T) Selector.free_harvester_selector(virtdb, db, T) Selector.graph_selector(virtdb, db, T) Selector.harvester_selector(virtdb, db, T) Selector.list_selector(virtdb, db, T) Selector.marc12_selector(virtdb, db, T) Selector.metric_selector(virtdb, db, T) Selector.run_all_harvesters_selector(virtdb, db, T) @staticmethod def authors_selector(virtdb, db, T): """authors selector. Note: selector table are store in a virtual database. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): connection to the database of the application T (gluon.languages.translator): language translator Returns: pyDAL.Table """ tp_authors = \ T("String containing the author names with their " "institutes number.") tp_affiliation = \ T("Institute number associated to CPPM authors") table = virtdb.define_table( "authors_selector", Field("authors", "text", comment=tp_authors, requires=IS_LENGTH(65535)), Field("affiliation", "string", comment=tp_affiliation)) return table @staticmethod def check_selector(virtdb, db, T): """check selector. Note: selector table are store in a virtual database. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): connection to the database of the application T (gluon.languages.translator): language translator Returns: pyDAL.Table """ mode_dry_run = T(DRY_RUN) table = virtdb.define_table( "check_selector", Field("year", "integer", default=current.request.now.year), Field("id_teams", "reference teams", label="Team"), Field("id_projects", "reference projects", label="Project"), Field("id_categories", "reference categories", label="Category"), Field("mode", "string", default=mode_dry_run), Field("format", "string", default="html", label="Report format")) table.id_categories.requires = IS_IN_DB(db, "categories.code") table.format.requires = IS_IN_SET(["html", "pdf", "tex"]) table.id_projects.requires = IS_IN_DB(db, "projects.project") table.id_teams.requires = IS_IN_DB(db, "teams.team") table.mode.requires = \ IS_IN_SET([mode_dry_run, T(MODE_CHANGE_STATUS)]) return table @staticmethod def edit_insert_selector(virtdb, db, T): """edit and insert selector. Note: selector table are store in a virtual database. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): connection to the database of the application T (gluon.languages.translator): language translator Returns: pyDAL.Table """ tp_record_id = T("The identifier of the record in the invenio store") table = virtdb.define_table( "edit_insert_selector", Field("id_teams", "reference teams", label="Team"), Field("id_projects", "reference projects", label="Project"), Field("host", "string", default=STORES[0], label="Store"), Field("record_id", "integer", comment=tp_record_id), Field("controller", "string", label="Automaton"), Field("id_categories", "reference categories", label="Category")) table.controller.requires = IS_IN_SET(CONTROLLERS) table.host.requires = IS_IN_SET(STORES) table.id_categories.requires = IS_IN_DB(db, "categories.code") table.id_projects.requires = IS_IN_DB(db, "projects.project") table.id_teams.requires = IS_IN_DB(db, "teams.team") return table @staticmethod def free_harvester_selector(virtdb, db, T): """free harvester selector. Note: selector table are store in a virtual database. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): connection to the database of the application T (gluon.languages.translator): language translator Returns: pyDAL.Table """ mode_dry_run = T(DRY_RUN) year = current.request.now.year table = virtdb.define_table( "free_harvester_selector", Field("year_start", "integer", default=year), Field("year_end", "integer"), Field("id_teams", "reference teams", label="Team"), Field("id_projects", "reference projects", label="Project"), Field("host", "string", default=STORES[1], label="Store"), Field("collections", "string", default="find ..."), Field("ratio", "double", default=1.0), Field("controller", "string", label="Automaton"), Field("id_categories", "reference categories", label="Category"), Field("mode", "string", default=mode_dry_run)) table.host.requires = IS_IN_SET(STORES) table.mode.requires = IS_IN_SET([mode_dry_run, MODE_LOAD_IN_DB]) table.id_categories.requires = IS_IN_DB(db, "categories.code") table.id_projects.requires = IS_IN_DB(db, "projects.project") table.id_teams.requires = IS_IN_DB(db, "teams.team") harvester_start_year = current.app.havester_start_year if harvester_start_year: table.year_start.requires = \ IS_INT_IN_RANGE(harvester_start_year, year) table.year_end.requires = \ IS_INT_IN_RANGE(harvester_start_year, year) return table @staticmethod def graph_selector(virtdb, db, T): """graph selector. Note: selector table are store in a virtual database. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): connection to the database of the application T (gluon.languages.translator): language translator Returns: pyDAL.Table """ year = current.request.now.year month_submit = T(MONTH_SUBMIT) table = virtdb.define_table( "graph_selector", Field("year_start", "integer", default=year), Field("year_end", "integer"), Field("id_teams", "reference teams", label="Team"), Field("id_projects", "reference projects", label="Project"), Field("id_categories", "reference categories", label="Category"), Field("author", "string"), Field("id_authors_roles", "reference authors_roles", label="Role"), Field("id_graphs", "reference graphs", label="Graph"), Field("cumulative", "boolean", default=True), Field("time", "string", default=month_submit), Field("format", "string", default="html", label="Format")) table.id_authors_roles.requires = IS_IN_DB(db, "authors_roles.role") table.id_categories.requires = IS_IN_DB(db, "categories.code") table.id_graphs.requires = IS_IN_DB(db, "graphs.graph") table.id_projects.requires = IS_IN_DB(db, "projects.project") table.id_teams.requires = IS_IN_DB(db, "teams.team") table.format.requires = IS_IN_SET(["html", "pdf", "png"]) table.time.requires = \ IS_IN_SET([month_submit, T(YEAR_SUBMIT), T(YEAR_PUBLISH)]) return table @staticmethod def harvester_selector(virtdb, db, T): """harvester selector. Note: selector table are store in a virtual database. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): connection to the database of the application T (gluon.languages.translator): language translator Returns: pyDAL.Table """ mode_dry_run = T(DRY_RUN) year = current.request.now.year table = virtdb.define_table( "harvester_selector", Field("year_start", "integer", default=year), Field("year_end", "integer"), Field("id_teams", "reference teams", label="Team"), Field("id_projects", "reference projects", label="Project"), Field("controller", "string", label="Automaton"), Field("mode", "string", default=mode_dry_run), Field("format", "string", default="html", label="Report format")) table.format.requires = IS_IN_SET(["html", "pdf", "tex"]) table.mode.requires = IS_IN_SET([mode_dry_run, T(MODE_LOAD_IN_DB)]) table.id_projects.requires = IS_IN_DB(db, "projects.project") table.id_teams.requires = IS_IN_DB(db, "teams.team") harvester_start_year = current.app.havester_start_year if harvester_start_year: table.year_start.requires = \ IS_INT_IN_RANGE(harvester_start_year, year) table.year_end.requires = \ IS_INT_IN_RANGE(harvester_start_year, year) return table @staticmethod def list_selector(virtdb, db, T): """list selector. Note: selector table are store in a virtual database. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): connection to the database of the application T (gluon.languages.translator): language translator Returns: pyDAL.Table """ year = current.request.now.year table = virtdb.define_table( "list_selector", Field("year_start", "integer", default=year), Field("year_end", "integer"), Field("id_teams", "reference teams", label="Team"), Field("id_projects", "reference projects", label="Project"), Field("id_categories", "reference categories", label="Category"), Field("author", "string"), Field("id_authors_roles", "reference authors_roles", label="Role"), Field("id_lists", "reference lists", label="List"), Field("format", "string", default="html")) table.id_authors_roles.requires = IS_IN_DB(db, "authors_roles.role") table.id_categories.requires = IS_IN_DB(db, "categories.code") table.id_lists.requires = IS_IN_DB(db, "lists.list") table.id_projects.requires = IS_IN_DB(db, "projects.project") table.id_teams.requires = IS_IN_DB(db, "teams.team") table.format.requires = IS_IN_SET(FORMATS) return table @staticmethod def marc12_selector(virtdb, db, T): """marc12 selector. Note: selector table are store in a virtual database. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): connection to the database of the application T (gluon.languages.translator): language translator Returns: pyDAL.Table """ mode_dry_run = T(DRY_RUN) tp_xml = T("The XML string describing the MARC12 record. " 'It Starts with ""') table = virtdb.define_table( "marc12_selector", Field("id_teams", "reference teams", label="Team"), Field("id_projects", "reference projects", label="Project"), Field("host", "string", default=STORES[0], label="Store"), Field("xml", "text", comment=tp_xml, label="MARCXML"), Field("controller", "string", label="Automaton"), Field("id_categories", "reference categories", label="Category"), Field("mode", "string", default=mode_dry_run)) table.controller.requires = IS_IN_SET(CONTROLLERS) table.host.requires = IS_IN_SET(STORES) table.id_categories.requires = IS_IN_DB(db, "categories.code") table.id_projects.requires = IS_IN_DB(db, "projects.project") table.id_teams.requires = IS_IN_DB(db, "teams.team") table.mode.requires = IS_IN_SET([mode_dry_run, T(MODE_LOAD_IN_DB)]) table.xml.requires = IS_MATCH("<\?xml*") return table @staticmethod def metric_selector(virtdb, db, T): """metric selector. Note: selector table are store in a virtual database. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): connection to the database of the application T (gluon.languages.translator): language translator Returns: pyDAL.Table """ year = current.request.now.year table = virtdb.define_table( "metric_selector", Field("year_start", "integer", default=year), Field("year_end", "integer"), Field("id_teams", "reference teams", label="Team"), Field("id_projects", "reference projects", label="Project"), Field("id_categories", "reference categories", label="Category"), Field("author", "string"), Field("id_authors_roles", "reference authors_roles", label="Role"), Field("id_metrics", "reference metrics", label="Metric")) table.id_authors_roles.requires = IS_IN_DB(db, "authors_roles.role") table.id_categories.requires = IS_IN_DB(db, "categories.code") table.id_metrics.requires = IS_IN_DB(db, "metrics.metric") table.id_projects.requires = IS_IN_DB(db, "projects.project") table.id_teams.requires = IS_IN_DB(db, "teams.team") return table @staticmethod def run_all_harvesters_selector(virtdb, db, T): """run all harvesters selector. Note: selector table are store in a virtual database. Args: virtdb (pyDAL.DAL): connection to the virtual database db (pyDAL.DAL): connection to the database of the application T (gluon.languages.translator): language translator Returns: pyDAL.Table """ mode_dry_run = T(DRY_RUN) year = current.request.now.year table = virtdb.define_table( "run_all_harvesters_selector", Field("year_start", "integer", default=year), Field("year_end", "integer"), Field("id_teams", "reference teams", label="Team"), Field("id_projects", "reference projects", label="Project"), Field("mode", "string", default=mode_dry_run), Field("format", "string", default="html", label="Report format")) table.format.requires = IS_IN_SET(["html", "pdf", "tex"]) table.mode.requires = IS_IN_SET([mode_dry_run, T(MODE_LOAD_IN_DB)]) table.id_projects.requires = IS_IN_DB(db, "projects.project") table.id_teams.requires = IS_IN_DB(db, "teams.team") harvester_start_year = current.app.harvester_start_year if harvester_start_year: table.year_start.requires = \ IS_INT_IN_RANGE(harvester_start_year, year) table.year_end.requires = \ IS_INT_IN_RANGE(harvester_start_year, year) return table