1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#
# encore/config.py
#
# Copyright (C) 2010 Damien Churchill <damoxc@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA.
#
import os
import json
import shutil
from xdg import BaseDirectory
from encore.component import Component
from encore.utils.oproxy import ObjectProxy
class Config(Component):
def __init__(self, test_dir=None):
super(Config, self).__init__('Config')
if test_dir is None:
self.resources = Resources()
else:
self.resources = Resources(config_testing_dir=test_dir)
self.cache_dir = self.resources.cache_dir
self.config_dir = self.resources.config_dir
self.data_dir = self.resources.data_dir
self.LOG_FILE = os.path.join(self.cache_dir, 'encore.log')
self.DB_FILE = os.path.join(self.cache_dir, 'media.db')
self.THUMB_DIR = os.path.join(self.cache_dir, 'thumbnails')
self.IMAGE_THUMB_DIR = os.path.join(self.THUMB_DIR, 'image')
self.VIDEO_THUMB_DIR = os.path.join(self.THUMB_DIR, 'video')
self.ALBUM_ART_DIR = os.path.join(self.cache_dir, 'album_art')
self.MOVIE_ART_DIR = os.path.join(self.cache_dir, 'movie_art')
self.read_config_file()
self.network_options = {
'type': 'local',
'host': 'localhost',
'port': 55545
}
class Resources(object):
"""
A warpper for the XDG directories. Also handles creation of a new setup
if the Encore directories within the XDG directories are missing.
"""
def __init__(self, resource='encore', config_testing_dir=None):
if config_testing_dir is None:
self.cache_dir = os.path.join(BaseDirectory.xdg_cache_home,
resource)
self.config_dir = os.path.join(BaseDirectory.xdg_config_home,
resource)
self.data_dir = os.path.join(BaseDirectory.xdg_data_home,
resource)
else:
self.cache_dir = os.path.join(config_testing_dir, 'cache')
self.config_dir = os.path.join(config_testing_dir, 'config')
self.data_dir = os.path.join(config_testing_dir, 'data')
# Ensure that the directories exist.
if not os.path.exists(self.cache_dir):
self.create_cache_hierarchy()
if not os.path.exists(self.config_dir):
self.create_configuration()
if not os.path.exists(self.data_dir):
self.create_initial_data()
def create_cache_hierarchy(self):
"""
Create the cache hierarchy that is assumed to exist.
"""
os.makedirs(os.path.join(self.cache_dir, 'album_art'))
os.makedirs(os.path.join(self.cache_dir, 'movie_art'))
os.makedirs(os.path.join(self.cache_dir, 'thumbnails', 'image'))
os.makedirs(os.path.join(self.cache_dir, 'thumbnails', 'video'))
def create_configuration(self):
"""
Create the user's configuration area and populate with the default
content configuration.
"""
def create_initial_data(self):
"""
Create the initial data directory and populate it with the default
data used by Encore.
"""
os.mkdir(self.data_dir)
config = ObjectProxy()
|