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

RFC: Partial modification of templates so that black is a no-op on the generated files #1730

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
93 changes: 50 additions & 43 deletions build/templates/__init__.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module_name = config['module_name']
registry_name = config['driver_registry'] if 'driver_registry' in config else config['driver_name']
%>

__version__ = '${config['module_version']}'
__version__ = "${config['module_version']}"

% if len(enums) > 0:
from ${module_name}.enums import * # noqa: F403,F401,H303
Expand Down Expand Up @@ -59,86 +59,93 @@ from ${module_name}.${c['file_name']} import ${c['ctypes_type']} # noqa: F401


def get_diagnostic_information():
'''Get diagnostic information about the system state that is suitable for printing or logging
"""Get diagnostic information about the system state that is suitable for printing or logging

returns: dict

note: Python bitness may be incorrect when running in a virtual environment
'''
"""
import os
import pkg_resources
import platform
import struct
import sys

def is_python_64bit():
return (struct.calcsize("P") == 8)
return struct.calcsize("P") == 8

def is_os_64bit():
return platform.machine().endswith('64')
return platform.machine().endswith("64")

def is_venv():
return 'VIRTUAL_ENV' in os.environ
return "VIRTUAL_ENV" in os.environ

info = {}
info['os'] = {}
info['python'] = {}
info['driver'] = {}
info['module'] = {}
if platform.system() == 'Windows':
info["os"] = {}
info["python"] = {}
info["driver"] = {}
info["module"] = {}
if platform.system() == "Windows":
try:
import winreg as winreg
except ImportError:
import _winreg as winreg

os_name = 'Windows'
os_name = "Windows"
try:
driver_version_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\National Instruments\${registry_name}\CurrentVersion")
driver_version_key = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
r"SOFTWARE\National Instruments\${registry_name}\CurrentVersion",
)
driver_version = winreg.QueryValueEx(driver_version_key, "Version")[0]
except WindowsError:
driver_version = 'Unknown'
elif platform.system() == 'Linux':
os_name = 'Linux'
driver_version = 'Unknown'
driver_version = "Unknown"
elif platform.system() == "Linux":
os_name = "Linux"
driver_version = "Unknown"
else:
raise SystemError('Unsupported platform: {}'.format(platform.system()))
raise SystemError("Unsupported platform: {}".format(platform.system()))

installed_packages = pkg_resources.working_set
installed_packages_list = [{'name': i.key, 'version': i.version, } for i in installed_packages]

info['os']['name'] = os_name
info['os']['version'] = platform.version()
info['os']['bits'] = '64' if is_os_64bit() else '32'
info['driver']['name'] = "${config['driver_name']}"
info['driver']['version'] = driver_version
info['module']['name'] = '${module_name}'
info['module']['version'] = "${config['module_version']}"
info['python']['version'] = sys.version
info['python']['bits'] = '64' if is_python_64bit() else '32'
info['python']['is_venv'] = is_venv()
info['python']['packages'] = installed_packages_list
installed_packages_list = [
{
"name": i.key,
"version": i.version,
}
for i in installed_packages
]

info["os"]["name"] = os_name
info["os"]["version"] = platform.version()
info["os"]["bits"] = "64" if is_os_64bit() else "32"
info["driver"]["name"] = "${config['driver_name']}"
info["driver"]["version"] = driver_version
info["module"]["name"] = "${module_name}"
info["module"]["version"] = "${config['module_version']}"
info["python"]["version"] = sys.version
info["python"]["bits"] = "64" if is_python_64bit() else "32"
info["python"]["is_venv"] = is_venv()
info["python"]["packages"] = installed_packages_list

return info


def print_diagnostic_information():
'''Print diagnostic information in a format suitable for issue report
"""Print diagnostic information in a format suitable for issue report

