Ejemplo n.º 1
0
def add_import(init_file, module_name):

    if not os.path.isfile(init_file):
        kf.put_contents(init_file,
                        "# -*- coding: utf-8 -*-\n\nimport %s\n" % module_name)
        return

    last_was_import = False
    inserted = False
    lines = []
    ## XXXvlab: could use readline
    for line in kf.get_contents(init_file).split("\n"):
        if not inserted and re.search('^import', line):
            lines.append(line)
            last_was_import = True
            continue
        if re.search('^import +%s *' % module_name, line):
            inserted = True  ## already inserted !
            break
        if last_was_import:
            lines.append("import %s" % module_name)
            last_was_import = False
            inserted = True
        lines.append(line)

    if inserted is False:
        lines.append("import %s" % module_name)
        lines.append("")

    kf.put_contents(init_file, "\n".join(lines))
    print "updated '%s'." % init_file
Ejemplo n.º 2
0
    def test_no_args_but_git(self):

        print self.w("""

            git init . &&
            git config user.name "Robert Dubois" &&
            git config user.email "*****@*****.**"

        """)
        out, err, errlvl = self.cmd(
            "NO_GIT_CONFIG= "  ## enables GIT CONFIG
            "OEM_INIT_TEMPLATE=$BASE_PATH/tests/fixtures/fake-template "
            "$tprog ")
        self.assertEqual(
            errlvl,
            0,
            msg=("Should succeed on simple directory and without args "
                 "(errlvl=%r)\n%s" % (errlvl, err)))
        self.assertEqual(err,
                         "",
                         msg="There should be no standard error outputed. "
                         "Current stderr:\n%r" % err)
        self.assertContains(
            kf.get_contents("LICENSE"),
            "Robert Dubois",
            msg="The author name should at least be in the LICENSE.")
Ejemplo n.º 3
0
def metadata_settings():
    dct = {
        'category': None,
        'url': None,
    }
    if os.path.isfile(get_metadata()):
        contents = kf.get_contents(get_metadata())
        dct.update(eval(contents))
    return dct
Ejemplo n.º 4
0
Archivo: tmpl.py Proyecto: pavelpy/oem
 def __div__(self, label):
     realpath = os.path.join(self.base, "%s" % label)
     if is_dir(realpath):
         return Registry(realpath)
     ## XXXvlab: conflict could occur if a dir is name LABEL and
     ## file is named LABEL.tpl
     tpl_realpath = realpath + ".tpl"
     if exists(tpl_realpath):
         return mk(kf.get_contents(tpl_realpath))
     raise KeyError("File %r not found" % tpl_realpath)
Ejemplo n.º 5
0
def load(filename):
    try:
        xml = objectify.parse(filename).getroot()
    except etree.XMLSyntaxError as e:
        context = 3
        file_lines = kf.get_contents(filename).split("\n")
        ctx_start = max(0, e.position[0] - context)
        ctx_stop = min(len(file_lines), e.position[0] + context)
        error_context = file_lines[ctx_start:ctx_stop]
        if ctx_start != 0:
            error_context = ["..."] + error_context
        if ctx_stop != len(file_lines):
            error_context = error_context + ["..."]
        raise SyntaxError(("File %r is not valid XML:\n  %s\n  | " %
                           (filename, e.msg)) + "\n  | ".join(error_context))
    except Exception as e:  ## I'm allowed because i'll re-raise it...
        print("==== CONTENT OF %r ====\n%s" \
              % (filename, kf.get_contents(filename)))
        raise
    return xml