Gssha File Object Base¶
This class is inherited by all other GsshaPy file classes. It defines interface used by file objects to read and
write files including the read()
and write()
methods.
-
class
gsshapy.base.
GsshaPyFileObjectBase
[source]¶ Abstract base class for all file objects in the GsshaPy ORM.
This base class provides two methods for reading and writing files:
read()
andwrite()
. These methods in turn call the private_read()
and_write()
methods which must be implemented by child classes.-
read
(directory, filename, session, spatial=False, spatialReferenceID=4236, replaceParamFile=None, **kwargs)[source]¶ Generic read file into database method.
Parameters: - directory (str) – Directory containing the file to be read.
- filename (str) – Name of the file which will be read (e.g.: ‘example.prj’).
- session (
sqlalchemy.orm.session.Session
) – SQLAlchemy session object bound to PostGIS enabled database. - spatial (bool, optional) – If True, spatially enabled objects will be read in as PostGIS spatial objects. Defaults to False.
- spatialReferenceID (int, optional) – Integer id of spatial reference system for the model. Required if spatial is True. Defaults to srid 4236.
- replaceParamFile (
gsshapy.orm.ReplaceParamFile
, optional) – ReplaceParamFile instance. Use this if the file you are reading contains replacement parameters.
-
write
(session, directory, name, replaceParamFile=None, **kwargs)[source]¶ Write from database back to file.
Parameters: - session (
sqlalchemy.orm.session.Session
) – SQLAlchemy session object bound to PostGIS enabled database. - directory (str) – Directory where the file will be written.
- name (str) – The name of the file that will be created (including the file extension is optional).
- replaceParamFile (
gsshapy.orm.ReplaceParamFile
, optional) – ReplaceParamFile instance. Use this if the file you are writing contains replacement parameters.
- session (
-
_read
(directory, filename, session, path, name, extension, spatial, spatialReferenceID, replaceParamFile)[source]¶ Private file object read method. Classes that inherit from this base class must implement this method.
The
read()
method that each file object inherits from this base class performs the processes common to all file read methods, after which it calls the file object’s_read()
(the preceding underscore denotes that the method is a private method).The purpose of the
_read()
method is to perform the file read operations that are specific to the file that the file object represents. This method should add any supporting SQLAlchemy objects to the session without committing. The commonread()
method handles the database commit for all file objects.The
read()
method processes the user input and passes on the information through the many parameters of the_read()
method. As the_read()
method should never be called by the user directly, the arguments will be defined in terms of what they offer for the developer of a new file object needing to implement this method.Parameters: - directory (str) – Directory containing the file to be read. Same as given by user in
read()
. - filename (str) – Name of the file which will be read (e.g.: ‘example.prj’). Same as given by user. Same as
given by user in
read()
. - session (
sqlalchemy.orm.session.Session
) – SQLAlchemy session object bound to PostGIS enabled database. Same as given by user inread()
. - path (str) – Directory and filename combined into the path to the file. This is a convenience parameter.
- name (str) – Name of the file without extension. This is a convenience parameter.
- extension (str) – Extension of the file without the name. This is a convenience parameter.
- spatial (bool, optional) – If True, spatially enabled objects will be read in as PostGIS spatial objects.
Defaults to False. Same as given by user in
read()
. - spatialReferenceID (int, optional) – Integer id of spatial reference system for the model. Required if
spatial is True. Same as given by user in
read()
. - replaceParamFile (
gsshapy.orm.ReplaceParamFile
, optional) – Handle the case when replacement parameters are used in place of normal variables. If this is not None, then the user expects there to be replacement variables in the file. Use the gsshapy.lib.parsetools.valueReadPreprocessor() to handle these.
- directory (str) – Directory containing the file to be read. Same as given by user in
-
_write
(session, openFile, replaceParamFile)[source]¶ Private file object write method. Classes that inherit from this base class must implement this method.
The
write()
method that each file object inherits from this base class performs the processes common to all file write methods, after which it calls the file object’s_write()
(the preceding underscore denotes that the method is a private method).The purpose of the
_write()
method is to perform the write operations that are specific to the file that the file object represents. This method is passed Python file object that has been “opened”. Thus, the developer implementing this method does not need to worry about paths, but simply writes to the opened file object.Some files have special naming conventions. In these cases, override the write() method and write a custom method.
The
write()
method processes the user input and passes on the information to the_write()
method. As the_write()
method should never be called by the user directly, the arguments will be defined in terms of what they offer for the developer of a new file object needing to implement this method.Parameters: - session (
sqlalchemy.orm.session.Session
) – SQLAlchemy session object bound to PostGIS enabled database. Use this object to query the database during file writing. Same as given by user inwrite()
. - openFile (
file
) – File object that has been instantiated and “opened” for writing by thewrite()
method. Write lines of the file directly to this object. (e.g.:openFile.write('foo')
) - replaceParamFile (
gsshapy.orm.ReplaceParamFile
, optional) – Handle the case when replacement parameters are used in place of normal variables. If this is not None, then the user expects there to be replacement variables in the file. Use the gsshapy.lib.parsetools.valueWritePreprocessor() to handle these.
- session (
-