Python os.py() Examples

The following are 30 code examples of os.py(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module os , or try the search function .
Example #1
Source File: pyexiftool.py    From elodie with Apache License 2.0 6 votes vote down vote up
def start(self):
        """Start an ``exiftool`` process in batch mode for this instance.

        This method will issue a ``UserWarning`` if the subprocess is
        already running.  The process is started with the ``-G`` and
        ``-n`` as common arguments, which are automatically included
        in every command you run with :py:meth:`execute()`.
        """
        if self.running:
            warnings.warn("ExifTool already running; doing nothing.")
            return
        with open(os.devnull, "w") as devnull:
            procargs = [self.executable, "-stay_open", "True",  "-@", "-",
                 "-common_args", "-G", "-n"];
            procargs.extend(self.addedargs)
            logging.debug(procargs) 
            self._process = subprocess.Popen(
                procargs,
                stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                stderr=devnull)
        self.running = True 
Example #2
Source File: test_process.py    From coveragepy with Apache License 2.0 6 votes vote down vote up
def test_append_data(self):
        self.make_b_or_c_py()

        out = self.run_command("coverage run b_or_c.py b")
        self.assertEqual(out, 'done\n')
        self.assert_exists(".coverage")
        self.assert_file_count(".coverage.*", 0)

        out = self.run_command("coverage run --append b_or_c.py c")
        self.assertEqual(out, 'done\n')
        self.assert_exists(".coverage")
        self.assert_file_count(".coverage.*", 0)

        # Read the coverage file and see that b_or_c.py has all 8 lines
        # executed.
        data = coverage.CoverageData()
        data.read()
        self.assertEqual(line_counts(data)['b_or_c.py'], 8) 
Example #3
Source File: test_process.py    From coveragepy with Apache License 2.0 6 votes vote down vote up
def test_append_data_with_different_file(self):
        self.make_b_or_c_py()

        self.make_file(".coveragerc", """\
            [run]
            data_file = .mycovdata
            """)

        out = self.run_command("coverage run b_or_c.py b")
        self.assertEqual(out, 'done\n')
        self.assert_doesnt_exist(".coverage")
        self.assert_exists(".mycovdata")

        out = self.run_command("coverage run --append b_or_c.py c")
        self.assertEqual(out, 'done\n')
        self.assert_doesnt_exist(".coverage")
        self.assert_exists(".mycovdata")

        # Read the coverage file and see that b_or_c.py has all 8 lines
        # executed.
        data = coverage.CoverageData(".mycovdata")
        data.read()
        self.assertEqual(line_counts(data)['b_or_c.py'], 8) 
Example #4
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_append_data(self):
        self.make_b_or_c_py()

        out = self.run_command("coverage run b_or_c.py b")
        self.assertEqual(out, 'done\n')
        self.assert_exists(".coverage")
        self.assertEqual(self.number_of_data_files(), 1)

        out = self.run_command("coverage run --append b_or_c.py c")
        self.assertEqual(out, 'done\n')
        self.assert_exists(".coverage")
        self.assertEqual(self.number_of_data_files(), 1)

        # Read the coverage file and see that b_or_c.py has all 7 lines
        # executed.
        data = coverage.CoverageData()
        data.read_file(".coverage")
        self.assertEqual(data.line_counts()['b_or_c.py'], 7) 
Example #5
Source File: test_process.py    From coveragepy with Apache License 2.0 6 votes vote down vote up
def test_missing_source_file(self):
        # Check what happens if the source is missing when reporting happens.
        self.make_file("fleeting.py", """\
            s = 'goodbye, cruel world!'
            """)

        self.run_command("coverage run fleeting.py")
        os.remove("fleeting.py")
        out = self.run_command("coverage html -d htmlcov")
        self.assertRegex(out, "No source for code: '.*fleeting.py'")
        self.assertNotIn("Traceback", out)

        # It happens that the code paths are different for *.py and other
        # files, so try again with no extension.
        self.make_file("fleeting", """\
            s = 'goodbye, cruel world!'
            """)

        self.run_command("coverage run fleeting")
        os.remove("fleeting")
        status, out = self.run_command_status("coverage html -d htmlcov")
        self.assertRegex(out, "No source for code: '.*fleeting'")
        self.assertNotIn("Traceback", out)
        self.assertEqual(status, 1) 
Example #6
Source File: setup.py    From scapy-ssl_tls with GNU General Public License v2.0 6 votes vote down vote up
def _post_install(dir_):
    """ Patches scapy config.py to add autoloading of the ssl_tls layer
    Takes a backup in the form of a config.py.bak file
    """

    def patch_ll(ll_match):
        match_start = ll_match.start()
        prefix = ll_match.start(1) - match_start
        suffix = ll_match.end(1) - match_start
        orig_code = ll_match.group(0)
        return orig_code[:prefix] + ', "ssl_tls",' + orig_code[suffix:]
    ll_regex = re.compile(r"load_layers = \[[^\]]*(,)[^,\]]*\]", re.DOTALL)

    scapy_locations = get_scapy_locations(get_site_packages())
    for scapy_location in scapy_locations:
        scapy_config = os.path.join(scapy_location, "config.py")
        if os.path.isfile(scapy_config):
            shutil.copy(scapy_config, scapy_config + ".bak")
            with open(scapy_config, "r+") as f:
                config_source = f.read()
                f.truncate(0)
                f.seek(0)
                f.write(ll_regex.sub(patch_ll, config_source)) 
Example #7
Source File: test_process.py    From coveragepy with Apache License 2.0 6 votes vote down vote up
def test_warnings_during_reporting(self):
        # While fixing issue #224, the warnings were being printed far too
        # often.  Make sure they're not any more.
        self.make_file("hello.py", """\
            import sys, os, the_other
            print("Hello")
            """)
        self.make_file("the_other.py", """\
            print("What?")
            """)
        self.make_file(".coveragerc", """\
            [run]
            source =
                .
                xyzzy
            """)

        self.run_command("coverage run hello.py")
        out = self.run_command("coverage html")
        self.assertEqual(out.count("Module xyzzy was never imported."), 0) 
Example #8
Source File: setup.py    From scapy-ssl_tls with GNU General Public License v2.0 6 votes vote down vote up
def get_site_packages():
    """
    This is a hack to work around site.getsitepackages() not working in
    virtualenv. See https://github.com/pypa/virtualenv/issues/355
    """
    # Another hack...
    # Relies on the fact that os.py is in the dir above site_packages
    os_location = os.path.dirname(os.__file__)
    site_packages = []
    # Consider Debain/Ubuntu custom
    for site in ["site-packages", "dist-packages"]:
        site_path = os.path.join(os_location, site)
        if os.path.isdir(site_path):
            site_packages.append(site_path)
    try:
        site_packages += _site.getsitepackages()
    except AttributeError, ex:
        print("WARNING: Error trying to call site.getsitepackages(). Exception: %r" % ex)
        print("         Do you have sufficient permissions?") 
        print("         Otherwise this could probably be virtualenv issue#355") 
Example #9
Source File: regsetup.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def RegisterShellInfo(searchPaths):
    """Registers key parts of the Python installation with the Windows Shell.

       Assumes a valid, minimal Python installation exists
       (ie, SetupCore() has been previously successfully run)
    """
    import regutil, win32con
    suffix = IsDebug()
    # Set up a pointer to the .exe's
    exePath = FindRegisterPythonExe("Python%s.exe" % suffix, searchPaths)
    regutil.SetRegistryDefaultValue(".py", "Python.File", win32con.HKEY_CLASSES_ROOT)
    regutil.RegisterShellCommand("Open", QuotedFileName(exePath)+" \"%1\" %*", "&Run")
    regutil.SetRegistryDefaultValue("Python.File\\DefaultIcon", "%s,0" % exePath, win32con.HKEY_CLASSES_ROOT)

    FindRegisterHelpFile("Python.hlp", searchPaths, "Main Python Documentation")
    FindRegisterHelpFile("ActivePython.chm", searchPaths, "Main Python Documentation")

    # We consider the win32 core, as it contains all the win32 api type
    # stuff we need.
#       FindRegisterApp("win32", ["win32con.pyc", "win32api%s.pyd" % suffix], searchPaths) 
Example #10
Source File: test_process.py    From coveragepy with Apache License 2.0 6 votes vote down vote up
def test_warns_if_never_run(self):
        # Note: the name of the function can't have "warning" in it, or the
        # absolute path of the file will have "warning" in it, and an assertion
        # will fail.
        out = self.run_command("coverage run i_dont_exist.py")
        path = python_reported_file('i_dont_exist.py')
        self.assertIn("No file to run: '{}'".format(path), out)
        self.assertNotIn("warning", out)
        self.assertNotIn("Exception", out)

        out = self.run_command("coverage run -m no_such_module")
        self.assertTrue(
            ("No module named no_such_module" in out) or
            ("No module named 'no_such_module'" in out)
            )
        self.assertNotIn("warning", out)
        self.assertNotIn("Exception", out) 
Example #11
Source File: test_process.py    From coveragepy with Apache License 2.0 6 votes vote down vote up
def test_warnings_trace_function_changed_with_threads(self):
        # https://bitbucket.org/ned/coveragepy/issue/164
        if env.METACOV:
            self.skipTest("Can't test tracers changing during metacoverage")

        self.make_file("bug164.py", """\
            import threading
            import time

            class MyThread (threading.Thread):
                def run(self):
                    print("Hello")

            thr = MyThread()
            thr.start()
            thr.join()
            """)
        out = self.run_command("coverage run --timid bug164.py")

        self.assertIn("Hello\n", out)
        self.assertNotIn("warning", out) 
Example #12
Source File: test_process.py    From coveragepy with Apache License 2.0 6 votes vote down vote up
def test_code_exits(self):
        self.make_file("exit.py", """\
            import sys
            def f1():
                print("about to exit..")
                sys.exit(17)

            def f2():
                f1()

            f2()
            """)

        # The important thing is for "coverage run" and "python" to have the
        # same output.  No traceback.
        status, out = self.run_command_status("coverage run exit.py")
        status2, out2 = self.run_command_status("python exit.py")
        self.assertMultiLineEqual(out, out2)
        self.assertMultiLineEqual(out, "about to exit..\n")
        self.assertEqual(status, status2)
        self.assertEqual(status, 17) 
Example #13
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_missing_source_file(self):
        # Check what happens if the source is missing when reporting happens.
        self.make_file("fleeting.py", """\
            s = 'goodbye, cruel world!'
            """)

        self.run_command("coverage run fleeting.py")
        os.remove("fleeting.py")
        out = self.run_command("coverage html -d htmlcov")
        self.assertRegex(out, "No source for code: '.*fleeting.py'")
        self.assertNotIn("Traceback", out)

        # It happens that the code paths are different for *.py and other
        # files, so try again with no extension.
        self.make_file("fleeting", """\
            s = 'goodbye, cruel world!'
            """)

        self.run_command("coverage run fleeting")
        os.remove("fleeting")
        status, out = self.run_command_status("coverage html -d htmlcov")
        self.assertRegex(out, "No source for code: '.*fleeting'")
        self.assertNotIn("Traceback", out)
        self.assertEqual(status, 1) 
Example #14
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_code_throws(self):
        self.make_file("throw.py", """\
            def f1():
                raise Exception("hey!")

            def f2():
                f1()

            f2()
            """)

        # The important thing is for "coverage run" and "python" to report the
        # same traceback.
        status, out = self.run_command_status("coverage run throw.py")
        out2 = self.run_command("python throw.py")
        if env.PYPY:
            # Pypy has an extra frame in the traceback for some reason
            out2 = re_lines(out2, "toplevel", match=False)
        self.assertMultiLineEqual(out, out2)

        # But also make sure that the output is what we expect.
        self.assertIn('File "throw.py", line 5, in f2', out)
        self.assertIn('raise Exception("hey!")', out)
        self.assertNotIn('coverage', out)
        self.assertEqual(status, 1) 
Example #15
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_code_exits(self):
        self.make_file("exit.py", """\
            import sys
            def f1():
                print("about to exit..")
                sys.exit(17)

            def f2():
                f1()

            f2()
            """)

        # The important thing is for "coverage run" and "python" to have the
        # same output.  No traceback.
        status, out = self.run_command_status("coverage run exit.py")
        status2, out2 = self.run_command_status("python exit.py")
        self.assertMultiLineEqual(out, out2)
        self.assertMultiLineEqual(out, "about to exit..\n")
        self.assertEqual(status, status2)
        self.assertEqual(status, 17) 
Example #16
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_warnings_during_reporting(self):
        # While fixing issue #224, the warnings were being printed far too
        # often.  Make sure they're not any more.
        self.make_file("hello.py", """\
            import sys, os, the_other
            print("Hello")
            """)
        self.make_file("the_other.py", """\
            print("What?")
            """)
        self.make_file(".coveragerc", """\
            [run]
            source =
                .
                xyzzy
            """)

        self.run_command("coverage run hello.py")
        out = self.run_command("coverage html")
        self.assertEqual(out.count("Module xyzzy was never imported."), 0) 
Example #17
Source File: test_process.py    From coveragepy with Apache License 2.0 6 votes vote down vote up
def make_b_or_c_py(self):
        """Create b_or_c.py, used in a few of these tests."""
        # "b_or_c.py b" will run 6 lines.
        # "b_or_c.py c" will run 7 lines.
        # Together, they run 8 lines.
        self.make_file("b_or_c.py", """\
            import sys
            a = 1
            if sys.argv[1] == 'b':
                b = 1
            else:
                c = 1
                c2 = 2
            d = 1
            print('done')
            """) 
Example #18
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_warn_preimported(self):
        self.make_file("hello.py", """\
            import goodbye
            import coverage
            cov = coverage.Coverage(include=["good*"], check_preimported=True)
            cov.start()
            print(goodbye.f())
            cov.stop()
            """)
        self.make_file("goodbye.py", """\
            def f():
                return "Goodbye!"
            """)
        goodbye_path = os.path.abspath("goodbye.py")

        out = self.run_command("python hello.py")
        self.assertIn("Goodbye!", out)

        msg = (
            "Coverage.py warning: "
            "Already imported a file that will be measured: {0} "
            "(already-imported)").format(goodbye_path)
        self.assertIn(msg, out) 
Example #19
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_lang_c(self):
        if env.JYTHON:
            # Jython as of 2.7.1rc3 won't compile a filename that isn't utf8.
            self.skipTest("Jython can't handle this test")
        # LANG=C forces getfilesystemencoding on Linux to 'ascii', which causes
        # failures with non-ascii file names. We don't want to make a real file
        # with strange characters, though, because that gets the test runners
        # tangled up.  This will isolate the concerns to the coverage.py code.
        # https://bitbucket.org/ned/coveragepy/issues/533/exception-on-unencodable-file-name
        self.make_file("weird_file.py", r"""
            globs = {}
            code = "a = 1\nb = 2\n"
            exec(compile(code, "wut\xe9\xea\xeb\xec\x01\x02.py", 'exec'), globs)
            print(globs['a'])
            print(globs['b'])
            """)
        self.set_environ("LANG", "C")
        out = self.run_command("coverage run weird_file.py")
        self.assertEqual(out, "1\n2\n") 
Example #20
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_deprecation_warnings(self):
        # Test that coverage doesn't trigger deprecation warnings.
        # https://bitbucket.org/ned/coveragepy/issue/305/pendingdeprecationwarning-the-imp-module
        self.make_file("allok.py", """\
            import warnings
            warnings.simplefilter('default')
            import coverage
            print("No warnings!")
            """)

        # Some of our testing infrastructure can issue warnings.
        # Turn it all off for the sub-process.
        self.del_environ("COVERAGE_TESTING")

        out = self.run_command("python allok.py")
        self.assertEqual(out, "No warnings!\n") 
Example #21
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def assert_tryexecfile_output(self, out1, out2):
        """Assert that the output we got is a successful run of try_execfile.py.

        `out1` and `out2` must be the same, modulo a few slight known platform
        differences.

        """
        # First, is this even credible try_execfile.py output?
        self.assertIn('"DATA": "xyzzy"', out1)

        if env.JYTHON:                  # pragma: only jython
            # Argv0 is different for Jython, remove that from the comparison.
            out1 = re_lines(out1, r'\s+"argv0":', match=False)
            out2 = re_lines(out2, r'\s+"argv0":', match=False)

        self.assertMultiLineEqual(out1, out2) 
Example #22
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_coverage_run_dir_is_like_python_dir(self):
        if env.PYVERSION == (3, 5, 4, 'final', 0):       # pragma: obscure
            self.skipTest("3.5.4 broke this: https://bugs.python.org/issue32551")
        with open(TRY_EXECFILE) as f:
            self.make_file("with_main/__main__.py", f.read())

        out_cov = self.run_command("coverage run with_main")
        out_py = self.run_command("python with_main")

        # The coverage.py results are not identical to the Python results, and
        # I don't know why.  For now, ignore those failures. If someone finds
        # a real problem with the discrepancies, we can work on it some more.
        ignored = r"__file__|__loader__|__package__"
        # PyPy includes the current directory in the path when running a
        # directory, while CPython and coverage.py do not.  Exclude that from
        # the comparison also...
        if env.PYPY:
            ignored += "|"+re.escape(os.getcwd())
        out_cov = re_lines(out_cov, ignored, match=False)
        out_py = re_lines(out_py, ignored, match=False)
        self.assert_tryexecfile_output(out_cov, out_py) 
Example #23
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_coverage_run_script_imports_doubledashsource(self):
        # This file imports try_execfile, which compiles it to .pyc, so the
        # first run will have __file__ == "try_execfile.py" and the second will
        # have __file__ == "try_execfile.pyc", which throws off the comparison.
        # Setting dont_write_bytecode True stops the compilation to .pyc and
        # keeps the test working.
        self.make_file("myscript", """\
            import sys; sys.dont_write_bytecode = True
            import process_test.try_execfile
            """)

        # These -m commands assume the coverage tree is on the path.
        out_cov = self.run_command(
            "coverage run --source process_test myscript"
        )
        out_py = self.run_command("python myscript")
        self.assert_tryexecfile_output(out_cov, out_py)

        st, out = self.run_command_status("coverage report")
        self.assertEqual(st, 0)
        self.assertEqual(self.line_count(out), 6, out) 
Example #24
Source File: exiftool.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def execute_json(self, *params):
        """Execute the given batch of parameters and parse the JSON output.

        This method is similar to :py:meth:`execute()`.  It
        automatically adds the parameter ``-j`` to request JSON output
        from ``exiftool`` and parses the output.  The return value is
        a list of dictionaries, mapping tag names to the corresponding
        values.  All keys are Unicode strings with the tag names
        including the ExifTool group name in the format <group>:<tag>.
        The values can have multiple types.  All strings occurring as
        values will be Unicode strings.  Each dictionary contains the
        name of the file it corresponds to in the key ``"SourceFile"``.

        The parameters to this function must be either raw strings
        (type ``str`` in Python 2.x, type ``bytes`` in Python 3.x) or
        Unicode strings (type ``unicode`` in Python 2.x, type ``str``
        in Python 3.x).  Unicode strings will be encoded using
        system's filesystem encoding.  This behaviour means you can
        pass in filenames according to the convention of the
        respective Python version – as raw strings in Python 2.x and
        as Unicode strings in Python 3.x.
        """
        params = map(fsencode, params)
        return json.loads(self.execute(b"-j", *params).decode("utf-8")) 
Example #25
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_excepthook_throw(self):
        if env.PYPY:
            self.skipTest("PyPy handles excepthook throws differently, punt for now.")
        self.make_file("excepthook_throw.py", """\
            import sys

            def excepthook(*args):
                # Write this message to stderr so that we don't have to deal
                # with interleaved stdout/stderr comparisons in the assertions
                # in the test.
                sys.stderr.write('in excepthook\\n')
                raise RuntimeError('Error Inside')

            sys.excepthook = excepthook

            raise RuntimeError('Error Outside')
            """)
        cov_st, cov_out = self.run_command_status("coverage run excepthook_throw.py")
        py_st, py_out = self.run_command_status("python excepthook_throw.py")
        if not env.JYTHON:
            self.assertEqual(cov_st, py_st)
            self.assertEqual(cov_st, 1)

        self.assertIn("in excepthook", py_out)
        self.assertEqual(cov_out, py_out) 
Example #26
Source File: exiftool.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def start(self):
        """Start an ``exiftool`` process in batch mode for this instance.

        This method will issue a ``UserWarning`` if the subprocess is
        already running.  The process is started with the ``-G`` and
        ``-n`` as common arguments, which are automatically included
        in every command you run with :py:meth:`execute()`.
        """
        if self.running:
            warnings.warn("ExifTool already running; doing nothing.")
            return
        with open(os.devnull, "w") as devnull:
            self._process = subprocess.Popen(
                [self.executable, "-stay_open", "True",  "-@", "-",
                 "-common_args", "-G", "-u", "-a", "-n"],
                stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                stderr=devnull)
        self.running = True 
Example #27
Source File: sconsign.py    From arnold-usd with Apache License 2.0 6 votes vote down vote up
def default_mapper(entry, name):
    '''
    Stringify an entry that doesn't have an explicit mapping.

    Args:
        entry:  entry
        name: field name

    Returns: str

    '''
    try:
        val = eval("entry." + name)
    except:
        val = None
    if sys.version_info.major >= 3 and isinstance(val, bytes):
        # This is a dirty hack for py 2/3 compatibility. csig is a bytes object
        # in Python3 while Python2 bytes are str. Hence, we decode the csig to a
        # Python3 string
        val = val.decode()
    return str(val) 
Example #28
Source File: test_process.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_warnings_trace_function_changed_with_threads(self):
        # https://bitbucket.org/ned/coveragepy/issue/164
        self.make_file("bug164.py", """\
            import threading
            import time

            class MyThread (threading.Thread):
                def run(self):
                    print("Hello")

            thr = MyThread()
            thr.start()
            thr.join()
            """)
        out = self.run_command("coverage run --timid bug164.py")

        self.assertIn("Hello\n", out)
        self.assertNotIn("warning", out) 
Example #29
Source File: pyexiftool.py    From elodie with Apache License 2.0 5 votes vote down vote up
def set_keywords_batch(self, mode, keywords, filenames):
        """Modifies the keywords tag for the given files.

        The first argument is the operation mode:
        KW_REPLACE: Replace (i.e. set) the full keywords tag with `keywords`.
        KW_ADD:     Add `keywords` to the keywords tag. 
                    If a keyword is present, just keep it.
        KW_REMOVE:  Remove `keywords` from the keywords tag. 
                    If a keyword wasn't present, just leave it.

        The second argument is an iterable of key words.    

        The third argument is an iterable of file names.

        The format of the return value is the same as for
        :py:meth:`execute()`.
        
        It can be passed into `check_ok()` and `format_error()`.
        """
        # Explicitly ruling out strings here because passing in a
        # string would lead to strange and hard-to-find errors
        if isinstance(keywords, basestring):
            raise TypeError("The argument 'keywords' must be "
                            "an iterable of strings")
        if isinstance(filenames, basestring):
            raise TypeError("The argument 'filenames' must be "
                            "an iterable of strings")
                
        params = []    
            
        kw_operation = {KW_REPLACE:"-%s=%s",
                        KW_ADD:"-%s+=%s",
                        KW_REMOVE:"-%s-=%s"}[mode]

        kw_params = [ kw_operation % (KW_TAGNAME, w)  for w in keywords ]
        
        params.extend(kw_params)            
        params.extend(filenames)
        logging.debug (params)
        return self.execute(*params) 
Example #30
Source File: pyexiftool.py    From elodie with Apache License 2.0 5 votes vote down vote up
def set_tags_batch(self, tags, filenames):
        """Writes the values of the specified tags for the given files.

        The first argument is a dictionary of tags and values.  The tag names may
        include group names, as usual in the format <group>:<tag>.

        The second argument is an iterable of file names.

        The format of the return value is the same as for
        :py:meth:`execute()`.
        
        It can be passed into `check_ok()` and `format_error()`.
        """
        # Explicitly ruling out strings here because passing in a
        # string would lead to strange and hard-to-find errors
        if isinstance(tags, basestring):
            raise TypeError("The argument 'tags' must be dictionary "
                            "of strings")
        if isinstance(filenames, basestring):
            raise TypeError("The argument 'filenames' must be "
                            "an iterable of strings")
                
        params = []
        params_utf8 = []
        for tag, value in tags.items():
            params.append(u'-%s=%s' % (tag, value))
            
        params.extend(filenames)
        params_utf8 = [x.encode('utf-8') for x in params]
        return self.execute(*params_utf8)