1
# -*- coding: utf-8 -*-
2
# Copyright 2009 Canonical Ltd.
3
# Author 2009 Didier Roche
5
# This file is part of Quickly
7
#This program is free software: you can redistribute it and/or modify it
8
#under the terms of the GNU General Public License version 3, as published
9
#by the Free Software Foundation.
11
#This program is distributed in the hope that it will be useful, but
12
#WITHOUT ANY WARRANTY; without even the implied warranties of
13
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
#PURPOSE. See the GNU General Public License for more details.
16
#You should have received a copy of the GNU General Public License along
17
#with this program. If not, see <http://www.gnu.org/licenses/>.
25
from gettext import gettext as _
27
from quickly import quicklyconfig
29
def quickly_name(name):
30
"""Enforce quickly name to be ascii only and lowercase
32
return formated name"""
34
permitted_characters = string.ascii_lowercase
35
permitted_characters += "_"
37
if c not in permitted_characters:
39
ERROR: unpermitted character in name.
40
Letters and underscore ("_") only.""")
45
class project_path_not_found(Exception):
48
def get_quickly_data_path():
49
"""Retrieve quickly data path
51
This path is by default <quickly_lib_path>/../data/ in trunk
52
and /usr/share/quickly in an installed version but this path
53
is specified at installation time.
56
# get pathname absolute or relative
57
if quicklyconfig.__quickly_data_directory__.startswith('/'):
58
pathname = quicklyconfig.__quickly_data_directory__
60
pathname = os.path.dirname(__file__) + '/' + quicklyconfig.__quickly_data_directory__
61
abs_data_path = os.path.abspath(pathname)
63
if os.path.exists(abs_data_path):
66
raise project_path_not_found
68
def get_template_directories():
69
"""Retrieve all directories where quickly templates are
71
:return a list of directories
74
# default to looking up templates in the current dir
75
template_directories = []
76
if os.path.exists(os.path.expanduser('~/.quickly-data/templates')):
77
template_directories.append(os.path.expanduser('~/.quickly-data/templates'))
79
# retrieve from trunk or installed version
80
abs_template_path = get_quickly_data_path() + '/templates/'
81
if os.path.exists(abs_template_path):
82
template_directories.append(abs_template_path)
84
if not template_directories:
85
print _("No template directory found. Aborting")
88
return template_directories
91
def get_template_directory(template):
92
"""Detect where the quickly template and if it exists"""
94
# check for the first available template in template_directories
95
for template_directory in get_template_directories():
96
template_path = template_directory + "/" + template
97
if os.path.exists(template_path):
100
template_found = False
102
# if still false, no template found in template_directories
103
if not template_found:
104
print _("ERROR: Template '%s' not found.") % template
111
def get_root_project_path(config_file_path=None):
112
"""Try to guess where the .quickly config file is.
114
config_file_path is optional (needed by the create command, for instance).
115
getcwd() is taken by default.
116
If nothing found, try to find it up to 6 parent directory
118
:return project_path. Raise a project_path_not_found elsewhere.
121
if config_file_path is None:
122
current_path = os.getcwd()
124
current_path = config_file_path
126
for related_directory in ('./', './', '../', '../../',
127
'../../../', '../../../../',
128
'../../../../../', '../../../../../../'):
129
quickly_file_path = os.path.abspath(current_path + '/' + related_directory)
130
quickly_file = quickly_file_path + "/.quickly"
131
if os.path.isfile(quickly_file):
132
return quickly_file_path
133
raise project_path_not_found()
135
def apply_file_rights(src_file_name, dest_file_name):
136
"""Keep file rights from src to dest"""
138
st = os.stat(src_file_name)
139
mode = stat.S_IMODE(st.st_mode)
140
os.chmod(dest_file_name, mode)