def _new_file(self, path):
        """Loads a new configuration file"""
        try:
            cfg = Config(path)
            database = cfg.database()
        except Exception as e:
            prompt.critical('Invalid configuration file\n\n{}'.format(e))
            return

        self._filebox.setText(path)
        self._equipment_records_table.set_database(database)
        self._connection_records_table.set_database(database)
        self._equipment_table.set_database(database)
        self._constants_table.update_table(cfg.root)
        self._tree.populate_tree()
        self._apply_filter()
Beispiel #2
0
def test_config_unit_units():
    # the "temperature" and "humidity" XML elements support an
    # attrib name "unit" or "units"
    cfg_min = Config(os.path.join(resources, 'config_unit_units.xml'))
    _, calibrations, _ = utils.initialize_webapp(cfg_min, '01234')
    assert len(calibrations) == 1

    cal = calibrations['b'][0]
    assert cal.serial == '01234'
    assert os.path.basename(cal.dbase_file) == 'iTHX-W3-5_01234.sqlite3'
    assert cal.component == ''
    assert cal.probe == ''
    assert cal.date == utils.fromisoformat('2020-12-17')
    assert cal.number == 'unit-units'
    assert cal.start_date == utils.fromisoformat('2020-12-11')
    assert cal.end_date == utils.fromisoformat('2020-12-14')
    assert cal.coverage_factor == 2.0
    assert cal.confidence == '95%'
    assert cal.temperature['unit'] == 'degree Celsius'
    assert cal.temperature['min'] == 15.0
    assert cal.temperature['max'] == 25.0
    assert cal.temperature['coefficients'] == [0.0]
    assert cal.temperature['expanded_uncertainty'] == 1.0
    assert cal.humidity['unit'] == 'percent-relative-humidity'
    assert cal.humidity['min'] == 30.0
    assert cal.humidity['max'] == 80.0
    assert cal.humidity['coefficients'] == [0.0]
    assert cal.humidity['expanded_uncertainty'] == 1.0
Beispiel #3
0
def test_connection_properties():

    dbase = Config(os.path.join(ROOT_DIR, 'db.xml')).database()
    props = dbase.records(serial='37871232')[0].connection.properties

    assert props['a'] == 1
    assert props['b'] == 1.1
    assert isinstance(props['c'], bool) and props['c']
    assert isinstance(props['d'], bool) and props['d']
    assert isinstance(props['e'], bool) and not props['e']
    assert isinstance(props['f'], bool) and not props['f']
    assert props['g'] is None
    assert props['h'] == ''
    assert props['i_termination'] == constants.LF
    assert props['j_termination'] == constants.CR
    assert props['k_termination'] == constants.CR + constants.LF
    assert props['l'] == 'some text'
    assert props['m'] == 'D:\\Data\\'
Beispiel #4
0
def test_encoding():

    IS_PYTHON2 = sys.version_info[0] == 2
    if IS_PYTHON2:
        reload(sys)  # required for the sys.setdefaultencoding() calls below

    print('')
    for cfg in ['utf8_txt.xml', 'cp1252_txt.xml', 'xlsx.xml']:
        db = Config(os.path.join(ROOT_DIR, 'db_encoding_' + cfg)).database()

        if IS_PYTHON2:
            if cfg.startswith('cp1252'):
                sys.setdefaultencoding(
                    'cp1252')  # a legacy encoding used by Microsoft Windows
            elif cfg.startswith('utf8'):
                sys.setdefaultencoding('utf-8')

        print(db.path)

        # test printing the database records
        for r in db.records():
            print(r)
            r.to_dict()
            r.to_xml()
        for r in db.connections():
            print(r)
            r.to_dict()
            r.to_xml()

        assert db.records(
            manufacturer='Kepco'
        )[0].manufacturer == u'Kepco and \u201cTMK\u201d shunt'
        assert db.records(
            model='MFF101/M'
        )[0].description == u'Motorized Filter Flip Mount for \xd825mm Optics'
