Esempio n. 1
0
def cambia_en_sxw(archivo, cambios):
    """ Abre el archivo 'archivo', que debe ser un documento de writer de OOo y
    cambia en el content.xml las claves indicadas en cambios por sus valores
    correspondientes.
    Devuelve un valor != 0 para indicar el error."""
    try:
        doc = zipfile.ZipFile(archivo, 'a')
    except:
        return -4
    rutatemp = os.path.join(tmpdir(), "temporaloo.txt")
    txt=open(rutatemp, 'w') #temporal para los cambios.
    texto=doc.read('content.xml')
    for cambio in cambios:
        nuevo = cambios[cambio]
        try:
            texto = texto.replace(cambio, nuevo)
        except:
            print "mod_informes.py: ", cambio, nuevo
            texto = "ERROR PARÁMETRO"
    txt.write(texto)
    txt.close()
    doc.write(rutatemp, 'content.xml')
    # OJO: Creo que duplica el archivo, se almacenan en el ZIP 2 contents.
    # OOo lee el último, así que de momento funciona, pero ojito en 
    # futuras versiones de oowrite. Ver especificaciones de los archivos
    # en la web de OpenOffice.org 
    doc.close()
    return 0
Esempio n. 2
0
    def test_publish_single_page(self):
        output_html_file = 'index.html'

        with tmpdir() as tmp:
            tmp_path = Path(tmp)
            valid_md = tmp_path / md.markdown_text['fname'][0]
            valid_md.write_text(md.markdown_text['content'][1])

            self.ssg.ifpath = str(valid_md)
            self.ssg.ofpath = output_html_file
            post = self.ssg.load_posts()
            self.assertTrue(post)

            tmpl = self.ssg.load_templates()
            self.assertTrue(tmpl)

            page = self.ssg.render(post)
            self.assertTrue(page)

            self.ssg.publish(page, tmpl)
            self.assertTrue(Path(self.ssg.ofpath).exists())

            # private test case tearDown: remove `index.html`
            try:
                remove(output_html_file)
            except OSError as e:
                raise e
Esempio n. 3
0
 def test_filepath_nothing_to_publish(self):
     with tmpdir() as tmp:
         tmp_path = Path(tmp)
         invalid_md = tmp_path / 'foo.mdx'
         invalid_md.write_text(md.markdown_text['content'][0])
         self.ssg.ifpath = str(tmp_path)
         self.assertFalse(self.ssg.load_posts())
Esempio n. 4
0
    def test_publish_all(self):
        with tmpdir() as tmp:
            tmp_path = Path(tmp)
            post_text = md.markdown_text['content']
            fnames = md.markdown_text['fname']

            touch_md = lambda x: tmp_path / x
            for i in range(len(post_text)):
                touch_md(fnames[i]).write_text(post_text[i])

            self.ssg.ifpath = str(tmp_path)
            posts = self.ssg.load_posts()
            self.assertTrue(posts)

            tmpls = self.ssg.load_templates()
            self.assertTrue(tmpls)

            pages = self.ssg.render(posts)
            self.assertTrue(pages)

            self.ssg.publish(pages, tmpls)
            self.assertTrue(Path(self.ssg.ofpath).exists)

            # private test case tearDown: remove `site`
            try:
                rmtree(self.ssg.ofpath)
            except OSError as e:
                raise e
Esempio n. 5
0
def gitlocal(name, email):
    dirpath = tmpdir()
    cmd = '''
    git init
    git config --local user.name '{}'
    git config --local user.email '{}'
    '''.format(name, email)
    output = subprocess.check_output(cmd, shell=True, cwd=dirpath.name)
    return dirpath
Esempio n. 6
0
    def test_clean_generated_pages(self):
        with tmpdir() as tmp:
            tmp_path = Path(tmp)
            file_to_clean = tmp_path / 'foo.html'
            file_to_clean.write_text('')
            file_not_to_clean = tmp_path / 'foo.txt'
            file_not_to_clean.write_text('')

            self.ssg.ofpath = str(tmp_path)
            self.ssg.clean()
            self.assertFalse(file_to_clean.exists())
            self.assertTrue(file_not_to_clean.exists())
Esempio n. 7
0
    def test_render_post(self):
        with tmpdir() as tmp:
            tmp_path = Path(tmp)
            valid_md = tmp_path / md.markdown_text['fname'][0]
            valid_md.write_text(md.markdown_text['content'][0])
            self.ssg.ifpath = str(tmp_path)
            posts = self.ssg.load_posts()
            self.assertTrue(posts)

            pages = self.ssg.render(posts)
            self.assertTrue(pages[0]['meta'])
            self.assertTrue(pages[0]['content'])
            self.assertEqual(pages[0]['fname'], 'foo.html')
