Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging file handler fix #134

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions LadybugTools_Engine/Compute/EPWtoCSV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.Engine.Python;
using BH.oM.Python;
using BH.oM.Base.Attributes;

using System.Collections.Generic;
using System.ComponentModel;
using System.IO;

namespace BH.Engine.LadybugTools
{
Expand All @@ -50,25 +49,22 @@ public static string EPWtoCSV(string epwFile, bool includeAdditional = false)
}

PythonEnvironment env = InstallPythonEnv_LBT(true);
string additionalProperties = includeAdditional ? "True" : "False";

string pythonScript = string.Join("\n", new List<string>()
epwFile = System.IO.Path.GetFullPath(epwFile);
string csvFile = System.IO.Path.ChangeExtension(epwFile, ".hbjson");

string script = Path.Combine(Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "epw_to_csv.py");

// run the process
string command = $"{env.Executable} {script} -e \"{epwFile}\" -a \"{includeAdditional}\"";
string result = Python.Compute.RunCommandStdout(command: command, hideWindows: true);

if (!File.Exists(csvFile))
{
"import traceback",
"from pathlib import Path",
"from ladybug.epw import EPW",
"",
$"epw_path = Path(r'{epwFile}')",
"csv_path = epw_path.with_suffix('.csv')",
"try:",
" from ladybugtools_toolkit.ladybug_extension.epw import epw_to_dataframe",
$" epw_to_dataframe(EPW(epw_path.as_posix()), include_additional={additionalProperties}).to_csv(csv_path.as_posix())",
" print(csv_path)",
"except Exception as exc:",
" print(traceback.format_exc())",
});
BH.Engine.Base.Compute.RecordError($"File conversion failed due to {result}");
}

return env.RunPythonString(pythonScript).Trim();
return csvFile;
}
}
}
73 changes: 0 additions & 73 deletions LadybugTools_Engine/Compute/EPWtoCustomObject.cs

This file was deleted.

33 changes: 13 additions & 20 deletions LadybugTools_Engine/Compute/GEMtoHBJSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.Engine.Python;
using BH.oM.Python;
using BH.oM.Base.Attributes;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;

namespace BH.Engine.LadybugTools
{
Expand All @@ -51,26 +49,21 @@ public static string GEMtoHBJSON(string gem)

PythonEnvironment env = InstallPythonEnv_LBT(true);

string gemFile = System.IO.Path.GetFullPath(gem);
string outputDirectory = System.IO.Path.GetDirectoryName(gem);
string fileName = System.IO.Path.GetFileNameWithoutExtension(gem);
gem = System.IO.Path.GetFullPath(gem);
string hbjsonFile = System.IO.Path.ChangeExtension(gem, ".hbjson");

string pythonScript = String.Join("\n", new List<string>()
string script = Path.Combine(Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "gem_to_hbjson.py");

// run the process
string command = $"{env.Executable} {script} -g \"{gem}\"";
string result = Python.Compute.RunCommandStdout(command: command, hideWindows: true);

if (!File.Exists(hbjsonFile))
{
"import traceback",
"from honeybee.model import Model",
"from honeybee_ies.reader import model_from_ies",
"",
"try:",
$" model = model_from_ies(r\"{gemFile}\")",
$" hbjson_file = model.to_hbjson(folder=r\"{outputDirectory}\", name=\"{fileName}\")",
" print(hbjson_file)",
"except Exception as exc:",
" print(traceback.format_exc())",
});
BH.Engine.Base.Compute.RecordError($"File conversion failed due to {result}");
}

return env.RunPythonString(pythonScript).Trim();
return hbjsonFile;
}
}
}

33 changes: 13 additions & 20 deletions LadybugTools_Engine/Compute/HBJSONtoGEM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.Engine.Python;
using BH.oM.Python;
using BH.oM.Base.Attributes;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;

namespace BH.Engine.LadybugTools
{
Expand All @@ -51,26 +49,21 @@ public static string HBJSONtoGEM(string hbjson)

PythonEnvironment env = InstallPythonEnv_LBT(true);

string hbjsonFile = System.IO.Path.GetFullPath(hbjson);
string outputDirectory = System.IO.Path.GetDirectoryName(hbjsonFile);
string fileName = System.IO.Path.GetFileNameWithoutExtension(hbjsonFile);
hbjson = System.IO.Path.GetFullPath(hbjson);
string gemFile = System.IO.Path.ChangeExtension(hbjson, ".gem");
tg359 marked this conversation as resolved.
Show resolved Hide resolved

string script = Path.Combine(Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "hbjson_to_gem.py");

string pythonScript = String.Join("\n", new List<string>()
// run the process
string command = $"{env.Executable} {script} -j \"{hbjson}\"";
string result = Python.Compute.RunCommandStdout(command: command, hideWindows: true);

if (!File.Exists(gemFile))
{
"import traceback",
"from honeybee.model import Model",
"from pathlib import Path",
"",
"try:",
$" model = Model.from_hbjson(r\"{hbjsonFile}\")",
$" gem_file = model.to_gem(r\"{outputDirectory}\", name=\"{fileName}\")",
" print(gem_file)",
"except Exception as exc:",
" print(traceback.format_exc())",
});
BH.Engine.Base.Compute.RecordError($"File conversion failed due to {result}");
}

return env.RunPythonString(pythonScript).Trim();
return gemFile;
}
}
}

6 changes: 5 additions & 1 deletion LadybugTools_Engine/Python/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# LadybugTools_Toolkit

