~ubuntu-branches/ubuntu/utopic/lazygal/utopic

« back to all changes in this revision

Viewing changes to lazygal/pathutils.py

  • Committer: Package Import Robot
  • Author(s): Michal Čihař, Michal Čihař, Jakub Wilk
  • Date: 2013-06-06 12:05:08 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20130606120508-e3g94vl8w9pw7za7
Tags: 0.8-1
[ Michal Čihař ]
* New upstream release.
  - Uses new Genshi templates (Closes: #696682).
  - Correctly handles wronly encoded artist in EXIF (Closes: #696648).
  - Uses GExiv2 (LP: #1074028).
* Depend on GExiv2 instead of pyexiv2.
* Bump standards to 3.9.4.
* Use debhelper 9.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Lazygal, a lazy static web gallery generator.
2
 
# Copyright (C) 2011-2012 Alexandre Rossi <alexandre.rossi@gmail.com>
 
1
# Deejayd, a media player daemon
 
2
# Copyright (C) 2013 Mickael Royer <mickael.royer@gmail.com>
 
3
#                    Alexandre Rossi <alexandre.rossi@gmail.com>
3
4
#
4
5
# This program is free software; you can redistribute it and/or modify
5
6
# it under the terms of the GNU General Public License as published by
8
9
#
9
10
# This program is distributed in the hope that it will be useful,
10
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
13
# GNU General Public License for more details.
13
14
#
14
15
# You should have received a copy of the GNU General Public License along
15
16
# with this program; if not, write to the Free Software Foundation, Inc.,
16
17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
 
18
 
import os, sys
 
19
 
 
20
import os
 
21
import sys
19
22
import posixpath
 
23
import logging
20
24
 
21
25
 
22
26
def is_root_posix(path):
24
28
 
25
29
 
26
30
def is_root_win32(path):
27
 
    return path[1:] == ':\\' # strip drive letter in comparison
 
31
    return path[1:] == ':\\'  # strip drive letter in comparison
28
32
 
29
33
 
30
34
if sys.platform == 'win32':
33
37
    is_root = is_root_posix
34
38
 
35
39
 
36
 
def path2unicode(path):
 
40
def path2unicode(path, errors='strict'):
37
41
    if type(path) is unicode:
38
42
        return path
39
43
    else:
40
 
        return path.decode(sys.getfilesystemencoding())
 
44
        return path.decode(sys.getfilesystemencoding(), errors)
41
45
 
42
46
 
43
47
def is_subdir_of(dir_path, path):
74
78
    return posixpath.join(*path_list)
75
79
 
76
80
 
 
81
def walk(top, walked=None, topdown=False):
 
82
    """
 
83
    This is a wrapper around os.walk() from the standard library:
 
84
    - following symbolic links on directories
 
85
    - whith barriers in place against walking twice the same directory,
 
86
      which may happen when two directory trees have symbolic links to
 
87
      each other's contents.
 
88
    """
 
89
    if walked is None: walked = []
 
90
 
 
91
    for root, dirs, files in os.walk(top, topdown=topdown):
 
92
        walked.append(os.path.realpath(root))
 
93
 
 
94
        # Follow symlinks if they have not been walked yet
 
95
        for d in dirs:
 
96
            d_path = os.path.join(root, d)
 
97
            if os.path.islink(d_path):
 
98
                if os.path.realpath(d_path) not in walked:
 
99
                    for x in walk(d_path, walked):
 
100
                        yield x
 
101
                else:
 
102
                    logging.error("Not following symlink '%s' because directory has already been processed." % d_path)
 
103
 
 
104
        yield root, dirs, files
 
105
 
 
106
 
 
107
def walk_and_do(top=None, walked=None, dcb=None, fcb=None, topdown=False):
 
108
    """
 
109
    This walk calls dcb on each found directory and fcb on each found
 
110
    file with the following arguments :
 
111
        - path
 
112
    """
 
113
    for root, dirs, files in walk(top, walked, topdown=topdown):
 
114
        if dcb is not None:
 
115
            dcb(root, dirs, files)
 
116
        if fcb is not None:
 
117
            map(lambda f: fcb(os.path.join(root, f)), files)
 
118
 
 
119
 
77
120
# vim: ts=4 sw=4 expandtab