Beispiel #5
0
def test_initialize_webapp_config_minimal():
    # the 'config_minimal.xml' file does not contain any of the Optional XML elements
    cfg_min = Config(os.path.join(resources, 'config_minimal.xml'))
    dropdown_options, calibrations, omegas = utils.initialize_webapp(cfg_min, serials)

    assert len(dropdown_options) == 3
    assert dropdown_options[0] == {'label': 'b', 'value': 'b'}
    assert dropdown_options[1] == {'label': 'f - Probe 1', 'value': 'f - Probe 1'}
    assert dropdown_options[2] == {'label': 'f - Probe 2', 'value': 'f - Probe 2'}

    assert len(calibrations) == 3
    assert len(calibrations['b']) == 3
    assert len(calibrations['f - Probe 1']) == 2
    assert len(calibrations['f - Probe 2']) == 1

    assert len(omegas) == 2
    assert '01234' in omegas
    assert '56789' in omegas
Beispiel #6
0
def test_database_io_errors():

    # no <path></path> tag
    with pytest.raises(IOError) as err:
        Config(os.path.join(ROOT_DIR, 'db_err0.xml')).database()
    assert '<path>' in str(err.value)

    # database file does not exist
    with pytest.raises(IOError):
        Config(os.path.join(ROOT_DIR, 'db_err1.xml')).database()

    # unsupported database file
    with pytest.raises(IOError) as err:
        Config(os.path.join(ROOT_DIR, 'db_err2.xml')).database()
    assert 'database' in str(err.value)

    # more than 1 Sheet in the Excel database
    with pytest.raises(IOError) as err:
        Config(os.path.join(ROOT_DIR, 'db_err3.xml')).database()
    assert 'sheets:\n  Equipment, Connections' in str(err.value)

    # the 'equipment' item in the xml file is not a valid Equipment Record
    with pytest.raises(AttributeError) as err:
        Config(os.path.join(ROOT_DIR, 'db_err4.xml')).database()
    assert 'attributes' in str(err.value)

    # the 'equipment' item in the xml file is not a unique Equipment Record
    with pytest.raises(AttributeError) as err:
        Config(os.path.join(ROOT_DIR, 'db_err5.xml')).database()
    assert 'unique' in str(err.value)

    # the 'equipment' item in the xml file is has multiple aliases
    with pytest.raises(ValueError) as err:
        Config(os.path.join(ROOT_DIR, 'db_err6.xml')).database()
    assert 'aliases' in str(err.value)

    # invalid Sheet name in Excel database
    with pytest.raises(ValueError) as err:
        Config(os.path.join(ROOT_DIR, 'db_err7.xml')).database()
    assert 'There is no sheet named \'Equipment\'' in str(err.value)
Beispiel #7
0
def test_database_user_defined():
    path = os.path.join(ROOT_DIR, 'db_user_defined.xml')
    cfg = Config(path)
    db = cfg.database()
    for record in db.records():
        if record.team == 'Any':
            assert len(record.user_defined) == 2
            assert record.user_defined['nothing_relevant'] == 'XXXXXXXXXX'
            assert record.user_defined['policies'] == 'MSLE.X.YYY'
        else:
            assert len(record.user_defined) == 0

    path = os.path.join(ROOT_DIR, 'db_user_defined_bad.xml')
    cfg = Config(path)
    db = cfg.database()
    for record in db.records():
        if record.team == 'Any':
            assert len(record.user_defined) == 1
            assert record.user_defined['policies'] == 'MSLE.X.YYY'
        else:
            assert len(record.user_defined) == 0
Beispiel #8
0
import os
from datetime import datetime, timedelta

import pytest
from msl.equipment import Config

from omega_logger import utils

resources = os.path.join(os.path.dirname(__file__), 'resources')

cfg = Config(os.path.join(resources, 'config.xml'))
cfg.find('log_dir').text = resources

