Iterate through the XF Format objects and separate them into XF and DXF
 formats.
767362
def prepare_formats #:nodoc:
      @formats.formats.each do |format|
        xf_index  = format.xf_index
        dxf_index = format.dxf_index

        @xf_formats[xf_index] = format   if xf_index
        @dxf_formats[dxf_index] = format if dxf_index
      end
    end
1176414
def format_data(df, **kwargs):
    """"""
    if isinstance(df, pd.Series):
        df = df.to_frame()

    # Check for R-style year columns, converting where necessary
    def convert_r_columns(c):
        try:
            first = c[0]
            second = c[1:]
            if first == 'X':
                try:
                    #  bingo! was X2015 R-style, return the integer
                    return int(second)
                except:
                    # nope, not an int, fall down to final return statement
                    pass
        except:
            # not a string/iterable/etc, fall down to final return statement
            pass
        return c
    df.columns = df.columns.map(convert_r_columns)

    # if `value` is given but not `variable`,
    # melt value columns and use column name as `variable`
    if 'value' in kwargs and 'variable' not in kwargs:
        value = kwargs.pop('value')
        value = value if islistable(value) else [value]
        _df = df.set_index(list(set(df.columns) - set(value)))
        dfs = []
        for v in value:
            if v not in df.columns:
                raise ValueError('column `{}` does not exist!'.format(v))
            vdf = _df[v].to_frame().rename(columns={v: 'value'})
            vdf['variable'] = v
            dfs.append(vdf.reset_index())
        df = pd.concat(dfs).reset_index(drop=True)

    # otherwise, rename columns or concat to IAMC-style or do a fill-by-value
    for col, value in kwargs.items():
        if col in df:
            raise ValueError('conflict of kwarg with column `{}` in dataframe!'
                             .format(col))

        if isstr(value) and value in df:
            df.rename(columns={value: col}, inplace=True)
        elif islistable(value) and all([c in df.columns for c in value]):
            df[col] = df.apply(lambda x: concat_with_pipe(x, value), axis=1)
            df.drop(value, axis=1, inplace=True)
        elif isstr(value):
            df[col] = value
        else:
            raise ValueError('invalid argument for casting `{}: {}`'
                             .format(col, value))

    # all lower case
    str_cols = [c for c in df.columns if isstr(c)]
    df.rename(columns={c: str(c).lower() for c in str_cols}, inplace=True)

    if 'notes' in df.columns:  # this came from the database
        logger().info('Ignoring notes column in dataframe')
        df.drop(columns='notes', inplace=True)
        col = df.columns[0]  # first column has database copyright notice
        df = df[~df[col].str.contains('database', case=False)]
        if 'scenario' in df.columns and 'model' not in df.columns:
            # model and scenario are jammed together in RCP data
            scen = df['scenario']
            df['model'] = scen.apply(lambda s: s.split('-')[0].strip())
            df['scenario'] = scen.apply(
                lambda s: '-'.join(s.split('-')[1:]).strip())

    # reset the index if meaningful entries are included there
    if not list(df.index.names) == [None]:
        df.reset_index(inplace=True)

    # format columns to lower-case and check that all required columns exist
    if not set(IAMC_IDX).issubset(set(df.columns)):
        missing = list(set(IAMC_IDX) - set(df.columns))
        raise ValueError("missing required columns `{}`!".format(missing))

    # check whether data in wide format (IAMC) or long format (`value` column)
    if 'value' in df.columns:
        # check if time column is given as `year` (int) or `time` (datetime)
        cols = df.columns
        if 'year' in cols:
            time_col = 'year'
        elif 'time' in cols:
            time_col = 'time'
        else:
            msg = 'invalid time format, must have either `year` or `time`!'
            raise ValueError(msg)
        extra_cols = list(set(cols) - set(IAMC_IDX + [time_col, 'value']))
    else:
        # if in wide format, check if columns are years (int) or datetime
        cols = set(df.columns) - set(IAMC_IDX)
        year_cols, time_cols, extra_cols = [], [], []
        for i in cols:
            try:
                int(i)  # this is a year
                year_cols.append(i)
            except (ValueError, TypeError):
                try:
                    dateutil.parser.parse(str(i))  # this is datetime
                    time_cols.append(i)
                except ValueError:
                    extra_cols.append(i)  # some other string
        if year_cols and not time_cols:
            time_col = 'year'
            melt_cols = year_cols
        elif not year_cols and time_cols:
            time_col = 'time'
            melt_cols = time_cols
        else:
            msg = 'invalid column format, must be either years or `datetime`!'
            raise ValueError(msg)
        df = pd.melt(df, id_vars=IAMC_IDX + extra_cols, var_name=time_col,
                     value_vars=sorted(melt_cols), value_name='value')

    # cast value columns to numeric, drop NaN's, sort data
    df['value'] = df['value'].astype('float64')
    df.dropna(inplace=True)

    # check for duplicates and return sorted data
    idx_cols = IAMC_IDX + [time_col] + extra_cols
    if any(df[idx_cols].duplicated()):
        raise ValueError('duplicate rows in `data`!')

    return sort_data(df, idx_cols), time_col, extra_cols
784963
def fields_to_dtypes(schema):
    
    datetime_types = ['date', 'datetime']
    datetime_fields = {
        f['name']: _TABLE_SCHEMA_DTYPE_MAPPING.get(f['type'], 'object')
        for f in schema['fields']
        if f['type'] in datetime_types}

    other_fields = {
        f['name']: _TABLE_SCHEMA_DTYPE_MAPPING.get(f['type'], 'object')
        for f in schema['fields']
        if f['type'] not in datetime_types}

    return {'dates': datetime_fields, 'other': other_fields}
973496
def prepare_fonts #:nodoc:
      fonts = {}

      @xf_formats.each { |format| format.set_font_info(fonts) }

      @font_count = fonts.size

      # For the DXF formats we only need to check if the properties have changed.
      @dxf_formats.each do |format|
        # The only font properties that can change for a DXF format are: color,
        # bold, italic, underline and strikethrough.
        if format.color? || format.bold? || format.italic? || format.underline? || format.strikeout?
          format.has_dxf_font(true)
        end
      end
    end
1001387
function parse_BrtXF(data, length) {
	var ixfeParent = data.read_shift(2);
	var ifmt = data.read_shift(2);
	parsenoop(data, length-4);
	return {ixfe:ixfeParent, ifmt:ifmt };
}
408416
function _storeAllXfs()
    {
        // _tmp_format is added by the constructor. We use this to write the default XF's
        // The default font index is 0
        //
        $format = $this->_tmp_format;
        for ($i = 0; $i <= 14; $i++) {
            $xf = $format->getXf('style'); // Style XF
            $this->_append($xf);
        }

        $xf = $format->getXf('cell');      // Cell XF
        $this->_append($xf);

        // User defined XFs
        $total_formats = count($this->_formats);
        for ($i = 0; $i < $total_formats; $i++) {
            $xf = $this->_formats[$i]->getXf('cell');
            $this->_append($xf);
        }
    }
824526
def setup_formats(self):
        
        methods = self.get_methods()
        for m in methods:
            #Methods named "from_X" will be assumed to convert from format X to the common format
            if m.startswith("from_"):
                self.input_formats.append(re.sub("from_" , "",m))
            #Methods named "to_X" will be assumed to convert from the common format to X
            elif m.startswith("to_"):
                self.output_formats.append(re.sub("to_","",m))
1150315
def parse_xrefs(self, token):
        
        out, end = [], 0
        token = token.replace("\\n", "\n")
        for m in re.finditer(self.xref_registry, token, re.VERBOSE | re.DOTALL):
            if m.start(0) > end:
                out.append(String(token[end:m.start(0)], self.current_obj))
            end = m.end(0)
            if m.group("type"):
                xref_type = {"+": ValueXRef,
                             "!": VariableXRef,
                             "@": ElementXRef}[m.group("type")]
                out.append(xref_type(m.group("xref"), self.current_obj))
            elif m.group("uri") is not None:
                path = m.group("uri")
                out.append(MetaURI(path, self.current_obj))
            elif m.group("repeat") is not None:
                repeat, separator, nodups = m.group("repeat", "separator", "nodups")
                if separator is None:
                    separator = ""
                if nodups is None:
                    nodups = ""
                out.append(MetaRepeat(self.parse_xrefs(repeat), separator, nodups, self.current_obj))
            elif m.group("block") is not None:
                path = m.group("block")
                out.append(MetaBlock(path, self.current_obj))
            elif m.group("choices") is not None:
                choices = m.group("choices")
                out.append(MetaChoice(choices, self.current_obj))
            else:
                startval, endval = m.group("start", "end")
                out.append(MetaRange(startval, endval, self.current_obj))
        if end < len(token):
            out.append(String(token[end:], self.current_obj))
        return out
1118065
def formats
      formats=[]
      f = self
      formats << f
      while f.parent
        formats << f.parent
        f = f.parent
      end
      formats.reverse
    end
1273000
def _xfs_prune_output(out, uuid):
    '''
    
    '''
    data = {}
    cnt = []
    cutpoint = False
    for line in [l.strip() for l in out.split("\n") if l]:
        if line.startswith("-"):
            if cutpoint:
                break
            else:
                cutpoint = True
                continue

        if cutpoint:
            cnt.append(line)

    for kset in [e for e in cnt[1:] if ':' in e]:
        key, val = [t.strip() for t in kset.split(":", 1)]
        data[key.lower().replace(" ", "_")] = val

    return data.get('uuid') == uuid and data or {}
626910
def flatten(items):
    
    for x in items:
        if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
            for sub_x in flatten(x):
                yield sub_x
        else:
            yield x
1176994
def FromTextFormat(cls, text):
    """"""
    tmp = cls.protobuf()  # pylint: disable=not-callable
    text_format.Merge(text, tmp)

    return cls.FromSerializedString(tmp.SerializeToString())
917651
def load_dxf(file_obj, **kwargs):
    

    def info(e):
        """
        Pull metadata based on group code, and return as a dict.
        """
        # which keys should we extract from the entity data
        # DXF group code : our metadata key
        get = {'8': 'layer'}

        # replace group codes with names and only
        # take info from the entity dict if it is in cand
        renamed = {get[k]: util.make_sequence(v)[0] for k,
                   v in e.items() if k in get}

        return renamed

    def convert_line(e):
        """
        Convert DXF LINE entities into trimesh Line entities.
        """
        # create a single Line entity
        entities.append(Line(points=len(vertices) + np.arange(2),
                             **info(e)))
        # add the vertices to our collection
        vertices.extend(np.array([[e['10'], e['20']],
                                  [e['11'], e['21']]],
                                 dtype=np.float64))

    def convert_circle(e):
        """
        Convert DXF CIRCLE entities into trimesh Circle entities
        """
        R = float(e['40'])
        C = np.array([e['10'],
                      e['20']]).astype(np.float64)
        points = to_threepoint(center=C[0:2],
                               radius=R)
        entities.append(Arc(points=(len(vertices) + np.arange(3)),
                            closed=True,
                            **info(e)))
        vertices.extend(points)

    def convert_arc(e):
        """
        Convert DXF ARC entities into into trimesh Arc entities.
        """
        # the radius of the circle
        R = float(e['40'])
        # the center point of the circle
        C = np.array([e['10'],
                      e['20']], dtype=np.float64)
        # the start and end angle of the arc, in degrees
        # this may depend on an AUNITS header data
        A = np.radians(np.array([e['50'],
                                 e['51']], dtype=np.float64))
        # convert center/radius/angle representation
        # to three points on the arc representation
        points = to_threepoint(center=C[0:2],
                               radius=R,
                               angles=A)
        # add a single Arc entity
        entities.append(Arc(points=len(vertices) + np.arange(3),
                            closed=False,
                            **info(e)))
        # add the three vertices
        vertices.extend(points)

    def convert_polyline(e):
        """
        Convert DXF LWPOLYLINE entities into trimesh Line entities.
        """
        # load the points in the line
        lines = np.column_stack((
            e['10'], e['20'])).astype(np.float64)

        # save entity info so we don't have to recompute
        polyinfo = info(e)

        # 70 is the closed flag for polylines
        # if the closed flag is set make sure to close
        is_closed = '70' in e and int(e['70'][0]) & 1
        if is_closed:
            lines = np.vstack((lines, lines[:1]))

        # 42 is the vertex bulge flag for LWPOLYLINE entities
        # "bulge" is autocad for "add a stupid arc using flags
        # in my otherwise normal polygon", it's like SVG arc
        # flags but somehow even more annoying
        if '42' in e:
            # get the actual bulge float values
            bulge = np.array(e['42'], dtype=np.float64)
            # what position were vertices stored at
            vid = np.nonzero(chunk[:, 0] == '10')[0]
            # what position were bulges stored at in the chunk
            bid = np.nonzero(chunk[:, 0] == '42')[0]
            # filter out endpoint bulge if we're not closed
            if not is_closed:
                bid_ok = bid < vid.max()
                bid = bid[bid_ok]
                bulge = bulge[bid_ok]
            # which vertex index is bulge value associated with
            bulge_idx = np.searchsorted(vid, bid)
            # convert stupid bulge to Line/Arc entities
            v, e = bulge_to_arcs(lines=lines,
                                 bulge=bulge,
                                 bulge_idx=bulge_idx,
                                 is_closed=is_closed)
            for i in e:
                # offset added entities by current vertices length
                i.points += len(vertices)
            vertices.extend(v)
            entities.extend(e)
            # done with this polyline
            return

        # we have a normal polyline so just add it
        # as single line entity and vertices
        entities.append(Line(
            points=np.arange(len(lines)) + len(vertices),
            **polyinfo))
        vertices.extend(lines)

    def convert_bspline(e):
        """
        Convert DXF Spline entities into trimesh BSpline entities.
        """
        # in the DXF there are n points and n ordered fields
        # with the same group code

        points = np.column_stack((e['10'],
                                  e['20'])).astype(np.float64)
        knots = np.array(e['40']).astype(np.float64)

        # if there are only two points, save it as a line
        if len(points) == 2:
            # create a single Line entity
            entities.append(Line(points=len(vertices) +
                                 np.arange(2),
                                 **info(e)))
            # add the vertices to our collection
            vertices.extend(points)
            return

        # check bit coded flag for closed
        # closed = bool(int(e['70'][0]) & 1)
        # check euclidean distance to see if closed
        closed = np.linalg.norm(points[0] -
                                points[-1]) < tol.merge

        # create a BSpline entity
        entities.append(BSpline(
            points=np.arange(len(points)) + len(vertices),
            knots=knots,
            closed=closed,
            **info(e)))
        # add the vertices
        vertices.extend(points)

    def convert_text(e):
        """
        Convert a DXF TEXT entity into a native text entity.
        """
        if '50' in e:
            # rotation angle converted to radians
            angle = np.radians(float(e['50']))
        else:
            # otherwise no rotation
            angle = 0.0

        # text with leading and trailing whitespace removed
        text = e['1'].strip()

        # height of text
        if '40' in e:
            height = float(e['40'])
        else:
            height = None

        # origin point
        origin = np.array([e['10'],
                           e['20']]).astype(np.float64)

        # an origin- relative point (so transforms work)
        vector = origin + [np.cos(angle), np.sin(angle)]

        # try to extract a (horizontal, vertical) text alignment
        align = ['center', 'center']
        try:
            align[0] = ['left', 'center', 'right'][int(e['72'])]
        except BaseException:
            pass

        # append the entity
        entities.append(Text(origin=len(vertices),
                             vector=len(vertices) + 1,
                             height=height,
                             text=text,
                             align=align))
        # append the text origin and direction
        vertices.append(origin)
        vertices.append(vector)

    # in a DXF file, lines come in pairs,
    # a group code then the next line is the value
    # we are removing all whitespace then splitting with the
    # splitlines function which uses the universal newline method
    raw = file_obj.read()
    # if we've been passed bytes
    if hasattr(raw, 'decode'):
        # search for the sentinel string indicating binary DXF
        # do it by encoding sentinel to bytes and subset searching
        if raw[:22].find(b'AutoCAD Binary DXF') != -1:
            if _teigha is None:
                # no converter to ASCII DXF available
                raise ValueError('binary DXF not supported!')
            else:
                # convert binary DXF to R14 ASCII DXF
                raw = _teigha_convert(raw, extension='dxf')
        else:
            # we've been passed bytes that don't have the
            # header for binary DXF so try decoding as UTF-8
            raw = raw.decode('utf-8', errors='ignore')

    # remove trailing whitespace
    raw = str(raw).strip()
    # without any spaces and in upper case
    cleaned = raw.replace(' ', '').strip().upper()

    # blob with spaces and original case
    blob_raw = np.array(str.splitlines(raw)).reshape((-1, 2))
    # if this reshape fails, it means the DXF is malformed
    blob = np.array(str.splitlines(cleaned)).reshape((-1, 2))

    # get the section which contains the header in the DXF file
    endsec = np.nonzero(blob[:, 1] == 'ENDSEC')[0]

    # get the section which contains entities in the DXF file
    entity_start = np.nonzero(blob[:, 1] == 'ENTITIES')[0][0]
    entity_end = endsec[np.searchsorted(endsec, entity_start)]
    entity_blob = blob[entity_start:entity_end]
    # store the entity blob with original case
    entity_raw = blob_raw[entity_start:entity_end]

    # store metadata
    metadata = {}

    # try reading the header, which may be malformed
    header_start = np.nonzero(blob[:, 1] == 'HEADER')[0]
    if len(header_start) > 0:
        header_end = endsec[np.searchsorted(endsec, header_start[0])]
        header_blob = blob[header_start[0]:header_end]

        # store some properties from the DXF header
        metadata['DXF_HEADER'] = {}
        for key, group in [('$ACADVER', '1'),
                           ('$DIMSCALE', '40'),
                           ('$DIMALT', '70'),
                           ('$DIMALTF', '40'),
                           ('$DIMUNIT', '70'),
                           ('$INSUNITS', '70'),
                           ('$LUNITS', '70')]:
            value = get_key(header_blob,
                            key,
                            group)
            if value is not None:
                metadata['DXF_HEADER'][key] = value

        # store unit data pulled from the header of the DXF
        # prefer LUNITS over INSUNITS
        # I couldn't find a table for LUNITS values but they
        # look like they are 0- indexed versions of
        # the INSUNITS keys, so for now offset the key value
        for offset, key in [(-1, '$LUNITS'),
                            (0, '$INSUNITS')]:
            # get the key from the header blob
            units = get_key(header_blob, key, '70')
            # if it exists add the offset
            if units is None:
                continue
            metadata[key] = units
            units += offset
            # if the key is in our list of units store it
            if units in _DXF_UNITS:
                metadata['units'] = _DXF_UNITS[units]
        # warn on drawings with no units
        if 'units' not in metadata:
            log.warning('DXF doesn\'t have units specified!')

    # find the start points of entities
    group_check = entity_blob[:, 0] == '0'
    inflection = np.nonzero(group_check)[0]

    # DXF object to trimesh object converters
    loaders = {'LINE': (dict, convert_line),
               'LWPOLYLINE': (util.multi_dict, convert_polyline),
               'ARC': (dict, convert_arc),
               'CIRCLE': (dict, convert_circle),
               'SPLINE': (util.multi_dict, convert_bspline)}

    # store loaded vertices
    vertices = []
    # store loaded entities
    entities = []

    # an old-style polyline entity strings its data across
    # multiple vertex entities like a real asshole
    polyline = None

    # loop through chunks of entity information
    for index in np.array_split(np.arange(len(entity_blob)),
                                inflection):

        # if there is only a header continue
        if len(index) < 1:
            continue

        # chunk will be an (n, 2) array of (group code, data) pairs
        chunk = entity_blob[index]

        # the string representing entity type
        entity_type = chunk[0][1]

        ############
        # special case old- style polyline entities
        if entity_type == 'POLYLINE':
            polyline = [dict(chunk)]
        # if we are collecting vertex entities
        elif polyline is not None and entity_type == 'VERTEX':
            polyline.append(dict(chunk))
        # the end of a polyline
        elif polyline is not None and entity_type == 'SEQEND':
            # pull the geometry information for the entity
            lines = np.array([[i['10'], i['20']]
                              for i in polyline[1:]],
                             dtype=np.float64)

            # check for a closed flag on the polyline
            if '70' in polyline[0]:
                # flag is bit- coded integer
                flag = int(polyline[0]['70'])
                # first bit represents closed
                is_closed = bool(flag & 1)
                if is_closed:
                    lines = np.vstack((lines, lines[:1]))

            # get the index of each bulged vertices
            bulge_idx = np.array([i for i, e in enumerate(polyline)
                                  if '42' in e],
                                 dtype=np.int64)
            # get the actual bulge value
            bulge = np.array([float(e['42'])
                              for i, e in enumerate(polyline)
                              if '42' in e],
                             dtype=np.float64)
            # convert bulge to new entities
            v, e = bulge_to_arcs(lines=lines,
                                 bulge=bulge,
                                 bulge_idx=bulge_idx,
                                 is_closed=is_closed)
            for i in e:
                # offset entities by existing vertices
                i.points += len(vertices)
            vertices.extend(v)
            entities.extend(e)

            # we no longer have an active polyline
            polyline = None
        elif entity_type == 'TEXT':
            # text entities need spaces preserved so take
            # group codes from clean representation (0- column)
            # and data from the raw representation (1- column)
            chunk_raw = entity_raw[index]
            # if we didn't use clean group codes we wouldn't
            # be able to access them by key as whitespace
            # is random and crazy, like: '  1 '
            chunk_raw[:, 0] = entity_blob[index][:, 0]
            try:
                convert_text(dict(chunk_raw))
            except BaseException:
                log.warning('failed to load text entity!',
                            exc_info=True)
        # if the entity contains all relevant data we can
        # cleanly load it from inside a single function
        elif entity_type in loaders:
            # the chunker converts an (n,2) list into a dict
            chunker, loader = loaders[entity_type]
            # convert data to dict
            entity_data = chunker(chunk)
            # append data to the lists we're collecting
            loader(entity_data)
        else:
            log.debug('Entity type %s not supported',
                      entity_type)

    # stack vertices into single array
    vertices = util.vstack_empty(vertices).astype(np.float64)

    # return result as kwargs for trimesh.path.Path2D constructor
    result = {'vertices': vertices,
              'entities': np.array(entities),
              'metadata': metadata}

    return result