note: Python bitness may be incorrect when running in a virtual environment
'''
"""
info = get_diagnostic_information()

row_format = ' {:<10} {}'
for type in ['OS', 'Driver', 'Module', 'Python']:
row_format = " {:<10} {}"
for type in ["OS", "Driver", "Module", "Python"]:
typename = type.lower()
print(type + ':')
print(type + ":")
for item in info[typename]:
if item != 'packages':
print(row_format.format(item.title() + ':', info[typename][item]))
print(' Installed Packages:')
for p in info['python']['packages']:
print((' ' * 8) + p['name'] + '==' + p['version'])
if item != "packages":
print(row_format.format(item.title() + ":", info[typename][item]))
print(" Installed Packages:")
for p in info["python"]["packages"]:
print((" " * 8) + p["name"] + "==" + p["version"])

return info


54 changes: 32 additions & 22 deletions build/templates/_attributes.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import hightime


class Attribute(object):
'''Base class for all typed attributes.'''
"""Base class for all typed attributes."""

def __init__(self, attribute_id):
self._attribute_id = attribute_id


class AttributeViInt32(Attribute):

def __get__(self, session, session_type):
return session._get_attribute_vi_int32(self._attribute_id)

Expand All @@ -26,16 +25,19 @@ class AttributeViInt32(Attribute):


class AttributeViInt32TimeDeltaMilliseconds(Attribute):

def __get__(self, session, session_type):
return hightime.timedelta(milliseconds=session._get_attribute_vi_int32(self._attribute_id))
return hightime.timedelta(
milliseconds=session._get_attribute_vi_int32(self._attribute_id)
)

def __set__(self, session, value):
session._set_attribute_vi_int32(self._attribute_id, _converters.convert_timedelta_to_milliseconds_int32(value).value)
session._set_attribute_vi_int32(
self._attribute_id,
_converters.convert_timedelta_to_milliseconds_int32(value).value,
)


class AttributeViInt64(Attribute):

def __get__(self, session, session_type):
return session._get_attribute_vi_int64(self._attribute_id)

Expand All @@ -44,7 +46,6 @@ class AttributeViInt64(Attribute):


class AttributeViReal64(Attribute):

def __get__(self, session, session_type):
return session._get_attribute_vi_real64(self._attribute_id)

Expand All @@ -53,16 +54,19 @@ class AttributeViReal64(Attribute):


class AttributeViReal64TimeDeltaSeconds(Attribute):

def __get__(self, session, session_type):
return hightime.timedelta(seconds=session._get_attribute_vi_real64(self._attribute_id))
return hightime.timedelta(
seconds=session._get_attribute_vi_real64(self._attribute_id)
)

def __set__(self, session, value):
session._set_attribute_vi_real64(self._attribute_id, _converters.convert_timedelta_to_seconds_real64(value).value)
session._set_attribute_vi_real64(
self._attribute_id,
_converters.convert_timedelta_to_seconds_real64(value).value,
)


class AttributeViString(Attribute):

def __get__(self, session, session_type):
return session._get_attribute_vi_string(self._attribute_id)

Expand All @@ -71,16 +75,17 @@ class AttributeViString(Attribute):


class AttributeViStringRepeatedCapability(Attribute):

def __get__(self, session, session_type):
return session._get_attribute_vi_string(self._attribute_id)

def __set__(self, session, value):
session._set_attribute_vi_string(self._attribute_id, _converters.convert_repeated_capabilities_without_prefix(value))
session._set_attribute_vi_string(
self._attribute_id,
_converters.convert_repeated_capabilities_without_prefix(value),
)


class AttributeViBoolean(Attribute):

def __get__(self, session, session_type):
return session._get_attribute_vi_boolean(self._attribute_id)

Expand All @@ -89,31 +94,36 @@ class AttributeViBoolean(Attribute):


class AttributeEnum(object):

def __init__(self, underlying_attribute_meta_class, enum_meta_class, attribute_id):
self._underlying_attribute = underlying_attribute_meta_class(attribute_id)
self._attribute_type = enum_meta_class
self._attribute_id = attribute_id

def __get__(self, session, session_type):
return self._attribute_type(self._underlying_attribute.__get__(session, session_type))
return self._attribute_type(
self._underlying_attribute.__get__(session, session_type)
)

def __set__(self, session, value):
if type(value) is not self._attribute_type:
raise TypeError('must be ' + str(self._attribute_type.__name__) + ' not ' + str(type(value).__name__))
raise TypeError(
"must be "
+ str(self._attribute_type.__name__)
+ " not "
+ str(type(value).__name__)
)
return self._underlying_attribute.__set__(session, value.value)


# nitclk specific attribute type
class AttributeSessionReference(Attribute):

def __get__(self, session, session_type):
# Import here to avoid a circular dependency when initial import happens
from ${module_name}.session import SessionReference

return SessionReference(session._get_attribute_vi_session(self._attribute_id))

def __set__(self, session, value):
session._set_attribute_vi_session(self._attribute_id, _converters.convert_to_nitclk_session_number(value))



session._set_attribute_vi_session(
self._attribute_id, _converters.convert_to_nitclk_session_number(value)
)
Loading