00001 import silme.io
00002 import silme.format
00003 from silme.io.clients import IOClient, FileFormatClient
00004 from silme.core.object import L10nPackage, L10nObject, EntityList, Blob
00005
00006 import os
00007 import sys
00008 import codecs
00009
00010 def register(Manager):
00011 Manager.register(FileClient)
00012
00013 class FileClient (FileFormatClient):
00014 name = 'file'
00015 desc = "File Client"
00016 type = IOClient.__name__
00017
00018 @classmethod
00019 def matches_path(cls, path):
00020 """
00021 tests if the ioclient should be used for this type of path
00022 Matches any:
00023 1) "/foo"
00024 2) "./foo"
00025 3) "file://foo"
00026 """
00027 return path.startswith('/') or \
00028 path.startswith('./') or \
00029 path.startswith('../') or \
00030 path.startswith('file://')
00031
00032 @classmethod
00033 def get_l10npackage(cls, path,
00034 code='default',
00035 object_type='l10nobject',
00036 source=None,
00037 ignore=['CVS','.svn','.DS_Store', '.hg']):
00038 l10npackage = FileFormatClient.get_l10npackage(path, code, object_type, source, ignore)
00039 (b_source, oe_source) = cls._get_source_policy(source)
00040
00041 try:
00042 l = os.listdir(path)
00043 except OSError, e:
00044 raise OSError('Not a directory: ' + path + ': ' + str(e))
00045 except Exception,e:
00046 raise Exception('Could not load directory: ' + path + ': ' + str(e))
00047 for elem in l:
00048 p2 = os.path.join(path, elem)
00049 if cls._should_ignore(ignore, path=p2, elems=[elem]):
00050 continue
00051
00052 if os.path.isdir(p2):
00053 l10npackage.packages[elem] = cls.get_l10npackage(p2, code=code, object_type=object_type, source=source, ignore=ignore)
00054 else:
00055 try:
00056 parser = silme.format.Manager.get(path=elem)
00057 except Exception:
00058 l10npackage.objects[elem] = cls.get_blob(p2, source=b_source)
00059 else:
00060 if object_type=='object':
00061 l10npackage.objects[elem] = cls.get_blob(p2, source=b_source)
00062 elif object_type=='entitylist':
00063 l10npackage.objects[elem] = cls.get_entitylist(p2, source=oe_source, code=code, parser=parser)
00064 else:
00065 l10npackage.objects[elem] = cls.get_l10nobject(p2, source=oe_source, code=code, parser=parser)
00066
00067 return l10npackage
00068
00069 @classmethod
00070 def write_blob(cls, blob, path):
00071 cls.write_source(blob.source,
00072 os.path.join(path, blob.id),
00073 encoding=None)
00074
00075 @classmethod
00076 def write_entitylist(cls, elist, path, encoding=None):
00077 if encoding is None and hasattr(elist, 'encoding'):
00078 encoding = elist.encoding
00079 try:
00080 format_parser = silme.format.Manager.get(path=elist.id)
00081 except Exception:
00082 raise Exception('No parser for given object type')
00083 string = format_parser.dump_entitylist(elist)
00084 cls.write_source(string,
00085 os.path.join(path, elist.id),
00086 encoding)
00087 return True
00088
00089 @classmethod
00090 def write_l10nobject(cls, object, path, encoding=None):
00091 if encoding is None and hasattr(object, 'encoding'):
00092 encoding = object.encoding
00093 try:
00094 format_parser = silme.format.Manager.get(path=object.id)
00095 except Exception:
00096 raise Exception('No parser for given object type ('+object.id+')')
00097 string = format_parser.dump_l10nobject(object)
00098 cls.write_source(string,
00099 os.path.join(path, object.id),
00100 format_parser.encoding)
00101 return True
00102
00103 @classmethod
00104 def write_object(cls, object, path, encoding=None):
00105 if isinstance(object, L10nObject):
00106 cls.write_l10nobject(object, path, encoding=encoding)
00107 elif isinstance(object, EntityList):
00108 cls.write_entitylist(object, path, encoding=encoding)
00109 elif isinstance(object, Blob):
00110 cls.write_blob(object, path)
00111 else:
00112 raise TypeError('Cannot write object of such type (%s)' % object.__class__.__name__)
00113
00114 @classmethod
00115 def write_l10npackage(cls, l10npack, path):
00116 if not os.path.isdir(path):
00117 os.makedirs(path)
00118 for i in l10npack.packages:
00119 if not os.path.isdir(os.path.join(path, i)):
00120 os.mkdir(os.path.join(path, i))
00121 cls.write_l10npackage(l10npack.packages[i], os.path.join(path, i))
00122 for i in l10npack.objects:
00123 cls.write_object(l10npack.objects[i], path)
00124 return True
00125
00126 @classmethod
00127 def write_source(cls, content, path, encoding=None):
00128 if encoding is None:
00129 f = open(path, mode='wb')
00130 f.write(content)
00131 f.close()
00132 else:
00133 cls._write_source_with_encoding(content, path, encoding=encoding)
00134 return True
00135
00136 @classmethod
00137 def path_type(cls, path):
00138 """
00139 returns 'package', 'object' depending on the path type
00140 """
00141 if os.path.isdir(path):
00142 return 'package'
00143 else:
00144 return 'object'
00145
00146
00147
00148 @classmethod
00149 def _write_source_with_encoding(cls, content, path, encoding=None):
00150 if encoding is None:
00151 encoding = 'utf_8'
00152 f = codecs.open(path, encoding=encoding, mode='w+')
00153 try:
00154
00155 if encoding in cls.bomdict.keys():
00156 f.write(cls.bomdict[encoding])
00157 f.write(content)
00158 except UnicodeEncodeError:
00159
00160 f = codecs.open(path, encoding='utf_8', mode='w+')
00161 try:
00162 f.write(content)
00163 except:
00164 raise Exception('could not write file: '+path)
00165 f.close()
00166 return True
00167
00168 @classmethod
00169 def _read_with_encoding(cls, path, encoding):
00170 file = codecs.open(path, 'rU', encoding=encoding)
00171 text = file.read()
00172 file.close()
00173 return text
00174
00175 @classmethod
00176 def _read_without_encoding(cls, path):
00177 file = open(path, 'rb')
00178 text = file.read()
00179 file.close()
00180 return text
00181