serials = '01234,56789'


def test_fromisoformat():
    dt = utils.fromisoformat('2020-02-23')
    assert dt == datetime(2020, month=2, day=23)

    dt = utils.fromisoformat('2020-02-23 12:32:05')
    assert dt == datetime(2020, month=2, day=23, hour=12, minute=32, second=5)

    dt = utils.fromisoformat('2020-02-23T12:32:05')
    assert dt == datetime(2020, month=2, day=23, hour=12, minute=32, second=5)

    dt = utils.fromisoformat('2020-02-23T12')
    assert dt == datetime(2020, month=2, day=23, hour=12, minute=0, second=0)

    dt = utils.fromisoformat('2020-02-23T12:10')
    assert dt == datetime(2020, month=2, day=23, hour=12, minute=10, second=0)
Beispiel #9
0
"""
Example showing how to interact with equipment and connection databases.
"""

# this "if" statement is used so that Sphinx does not execute this script when the docs are being built
if __name__ == '__main__':
    from msl.equipment import Config
    from msl.examples.equipment import EXAMPLES_DIR

    # load the database
    dbase = Config(EXAMPLES_DIR + '/example2.xml').database()

    # access the equipment record for each <equipment> XML element by the alias that we assigned to it
    print('Access equipment records from the alias that was assigned to the equipment...')
    print(dbase.equipment['ref'])
    print(dbase.equipment['DUT'])
    print(dbase.equipment['mono'])
    print(dbase.equipment['sensor'])
    print(dbase.equipment['adaptor'])
    print(dbase.equipment['filter_flipper'])
    print('')

    # search for equipment records based on some keywords
    print('All equipment in the database manufactured by Hewlett Packard...')
    for record in dbase.records(manufacturer='H.*P'):
        print(record)
    print('')

    # show all of the connection records in the database
    print('All connection records in the database that use GPIB...')
    for record in dbase.connections(address='GPIB*'):
Beispiel #10
0
def start(*args):
    """Entry point for the console script."""
    if not args:
        args = sys.argv[1:]
        if not args:
            args = ['--help']

    parser = argparse.ArgumentParser(
        description=
        'Records the temperature, humidity and dew point from OMEGA iServers\n'
        'to a database and creates a web application to interact with the data.',
        formatter_class=argparse.RawTextHelpFormatter,
    )
    parser.add_argument('-V',
                        '--version',
                        action='version',
                        version='{}'.format(__version__),
                        help='show the version number and exit')
    parser.add_argument('config', help='the path to a configuration file')
    parser.add_argument('-b',
                        '--backup',
                        action='store_true',
                        default=False,
                        help='perform a database backup and exit')
    parser.add_argument('-t',
                        '--test-email',
                        action='store_true',
                        default=False,
                        help='send a test email and exit')
    args = parser.parse_args(args)

    try:
        cfg = Config(os.path.abspath(args.config))
    except OSError as e:
        print(f'{e.__class__.__name__}: {e}', file=sys.stderr)
        return 1

    if args.test_email:
        smtp = cfg.find('smtp')
        if smtp is None:
            print(
                'There is no "smtp" element in the config file. Cannot send email.',
                file=sys.stderr)
            return 1
        try:
            email(smtp, 'Test', subject='[omega-logger] Test')
        except Exception as e:
            print(f'{e.__class__.__name__}: {e}\nCannot send email',
                  file=sys.stderr)
            return 1
        else:
            print('Success')
            return

    log_dir = cfg.value('log_dir')
    if not log_dir:
        print(
            'There is no "log_dir" element in the config file.\n'
            'What directory do you want to log the data to?',
            file=sys.stderr)
        return 1

    if not os.path.isdir(log_dir):
        print(f'The log_dir value of {log_dir!r} is not a valid directory.',
              file=sys.stderr)
        return 1

    if args.backup:
        return run_backup(cfg)
    return run_webapp(cfg)
