Silme:Tutorial:FormatParser

From Braniecki's Wiki
Jump to navigation Jump to search

In the previous chapter we were talking about loading/saving a file.

Fortunately we made it without actually thinking on how the library know how to parse files. In this chapter, we'll review this topic.

In most cases IOClient is able to read and write a stream of data to file system, zip package or RCS repository. But as we work on localization tools, we need to operate on the L10nObject structure that has entities, comments and potentially other data inside. To do this we have to parse this input string and then we have to serialize the object back into string.

For this we have a FormatParser package.

FPManager

FPManager is very simple to IOManager and serves as a controller class for FormatParsers.

from mozilla.l10n.object import L10nObject, L10nPackage from mozilla.l10n.fp.FPManager import FPManager from mozilla.l10n.fp.object.dtd import DTDFormatParser

fp = FPManager.get('dtd')

string = '<!ENTITY test "value">\n\n<!ENTITY test2 "value2">\n\n\n'

l10nObject = fp.buildL10nObjectFromString(string)

In many cases you will not need to use it because it's automagically used by IOClient without you having to bother about it. You control the list of formats the system supports by adding or removing imports.

When you import a format parser, it will be used while reading L10nPackages and L10nObjects without any more work. But you can also use FormatParsers on your own if you want.

FormatParser

reading

In the basic situation you can use FormatParser to parse a string into L10nObject or EntityList:

from mozilla.l10n.object import L10nObject, L10nPackage from mozilla.l10n.fp.FPManager import FPManager from mozilla.l10n.fp.object.dtd import DTDFormatParser

fp = FPManager.get('dtd')

string = '<!ENTITY test "value">\n\n<!ENTITY test2 "value2">\n\n\n'

l10nObject = fp.buildL10nObjectFromString(string)

entityList = fp.buildEntityListFromString(string)

Building EntityList is of course faster because it will ignore strings and comments and all other potentially meaningful content.

writing

But the major drawback from using EntityList instead of L10nObject is that you loose ability to write it back to the original file and have it 100% the same.

FormatParser is able to serialize any EntityList or L10nObject but it will use generic template if it's not writing an L10nObject and if the L10nObject has not been read from the same format.

from mozilla.l10n.object import L10nObject, L10nPackage from mozilla.l10n.fp.FPManager import FPManager from mozilla.l10n.fp.object.dtd import DTDFormatParser from mozilla.l10n.fp.object.gettext import PoFormatParser

fp = FPManager.get('dtd') fp2 = FPManager.get('po') string = '<!ENTITY test "value">\n\n<!ENTITY test2 "value2">\n\n\n' l10nObject = fp.buildL10nObjectFromString(string) entityList = fp.buildEntityListFromString(string)

string2 = fp2.dumpL10nObjectToString(l10nObject) # it will create a very simple PO object with the data from L10nObject

string3 = fp.dumpEntityListToString(entityList) # it will create a simple DTD string without any data beside of generic entities from the list