def get_matlab_info(cls): # pragma: matlab r"""Determine the root directory where Matlab is installed and the version that is installed (if Matlab is installed at all). This will fail if Matlab is not installed, cannot be started, or does not operate as expected. Returns: tuple: Matlab root directory and Matlab version string. Raises: RuntimeError: If Matlab cannot be started or the root directory or release cannot be determiend. """ mtl_id = '=MATLABROOT=' cmd = ("fprintf('" + mtl_id + "%s" + mtl_id + "R%s" + mtl_id + "'" + ",matlabroot,version('-release'));") mtl_proc = cls.run_executable([cmd]) mtl_id = backwards.match_stype(mtl_proc, mtl_id) if mtl_id not in mtl_proc: # pragma: debug raise RuntimeError(("Could not locate ID string (%s) in " "output (%s).") % (mtl_id, mtl_proc)) parts = mtl_proc.split(mtl_id) if len(parts) < 3: # pragma: debug raise RuntimeError(("Could not get matlabroot/version from " "output (%s).") % (mtl_proc)) matlabroot = backwards.as_str(parts[-3]) release = backwards.as_str(parts[-2]) return matlabroot, release
def locate_matlabroot(): # pragma: matlab r"""Find directory that servers as matlab root. Returns: str: Full path to matlabroot directory. """ # if not _matlab_installed: # pragma: no matlab # raise RuntimeError("Matlab is not installed.") mtl_id = '=MATLABROOT=' cmd = "fprintf('" + mtl_id + "%s" + mtl_id + "', matlabroot); exit();" mtl_cmd = [ 'matlab', '-nodisplay', '-nosplash', '-nodesktop', '-nojvm', '-r', '%s' % cmd ] try: mtl_proc = subprocess.check_output(mtl_cmd) except subprocess.CalledProcessError: # pragma: no matlab raise RuntimeError("Could not run matlab.") mtl_id = backwards.match_stype(mtl_proc, mtl_id) if mtl_id not in mtl_proc: # pragma: debug print(mtl_proc) raise RuntimeError("Could not locate matlab root id (%s) in output." % mtl_id) mtl_root = mtl_proc.split(mtl_id)[-2] return backwards.as_str(mtl_root)
def test_match_stype(): r"""Test string type matching.""" slist = ['hello', b'hello', u'hello', bytearray('hello', 'utf-8')] for s1 in slist: for s2 in slist: assert_equal(backwards.match_stype(s1, s2), s1) assert_raises(TypeError, backwards.match_stype, 1, 'hello')
def func_serialize(self, args): r"""Serialize a message. Args: args (obj): Python object to be serialized. Returns: bytes, str: Serialized message. """ if not isinstance(args, pandas.DataFrame): raise TypeError(("Pandas DataFrame required. Invalid type " + "of '%s' provided.") % type(args)) fd = backwards.StringIO() if backwards.PY2: # pragma: Python 2 args_ = args else: # pragma: Python 3 # For Python 3 and higher, bytes need to be encoded args_ = copy.deepcopy(args) for c in args.columns: if isinstance(args_[c][0], backwards.bytes_type): args_[c] = args_[c].apply(lambda s: s.decode('utf-8')) if self.field_names is None: self.field_names = self.get_field_names() args_ = self.apply_field_names(args_, self.field_names) args_.to_csv( fd, index=False, # Not in pandas <0.24 # line_terminator=backwards.as_str(self.newline), sep=backwards.as_str(self.delimiter), mode='wb', encoding='utf8', header=(not self.dont_write_header)) if self.write_header_once: self.dont_write_header = True out = fd.getvalue() fd.close() # Required to change out \r\n for \n on windows out = out.replace(backwards.match_stype(out, platform._newline), backwards.match_stype(out, self.newline)) return backwards.as_bytes(out)
def get_testing_options(cls, read_meth='read', open_as_binary=True, **kwargs): r"""Method to return a dictionary of testing options for this class. Returns: dict: Dictionary of variables to use for testing. Key/value pairs: kwargs (dict): Keyword arguments for comms tested with the provided content. send (list): List of objects to send to test file. recv (list): List of objects that will be received from a test file that was sent the messages in 'send'. contents (bytes): Bytes contents of test file created by sending the messages in 'send'. """ out = super(FileComm, cls).get_testing_options(**kwargs) out['kwargs']['read_meth'] = read_meth out['kwargs']['open_as_binary'] = open_as_binary if (read_meth == 'read') and isinstance(out['recv'][0], backwards.bytes_type): out['recv'] = [b''.join(out['recv'])] if not open_as_binary: out['contents'] = out['contents'].replace( backwards.match_stype(out['contents'], '\n'), backwards.match_stype(out['contents'], platform._newline)) return out
def cformat2pyscanf(cfmt): r"""Convert a c format specification string to a version that the python scanf module can use. Args: cfmt (str, bytes, list): C format specification string or list of format strings. Returns: str, bytes, list: Version of cfmt or list of cfmts that can be parsed by scanf. Raises: TypeError: If cfmt is not a bytes/str/list. ValueError: If there are not an format codes in the format string. """ if not (isinstance(cfmt, list) or isinstance(cfmt, backwards.string_types)): raise TypeError( "Input must be a string, bytes string, or list, not %s" % type(cfmt)) if isinstance(cfmt, list): return [cformat2pyscanf(f) for f in cfmt] cfmt_out = backwards.as_str(cfmt) fmt_list = extract_formats(cfmt_out) if len(fmt_list) == 0: raise ValueError("Could not locate any format codes in the " + "provided format string (%s)." % cfmt) for cfmt_str in fmt_list: # Hacky, but necessary to handle concatenation of a single byte if cfmt_str[-1] == 's': out = '%s' else: out = cfmt_str # if cfmt_str[-1] == 'j': # # Handle complex format specifier # out = '%g%+gj' # else: # out = backwards.as_str(_fmt_char) # out += cfmt_str[-1] # out = out.replace('h', '') # out = out.replace('l', '') # out = out.replace('64', '') cfmt_out = cfmt_out.replace(cfmt_str, out, 1) cfmt_out = backwards.match_stype(cfmt, cfmt_out) return cfmt_out
def extract_formats(fmt_str): r"""Locate format codes within a format string. Args: fmt_str (str, bytes): Format string. Returns: list: List of identified format codes. """ fmt_regex = ("%(?:\\d+\\$)?[+-]?(?:[ 0]|\'.{1})?-?\\d*(?:\\.\\d+)?" + "[lhjztL]*(?:64)?[bcdeEufFgGosxXi]" + "(?:%(?:\\d+\\$)?[+-](?:[ 0]|\'.{1})?-?\\d*(?:\\.\\d+)?" + "[lhjztL]*[eEfFgG]j)?") out = re.findall(fmt_regex, backwards.as_str(fmt_str)) out = [backwards.match_stype(fmt_str, f) for f in out] return out
def get_matlab_version(): # pragma: matlab r"""Determine the version of matlab that is installed, if at all. Returns: str: Matlab version string. """ mtl_id = '=MATLABROOT=' cmd = "fprintf('" + mtl_id + "R%s" + mtl_id + "', version('-release')); exit();" mtl_cmd = [ 'matlab', '-nodisplay', '-nosplash', '-nodesktop', '-nojvm', '-r', '%s' % cmd ] try: mtl_proc = subprocess.check_output(mtl_cmd) except subprocess.CalledProcessError: # pragma: no matlab raise RuntimeError("Could not run matlab.") mtl_id = backwards.match_stype(mtl_proc, mtl_id) if mtl_id not in mtl_proc: # pragma: debug print(mtl_proc) raise RuntimeError("Could not locate matlab root id (%s) in output." % mtl_id) mtl_root = mtl_proc.split(mtl_id)[-2] return backwards.as_str(mtl_root)