Example #1
0
def format_element(bfo, us="yes"):
    """
    Return dategui for the best available date.

    Looking in several locations for it (date, eprint, journal, date added).

    @param bfo: BibFormatObject for current record
    @type nfo: object

    @param us: us style date Mon dd, yyyy (default), otherwise dd mon yyyy
    @type us: str

    @return: string of formatted date, or None of no date is found.
    """
    datestruct = get_date(bfo)
    if datestruct:
        dummy_time = (0, 0, 44, 2, 320, 0)
        # if we have all 3 use dategui:
        if len(datestruct) == 3:
            datestruct = tuple(datestruct[0:3]) + dummy_time
            date = re.sub(',\s00:00$', '', convert_datestruct_to_dategui(datestruct))
            if us == "yes":
                return(re.sub(r' 0(\d),', r' \1,', (re.sub(r'(\d{2})\s(\w{3})', r'\2 \1,', date))))
            else:
                return(date)
        elif len(datestruct) == 2:
            # if we have only the month, pass the converter a dummy day and
            # then strip it
            datestruct = tuple(datestruct[0:2]) + (1,) + dummy_time
            date = re.sub(r',\s00:00$', '', convert_datestruct_to_dategui(datestruct))
            return re.sub(r'\d+\s+(\w+)', r'\1', date)
        elif len(datestruct) == 1:
            # only the year
            return datestruct[0]
    return None
Example #2
0
 def _format_date(date):
     """
     This is a special Jinja2 filter that will call
     convert_datetext_to_dategui to print a human friendly date.
     """
     if isinstance(date, datetime):
         return convert_datestruct_to_dategui(
             date.timetuple(),
             getattr(g, 'ln', app.config['CFG_SITE_LANG'])).decode('utf-8')
     return convert_datetext_to_dategui(
         date, getattr(g, 'ln', app.config['CFG_SITE_LANG'])).decode('utf-8')
Example #3
0
    def _format_date(date):
        """
        Format a date into a human friendly format.

        It uses :py:func:`invenio.utils.date.convert_datetext_to_dategui`
        """
        if isinstance(date, datetime):
            return convert_datestruct_to_dategui(
                date.timetuple(),
                getattr(g, 'ln', app.config['CFG_SITE_LANG'])).decode('utf-8')
        return convert_datetext_to_dategui(
            date, getattr(g, 'ln', app.config['CFG_SITE_LANG'])
        ).decode('utf-8')
Example #4
0
    def _format_date(date):
        """
        Format a date into a human friendly format.

        It uses :py:func:`invenio.utils.date.convert_datetext_to_dategui`
        """
        if isinstance(date, datetime):
            return convert_datestruct_to_dategui(
                date.timetuple(),
                getattr(g, 'ln', app.config['CFG_SITE_LANG'])).decode('utf-8')
        return convert_datetext_to_dategui(
            date, getattr(g, 'ln',
                          app.config['CFG_SITE_LANG'])).decode('utf-8')
Example #5
0
 def test_convert_buggy_datestruct_to_dategui_sk(self):
         expected = 'nepríst.'
         struct = struct_time((-1, 11, 16, 15, 11, 44, 2, 320, 0))
         result = dateutils.convert_datestruct_to_dategui(struct, "sk")
         self.assertEqual(expected, result)
Example #6
0
 def test_convert_datestruct_to_dategui_sk(self):
     expected = '16 júl 2005, 15:11'
     struct = struct_time((2005, 7, 16, 15, 11, 44, 2, 320, 0))
     result = dateutils.convert_datestruct_to_dategui(struct, "sk")
     self.assertEqual(expected, result)
Example #7
0
 def test_convert_incomplete_datestruct_to_dategui_en(self):
     expected = 'N/A'
     struct = struct_time((0, 0, 0, 15, 11, 44, 2, 320, 0))
     result = dateutils.convert_datestruct_to_dategui(struct, "en")
     self.assertEqual(expected, result)
