def update(existing, add=False, **kwargs): """Update an existing header dictionary with the values in kwargs, adding new values only if add is true. @return: Updated dictionary of header entries @rtype: dict """ headerargs = dictutils.ordereddict() fixedargs = dictutils.cidict() for key, value in kwargs.items(): key = key.replace("_", "-") if key.islower(): key = key.title() fixedargs[key] = value removed = [] for key in poheader.header_order: if existing.has_key(key): if key in fixedargs: headerargs[key] = fixedargs.pop(key) else: headerargs[key] = existing[key] removed.append(key) elif add and fixedargs.has_key(key): headerargs[key] = fixedargs.pop(key) for key, value in existing.iteritems(): if not key in removed: headerargs[key] = value if add: for key in fixedargs: headerargs[key] = fixedargs[key] return headerargs
def test_update(): '''test the update function''' # do we really add nothing if add==False ? d = poheader.update({}, test='hello') assert len(d) == 0 # do we add if add==True ? d = poheader.update({}, add=True, Test='hello') assert len(d) == 1 assert d['Test'] == 'hello' # do we really update ? d = poheader.update({'Test': 'hello'}, add=True, Test='World') assert len(d) == 1 assert d['Test'] == 'World' # does key rewrite work ? d = poheader.update({}, add=True, test_me='hello') assert d['Test-Me'] == 'hello' # is the order correct ? d = ordereddict() d['Project-Id-Version'] = 'abc' d['POT-Creation-Date'] = 'now' d = poheader.update(d, add=True, Test='hello', Report_Msgid_Bugs_To='*****@*****.**') assert d.keys()[0] == "Project-Id-Version" assert d.keys()[1] == "Report-Msgid-Bugs-To" assert d.keys()[2] == "POT-Creation-Date" assert d.keys()[3] == "Test"
def update(existing, add=False, **kwargs): """Update an existing header dictionary with the values in kwargs, adding new values only if add is true. :return: Updated dictionary of header entries :rtype: dict """ headerargs = dictutils.ordereddict() fixedargs = dictutils.cidict() for key, value in kwargs.items(): key = key.replace("_", "-") if key.islower(): key = key.title() fixedargs[key] = value removed = [] for key in poheader.header_order: if key in existing: if key in fixedargs: headerargs[key] = fixedargs.pop(key) else: headerargs[key] = existing[key] removed.append(key) elif add and key in fixedargs: headerargs[key] = fixedargs.pop(key) for key, value in existing.iteritems(): if not key in removed: headerargs[key] = value if add: for key in fixedargs: headerargs[key] = fixedargs[key] return headerargs
class el(common.Common): """This class represents Greek.""" # Greek uses ; as question mark and the middot instead sentenceend = ".!;…" sentencere = re.compile( r""" (?s) # make . also match newlines .*? # anything, but match non-greedy [%s] # the puntuation for sentence ending \s+ # the spacing after the puntuation (?=[^a-zά-ώ\d]) # lookahead that next part starts with caps """ % sentenceend, re.VERBOSE | re.UNICODE) puncdict = ordereddict([ (";", "·"), ("?", ";"), ]) # Valid latin characters for use as accelerators valid_latin_accel = ("abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "1234567890") # Valid greek characters for use as accelerators (accented characters # and "ς" omitted) valid_greek_accel = ("αβγδεζηθικλμνξοπρστυφχψω" "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ") # Valid accelerators validaccel = "".join([valid_latin_accel, valid_greek_accel])
def makeheaderdict(self, charset="CHARSET", encoding="ENCODING", project_id_version=None, pot_creation_date=None, po_revision_date=None, last_translator=None, language_team=None, mime_version=None, plural_forms=None, report_msgid_bugs_to=None, **kwargs): """Create a header dictionary with useful defaults. pot_creation_date can be None (current date) or a value (datetime or string) po_revision_date can be None (form), False (=pot_creation_date), True (=now), or a value (datetime or string) :return: Dictionary with the header items :rtype: dict """ if project_id_version is None: project_id_version = "PACKAGE VERSION" if pot_creation_date is None or pot_creation_date == True: pot_creation_date = time.strftime("%Y-%m-%d %H:%M") + tzstring() if isinstance(pot_creation_date, time.struct_time): pot_creation_date = time.strftime("%Y-%m-%d %H:%M", pot_creation_date) + tzstring() if po_revision_date is None: po_revision_date = "YEAR-MO-DA HO:MI+ZONE" elif po_revision_date == False: po_revision_date = pot_creation_date elif po_revision_date == True: po_revision_date = time.strftime("%Y-%m-%d %H:%M") + tzstring() if isinstance(po_revision_date, time.struct_time): po_revision_date = time.strftime("%Y-%m-%d %H:%M", po_revision_date) + tzstring() if last_translator is None: last_translator = "FULL NAME <EMAIL@ADDRESS>" if language_team is None: language_team = "LANGUAGE <*****@*****.**>" if mime_version is None: mime_version = "1.0" if report_msgid_bugs_to is None: report_msgid_bugs_to = "" defaultargs = dictutils.ordereddict() defaultargs["Project-Id-Version"] = project_id_version defaultargs["Report-Msgid-Bugs-To"] = report_msgid_bugs_to defaultargs["POT-Creation-Date"] = pot_creation_date defaultargs["PO-Revision-Date"] = po_revision_date defaultargs["Last-Translator"] = last_translator defaultargs["Language-Team"] = language_team defaultargs["MIME-Version"] = mime_version defaultargs["Content-Type"] = "text/plain; charset=%s" % charset defaultargs["Content-Transfer-Encoding"] = encoding if plural_forms: defaultargs["Plural-Forms"] = plural_forms defaultargs["X-Generator"] = self.x_generator return update(defaultargs, add=True, **kwargs)
def makeheaderdict(self, charset="CHARSET", encoding="ENCODING", project_id_version=None, pot_creation_date=None, po_revision_date=None, last_translator=None, language_team=None, mime_version=None, plural_forms=None, report_msgid_bugs_to=None, **kwargs): """Create a header dictionary with useful defaults. pot_creation_date can be None (current date) or a value (datetime or string) po_revision_date can be None (form), False (=pot_creation_date), True (=now), or a value (datetime or string) @return: Dictionary with the header items @rtype: dict """ if project_id_version is None: project_id_version = "PACKAGE VERSION" if pot_creation_date is None or pot_creation_date == True: pot_creation_date = time.strftime("%Y-%m-%d %H:%M") + tzstring() if isinstance(pot_creation_date, time.struct_time): pot_creation_date = time.strftime("%Y-%m-%d %H:%M", pot_creation_date) + tzstring() if po_revision_date is None: po_revision_date = "YEAR-MO-DA HO:MI+ZONE" elif po_revision_date == False: po_revision_date = pot_creation_date elif po_revision_date == True: po_revision_date = time.strftime("%Y-%m-%d %H:%M") + tzstring() if isinstance(po_revision_date, time.struct_time): po_revision_date = time.strftime("%Y-%m-%d %H:%M", po_revision_date) + tzstring() if last_translator is None: last_translator = "FULL NAME <EMAIL@ADDRESS>" if language_team is None: language_team = "LANGUAGE <*****@*****.**>" if mime_version is None: mime_version = "1.0" if report_msgid_bugs_to is None: report_msgid_bugs_to = "" defaultargs = dictutils.ordereddict() defaultargs["Project-Id-Version"] = project_id_version defaultargs["Report-Msgid-Bugs-To"] = report_msgid_bugs_to defaultargs["POT-Creation-Date"] = pot_creation_date defaultargs["PO-Revision-Date"] = po_revision_date defaultargs["Last-Translator"] = last_translator defaultargs["Language-Team"] = language_team defaultargs["MIME-Version"] = mime_version defaultargs["Content-Type"] = "text/plain; charset=%s" % charset defaultargs["Content-Transfer-Encoding"] = encoding if plural_forms: defaultargs["Plural-Forms"] = plural_forms defaultargs["X-Generator"] = self.x_generator return update(defaultargs, add=True, **kwargs)
def parseheaderstring(input): """Parses an input string with the definition of a PO header and returns the interpreted values as a dictionary.""" headervalues = dictutils.ordereddict() for line in input.split("\n"): if not line or ":" not in line: continue key, value = line.split(":", 1) #We don't want unicode keys key = str(key.strip()) headervalues[key] = value.strip() return headervalues
def test_add(): d = dictutils.ordereddict() d[2] = 3 assert len(d.order) == 1
def test_pop(): d = dictutils.ordereddict() d[2] = 3 value = d.pop(2) assert len(d.order) == 0 assert value == 3
def test_delete(): d = dictutils.ordereddict() d[2] = 3 del d[2] assert len(d.order) == 0