Back to index

plone3  3.1.7
Public Member Functions | Public Attributes
PlacelessTranslationService.msgfmt.Msgfmt Class Reference

List of all members.

Public Member Functions

def __init__
def readPoData
def add
def generate
def get
def getAsFile
def __call__

Public Attributes

 po
 name
 messages

Detailed Description

Definition at line 47 of file msgfmt.py.


Constructor & Destructor Documentation

def PlacelessTranslationService.msgfmt.Msgfmt.__init__ (   self,
  po,
  name = 'unknown' 
)

Definition at line 49 of file msgfmt.py.

00049 
00050     def __init__(self, po, name='unknown'):
00051         self.po = po
00052         self.name = name
00053         self.messages = {}


Member Function Documentation

Definition at line 178 of file msgfmt.py.

00178 
00179     def __call__(self):
00180         return self.getAsFile()

Here is the call graph for this function:

Here is the caller graph for this function:

def PlacelessTranslationService.msgfmt.Msgfmt.add (   self,
  id,
  str,
  fuzzy 
)

Definition at line 68 of file msgfmt.py.

00068 
00069     def add(self, id, str, fuzzy):
00070         "Add a non-empty and non-fuzzy translation to the dictionary."
00071         if str and not fuzzy:
00072             self.messages[id] = str

Here is the caller graph for this function:

Definition at line 73 of file msgfmt.py.

00073 
00074     def generate(self):
00075         "Return the generated output."
00076         keys = self.messages.keys()
00077         # the keys are sorted in the .mo file
00078         keys.sort()
00079         offsets = []
00080         ids = strs = ''
00081         for id in keys:
00082             # For each string, we need size and file offset.  Each string is NUL
00083             # terminated; the NUL does not count into the size.
00084             offsets.append((len(ids), len(id), len(strs), len(self.messages[id])))
00085             ids += id + '\0'
00086             strs += self.messages[id] + '\0'
00087         output = ''
00088         # The header is 7 32-bit unsigned integers.  We don't use hash tables, so
00089         # the keys start right after the index tables.
00090         # translated string.
00091         keystart = 7*4+16*len(keys)
00092         # and the values start after the keys
00093         valuestart = keystart + len(ids)
00094         koffsets = []
00095         voffsets = []
00096         # The string table first has the list of keys, then the list of values.
00097         # Each entry has first the size of the string, then the file offset.
00098         for o1, l1, o2, l2 in offsets:
00099             koffsets += [l1, o1+keystart]
00100             voffsets += [l2, o2+valuestart]
00101         offsets = koffsets + voffsets
00102         output = struct.pack("Iiiiiii",
00103                              0x950412deL,       # Magic
00104                              0,                 # Version
00105                              len(keys),         # # of entries
00106                              7*4,               # start of key index
00107                              7*4+len(keys)*8,   # start of value index
00108                              0, 0)              # size and offset of hash table
00109         output += array.array("i", offsets).tostring()
00110         output += ids
00111         output += strs
00112         return output
00113 

Here is the caller graph for this function:

Definition at line 114 of file msgfmt.py.

00114 
00115     def get(self):
00116         """ """
00117         ID = 1
00118         STR = 2
00119 
00120         section = None
00121         fuzzy = 0
00122 
00123         lines = self.readPoData()
00124 
00125         # Parse the catalog
00126         lno = 0
00127         for l in lines:
00128             lno += 1
00129             # If we get a comment line after a msgstr or a line starting with
00130             # msgid, this is a new entry
00131             # XXX: l.startswith('msgid') is needed because not all msgid/msgstr
00132             # pairs in the plone pos have a leading comment
00133             if (l[0] == '#' or l.startswith('msgid')) and section == STR:
00134                 self.add(msgid, msgstr, fuzzy)
00135                 section = None
00136                 fuzzy = 0
00137             # Record a fuzzy mark
00138             if l[:2] == '#,' and 'fuzzy' in l:
00139                 fuzzy = 1
00140             # Skip comments
00141             if l[0] == '#':
00142                 continue
00143             # Now we are in a msgid section, output previous section
00144             if l.startswith('msgid'):
00145                 section = ID
00146                 l = l[5:]
00147                 msgid = msgstr = ''
00148             # Now we are in a msgstr section
00149             elif l.startswith('msgstr'):
00150                 section = STR
00151                 l = l[6:]
00152             # Skip empty lines
00153             l = l.strip()
00154             if not l:
00155                 continue
00156             # XXX: Does this always follow Python escape semantics?
00157             # XXX: eval is evil because it could be abused
00158             try:
00159                 l = eval(l, globals())
00160             except Exception, msg:
00161                 raise PoSyntaxError('%s (line %d of po file %s): \n%s' % (msg, lno, self.name, l))
00162             if section == ID:
00163                 msgid += l
00164             elif section == STR:
00165                 msgstr += l
00166             else:
00167                 raise PoSyntaxError('error in line %d of po file %s' % (lno, self.name))
00168 
00169         # Add last entry
00170         if section == STR:
00171             self.add(msgid, msgstr, fuzzy)
00172 
00173         # Compute output
00174         return self.generate()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 175 of file msgfmt.py.

00175 
00176     def getAsFile(self):
00177         return StringIO(self.get())

Here is the caller graph for this function:

read po data from self.po and store it in self.poLines 

Definition at line 54 of file msgfmt.py.

00054 
00055     def readPoData(self):
00056         """ read po data from self.po and store it in self.poLines """
00057         output = []
00058         if isinstance(self.po, file):
00059             self.po.seek(0)
00060             output = self.po.readlines()
00061         if isinstance(self.po, list):
00062             output = self.po
00063         if isinstance(self.po, str):
00064             output = open(self.po, 'rb').readlines()
00065         if not output:
00066             raise ValueError, "self.po is invalid! %s" % type(self.po)
00067         return output

Here is the caller graph for this function:


Member Data Documentation

Definition at line 52 of file msgfmt.py.

Definition at line 51 of file msgfmt.py.

Definition at line 50 of file msgfmt.py.


The documentation for this class was generated from the following file: