def find_dataframe(mxd: '_arcpy.mapping.MapDocument', name: str = None, case_sensitive: bool = False): """ Finds a data frame by its (case-insensitive) name in the given ArcMap document (MXD) and returns it. If no *name* is specified, the active data frame in the given Map Document is returned. If no data frame was found at all, ``None`` is returned. This can only happen, when looking for a specific *name*. :param mxd: A :class:`arcpy.mapping.MapDocument` instance. :param name: The name of the data frame to search for. :param case_sensitive: If ``True``, the data frame name needs to match exactly. If ``False`` (default), the data frame character case is ignored. :rtype: arcpy._mapping.DataFrame .. seealso:: https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/dataframe-class.htm """ df_ref = None if isinstance(name, str): name = _tu.to_str(name if case_sensitive else name.lower()) for df in _arcpy.mapping.ListDataFrames(mxd): df_name = df.name if case_sensitive else df.name.lower() if df_name == name: df_ref = df break else: df_ref = mxd.activeDataFrame return df_ref
def find_parent(path, name): """ Finds within *path* the parent directory that contains a file or directory with the given *name*. Note that *path* and *name* values are matched case-insensitively, but the found path is returned in the original case (as a normalized path). If no matches have been found or if the match has no parent, an empty string is returned. If there are multiple matches, the parent path of the first match is returned. Examples: >>> find_parent('C:\\Projects\\parent\\LEVEL0\\level1\\level2.txt', 'level0') 'C:\\Projects\\parent' >>> find_parent('C:\\Projects\\parent\\LEVEL\\level\\level.txt', 'level') 'C:\\Projects\\parent' >>> find_parent('C:\\Projects\\some_dir', 'C:') '' >>> find_parent('C:\\Projects\\parent\\must_include_extension.txt', 'must_include_extension') '' :param path: The path to search. :param name: The name for which to search the parent directory in *path*. This value can be a file name (with extension!) or a directory name. Partial paths, pre- or suffixes or regular expressions are not allowed here and will return None. :type path: str, unicode :type name: str, unicode :rtype: str, unicode """ def match_parts(plist, n): # Search for n in plist and yield the parts up to n n_ci = n.lower() if n_ci not in (p.lower() for p in plist): return for p in plist: if p.lower() == n_ci: return yield p # Get path type (unicode or str) so we correctly join the path parts without encoding issues path_type = type(path) # Validate arguments _vld.pass_if(issubclass(path_type, basestring), TypeError, 'path attribute must be a string') _vld.pass_if(isinstance(name, basestring), TypeError, 'name attribute must be a string') # Split path into parts list, encode or decode name if name type does not match path type parts = normalize(path, False).split(_os.sep) if not isinstance(name, path_type): name = _tu.to_str(name) if isinstance( name, unicode) else _tu.to_unicode(name) # Return the concatenated path parts up til the match (or an empty string if nothing was found) return path_type(_os.sep).join(match_parts(parts, name)) or path_type( _const.CHAR_EMPTY)
def delimit_fields(self, datasource): """ Updates the fields in the query by wrapping them in the appropriate delimiters for the current data source. :param datasource: The path to the data source (e.g. SDE connection, feature class, etc.) or a :class:`gpf.paths.Workspace` instance. .. seealso:: https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/addfielddelimiters.htm """ for i, (part, is_field) in enumerate(self._parts): if not is_field: continue # Call arcpy function on "clean" version of the field name (to prevent adding duplicate delimiters) part = _arcpy.AddFieldDelimiters( _tu.to_str(datasource) if isinstance(datasource, _Ws) else datasource, part.strip('[]"')) self._parts[i] = (part, is_field)
def find_layer(name: str, mxd: _tp.Union[str, '_arcpy.mapping.MapDocument'] = None, dataframe: str = None, case_sensitive: bool = False) -> '_arcpy.mapping.Layer': """ Finds a **single** layer by its (case-insensitive) name in the specified ArcMap document (or current one). If the layer was not found, ``None`` is returned. :param name: Name of the layer to find. If the layer name exists multiple times in different group layers, you can prefix the layer with the group layer name followed by a forward slash (/). :param mxd: The path to the ArcMap Document (MXD) or a MapDocument instance in which to find the layer. If no MXD is specified, the search will take place in the current MXD (if any). :param dataframe: The name of the data frame in which to find the layer. If no data frame is specified and/or there is only 1 data frame, the search will take place in the active data frame. :param case_sensitive: If ``True``, the layer name needs to match exactly. If ``False`` (default), the layer character case is ignored. Note that this setting also affects the *dataframe* argument, when specified. :raises ValueError: If no layer name was specified, or if no map document was found. .. seealso:: https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/layer-class.htm """ # Validation _vld.pass_if(_vld.has_value(name), ValueError, 'Layer name has not been specified') name = _tu.to_str(_paths.normalize(name, not case_sensitive)) mxd_ref, df_ref = _get_mxd_df(mxd, dataframe, case_sensitive) # Find the layer by name for lyr in _arcpy.mapping.ListLayers(mxd_ref, data_frame=df_ref): lyr_name = lyr.name if case_sensitive else lyr.name.lower() if lyr_name == name or _paths.normalize(lyr.longName, not case_sensitive) == name: return lyr return None
def find_layers(wildcard: str = None, mxd: _tp.Union[str, '_arcpy.mapping.MapDocument'] = None, dataframe: str = None) -> _tp.List['_arcpy.mapping.Layer']: """ Finds **all** layers that match a certain wild card expression in the specified ArcMap document (or current one). All matching layers are returned as a list of Layer instances. If no layers were found, an empty list is returned. :param wildcard: Layer name search string (with wild card characters). Unlike the :func:`find_layer` method, it is **not** possible to use a group layer prefix here. Furthermore, the search string is case sensitive. If this value is not specified, all layers in the map document will be returned. :param mxd: The path to the ArcMap Document (MXD) or a MapDocument instance in which to find the layer(s). If no MXD is specified, the search will take place in the current MXD (if any). :param dataframe: The case-insensitive name of the data frame in which to find the layer(s). If no data frame is specified and/or there is only 1 data frame, the search will take place in the active data frame. :raises ValueError: If no map document was found. .. seealso:: https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/layer-class.htm """ wildcard = None if not wildcard else _tu.to_str(wildcard) mxd_ref, df_ref = _get_mxd_df(mxd, dataframe) return _arcpy.mapping.ListLayers(mxd_ref, wildcard, df_ref) or []
def test_tostr(): assert textutils.to_str(5) == '5' assert textutils.to_str('täst') == 'täst' assert textutils.to_str('test') == 'test' assert textutils.to_str(b'test') == 'test'
def test_tostr(): assert textutils.to_str(5) == '5' assert textutils.to_str( 'täst'.decode('utf8')) == 'täst'.decode('utf8').encode('utf8') assert textutils.to_str('test') == 'test'