Difference between revisions of "Silme:Tutorial:IOClient"

From Braniecki's Wiki
Jump to navigation Jump to search
Line 13: Line 13:
  
 
IOClient is an abstract class that is a base for every I/O client that can read and write objects. For example a FileClient can read a file into memory and write it back to the hard drive. Or it can load directory as L10nPackage. SvnClient can load L10nPackage from the directory in SVN repository, JarClient can load a '''*.jar''' file as L10nPackage and SQLiteClient can read tables as L10nObjects.
 
IOClient is an abstract class that is a base for every I/O client that can read and write objects. For example a FileClient can read a file into memory and write it back to the hard drive. Or it can load directory as L10nPackage. SvnClient can load L10nPackage from the directory in SVN repository, JarClient can load a '''*.jar''' file as L10nPackage and SQLiteClient can read tables as L10nObjects.
 +
 +
=== Reading ===
  
 
In the simple case you just use IOManager to load a proper IOClient and then interact with selected type of IO. For example:
 
In the simple case you just use IOManager to load a proper IOClient and then interact with selected type of IO. For example:
Line 25: Line 27:
 
l10nPackage = ioClient.getL10nPackage(path='./test/')
 
l10nPackage = ioClient.getL10nPackage(path='./test/')
  
 +
 +
l10nObject = ioClient.getL10nObject(path='./test/example.dtd')
  
 
ioClient2 = IOManager.get('jar')
 
ioClient2 = IOManager.get('jar')
Line 31: Line 35:
  
 
In result we have two l10nPackages, one from the directory on the hard drive, and another extracted from the .jar file.
 
In result we have two l10nPackages, one from the directory on the hard drive, and another extracted from the .jar file.
 +
Also, we have an l10nObject from the hard drive.
 +
 +
 +
=== Writing ===
 +
 +
To write down an L10nObject or L10nPackage you simply use the same IOClient:
 +
 +
<code lang="python">
 +
from mozilla.l10n.object import L10nObject, L10nPackage
 +
from mozilla.l10n.io.IOManager import IOManager
 +
from mozilla.l10n.io.file import FileClient
 +
 +
ioClient = IOManager.get('file')
 +
ioClient.writeL10nPackage(l10nPackage, path='../newpath')
 +
 +
ioClient.writeObject(l10nObject, path='../../example.dtd')
 +
 +
</code>
 +
 +
IOClient lets you read and write between the objects in the memory of the program and an actual storage of the data. It may be a database, hard drive, zip package or an RCS.

Revision as of 00:25, 26 June 2008

As you probably noticed, until now we did not get into any way to actually operate on the l10n objects outside of memory. By this moment you should know how to create an Entity, Comment, how to create an EntityList, L10nObject and L10nPackage and bundle it all together.

Now we'll focus on the ways you can use to load/save those objects to actually be able to use the system in the real life.

There are two levels of this operations. First one is an actual loading entities from somewhere to internal structure like EntityList, L10nObject or L10nPackage and saving it to some place. This is covered by IOClient family.

IOManager

IOManager class is a command center for all kinds of IOClients classes. It means that you should use it to load a proper I/O class. Currently, the project supports several I/O's like file, svn, sqlite, jar.


IOClient

IOClient is an abstract class that is a base for every I/O client that can read and write objects. For example a FileClient can read a file into memory and write it back to the hard drive. Or it can load directory as L10nPackage. SvnClient can load L10nPackage from the directory in SVN repository, JarClient can load a *.jar file as L10nPackage and SQLiteClient can read tables as L10nObjects.

Reading

In the simple case you just use IOManager to load a proper IOClient and then interact with selected type of IO. For example:

from mozilla.l10n.object import L10nObject, L10nPackage from mozilla.l10n.io.IOManager import IOManager from mozilla.l10n.io.file import FileClient from mozilla.l10n.io.jar import JarClient

ioClient = IOManager.get('file') # you can you other types here like: jar, sqlite, mysql, svn l10nPackage = ioClient.getL10nPackage(path='./test/')


l10nObject = ioClient.getL10nObject(path='./test/example.dtd')

ioClient2 = IOManager.get('jar') l10nPackage2 = ioClient.getL10nPackage(path='./test.jar')

In result we have two l10nPackages, one from the directory on the hard drive, and another extracted from the .jar file. Also, we have an l10nObject from the hard drive.


Writing

To write down an L10nObject or L10nPackage you simply use the same IOClient:

from mozilla.l10n.object import L10nObject, L10nPackage from mozilla.l10n.io.IOManager import IOManager from mozilla.l10n.io.file import FileClient

ioClient = IOManager.get('file') ioClient.writeL10nPackage(l10nPackage, path='../newpath')

ioClient.writeObject(l10nObject, path='../../example.dtd')

IOClient lets you read and write between the objects in the memory of the program and an actual storage of the data. It may be a database, hard drive, zip package or an RCS.