Skip to content

Commit

Permalink
Add Storages and ZIP modules
Browse files Browse the repository at this point in the history
  • Loading branch information
PMeira committed Jul 15, 2022
1 parent df912d6 commit 440bcc7
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 0 deletions.
108 changes: 108 additions & 0 deletions opendssdirect/Storages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from ._utils import (
lib,
codec,
CheckForError,
get_string,
get_float64_array,
get_string_array,
)


def AllNames():
"""(read-only) List of strings with all Storage names"""
return CheckForError(get_string_array(lib.Storages_Get_AllNames))


def Count():
"""(read-only) Number of Storages"""
return CheckForError(lib.Storages_Get_Count())


def Idx(*args):
"""
Get/set active Storage by index; 1..Count
"""
# Getter
if len(args) == 0:
return CheckForError(lib.Storages_Get_idx())

# Setter
Value, = args
CheckForError(lib.Storages_Set_idx(Value))


def First():
"""Set first Storage active; returns 0 if none."""
return CheckForError(lib.Storages_Get_First())


def Next():
"""Sets next Storage active; returns 0 if no more."""
return CheckForError(lib.Storages_Get_Next())


def Name(*args):
"""
Get/set the name of the active Storage
"""
# Getter
if len(args) == 0:
return CheckForError(get_string(lib.Storages_Get_Name()))

# Setter
Value, = args
if type(Value) is not bytes:
Value = Value.encode(codec)

CheckForError(lib.Storages_Set_Name(Value))


def puSOC(*args):
"""Per unit state of charge"""
# Getter
if len(args) == 0:
return CheckForError(lib.Storages_Get_puSOC())

# Setter
Value, = args
CheckForError(lib.Storages_Set_puSOC(Value))


def State(*args):
"""
Get/set state: 0=Idling; 1=Discharging; -1=Charging;
Related enumeration: StorageStates
"""
# Getter
if len(args) == 0:
return CheckForError(lib.Storages_Get_State())

# Setter
Value, = args
CheckForError(lib.Storages_Set_State(Value))


def RegisterNames():
"""Array of Names of all Storage energy meter registers"""
return CheckForError(get_string_array(lib.Storages_Get_RegisterNames))


def RegisterValues():
"""Array of values in Storage registers."""
return get_float64_array(lib.Storages_Get_RegisterValues)


_columns = ["Name", "Idx", "RegisterNames", "RegisterValues", "puSOC", "State"]
__all__ = [
"puSOC",
"State",
"RegisterNames",
"RegisterValues",
"Idx",
"First",
"Next",
"AllNames",
"Count",
"Name",
]
88 changes: 88 additions & 0 deletions opendssdirect/ZIP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from ._utils import lib, ffi, api_util, codec, CheckForError, get_string_array


def Open(FileName):
"""
Opens and prepares a ZIP file to be used by the DSS text parser.
Currently, the ZIP format support is limited by what is provided in the Free Pascal distribution.
Besides that, the full filenames inside the ZIP must be shorter than 256 characters.
The limitations should be removed in a future revision.
(API Extension)
"""
if type(FileName) is not bytes:
FileName = FileName.encode(codec)
CheckForError(lib.ZIP_Open(FileName))


def Close():
"""
Closes the current open ZIP file
(API Extension)
"""
CheckForError(lib.ZIP_Close())


def Redirect(FileInZip):
"""
Runs a "Redirect" command inside the current (open) ZIP file.
In the current implementation, all files required by the script must
be present inside the ZIP, using relative paths. The only exceptions are
memory-mapped files.
(API Extension)
"""
if type(FileInZip) is not bytes:
FileName = FileInZip.encode(codec)
CheckForError(lib.ZIP_Redirect(FileInZip))


def Extract(FileName):
"""
Extracts the contents of the file "FileName" from the current (open) ZIP file.
Returns a byte-string.
(API Extension)
"""
api_util = self._api_util
ptr = api_util.ffi.new("int8_t**")
cnt = api_util.ffi.new("int32_t[4]")
if type(FileName) is not bytes:
FileName = FileName.encode(api_util.codec)
CheckForError(lib.ZIP_Extract(ptr, cnt, FileName))
result = bytes(api_util.ffi.buffer(ptr[0], cnt[0]))
lib.DSS_Dispose_PByte(ptr)
return result


def List(regexp=None):
"""
List of strings consisting of all names match the regular expression provided in regexp.
If no expression is provided, all names in the current open ZIP are returned.
See https://regex.sorokin.engineer/en/latest/regular_expressions.html for information on
the expression syntax and options.
(API Extension)
"""
if regexp is None or not regexp:
regexp = ffi.NULL
elif type(regexp) is not bytes:
regexp = regexp.encode(codec)
return CheckForError(get_string_array(lib.ZIP_List, regexp))


def Contains(Name):
"""
Check if the given path name is present in the current ZIP file.
(API Extension)
"""
if type(Name) is not bytes:
Name = Name.encode(codec)
return CheckForError(lib.ZIP_Contains(Name)) != 0


_columns = []
__all__ = ["Open", "Close", "Redirect", "Extract", "List", "Contains"]

0 comments on commit 440bcc7

Please sign in to comment.