861782
def split_input(cls, mapper_spec):
    """"""
    filelists = mapper_spec.params[cls.FILES_PARAM]
    max_values_count = mapper_spec.params.get(cls.MAX_VALUES_COUNT_PARAM, -1)
    max_values_size = mapper_spec.params.get(cls.MAX_VALUES_SIZE_PARAM, -1)
    return [cls([0] * len(files), max_values_count, max_values_size)
            for files in filelists]
1009648
def _build_xrefs(self):
        '''
        
        '''
        xrefs = set()

        plugins = self.plugins()
        for plugin in plugins:
            for xref in plugin['xrefs'].split(', '):
                xrf = xref.replace('-', '_').split(':')[0]
                if xrf is not '':
                    xrefs.add(xrf)
        self._xrefs = list(xrefs)
604047
def _SerializeEntries(entries):
  """"""

  output = []
  for python_format, wire_format, type_descriptor in entries:

    if wire_format is None or (python_format and
                               type_descriptor.IsDirty(python_format)):
      wire_format = type_descriptor.ConvertToWireFormat(python_format)

    precondition.AssertIterableType(wire_format, bytes)
    output.extend(wire_format)

  return b"".join(output)
314476
def format_xpaths(xpath_map, *args, **kwargs):
    

    formatted = {}.fromkeys(xpath_map)

    for key, xpath in iteritems(xpath_map):
        formatted[key] = xpath.format(*args, **kwargs)

    return formatted