# Copyright (C) 2010 Leonard Thomas
#
# This file is part of Dodai.
#
# Dodai 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 of the License, or
# (at your option) any later version.
#
# Dodai 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 Dodai. If not, see .
import sys
import os
import platform
from dodai.tools.odict import OrderedDict
import unicodedata
def home_directory():
"""Returns the full real path to the home directory of the user who
is executing this script.
"""
out = None
try:
from win32com.shell import shellcon, shell
out = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
except ImportError:
out = os.path.expanduser("~")
return os.path.realpath(out)
def config_directory_system(project_name=None):
"""Returns the system config directory with the passed in project
appended to the path. Returns None for windows and java systems.
"""
path = None
if platform.system and platform.system() not in ['Windows', 'Java']:
if project_name:
path = os.path.join('/etc', project_name.strip())
else:
path = '/etc'
return path
def config_directory_user(project_name):
"""Returns the config direcotry of the project which is located in
the user's home directory. Returns None for Java and unknown systems
"""
path = None
project = '.{0}'.format(project_name.strip())
if platform.system and platform.system() not in ['Java']:
home_dir = home_directory()
path = os.path.join(home_dir, project)
return path
def config_directory_project():
"""Returns the config directory that is located in the same directory
of the executable that ran this script
"""
path = os.path.dirname(os.path.abspath(sys.argv[0]))
return os.path.join(path, 'config')
def config_directories(project_name):
"""Returns a list of possible project directories
"""
dirs = []
dir = config_directory_system(project_name)
if dir:
dirs.append(dir)
dir = config_directory_user(project_name)
if dir:
dirs.append(dir)
dir = config_directory_project()
if dir:
dirs.append(dir)
return dirs
def list_to_english(data):
"""Takes the input list and creates a string with each option
encased in single quotes and seperated by a comma with exception
of the last option which is prefixed with the word 'and'
"""
if data:
if len(data) > 1:
out = []
last = "{0}".format(data.pop())
for row in data:
out.append("{0}".format(row))
out = ', '.join(out)
return "{0} and {1}".format(out, last)
else:
return "{0}".format(data.pop())
def quote_list(data, double_quotes=False):
"""Takes the give list (data) and adds quotes around each item. Returns
a list
"""
out = []
if double_quotes:
quote = '"'
else:
quote = "'"
for item in data:
item = "{quote}{item}{quote}".format(quote=quote, item=item)
out.append(item)
return out
def normalize_unicode(data):
"""Normalizes the unicode data so that compare functions will work
correctly.
"""
data = unicode(data)
data = unicodedata.normalize('NFC', data)
return data
class Section(OrderedDict):
"""An ordered dictionary object that has the added benfit of holding
a name or title of this grouping.
"""
def __init__(self, title):
self.set_section_title(title)
super(Section,self).__init__()
def get_section_title(self):
return self.___title___
def set_section_title(self, val):
self.___title___ = val
def __repr__(self):
return "".format(self.get_section_title())