Example #8
0
def update_webdoc_cache(webdoc, mode=1, verbose=0, languages=CFG_SITE_LANGS):
    """
    Update the cache (on disk) of the given webdoc.

    Parameters:

            webdoc       - *string* the name of a webdoc that can be
                           found in standard webdoc dir, or a webdoc
                           filepath.

            mode         - *int* update cache mode:
                                - 0 : do not update
                                - 1 : only if necessary (webdoc source
                                      is newer than its cache)
                                - 2 : always update
    """
    if mode in [1, 2]:
        (webdoc_source_path, \
         webdoc_cache_dir, \
         webdoc_name,\
         webdoc_source_modification_date, \
         webdoc_cache_modification_date) = get_webdoc_info(webdoc)

        if mode == 1 and \
               webdoc_source_modification_date < webdoc_cache_modification_date and \
               get_mo_last_modification() < webdoc_cache_modification_date:
            # Cache was updated after source. No need to update
            return
        (webdoc_source, \
         webdoc_cache_dir, \
         webdoc_name) = read_webdoc_source(webdoc)

        if webdoc_source is not None:
            htmls = transform(webdoc_source, languages=languages)
            for (lang, body, title, keywords, \
                 navtrail, lastupdated, description) in htmls:
                # Body
                if body is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file('%(name)s.body%(lang)s.html' % \
                                         {'name': webdoc_name,
                                          'lang': '-'+lang},
                                         webdoc_cache_dir,
                                         body,
                                         verbose)
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Title
                if title is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file('%(name)s.title%(lang)s.html' % \
                                         {'name': webdoc_name,
                                          'lang': '-'+lang},
                                         webdoc_cache_dir,
                                         title,
                                         verbose)
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Keywords
                if keywords is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file('%(name)s.keywords%(lang)s.html' % \
                                         {'name': webdoc_name,
                                          'lang': '-'+lang},
                                         webdoc_cache_dir,
                                         keywords,
                                         verbose)
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Navtrail
                if navtrail is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file('%(name)s.navtrail%(lang)s.html' % \
                                         {'name': webdoc_name,
                                          'lang': '-'+lang},
                                         webdoc_cache_dir,
                                         navtrail,
                                         verbose)
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Description
                if description is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file('%(name)s.description%(lang)s.html' % \
                                         {'name': webdoc_name,
                                          'lang': '-'+lang},
                                         webdoc_cache_dir,
                                         description,
                                         verbose)
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Last updated timestamp (CVS timestamp)
                if lastupdated is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file('%(name)s.lastupdated%(lang)s.html' % \
                                         {'name': webdoc_name,
                                          'lang': '-'+lang},
                                         webdoc_cache_dir,
                                         lastupdated,
                                         verbose)
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Last updated cache file
                try:
                    write_cache_file('last_updated',
                                     webdoc_cache_dir,
                                     convert_datestruct_to_dategui(
                                         time.localtime()),
                                     verbose=0)
                except IOError as e:
                    print(e)
                except OSError as e:
                    print(e)

            if verbose > 0:
                print('Written cache in %s' % webdoc_cache_dir)
Example #9
0
    def tmpl_bibdocfile_filelist(self, ln, recid, name, version, md,
                                 superformat, subformat, nice_size_f,
                                 description, comment):
        """
        Displays a file in the file list.

        Parameters:

          - 'ln' *string* - The language to display the interface in

          - 'recid' *int* - The id of the record

          - 'name' *string* - The name of the file

          - 'version' *string* - The version

          - 'md' *datetime* - the modification date

          - 'superformat' *string* - The display superformat

          - 'subformat' *string* - The display subformat

          - 'nice_size_f' *string* - The nice_size of the file

          - 'description' *string* - The description that might have been associated
          to the particular file

          - 'comment' *string* - The comment that might have been associated
          to the particular file
        """

        # load the right message language
        _ = gettext_set_language(ln)

        urlbase = '%s/%s/%s/files/%s' % (
            CFG_SITE_URL, CFG_SITE_RECORD, recid, '%s%s' %
            (cgi.escape(urllib.quote(name), True), superformat))

        urlargd = {'version': version}
        if subformat:
            urlargd['subformat'] = subformat

        link_label = '%s%s' % (name, superformat)
        if subformat:
            link_label += ' (%s)' % subformat

        link = create_html_link(urlbase, urlargd, cgi.escape(link_label))

        return """<tr>
                    <td valign="top">
                      <small>%(link)s</small>
                    </td>
                    <td valign="top">
                      <font size="-2" color="green">[%(nice_size)s]</font>
                      <font size="-2"><em>%(md)s</em>
                      %(comment)s
                    </td>
                    <td valign="top"><em>%(description)s</em></td>
                    </tr>""" % {
            'link':
            link,
            'nice_size':
            nice_size_f,
            'md':
            convert_datestruct_to_dategui(md.timetuple(), ln),
            'description':
            cgi.escape(description),
            'comment':
            comment and """<br /><font size="-2"><em>%s</em></font>""" %
            cgi.escape(comment) or '',
        }
