예제 #1
0
 def test_write_file(self):
     read_text = fs_utils.read_file("./tests/fixtures/arquivo-utf8.txt")
     fs_utils.write_file("./tests/fixtures/arquivo-utf8-written.txt",
                         read_text)
     written_text = fs_utils.read_file(
         "./tests/fixtures/arquivo-utf8-written.txt")
     self.assertIn(written_text, read_text)
예제 #2
0
    def validate(self, article, outputs, pkgfiles):
        article_display_report = None
        article_validation_report = None

        if article.tree is None:
            content = validation_status.STATUS_BLOCKING_ERROR + ': ' + _(
                'Unable to get data from {item}. ').format(
                    item=article.new_prefix)
        else:
            content_validation = article_content_validations.ArticleContentValidation(
                self.pkgissuedata.journal, article, pkgfiles,
                (self.registered_issue_data.articles_db_manager is not None),
                False, self.doi_validator, self.config)
            article_display_report = article_data_reports.ArticleDisplayReport(
                content_validation)
            article_validation_report = article_data_reports.ArticleValidationReport(
                content_validation)

            content = []

            if self.is_xml_generation:
                content.append(article_display_report.issue_header)
                content.append(article_display_report.article_front)
                content.append(
                    article_validation_report.validations(
                        display_all_message_types=False))
                content.append(article_display_report.display_formulas)
                content.append(article_display_report.table_tables)
                r = fs_utils.read_file(outputs.images_report_filename) or ''
                r = r[r.find('<body'):]
                r = r[r.find('>') + 1:]
                r = r[:r.find('</body>')]
                content.append(r)
                content.append(article_display_report.article_body)
                content.append(article_display_report.article_back)

            else:
                content.append(
                    article_validation_report.validations(
                        display_all_message_types=False))
                content.append(article_display_report.display_formulas)
                content.append(article_display_report.table_tables)
                r = fs_utils.read_file(outputs.images_report_filename) or ''

                r = r[r.find('<body'):]
                r = r[r.find('>') + 1:]
                r = r[:r.find('</body>')]
                content.append(r)
                content.append(article_display_report.files_and_href())
            content = ''.join(content)
        r = validations_module.ValidationsResult()
        r.message = content
        return r, article_display_report
예제 #3
0
    def mail_gerapadrao_is_busy(self):
        print('gerapadrao is running. Wait ...')
        if self.mailer:
            msg = []
            if os.path.isfile(self.config.gerapadrao_scilista):
                msg.append(
                    'Running:\n' +
                    fs_utils.read_file(self.config.gerapadrao_scilista) +
                    '\n\n')
            if os.path.isfile(self.config.collection_scilista):
                msg.append('Waiting:\n' + sort_scilista(
                    fs_utils.read_file(self.config.collection_scilista)))

            if len(msg) > 0:
                self.mailer.send_message(self.config.email_to_adm,
                                         'gerapadrao is busy', ''.join(msg))
예제 #4
0
 def __init__(self, subject, message_template_filename):
     self.subject = subject
     self.message_template = ''
     if len(message_template_filename) > 0 and os.path.isfile(
             message_template_filename):
         self.message_template = fs_utils.read_file(
             message_template_filename)
예제 #5
0
 def __init__(self, str_or_filepath, do_changes=True, recover=False):
     self.do_changes = do_changes
     self.recover = recover
     self.changed = False
     self._xml = None
     self._content = None
     self.filename = None
     if os.path.isfile(str_or_filepath):
         self.filename = str_or_filepath
         try:
             str_or_filepath = fs_utils.read_file(self.filename)
         except UnicodeError:
             str_or_filepath = fs_utils.read_file(self.filename,
                                                  encode="iso-8859-1")
     self.original = str_or_filepath
     self.content = str_or_filepath
예제 #6
0
    def __init__(self, filename=None):
        self._data = {}

        filename = filename or get_configuration_filename()
        if filename:
            coding = 'utf-8'
            if filename.endswith('scielo_paths.ini'):
                coding = 'iso-8859-1'
            self._data.update(get_data(fs_utils.read_file(filename, coding)))

        for item in ['scielo_env.ini', 'scielo_collection.ini']:
            file_path = os.path.join(BIN_PATH, item)
            self._data.update(get_data(fs_utils.read_file(file_path)))

        self.interative_mode = self._data.get('Serial Directory') is not None
        self.is_windows = self.interative_mode
예제 #7
0
 def consume_collection_scilista(self):
     try:
         content = fs_utils.read_file(self.scilista_file)
     except (IOError, OSError, ValueError, TypeError):
         content = ''
     else:
         fs_utils.delete_file_or_folder(self.scilista_file)
     return content