The code contained here is intended to be used in conjunction with the LadybugTools_Toolkit, as part of a wider [BHoM](https://bhom.xyz/) installation. Its main purpose is to augment capabilities of the code provided by [Ladybug Tools](https://github.com/ladybug-tools).
The code contained here is intended to be used in conjunction with the LadybugTools_Toolkit, as part of a wider [BHoM](https://bhom.xyz/) installation. Its main purpose is to provide shortcuts to shorten workflows using code provided by [Ladybug Tools](https://github.com/ladybug-tools).

## Installation

For this toolkit to work successfully, [Pollination](https://www.pollination.cloud/) should be installed first, after which the method `BH.Engine.LadybugTools.Compute.InstallPythonEnv_LBT` should be run. This will create a mirror-image of the Pollination Python environment within the BHoM installation directory (`C:\ProgramData\BHoM\Extensions\PythonEnvironments\LadybugTools_Toolkit`) from which the code here can be accessed.
4 changes: 3 additions & 1 deletion LadybugTools_Engine/Python/run_tests.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
SET PYTHON_EXE=C:\ProgramData\BHoM\Extensions\PythonEnvironments\LadybugTools_Toolkit\Scripts\python.exe
SET TEST_DIR=C:\ProgramData\BHoM\Extensions\PythonCode\LadybugTools_Toolkit\tests

"%PYTHON_EXE%" -m pytest --cov-report term --cov-report html:cov_html --cov ladybugtools_toolkit -v "%C:\ProgramData\BHoM\Extensions\PythonCode\LadybugTools_Toolkit\tests%"
@REM C:\ProgramData\BHoM\Extensions\PythonEnvironments\LadybugTools_Toolkit\Scripts\python.exe -m pytest --cov-report term --cov-report html:cov_html --cov ladybugtools_toolkit -v C:\ProgramData\BHoM\Extensions\PythonCode\LadybugTools_Toolkit\tests

"%PYTHON_EXE%" -m pytest --cov-report term --cov-report html:cov_html --cov ladybugtools_toolkit -v "%TEST_DIR%"
cmd /k
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
"""Root for the bhom subpackage."""

from .analytics import decorator_factory
from .log import console_logger
from pathlib import Path # pylint: disable=E0401

CONSOLE_LOGGER = console_logger()
from win32api import HIWORD, LOWORD, GetFileVersionInfo

BHOM_ASSEMBLIES_DIRECTORY = Path("C:/ProgramData/BHoM/Assemblies")
BHOM_DIRECTORY = Path("C:/ProgramData/BHoM")
BHOM_LOG_FOLDER = Path("C:/ProgramData/BHoM/Logs")

PYTHON_CODE_DIRECTORY = Path("C:/ProgramData/BHoM/Extensions/PythonCode")
PYTHON_ENVIRONMENTS_DIRECTORY = Path(
"C:/ProgramData/BHoM/Extensions/PythonEnvironments"
)
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved

TOOLKIT_NAME = "LadybugTools_Toolkit"

_file_version_ms = GetFileVersionInfo(
(BHOM_ASSEMBLIES_DIRECTORY / "BHoM.dll").as_posix(), "\\"
)["FileVersionMS"]

BHOM_VERSION = f"{HIWORD(_file_version_ms)}.{LOWORD(_file_version_ms)}"
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@
from typing import Any, Callable

# pylint: enable=E0401
from .log import analytics_logger
from .util import bhom_version, csharp_ticks, toolkit_name

LOGGER = analytics_logger()
BHoM_VERSION = bhom_version()
from .logging import ANALYTICS_LOGGER
from .util import csharp_ticks
from . import BHOM_VERSION, TOOLKIT_NAME


def decorator_factory(disable: bool = False) -> Callable:
def bhom_analytics() -> Callable:
"""Decorator for capturing usage data.

Arguments
---------
disable : bool, optional
Whether to disable the decorator, by default False

Returns
-------
Callable
Expand All @@ -47,9 +41,6 @@ def decorator(function: Callable):
def wrapper(*args, **kwargs) -> Any:
"""A wrapper around the function that captures usage analytics."""

if disable:
return function(*args, **kwargs)

_id = uuid.uuid4()

# get the data being passed to the function, expected dtype and return type
Expand All @@ -59,7 +50,7 @@ def wrapper(*args, **kwargs) -> Any:
_args = [f'{{"_t": "{argspec[k]}", "Name": "{k}"}}' for k in argspec.keys()]

exec_metadata = {
"BHoMVersion": bhom_version(),
"BHoMVersion": BHOM_VERSION,
"BHoM_Guid": _id,
"CallerName": function.__name__,
"ComponentId": _id,
Expand All @@ -76,14 +67,14 @@ def wrapper(*args, **kwargs) -> Any:
"MethodName": function.__name__,
"Parameters": _args,
"TypeName": f"{function.__module__}.{function.__qualname__}",
"_bhomVersion": BHoM_VERSION,
"_bhomVersion": BHOM_VERSION,
"_t": "Python",
},
"Time": {
"$date": csharp_ticks(short=True),
},
"UI": "Python",
"UiVersion": toolkit_name(),
"UiVersion": TOOLKIT_NAME,
"_t": "BH.oM.UI.UsageLogEntry",
}

Expand All @@ -93,7 +84,9 @@ def wrapper(*args, **kwargs) -> Any:
exec_metadata["Errors"].extend(sys.exc_info())
raise exc
finally:
LOGGER.info(json.dumps(exec_metadata, default=str, indent=None))
ANALYTICS_LOGGER.info(
json.dumps(exec_metadata, default=str, indent=None)
)

return result

Expand Down

This file was deleted.

Loading