Example #10
0
    def tmpl_display_job_history(self, job_results, language = CFG_SITE_LANG):
        """Creates a page displaying information about
        the job results given as a parameter.

        @param job_results: List of JobResult objects containing
        information about the job results that have to be displayed
        @param language: language of the page

        @return: The HTML content of the page
        """
        _ = gettext_set_language(language)

        table_rows = ""
        for current_job_result in job_results:
            current_job = current_job_result.get_job()
            # convert execution date into text proper to be shown to the user
            execution_date_time = current_job_result.get_execution_date_time()
            date = convert_datestruct_to_dategui(execution_date_time)
            # obtain text corresponding to the frequency of execution
            frequency = current_job.get_frequency()
            frequency_text = self._get_frequency_text(frequency, language)
            # set the status text
            if current_job_result.STATUS_CODE_OK == current_job_result.get_status():
                status = _("OK")
            else:
                status = _("Error")

            records_found = current_job_result.get_number_of_records_found()

            row = """<tr>
            <td><a href="%(job_results_url)s?result_id=%(job_result_id)s&ln=%(language)s">%(job_name)s</a></td>
            <td>%(job_frequency)s</td>
            <td>%(execution_date)s</td>
            <td><b>%(status)s</b>
                <a href="%(display_job_result_url)s?result_id=%(job_result_id)s&ln=%(language)s">
                <small>%(number_of_records_found)s %(label_records_found)s</small>
                </a>
            </td>
            </tr>""" %  self._html_escape_dictionary({
                        "job_name" : current_job.get_name(),
                        "job_frequency" : frequency_text,
                        "execution_date" : date,
                        "status" : status,
                        "number_of_records_found" : records_found,
                        "label_records_found" : _("records found"),
                        "job_results_url" : self._JOB_RESULTS_URL,
                        "display_job_result_url" : self._DISPLAY_JOB_RESULT_URL,
                        "language" : language,
                        "job_result_id" : current_job_result.get_id()
                        })
            table_rows += row

        body = """
        <table class="spacedcells">
            <th>%(label_job_name)s</th>
            <th>%(label_job_frequency)s</th>
            <th>%(label_execution_date)s</th>
            <th>%(label_status)s</th>
            %(table_rows)s
        </table>
        """ % {
            "table_rows" : table_rows,
            "label_job_name" : _("Job"),
            "label_job_frequency" : _("Run"),
            "label_execution_date" : _("Date"),
            "label_status" : _("Status")
            }

        return body