예제 #8
0
 def load_xml(self):
     content = fs_utils.read_file(self.file_path)
     content = xml_utils.insert_break_lines(content)
     self.tree, self.loading_error = xml_utils.load_xml(content)
     if self.loading_error:
         content = xml_utils.numbered_lines(content)
         if content.startswith("1: <?xml"):
             content = content[content.find("?>") + 2:].strip()
         self.loading_error = (self.file_path + "\n\n" +
                               self.loading_error + "\n\n" + content)
         fs_utils.write_file(self.file_path, content)
예제 #9
0
    def read(self, filename):
        rec_list = []
        iso_content = fs_utils.read_file(filename, 'iso-8859-1')
        utf8_content = encoding.decode(iso_content)
        utf8_content = html.unescape(utf8_content)
        utf8_content = utf8_content.replace("\\^", PRESERVECIRC)

        records = utf8_content.split('!ID ')
        for record in records[1:]:
            data = self._get_record_data(record)
            rec_list.append(data)
        return rec_list
예제 #10
0
 def test_write_python2(self):
     file_path = "/tmp/isso_eh_teste.id"
     record = {
         "4": [
             {
                 "x": "&#91;",
                 "3": "subcampo 4b1"
             },
             {
                 "x": "subcxmpo 4x2",
                 "3": "&ccedil;"
             },
             {
                 "x": "x ^ y"
             },
         ],
         "5":
         "x ^ y",
         "6":
         "&ccedil;",
         "77":
         "&#91;",
         "1":
         u"磨",
         "2": ["x ^ y", "&#91;", "&ccedil;"],
         "3": {
             "_": "sem subcampo",
             "b": "subcampo 3b",
             "a": "subcampo 3a"
         },
         "9999":
         "",
     }
     data = ("!ID 000001\n"
             "!v001!&#30952;\n"
             "!v002!x \\^ y\n"
             "!v002![\n"
             "!v002!ç\n"
             "!v003!sem subcampo^asubcampo 3a^bsubcampo 3b\n"
             "!v004!^3subcampo 4b1^x[\n"
             "!v004!^3ç^xsubcxmpo 4x2\n"
             "!v004!^xx \\^ y\n"
             "!v005!x \\^ y\n"
             "!v006!ç\n"
             "!v077![\n")
     records = [record]
     self.idfile.write(file_path, records)
     x = fs_utils.read_file(file_path, "iso-8859-1")
     self.assertEqual(x, data)
예제 #11
0
 def validate_style(self, xml_obj, report_filename):
     if os.path.isfile(report_filename):
         os.unlink(report_filename)
     transformed = None
     if xml_obj:
         transformed = xml_utils.transform(xml_obj,
                                           self.dtd_files.xsl_prep_report)
     if transformed:
         transformed = xml_utils.transform(transformed,
                                           self.dtd_files.xsl_report)
         xml_utils.write(report_filename, transformed)
         result = fs_utils.read_file(report_filename)
     if not os.path.isfile(report_filename):
         result = 'ERROR: ' + _('Unable to create') + ' ' + report_filename
         fs_utils.write_file(report_filename, result)
     return style_checker_statistics(result)
예제 #12
0
def html2table():
    _items = []
    c = fs_utils.read_file(SYMBOLS_HTML)
    c = c.replace('<tr', '~BREAK~<tr').replace('</tr>', '</tr>~BREAK~')
    items = [item for item in c.split('~BREAK~') if item.startswith('<tr') and item.endswith('</tr>') and 'Symbol' in item]
    for item in items:
        item = item.replace('<td ', '<td>')
        cells = item.split('</td><td>')
        if len(cells) == 7:
            _char = cells[0]
            _ent = cells[4]
            _def = cells[5]
            _char = _char[0:_char.rfind('</font>')]
            _char = _char[_char.rfind('>')+1:]
            _ent = _ent[_ent.rfind('&'):]
            _ent = _ent[0:_ent.rfind(';')+1]
            _items.append(_char + '\t' + _ent + '\t' + _def)
    fs_utils.write_file(SYMBOLS_CSV, '\n'.join(_items))
예제 #13
0
def js_styles(has_math=False):
    js = fs_utils.read_file(HTML_REPORTS_PATH + '/html_reports_collapsible.js')
    if has_math:
        js += """<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
    config: ["MMLorHTML.js"],
    jax: ["input/TeX","input/MathML","output/HTML-CSS","output/NativeMML", "output/PreviewHTML"],
    extensions: ["tex2jax.js","mml2jax.js","MathMenu.js","MathZoom.js", "fast-preview.js", "AssistiveMML.js", "[Contrib]/a11y/accessibility-menu.js"],
    TeX: {
    extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
    }
    });
    </script>"""
        if os.path.isfile(MathJax_PATH + '/MathJax.js'):
            js += '<script type="text/javascript" src="{mathjax_path}/MathJax.js?config=MML_HTMLorMM-full"></script>'.format(
                mathjax_path=MathJax_PATH)
        else:
            js += '<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js"></script>'
    return js
