~ubuntu-branches/ubuntu/precise/ubuntuone-control-panel/precise

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/utils/linux.py

  • Committer: Package Import Robot
  • Author(s): Natalia Bidart (nessita)
  • Date: 2012-03-21 14:11:29 UTC
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: package-import@ubuntu.com-20120321141129-c444j5xs1pf8rsfv
Tags: upstream-2.99.91
ImportĀ upstreamĀ versionĀ 2.99.91

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright 2012 Canonical Ltd.
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License version 3, as published
 
7
# by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
# PURPOSE.  See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along
 
15
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
"""Miscelaneous functions and constants for linux."""
 
18
 
 
19
import codecs
 
20
import os
 
21
 
 
22
from dirspec.basedir import xdg_config_home
 
23
from ubuntuone.controlpanel.logger import setup_logging
 
24
 
 
25
 
 
26
logger = setup_logging('utils.linux')
 
27
 
 
28
 
 
29
def default_folders(user_home='', dirs_path=None):
 
30
    """Return a list of the folders to add by default."""
 
31
    result = []
 
32
 
 
33
    if dirs_path is None:
 
34
        dirs_path = os.path.join(xdg_config_home, u'user-dirs.dirs')
 
35
 
 
36
    if not os.path.exists(dirs_path):
 
37
        logger.warning('default_folders: dirs_path %r does not exist.',
 
38
                       dirs_path)
 
39
        return result
 
40
 
 
41
    # pylint: disable=W0702
 
42
 
 
43
    try:
 
44
        with codecs.open(dirs_path, encoding='utf-8') as f:
 
45
            for line in f:
 
46
                if line.startswith(u'#'):
 
47
                    continue
 
48
 
 
49
                try:
 
50
                    _, value = line.strip().split(u'=')
 
51
                    value = value.strip(u'"').replace(u'$HOME', user_home)
 
52
                except:
 
53
                    logger.exception('default_folders: can not row %r:', line)
 
54
                else:
 
55
                    result.append(value)
 
56
    except:
 
57
        logger.exception('default_folders: can not load file %r:', dirs_path)
 
58
 
 
59
    return result