Example #11
0
def update_webdoc_cache(webdoc, mode=1, verbose=0, languages=CFG_SITE_LANGS):
    """
    Update the cache (on disk) of the given webdoc.

    Parameters:

            webdoc       - *string* the name of a webdoc that can be
                           found in standard webdoc dir, or a webdoc
                           filepath.

            mode         - *int* update cache mode:
                                - 0 : do not update
                                - 1 : only if necessary (webdoc source
                                      is newer than its cache)
                                - 2 : always update
    """
    if mode in [1, 2]:
        (
            webdoc_source_path,
            webdoc_cache_dir,
            webdoc_name,
            webdoc_source_modification_date,
            webdoc_cache_modification_date,
        ) = get_webdoc_info(webdoc)

        if (
            mode == 1
            and webdoc_source_modification_date < webdoc_cache_modification_date
            and get_mo_last_modification() < webdoc_cache_modification_date
        ):
            # Cache was updated after source. No need to update
            return
        (webdoc_source, webdoc_cache_dir, webdoc_name) = read_webdoc_source(webdoc)

        if webdoc_source is not None:
            htmls = transform(webdoc_source, languages=languages)
            for (lang, body, title, keywords, navtrail, lastupdated, description) in htmls:
                # Body
                if body is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file(
                            "%(name)s.body%(lang)s.html" % {"name": webdoc_name, "lang": "-" + lang},
                            webdoc_cache_dir,
                            body,
                            verbose,
                        )
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Title
                if title is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file(
                            "%(name)s.title%(lang)s.html" % {"name": webdoc_name, "lang": "-" + lang},
                            webdoc_cache_dir,
                            title,
                            verbose,
                        )
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Keywords
                if keywords is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file(
                            "%(name)s.keywords%(lang)s.html" % {"name": webdoc_name, "lang": "-" + lang},
                            webdoc_cache_dir,
                            keywords,
                            verbose,
                        )
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Navtrail
                if navtrail is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file(
                            "%(name)s.navtrail%(lang)s.html" % {"name": webdoc_name, "lang": "-" + lang},
                            webdoc_cache_dir,
                            navtrail,
                            verbose,
                        )
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Description
                if description is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file(
                            "%(name)s.description%(lang)s.html" % {"name": webdoc_name, "lang": "-" + lang},
                            webdoc_cache_dir,
                            description,
                            verbose,
                        )
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Last updated timestamp (CVS timestamp)
                if lastupdated is not None or lang == CFG_SITE_LANG:
                    try:
                        write_cache_file(
                            "%(name)s.lastupdated%(lang)s.html" % {"name": webdoc_name, "lang": "-" + lang},
                            webdoc_cache_dir,
                            lastupdated,
                            verbose,
                        )
                    except IOError as e:
                        print(e)
                    except OSError as e:
                        print(e)

                # Last updated cache file
                try:
                    write_cache_file(
                        "last_updated", webdoc_cache_dir, convert_datestruct_to_dategui(time.localtime()), verbose=0
                    )
                except IOError as e:
                    print(e)
                except OSError as e:
                    print(e)

            if verbose > 0:
                print("Written cache in %s" % webdoc_cache_dir)
Example #12
0
 def test_convert_datestruct_to_dategui_en(self):
     expected = '16 Nov 2005, 15:11'
     struct = struct_time((2005, 11, 16, 15, 11, 44, 2, 320, 0))
     result = dateutils.convert_datestruct_to_dategui(struct, "en")
     self.assertEqual(expected, result)
Example #13
0
    def tmpl_bibdocfile_filelist(self, ln, recid, name, version, md, superformat, subformat, nice_size_f, description, comment):
        """
        Displays a file in the file list.

        Parameters:

          - 'ln' *string* - The language to display the interface in

          - 'recid' *int* - The id of the record

          - 'name' *string* - The name of the file

          - 'version' *string* - The version

          - 'md' *datetime* - the modification date

          - 'superformat' *string* - The display superformat

          - 'subformat' *string* - The display subformat

          - 'nice_size_f' *string* - The nice_size of the file

          - 'description' *string* - The description that might have been associated
          to the particular file

          - 'comment' *string* - The comment that might have been associated
          to the particular file
        """

        # load the right message language
        _ = gettext_set_language(ln)

        urlbase = '%s/%s/%s/files/%s' % (
            CFG_SITE_URL,
            CFG_SITE_RECORD,
            recid,
            '%s%s' % (cgi.escape(urllib.quote(name), True), superformat))

        urlargd = {'version' : version}
        if subformat:
            urlargd['subformat'] = subformat

        link_label = '%s%s' % (name, superformat)
        if subformat:
            link_label += ' (%s)' % subformat

        link = create_html_link(urlbase, urlargd, cgi.escape(link_label))

        return """<tr>
                    <td valign="top">
                      <small>%(link)s</small>
                    </td>
                    <td valign="top">
                      <font size="-2" color="green">[%(nice_size)s]</font>
                      <font size="-2"><em>%(md)s</em>
                      %(comment)s
                    </td>
                    <td valign="top"><em>%(description)s</em></td>
                    </tr>""" % {
                      'link' : link,
                      'nice_size' : nice_size_f,
                      'md' : convert_datestruct_to_dategui(md.timetuple(), ln),
                      'description' : cgi.escape(description),
                      'comment' : comment and """<br /><font size="-2"><em>%s</em></font>""" % cgi.escape(comment) or '',
                    }