예제 #14
0
def validate(xml_obj, dtd_external_id=None, dtd_file_path=None):
    """
    Valida contra uma DTD informando qual pelos parâmetros:
        dtd_external_id ou dtd_file_path
    """
    dtd_is_valid = False
    dtd_errors = []
    try:
        dtd = None
        if dtd_external_id:
            dtd = etree.DTD(external_id=dtd_external_id.encode())
        if not dtd and dtd_file_path:
            dtd = etree.DTD(StringIO(fs_utils.read_file(dtd_file_path)))
        if dtd:
            dtd_is_valid = dtd.validate(xml_obj)
            dtd_errors = format_validations_msg(dtd.error_log)
    except Exception as e:
        dtd_errors = [str(e)]
    return dtd_is_valid, dtd_errors
예제 #15
0
    def validate(self, file_path, outputs):
        separator = '\n\n\n' + '.........\n\n\n'

        # erro no nome do arquivo
        name_error = self._name_error(file_path, separator)

        # erro de conversao de markup a xml, se aplicavel
        mkp2xml_error = self._mkp2xml_error(outputs.mkp2xml_report_filename)

        # cria relatorio de errors de dtd
        valid_dtd, dtd_errors = self._dtd_error(outputs.dtd_report_filename)

        # cria relatorio de erros gerais
        fs_utils.write_file(outputs.err_filename,
                            mkp2xml_error + name_error + dtd_errors)

        # cria relatorio de errors de estilo
        xml_f, xml_e, xml_w = self.style_validation_report(
            outputs.style_report_filename)

        # conta e monta mensagem de erro sumarizada
        err_messages = self._err_messages(valid_dtd, name_error)
        xml_f += len(err_messages)
        if err_messages:
            err_messages = ''.join(err_messages)
            err_messages = rst_title(_('Summary')) + err_messages + separator
            err_messages = [err_messages.replace('\n', '<br/>')]

        if outputs.ctrl_filename:
            # aviso para o Markup de que terminou de gerar os relatorios
            fs_utils.write_file(outputs.ctrl_filename, 'Finished')
        elif xml_f + xml_e + xml_w == 0:
            fs_utils.delete_file_or_folder(outputs.style_report_filename)

        report_content = err_messages
        for rep_file in [outputs.err_filename, outputs.style_report_filename]:
            if os.path.isfile(rep_file):
                text = extract_report_core(fs_utils.read_file(rep_file))
                report_content.append(text)
        r = validations_module.ValidationsResult()
        r.message = ''.join(report_content)
        return r
예제 #16
0
 def test_read_file_returns_none(self):
     self.assertIsNone(fs_utils.read_file("file_does_not_exist"))
예제 #17
0
def css_styles():
    css_file = HTML_REPORTS_PATH + '/html_reports.css'
    css = '<style>' + fs_utils.read_file(css_file) + '</style>'
    return css
예제 #18
0
 def email_header(self, filename):
     header = ''
     if filename is not None:
         filename = os.path.join(EMAIL_TEMPLATE_MESSAGES_PATH, filename)
         header = fs_utils.read_file(filename)
     return header
예제 #19
0
 def test_read_file(self):
     text = fs_utils.read_file("./tests/fixtures/arquivo-utf8.txt")
     if python_version < 3:
         self.assertIn(u"宿", text)
     else:
         self.assertIn("宿", text)
예제 #20
0
 def _dtd_error(self, dtd_report_filename):
     valid_dtd = self.structure_validation_report(dtd_report_filename)
     dtd_errors = fs_utils.read_file(dtd_report_filename) or ''
     if len(dtd_errors) > 0:
         dtd_errors = rst_title(_('DTD errors')) + dtd_errors
     return valid_dtd, dtd_errors
예제 #21
0
 def _mkp2xml_error(self, mkp2xml_report_filename):
     return fs_utils.read_file(mkp2xml_report_filename) or ''
예제 #22
0
 def _read(self):
     if os.path.isfile(self.filename):
         self._message = fs_utils.read_file(self.filename)
     else:
         self._message = ''
예제 #23
0
def styles():
    css_file = HTML_REPORTS_PATH + '/html_reports.css'
    css = '<style>' + fs_utils.read_file(css_file) + '</style>'
    js = fs_utils.read_file(HTML_REPORTS_PATH + '/html_reports_collapsible.js')
    return css_styles() + js_styles() + save_report_js()
예제 #24
0
 def msg_from_files(self, param_files):
     msg = ''
     for f in param_files:
         content = fs_utils.read_file(f)
         msg += content + '\n' + '=' * 80 + '\n'
     return msg