Esempio n. 8
0
    def install(pth):  # pylint: disable=R0914
        """
        Install a database
        Args:
            pth   (str): A local path or URL to database installation file
        """
        # pylint: disable=C0415

        pat = options.github_pat
        import shutil as sh
        from tempfile import TemporaryDirectory as tmpdir
        with tmpdir() as tdir:
            if pth.startswith('http://') or pth.startswith('https://'):
                from urllib.request import Request
                from urllib.request import urlopen
                from urllib.parse import urlparse
                fn = os.path.basename(urlparse(pth).path)
                path_to_file = os.path.join(tdir, fn)
                print('Downloading database...')
                req = Request(pth)

                if pat != '':
                    req.add_header("Authorization", "token {}".format(pat))

                with urlopen(req) as resp, open(path_to_file, 'wb') as f:
                    sh.copyfileobj(resp, f)
            elif os.path.exists(pth):
                fn = os.path.basename(pth)
                path_to_file = os.path.join(tdir, fn)
                sh.copyfile(pth, path_to_file)
            else:
                raise ValueError('pth argument is not valid.')

            sh.unpack_archive(path_to_file, tdir)
            archive_dir = [p for p in os.scandir(tdir) if p.is_dir()]
            if len(archive_dir) > 0:
                archive_dir = os.path.join(tdir, archive_dir[0].name)
            else:
                archive_dir = tdir

            install_script = os.path.join(archive_dir, 'install.py')
            if os.path.exists(install_script):
                from importlib.util import spec_from_file_location
                from importlib.util import module_from_spec
                spec = spec_from_file_location("install", install_script)
                script = module_from_spec(spec)
                spec.loader.exec_module(script)
                if script.agree_to_lic():
                    script.install(options.db_path)
            else:
                raise FileNotFoundError('Installation script was not found')
Esempio n. 9
0
def run_experiments(base):
    """Run all experiments found in base param.
    and check that flambe executes without errors.

    Before running the configs, it updates the save_path to
    be a tempdir and updates (potentially) the iteration's
    params (if found) to be 1.

    """
    for fname in os.listdir(base):
        full_f = os.path.join(base, fname)
        if os.path.isfile(full_f) and fname.endswith('yaml'):
            with tmpdir() as d, tmpfile() as f:
                new_exp = _preprocess_experiment(full_f, d)
                if new_exp:
                    yaml.dump_all(new_exp, f)
                    ret = subprocess.run(['flambe', f.name, '-i'])
                    assert ret.returncode == 0
Esempio n. 10
0
def test_exporter_builder():
    with tmpdir() as d, tmpdir() as d2, tmpfile(
            mode="w", suffix=".yaml") as f, tmpfile(mode="w",
                                                    suffix=".yaml") as f2:
        # First run an experiment
        exp = """
!Experiment

name: exporter
save_path: {}

pipeline:
  dataset: !SSTDataset
    transform:
      text: !TextField
      label: !LabelField
  model: !TextClassifier
    embedder: !Embedder
      embedding: !torch.Embedding
        num_embeddings: !@ dataset.text.vocab_size
        embedding_dim: 30
      encoder: !PooledRNNEncoder
        input_size: 30
        rnn_type: lstm
        n_layers: 1
        hidden_size: 16
    output_layer: !SoftmaxLayer
      input_size: !@ model[embedder].encoder.rnn.hidden_size
      output_size: !@ dataset.label.vocab_size

  exporter: !Exporter
    model: !@ model
    text: !@ dataset.text
"""

        exp = exp.format(d)
        f.write(exp)
        f.flush()
        ret = subprocess.run(['flambe', f.name, '-i'])
        assert ret.returncode == 0

        # Then run a builder

        builder = """
flambe_inference: tests/data/dummy_extensions/inference/
---

!Builder

destination: {0}

component: !flambe_inference.DummyInferenceEngine
  model: !TextClassifier.load_from_path
    path: {1}
"""
        base = os.path.join(d, "output__exporter", "exporter")
        path_aux = [
            x for x in os.listdir(base) if os.path.isdir(os.path.join(base, x))
        ][0]  # Should be only 1 folder bc of no variants
        model_path = os.path.join(base, path_aux, "checkpoint",
                                  "checkpoint.flambe", "model")

        builder = builder.format(d2, model_path)
        f2.write(builder)
        f2.flush()

        ret = subprocess.run(['flambe', f2.name, '-i'])
        assert ret.returncode == 0

        # The extensions needs to be imported using extensions.py module
        extensions.import_modules(["flambe_inference"])

        # Import the module after import_modules (which registered tags already)
        from flambe_inference import DummyInferenceEngine

        eng1 = flambe.load(d2)

        assert type(eng1) is DummyInferenceEngine
        assert type(eng1.model) is TextClassifier

        extension_path = os.path.join(
            os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
            "tests/data/dummy_extensions/inference")
        assert eng1._extensions == {"flambe_inference": extension_path}

        eng2 = DummyInferenceEngine.load_from_path(d2)

        assert type(eng2) is DummyInferenceEngine
        assert type(eng2.model) is TextClassifier

        assert eng2._extensions == {"flambe_inference": extension_path}

        assert module_equals(eng1.model, eng2.model)
Esempio n. 11
0
def _directory(make_project):
    if make_project:
        return _make_project_directory(make_project)

    from tempfile import TemporaryDirectory as tmpdir
    return tmpdir()
Esempio n. 12
0
 def test_send_codehilite_style(self):
     with tmpdir() as tmp:
         tmp_path = Path(tmp)
         hlcss = tmp_path / 'codehilite.css'
         self.ssg.send_codehilite_style('emacs', str(tmp_path))
         self.assertTrue(hlcss.exists())
Esempio n. 13
0
def gitbare():
    dirpath = tmpdir()
    cmd = 'git init --bare'
    output = subprocess.check_output(cmd, shell=True, cwd=dirpath.name)
    return dirpath