Example #14
0
    def tmpl_display_job_history(self, job_results, language=CFG_SITE_LANG):
        """Creates a page displaying information about
        the job results given as a parameter.

        @param job_results: List of JobResult objects containing
        information about the job results that have to be displayed
        @param language: language of the page

        @return: The HTML content of the page
        """
        _ = gettext_set_language(language)

        table_rows = ""
        for current_job_result in job_results:
            current_job = current_job_result.get_job()
            # convert execution date into text proper to be shown to the user
            execution_date_time = current_job_result.get_execution_date_time()
            date = convert_datestruct_to_dategui(execution_date_time)
            # obtain text corresponding to the frequency of execution
            frequency = current_job.get_frequency()
            frequency_text = self._get_frequency_text(frequency, language)
            # set the status text
            if current_job_result.STATUS_CODE_OK == current_job_result.get_status(
            ):
                status = _("OK")
            else:
                status = _("Error")

            records_found = current_job_result.get_number_of_records_found()

            row = """<tr>
            <td><a href="%(job_results_url)s?result_id=%(job_result_id)s&ln=%(language)s">%(job_name)s</a></td>
            <td>%(job_frequency)s</td>
            <td>%(execution_date)s</td>
            <td><b>%(status)s</b>
                <a href="%(display_job_result_url)s?result_id=%(job_result_id)s&ln=%(language)s">
                <small>%(number_of_records_found)s %(label_records_found)s</small>
                </a>
            </td>
            </tr>""" % self._html_escape_dictionary(
                {
                    "job_name": current_job.get_name(),
                    "job_frequency": frequency_text,
                    "execution_date": date,
                    "status": status,
                    "number_of_records_found": records_found,
                    "label_records_found": _("records found"),
                    "job_results_url": self._JOB_RESULTS_URL,
                    "display_job_result_url": self._DISPLAY_JOB_RESULT_URL,
                    "language": language,
                    "job_result_id": current_job_result.get_id()
                })
            table_rows += row

        body = """
        <table class="spacedcells">
            <th>%(label_job_name)s</th>
            <th>%(label_job_frequency)s</th>
            <th>%(label_execution_date)s</th>
            <th>%(label_status)s</th>
            %(table_rows)s
        </table>
        """ % {
            "table_rows": table_rows,
            "label_job_name": _("Job"),
            "label_job_frequency": _("Run"),
            "label_execution_date": _("Date"),
            "label_status": _("Status")
        }

        return body
Example #15
0
 def test_convert_buggy_datestruct_to_dategui_sk(self):
     expected = 'nepríst.'
     struct = struct_time((-1, 11, 16, 15, 11, 44, 2, 320, 0))
     result = dateutils.convert_datestruct_to_dategui(struct, "sk")
     self.assertEqual(expected, result)
Example #16
0
 def test_convert_datestruct_to_dategui_sk(self):
     expected = '16 júl 2005, 15:11'
     struct = struct_time((2005, 7, 16, 15, 11, 44, 2, 320, 1))
     result = dateutils.convert_datestruct_to_dategui(struct, "sk")
     self.assertEqual(expected, result)
Example #17
0
 def test_convert_incomplete_datestruct_to_dategui_en(self):
     expected = 'N/A'
     struct = struct_time((0, 0, 0, 15, 11, 44, 2, 320, 0))
     result = dateutils.convert_datestruct_to_dategui(struct, "en")
     self.assertEqual(expected, result)