Beispiel #11
0
                dcc.Tab(label='Dewpoint', value='dewpoint'),
            ],
            style={'display': 'inline-block'},
        ),
        html.Div(id='plot-viewer'),
        html.Div(id='current-readings-viewer'),
        dcc.Interval(
            id='current-readings-interval',
            interval=cfg.value('current_readings/interval', 10) * 1000,
        ),
    ])


try:
    path, serials = sys.argv[1:]
    cfg = Config(path)
    dropdown_options, calibrations, omegas = initialize_webapp(cfg, serials)
except:
    traceback.print_exc(file=sys.stderr)
    input('Press <ENTER> to close ...')
    sys.exit(1)

app = dash.Dash()
app.title = 'OMEGA iServers'
app.layout = serve_layout  # set app.layout to a function to serve a dynamic layout on every page load
app.server.config['JSONIFY_PRETTYPRINT_REGULAR'] = True


def read_raw(omega):
    """Read the raw temperature, humidity and dewpoint values from an OMEGA iServer.
Beispiel #12
0
def test_database():

    path = os.path.join(ROOT_DIR, 'db.xml')
    c = Config(path)

    dbase = c.database()
    assert path == dbase.path
    assert len(dbase.records()) == 7 + 18
    assert len(dbase.records(manufacturer='NOPE!')) == 0
    assert len(
        dbase.records(manufacturer='^Ag')) == 10  # all records from Agilent
    assert len(dbase.records(manufacturer='^Ag', connection=True)
               ) == 3  # all records from Agilent with a ConnectionRecord
    assert len(dbase.records(manufacturer='Agilent', model='83640L')) == 1
    assert len(dbase.records(
        manufacturer=r'H.*P')) == 2  # all records from Hewlett Packard
    assert len(dbase.records(manufacturer=r'H.*P|^Ag')
               ) == 12  # all records from Hewlett Packard or Agilent
    assert len(dbase.records(manufacturer='Bd6d850614')) == 1
    assert len(dbase.records(model='00000')) == 1
    num_connections_expected = 4
    assert len(dbase.records(connection=True)) == num_connections_expected
    assert len(dbase.records(
        connection=False)) == len(dbase.records()) - num_connections_expected
    assert len(dbase.records(connection=1)) == num_connections_expected
    assert len(dbase.records(
        connection=0)) == len(dbase.records()) - num_connections_expected
    assert len(
        dbase.records(connection='anything that converts to a bool=True')
    ) == num_connections_expected
    assert len(dbase.records(
        connection='')) == len(dbase.records()) - num_connections_expected

    dbase.records(
        flags=1
    )  # a flags argument name is ok even though it not an EquipmentRecord property name
    with pytest.raises(NameError):
        dbase.records(unknown_name=None)

    assert len(dbase.connections()) == 10
    assert len(dbase.connections(backend='MSL')) == 5
    assert len(dbase.connections(backend=constants.Backend.MSL)) == 5
    assert len(dbase.connections(backend='PYVISA')) == 0
    assert len(dbase.connections(backend='PyVISA')) == 5
    assert len(dbase.connections(backend=constants.Backend.PyVISA)) == 5
    assert len(dbase.connections(backend='PyVISA|MSL')) == 10
    assert len(dbase.connections(backend='XXXXX')) == 0
    assert len(dbase.connections(serial='A10008')) == 1
    assert len(
        dbase.connections(manufacturer='^Ag')) == 4  # all records from Agilent
    assert len(dbase.connections(model='DTMc300V_sub')) == 1
    assert len(dbase.connections(manufacturer='Agilent', serial='G00001')) == 1
    assert len(dbase.connections(manufacturer='Agilent|Fluke|Thorlabs')) == 6
    assert len(dbase.connections(interface='SERIAL')
               ) == 2  # != 3 since "Coherent Scientific" uses PyVISA
    assert len(dbase.connections(interface=constants.MSLInterface.SDK)) == 2
    assert len(dbase.connections(interface='SERIAL|SDK')) == 4
    assert len(dbase.connections(interface=constants.MSLInterface.SERIAL)
               ) == 2  # != 3 since "Coherent Scientific" uses PyVISA
    assert len(dbase.connections(interface='XXXXXX')) == 0

    dbase.connections(
        flags=1
    )  # a flags argument name is ok even though it not a ConnectionRecord property name
    with pytest.raises(NameError):
        dbase.connections(unknown_name=None)

    assert len(dbase.equipment) == 2
    assert '712ae' in dbase.equipment  # the model number is used as the key
    assert 'dvm' in dbase.equipment  # the alias is used as the key
Beispiel #13
0
def test_json_and_xml_db():

    for filename in ['config_json.xml', 'config_xml.xml']:

        db = Config(os.path.join(ROOT_DIR, filename)).database()

        #
        # EquipmentRecords
        #

        assert len(db.records()) == 3
        assert len(db.records(is_operable=True)) == 2
        assert len(db.records(is_operable=False)) == 1
        assert len(db.records(category='Logger')) == 1

        records = db.records(unique_key='AK1')
        assert len(records) == 1
        r = records[0]
        assert len(r.calibrations) == 2
        c0 = r.calibrations[0]
        assert c0.report_date == datetime.date(2012, 10, 20)
        assert c0.calibration_date == datetime.date(2012, 10, 20)
        assert c0.report_number == 'PTB 44183/12'
        assert c0.calibration_cycle == 5
        assert len(c0.measurands) == 1
        m = c0.measurands['spectral_radiance_d6']
        assert m.type == 'spectral_radiance_d6'
        assert m.unit == ''
        assert m.conditions.measured_area_diameter == 10
        assert m.conditions.measured_area_diameter_unit == 'mm'
        assert m.conditions.bandwidth_below_1100_nm == 3
        assert m.conditions.bandwidth_below_1100_nm_unit == 'nm'
        assert m.conditions.bandwidth_above_1100_nm == 6
        assert m.conditions.bandwidth_above_1100_nm_unit == 'nm'
        assert m.calibration.calibration_type == 'dependent_artefact_values'
        assert m.calibration.dependent_parameter == 'wavelength'
        assert m.calibration.dependent_unit == 'nm'
        assert m.calibration.dependent_minimum == 250
        assert m.calibration.dependent_maximum == 2450
        with pytest.raises(TypeError):  # cannot change value
            m.calibration.dependent_maximum = 1000
        assert m.calibration.artefact_values == ((250, 0.938), (260, 0.945),
                                                 (270, 0.950), (2450, 0.934))
        assert m.calibration.expanded_uncertainty == ((0, 0.011), (0, 0.011),
                                                      (0, 0.004), (0, 0.019))
        assert m.calibration.coverage_factor == 2
        assert m.calibration.level_of_confidence == 0.95
        assert m.calibration.correlation_matrix == ()
        c1 = r.calibrations[1]
        assert c1.report_date == datetime.date(2012, 10, 20)
        assert c1.calibration_date == datetime.date(2012, 10, 20)
        assert c1.report_number == 'PTB 44188/12'
        assert c1.calibration_cycle == 5
        assert len(c1.measurands) == 1
        m = c1.measurands['spectral_radiance_factor']
        assert m.type == 'spectral_radiance_factor'
        assert m.unit == ''
        assert m.conditions.measured_area_diameter == "ellipse, 10 and 10/cos(theta_d)"
        assert m.conditions.measured_area_diameter_unit == 'mm'
        assert m.conditions.bandwidth_below_900_nm == 3
        assert m.conditions.bandwidth_below_900_nm_unit == 'nm'
        assert m.conditions.bandwidth_above_900_nm == 6
        assert m.conditions.bandwidth_above_900_nm_unit == 'nm'
        assert m.conditions.divergence_of_incident_beam == 1.5
        assert m.conditions.divergence_of_incident_beam_unit == 'degrees'
        assert m.conditions.divergence_of_detection_beam == 0.32
        assert m.conditions.divergence_of_detection_beam_unit == 'degrees'
        assert m.calibration.calibration_type == 'dependent_artefact_values'
        assert m.calibration.dependent_measurands.wavelength.minimum == 350
        assert m.calibration.dependent_measurands.wavelength['maximum'] == 800
        assert m.calibration.dependent_measurands.wavelength.unit == 'nm'
        assert m.calibration.dependent_measurands['incident_angle'][
            'minimum'] == 45
        assert m.calibration.dependent_measurands[
            'incident_angle'].maximum == 45
        assert m.calibration.dependent_measurands.incident_angle.unit == 'degrees'
        assert m.calibration.dependent_measurands.detection_angle.minimum == -30
        assert m.calibration['dependent_measurands'][
            'detection_angle'].maximum == 65
        assert m.calibration.dependent_measurands.detection_angle.unit == 'degrees'
        assert m.calibration.artefact_values == ((350, 45, -30, 1.039),
                                                 (400, 45, -30,
                                                  1.048), (800, 45, 65, 0.909))
        assert m.calibration.expanded_uncertainty == ((0, 0, 0, 0.017),
                                                      (0, 0, 0, 0.005),
                                                      (0, 0, 0, 0.002),
                                                      (0, 0, 0, 0.002))
        assert m.calibration.coverage_factor == 2
        assert m.calibration.level_of_confidence == 0.95
        assert m.calibration.correlation_matrix == ()
        assert r.category == 'reflectance standard'
        assert r.connection is None
        assert r.description == 'spectralon 99% reflectance standard'
        assert r.is_operable
        assert len(r.maintenances) == 0
        assert r.manufacturer == 'Labsphere'
        assert r.model == 'AS-01159-060, USRS-99-020, BT69E'
        assert r.serial == '0.99'
        assert r.team == 'Light'
        assert r.unique_key == 'AK1'
        assert isinstance(r.user_defined, RecordDict)
        assert len(r.user_defined) == 0

        records = db.records(manufacturer='OMEGA')
        assert len(records) == 1
        r = records[0]
        assert len(r.calibrations) == 2
        c0 = r.calibrations[0]
        assert c0.report_date == datetime.date(2018, 7, 21)
        assert c0.calibration_date == datetime.date(2018, 6, 8)
        assert c0.report_number == 'Humidity/2018/386'
        assert c0.calibration_cycle == 2
        assert len(c0.measurands) == 2
        t = c0.measurands['temperature']
        assert t.type == 'temperature'
        assert t.unit == 'C'
        assert t.conditions.lab_temperature == 21
        assert t.conditions.lab_temperature_uncertainty == 1
        assert t.conditions.lab_temperature_unit == 'C'
        assert t.calibration.minimum == 18
        assert t.calibration.maximum == 24
        assert t.calibration.correction_coefficients == (0.01, )
        assert t.calibration.expanded_uncertainty == 0.13
        assert t.calibration.coverage_factor == 2
        assert t.calibration.level_of_confidence == 0.95
        assert t.calibration.correlation_matrix == ()
        h = c0.measurands['humidity']
        assert h.type == 'humidity'
        assert h.unit == '%rh'
        assert h.conditions.lab_temperature == 21
        assert h.conditions.lab_temperature_uncertainty == 1
        assert h.conditions.lab_temperature_unit == 'C'
        assert h.calibration.minimum == 30
        assert h.calibration.maximum == 85
        assert h.calibration.correction_coefficients == (-9.5, 0.326, -0.00505,
                                                         0.0000321)
        assert h.calibration.expanded_uncertainty == 0.9
        assert h.calibration.coverage_factor == 2
        assert h.calibration.level_of_confidence == 0.95
        assert h.calibration.correlation_matrix == ()
        c1 = r.calibrations[1]
        assert c1.report_date == datetime.date(2016, 2, 22)
        assert c1.calibration_date == datetime.date(2016, 1, 20)
        assert c1.report_number == 'Humidity/2016/322'
        assert c1.calibration_cycle == 2
        assert len(c1.measurands) == 2
        t = c1.measurands['temperature']
        assert t.type == 'temperature'
        assert t.unit == 'C'
        assert t.conditions.lab_temperature == 21
        assert t.conditions.lab_temperature_uncertainty == 1
        assert t.conditions.lab_temperature_unit == 'C'
        assert t.calibration.minimum == 17
        assert t.calibration.maximum == 23
        assert t.calibration.correction_coefficients == (0.05, )
        assert t.calibration.expanded_uncertainty == 0.12
        assert t.calibration.coverage_factor == 2
        assert t.calibration.level_of_confidence == 0.95
        assert t.calibration.correlation_matrix == ()
        h = c1.measurands['humidity']
        assert h.type == 'humidity'
        assert h.unit == '%rh'
        assert h.conditions.lab_temperature == 21
        assert h.conditions.lab_temperature_uncertainty == 1
        assert h.conditions.lab_temperature_unit == 'C'
        assert h.calibration.minimum == 30
        assert h.calibration.maximum == 80
        assert h.calibration.correction_coefficients == (-3.44, 0.0487)
        assert h.calibration.expanded_uncertainty == 0.8
        assert h.calibration.coverage_factor == 2
        assert h.calibration.level_of_confidence == 0.95
        assert h.calibration.correlation_matrix == ()
        assert r.category == "Logger"
        assert r.description == "Temperature, relative humidity and dew point reader"
        assert r.is_operable
        assert len(r.maintenances) == 2
        m0 = r.maintenances[0]
        assert m0.date == datetime.date(2019, 3, 24)
        assert m0.comment == 'Nothing changed'
        m1 = r.maintenances[1]
        assert m1.date == datetime.date(2018, 1, 17)
        assert m1.comment == 'ABCDEF ghijkl MNOP qrstuvwxyz'
        assert r.manufacturer == "OMEGA"
        assert r.model == "iTHX-W3-5"
        assert r.serial == "4070777"
        assert r.team == 'Light'
        assert r.unique_key == "137154e9-da33-46c9-b85b-3a1a351969d6"
        assert len(r.user_defined) == 1
        assert r.user_defined['my_custom_key'] == "whatever I want"

        r = db.records(manufacturer='foo')[0]
        assert r.manufacturer == 'foo'
        assert r.model == 'bar'
        assert r.maintenances == tuple()
        assert not r.is_operable
        assert r.serial == 'xyz'
        assert r.calibrations == tuple()
        assert r.team == 'Light'
        assert len(r.user_defined) == 0

        #
        # ConnectionRecords
        #

        assert len(db.connections()) == 3
        assert len(db.connections(backend=Backend.MSL)) == 2
        assert len(db.connections(backend=Backend.PyVISA)) == 1
        assert len(db.connections(manufacturer='foo')) == 1

        c = db.connections(manufacturer='foo')[0]
        assert c is db.equipment['The A Team'].connection
        assert c.manufacturer == 'foo'
        assert c.model == 'bar'
        assert c.address == 'COM7'
        assert c.backend == Backend.MSL
        assert c.serial == 'xyz'
        assert len(c.properties) == 5
        assert c.properties['timeout'] == 5
        assert c.properties['baud_rate'] == 38400
        assert c.properties['termination'] == b'\r\n'
        assert c.properties['parity'] == constants.Parity.ODD
        assert c.properties['data_bits'] == constants.DataBits.SEVEN

        c = db.connections(manufacturer='Company B')[0]
        assert c.manufacturer == 'Company B'
        assert c.model == 'DEF'
        assert c.address == 'TCP::169.254.146.227::9221'
        assert c.backend == Backend.MSL
        assert c.serial == '123456'
        assert isinstance(c.properties, dict)
        assert len(c.properties) == 0

        c = db.connections(manufacturer='Company C')[0]
        assert c.manufacturer == 'Company C'
        assert c.model == 'GHI'
        assert c.address == 'GPIB::22'
        assert c.backend == Backend.PyVISA
        assert c.serial == 'aabbcc'
        assert len(c.properties) == 1
        assert c.properties['termination'] == b'\r'

        #
        # Equipment tag
        #

        assert db.connections(
            manufacturer='foo')[0] is db.equipment['The A Team'].connection
Beispiel #14
0
def test_datetime_range_picker_kwargs_config_minimal():
    # the 'config_minimal.xml' file does not contain any of the Optional XML elements
    cfg_min = Config(os.path.join(resources, 'config_minimal.xml'))
    kwargs = utils.datetime_range_picker_kwargs(cfg_min)
    assert isinstance(kwargs, dict)
    assert not kwargs
Beispiel #15
0
"""
Example showing how to open a connection in demo mode.
"""

# this "if" statement is used so that Sphinx does not execute this script when the docs are being built
if __name__ == '__main__':
    from msl.equipment import Config
    from msl.examples.equipment import EXAMPLES_DIR

    # load the database
    cfg = Config(EXAMPLES_DIR + '/example2.xml')
    dbase = cfg.database()

    # get a specific equipment record (a DMM from Agilent) from the database and
    # then connect to this DMM in demo mode to send some messages to it
    dmm_record = dbase.records(manufacturer='Agilent', serial="537179")[0]
    dmm = dmm_record.connect(demo=True)
    print(dmm.query('*IDN?'))
    def get_reading(self):
        syl = record.connect()
        syl.serial.flush()
        reading = syl.query('PRI?')
        syl.disconnect()

        return reading.strip("~")


if __name__ == "__main__":
    from time import time_ns
    from msl.equipment import Config

    config = r'C:\Users\r.hawke\PycharmProjects\CetoniSP\config.xml'
    cfg = Config(config)  # loads cfg file
    db = cfg.database()  # loads database
    equipment = db.equipment  # loads subset of database with equipment being used
    record = db.equipment['sylvac75']

    syl = DialGauge(record)

    savepath = r'C:\Users\r.hawke\PycharmProjects\CetoniSP\data_files\RFC_cal_vacuum/DialGauge_{}_{}.csv'.format(
        12,
        time_ns() / 1e9)
    with open(savepath, mode='w') as fp:
        fp.write("Mmmt No.,Timestamp (s),Height (mm)\n")
        for i in range(50):
            print(i)
            fp.write("{},{},{}\n".format(i,
                                         time_ns() / 1e9, syl.get_reading()))
Beispiel #17
0
    default_msec_format = '%s.%03d'

    def __init__(self, fmt, alias):
        """Inserts the alias of the equipment record into a logging message."""
        super(AliasFormatter, self).__init__(fmt=fmt)
        self.alias = alias

    def format(self, rec):
        if self.alias and rec.levelno > logging.WARNING:
            rec.msg = f'[{self.alias}] {rec.msg}'
        return super(AliasFormatter, self).format(rec)


try:
    path, serial = sys.argv[1:]
    cfg = Config(path)
    db = cfg.database()

    records = db.records(manufacturer='OMEGA',
                         serial=serial,
                         flags=re.IGNORECASE)
    if not records:
        raise ValueError(f'No equipment record exists for '
                         f'manufacturer=OMEGA and serial={serial}')
    if len(records) > 1:
        raise ValueError(f'Multiple equipment record exists for '
                         f'manufacturer=OMEGA and serial={serial}')

    record = records[0]

    hdlr = logging.StreamHandler(sys.stdout)