text:
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Logging;
using Umbraco.Core.Mapping;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Core.Services;
using Umbraco.Core.Exceptions;
using Umbraco.Core.Models.ContentEditing;

namespace Umbraco.Web.Models.Mapping
{
    /// <summary>
    /// Defines mappings for content/media/members type mappings
    /// </summary>
    internal class ContentTypeMapDefinition : IMapDefinition
    {
        private readonly CommonMapper _commonMapper;
        private readonly PropertyEditorCollection _propertyEditors;
        private readonly IDataTypeService _dataTypeService;
        private readonly IFileService _fileService;
        private readonly IContentTypeService _contentTypeService;
        private readonly IMediaTypeService _mediaTypeService;
        private readonly IMemberTypeService _memberTypeService;
        private readonly ILogger _logger;
        private readonly IUmbracoSettingsSection _umbracoSettingsSection;

        public ContentTypeMapDefinition(CommonMapper commonMapper, PropertyEditorCollection propertyEditors, IDataTypeService dataTypeService, IFileService fileService,
            IContentTypeService contentTypeService, IMediaTypeService mediaTypeService, IMemberTypeService memberTypeService,
            ILogger logger, IUmbracoSettingsSection umbracoSettingsSection)
        {
            _commonMapper = commonMapper;
            _propertyEditors = propertyEditors;
            _dataTypeService = dataTypeService;
            _fileService = fileService;
            _contentTypeService = contentTypeService;
            _mediaTypeService = mediaTypeService;
            _memberTypeService = memberTypeService;
            _logger = logger;
            _umbracoSettingsSection = umbracoSettingsSection;
        }

        public void DefineMaps(UmbracoMapper mapper)
        {
            mapper.Define<DocumentTypeSave, IContentType>((source, context) => new ContentType(source.ParentId), Map);
            mapper.Define<MediaTypeSave, IMediaType>((source, context) => new MediaType(source.ParentId), Map);
            mapper.Define<MemberTypeSave, IMemberType>((source, context) => new MemberType(source.ParentId), Map);

            mapper.Define<IContentType, DocumentTypeDisplay>((source, context) => new DocumentTypeDisplay(), Map);
            mapper.Define<IMediaType, MediaTypeDisplay>((source, context) => new MediaTypeDisplay(), Map);
            mapper.Define<IMemberType, MemberTypeDisplay>((source, context) => new MemberTypeDisplay(), Map);

            mapper.Define<PropertyTypeBasic, PropertyType>(
                (source, context) =>
                {
                    var dataType = _dataTypeService.GetDataType(source.DataTypeId);
                    if (dataType == null) throw new NullReferenceException("No data type found with id " + source.DataTypeId);
                    return new PropertyType(dataType, source.Alias);
                }, Map);

            // TODO: isPublishing in ctor?
            mapper.Define<PropertyGroupBasic<PropertyTypeBasic>, PropertyGroup>((source, context) => new PropertyGroup(false), Map);
            mapper.Define<PropertyGroupBasic<MemberPropertyTypeBasic>, PropertyGroup>((source, context) => new PropertyGroup(false), Map);

            mapper.Define<IContentTypeComposition, ContentTypeBasic>((source, context) => new ContentTypeBasic(), Map);
            mapper.Define<IContentType, ContentTypeBasic>((source, context) => new ContentTypeBasic(), Map);
            mapper.Define<IMediaType, ContentTypeBasic>((source, context) => new ContentTypeBasic(), Map);
            mapper.Define<IMemberType, ContentTypeBasic>((source, context) => new ContentTypeBasic(), Map);

            mapper.Define<DocumentTypeSave, DocumentTypeDisplay>((source, context) => new DocumentTypeDisplay(), Map);
            mapper.Define<MediaTypeSave, MediaTypeDisplay>((source, context) => new MediaTypeDisplay(), Map);
            mapper.Define<MemberTypeSave, MemberTypeDisplay>((source, context) => new MemberTypeDisplay(), Map);

            mapper.Define<PropertyGroupBasic<PropertyTypeBasic>, PropertyGroupDisplay<PropertyTypeDisplay>>((source, context) => new PropertyGroupDisplay<PropertyTypeDisplay>(), Map);
            mapper.Define<PropertyGroupBasic<MemberPropertyTypeBasic>, PropertyGroupDisplay<MemberPropertyTypeDisplay>>((source, context) => new PropertyGroupDisplay<MemberPropertyTypeDisplay>(), Map);

            mapper.Define<PropertyTypeBasic, PropertyTypeDisplay>((source, context) => new PropertyTypeDisplay(), Map);
            mapper.Define<MemberPropertyTypeBasic, MemberPropertyTypeDisplay>((source, context) => new MemberPropertyTypeDisplay(), Map);
        }

        // no MapAll - take care
        private void Map(DocumentTypeSave source, IContentType target, MapperContext context)
        {
            MapSaveToTypeBase<DocumentTypeSave, PropertyTypeBasic>(source, target, context);
            MapComposition(source, target, alias => _contentTypeService.Get(alias));
            MapHistoryCleanup(source, target);

            target.AllowedTemplates = source.AllowedTemplates
                .Where(x => x != null)
                .Select(_fileService.GetTemplate)
                .Where(x => x != null)
                .ToArray();

            target.SetDefaultTemplate(source.DefaultTemplate == null ? null : _fileService.GetTemplate(source.DefaultTemplate));
        }

        private static void MapHistoryCleanup(DocumentTypeSave source, IContentType target)
        {
            // If source history cleanup is null we don't have to map all properties
            if (source.HistoryCleanup is null)
            {
                target.HistoryCleanup = null;
                return;
            }

            // We need to reset the dirty properties, because it is otherwise true, just because the json serializer has set properties
            target.HistoryCleanup.ResetDirtyProperties(false);
            if (target.HistoryCleanup.PreventCleanup != source.HistoryCleanup.PreventCleanup)
            {
                target.HistoryCleanup.PreventCleanup = source.HistoryCleanup.PreventCleanup;
            }

            if (target.HistoryCleanup.KeepAllVersionsNewerThanDays != source.HistoryCleanup.KeepAllVersionsNewerThanDays)
            {
                target.HistoryCleanup.KeepAllVersionsNewerThanDays = source.HistoryCleanup.KeepAllVersionsNewerThanDays;
            }

            if (target.HistoryCleanup.KeepLatestVersionPerDayForDays !=
                source.HistoryCleanup.KeepLatestVersionPerDayForDays)
            {
                target.HistoryCleanup.KeepLatestVersionPerDayForDays = source.HistoryCleanup.KeepLatestVersionPerDayForDays;
            }
        }

        // no MapAll - take care
        private void Map(MediaTypeSave source, IMediaType target, MapperContext context)
        {
            MapSaveToTypeBase<MediaTypeSave, PropertyTypeBasic>(source, target, context);
            MapComposition(source, target, alias => _mediaTypeService.Get(alias));
        }

        // no MapAll - take care
        private void Map(MemberTypeSave source, IMemberType target, MapperContext context)
        {
            MapSaveToTypeBase<MemberTypeSave, MemberPropertyTypeBasic>(source, target, context);
            MapComposition(source, target, alias => _memberTypeService.Get(alias));

            foreach (var propertyType in source.Groups.SelectMany(x => x.Properties))
            {
                var localCopy = propertyType;
                var destProp = target.PropertyTypes.SingleOrDefault(x => x.Alias.InvariantEquals(localCopy.Alias));
                if (destProp == null) continue;
                target.SetMemberCanEditProperty(localCopy.Alias, localCopy.MemberCanEditProperty);
                target.SetMemberCanViewProperty(localCopy.Alias, localCopy.MemberCanViewProperty);
                target.SetIsSensitiveProperty(localCopy.Alias, localCopy.IsSensitiveData);
            }
        }

        // no MapAll - take care
        private void Map(IContentType source, DocumentTypeDisplay target, MapperContext context)
        {
            MapTypeToDisplayBase<DocumentTypeDisplay, PropertyTypeDisplay>(source, target);

            target.HistoryCleanup = new HistoryCleanupViewModel()
            {
                PreventCleanup = source.HistoryCleanup?.PreventCleanup ?? false,
                KeepAllVersionsNewerThanDays = source.HistoryCleanup?.KeepAllVersionsNewerThanDays,
                KeepLatestVersionPerDayForDays = source.HistoryCleanup?.KeepLatestVersionPerDayForDays,
                GlobalKeepAllVersionsNewerThanDays = _umbracoSettingsSection.Content.ContentVersionCleanupPolicyGlobalSettings.KeepAllVersionsNewerThanDays,
                GlobalKeepLatestVersionPerDayForDays = _umbracoSettingsSection.Content.ContentVersionCleanupPolicyGlobalSettings.KeepLatestVersionPerDayForDays,
                GlobalEnableCleanup = _umbracoSettingsSection.Content.ContentVersionCleanupPolicyGlobalSettings.EnableCleanup,
            };

            target.AllowCultureVariant = source.VariesByCulture();
            target.AllowSegmentVariant = source.VariesBySegment();
            target.ContentApps = _commonMapper.GetContentApps(source);

            //sync templates
            target.AllowedTemplates = context.MapEnumerable<ITemplate, EntityBasic>(source.AllowedTemplates);

            if (source.DefaultTemplate != null)
                target.DefaultTemplate = context.Map<EntityBasic>(source.DefaultTemplate);

            //default listview
            target.ListViewEditorName = Constants.Conventions.DataTypes.ListViewPrefix + "Content";

            if (string.IsNullOrEmpty(source.Alias)) return;

            var name = Constants.Conventions.DataTypes.ListViewPrefix + source.Alias;
            if (_dataTypeService.GetDataType(name) != null)
                target.ListViewEditorName = name;
        }

        // no MapAll - take care
        private void Map(IMediaType source, MediaTypeDisplay target, MapperContext context)
        {
            MapTypeToDisplayBase<MediaTypeDisplay, PropertyTypeDisplay>(source, target);

            //default listview
            target.ListViewEditorName = Constants.Conventions.DataTypes.ListViewPrefix + "Media";
            target.IsSystemMediaType = source.IsSystemMediaType();

            if (string.IsNullOrEmpty(source.Name)) return;

            var name = Constants.Conventions.DataTypes.ListViewPrefix + source.Name;
            if (_dataTypeService.GetDataType(name) != null)
                target.ListViewEditorName = name;
        }

        // no MapAll - take care
        private void Map(IMemberType source, MemberTypeDisplay target, MapperContext context)
        {
            MapTypeToDisplayBase<MemberTypeDisplay, MemberPropertyTypeDisplay>(source, target);

            //map the MemberCanEditProperty,MemberCanViewProperty,IsSensitiveData
            foreach (var propertyType in source.PropertyTypes)
            {
                var localCopy = propertyType;
                var displayProp = target.Groups.SelectMany(dest => dest.Properties).SingleOrDefault(dest => dest.Alias.InvariantEquals(localCopy.Alias));
                if (displayProp == null) continue;
                displayProp.MemberCanEditProperty = source.MemberCanEditProperty(localCopy.Alias);
                displayProp.MemberCanViewProperty = source.MemberCanViewProperty(localCopy.Alias);
                displayProp.IsSensitiveData = source.IsSensitiveProperty(localCopy.Alias);
            }
        }

        // Umbraco.Code.MapAll -Blueprints
        private static void Map(IContentTypeBase source, ContentTypeBasic target, string entityType)
        {
            target.Udi = Udi.Create(entityType, source.Key);
            target.Alias = source.Alias;
            target.CreateDate = source.CreateDate;
            target.Description = source.Description;
            target.Icon = source.Icon;
            target.Id = source.Id;
            target.IsContainer = source.IsContainer;
            target.IsElement = source.IsElement;
            target.Key = source.Key;
            target.Name = source.Name;
            target.ParentId = source.ParentId;
            target.Path = source.Path;
            target.Thumbnail = source.Thumbnail;
            target.Trashed = source.Trashed;
            target.UpdateDate = source.UpdateDate;
        }

        // no MapAll - uses the IContentTypeBase map method, which has MapAll
        private static void Map(IContentTypeComposition source, ContentTypeBasic target, MapperContext context)
        {
            Map(source, target, Constants.UdiEntityType.MemberType);
        }

        // no MapAll - uses the IContentTypeBase map method, which has MapAll
        private static void Map(IContentType source, ContentTypeBasic target, MapperContext context)
        {
            Map(source, target, Constants.UdiEntityType.DocumentType);
        }

        // no MapAll - uses the IContentTypeBase map method, which has MapAll
        private static void Map(IMediaType source, ContentTypeBasic target, MapperContext context)
        {
            Map(source, target, Constants.UdiEntityType.MediaType);
        }

        // no MapAll - uses the IContentTypeBase map method, which has MapAll
        private static void Map(IMemberType source, ContentTypeBasic target, MapperContext context)
        {
            Map(source, target, Constants.UdiEntityType.MemberType);
        }

        // Umbraco.Code.MapAll -CreateDate -DeleteDate -UpdateDate
        // Umbraco.Code.MapAll -SupportsPublishing -Key -PropertyEditorAlias -ValueStorageType -Variations
        private static void Map(PropertyTypeBasic source, PropertyType target, MapperContext context)
        {
            target.Name = source.Label;
            target.DataTypeId = source.DataTypeId;
            target.DataTypeKey = source.DataTypeKey;
            target.Mandatory = source.Validation.Mandatory;
            target.MandatoryMessage = source.Validation.MandatoryMessage;
            target.ValidationRegExp = source.Validation.Pattern;
            target.ValidationRegExpMessage = source.Validation.PatternMessage;
            target.SetVariesBy(ContentVariation.Culture, source.AllowCultureVariant);
            target.SetVariesBy(ContentVariation.Segment, source.AllowSegmentVariant);

            if (source.Id > 0)
                target.Id = source.Id;

            if (source.GroupId > 0)
            {
                if (target.PropertyGroupId?.Value != source.GroupId)
                {
                    target.PropertyGroupId = new Lazy<int>(() => source.GroupId, false);
                }
            }

            target.Alias = source.Alias;
            target.Description = source.Description;
            target.SortOrder = source.SortOrder;
            target.LabelOnTop = source.LabelOnTop;
        }

        // no MapAll - take care
        private void Map(DocumentTypeSave source, DocumentTypeDisplay target, MapperContext context)
        {
            MapTypeToDisplayBase<DocumentTypeSave, PropertyTypeBasic, DocumentTypeDisplay, PropertyTypeDisplay>(source, target, context);

            //sync templates
            var destAllowedTemplateAliases = target.AllowedTemplates.Select(x => x.Alias);
            //if the dest is set and it's the same as the source, then don't change
            if (destAllowedTemplateAliases.SequenceEqual(source.AllowedTemplates) == false)
            {
                var templates = _fileService.GetTemplates(source.AllowedTemplates.ToArray());
                target.AllowedTemplates = source.AllowedTemplates
                    .Select(x =>
                    {
                        var template = templates.SingleOrDefault(t => t.Alias == x);
                        return template != null
                            ? context.Map<EntityBasic>(template)
                            : null;
                    })
                    .WhereNotNull()
                    .ToArray();
            }

            if (source.DefaultTemplate.IsNullOrWhiteSpace() == false)
            {
                //if the dest is set and it's the same as the source, then don't change
                if (target.DefaultTemplate == null || source.DefaultTemplate != target.DefaultTemplate.Alias)
                {
                    var template = _fileService.GetTemplate(source.DefaultTemplate);
                    target.DefaultTemplate = template == null ? null : context.Map<EntityBasic>(template);
                }
            }
            else
            {
                target.DefaultTemplate = null;
            }
        }

        // no MapAll - take care
        private void Map(MediaTypeSave source, MediaTypeDisplay target, MapperContext context)
        {
            MapTypeToDisplayBase<MediaTypeSave, PropertyTypeBasic, MediaTypeDisplay, PropertyTypeDisplay>(source, target, context);
        }

        // no MapAll - take care
        private void Map(MemberTypeSave source, MemberTypeDisplay target, MapperContext context)
        {
            MapTypeToDisplayBase<MemberTypeSave, MemberPropertyTypeBasic, MemberTypeDisplay, MemberPropertyTypeDisplay>(source, target, context);
        }

        // Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate -Key -PropertyTypes
        private static void Map(PropertyGroupBasic<PropertyTypeBasic> source, PropertyGroup target, MapperContext context)
        {
            if (source.Id > 0)
                target.Id = source.Id;
            target.Key = source.Key;
            target.Type = source.Type;
            target.Name = source.Name;
            target.Alias = source.Alias;
            target.SortOrder = source.SortOrder;
        }

        // Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate -Key -PropertyTypes
        private static void Map(PropertyGroupBasic<MemberPropertyTypeBasic> source, PropertyGroup target, MapperContext context)
        {
            if (source.Id > 0)
                target.Id = source.Id;
            target.Key = source.Key;
            target.Type = source.Type;
            target.Name = source.Name;
            target.Alias = source.Alias;
            target.SortOrder = source.SortOrder;
        }

        // Umbraco.Code.MapAll -ContentTypeId -ParentTabContentTypes -ParentTabContentTypeNames
        private static void Map(PropertyGroupBasic<PropertyTypeBasic> source, PropertyGroupDisplay<PropertyTypeDisplay> target, MapperContext context)
        {
            target.Inherited = source.Inherited;
            if (source.Id > 0)
                target.Id = source.Id;
            target.Key = source.Key;
            target.Type = source.Type;
            target.Name = source.Name;
            target.Alias = source.Alias;
            target.SortOrder = source.SortOrder;
            target.Properties = context.MapEnumerable<PropertyTypeBasic, PropertyTypeDisplay>(source.Properties);
        }

        // Umbraco.Code.MapAll -ContentTypeId -ParentTabContentTypes -ParentTabContentTypeNames
        private static void Map(PropertyGroupBasic<MemberPropertyTypeBasic> source, PropertyGroupDisplay<MemberPropertyTypeDisplay> target, MapperContext context)
        {
            target.Inherited = source.Inherited;
            if (source.Id > 0)
                target.Id = source.Id;
            target.Key = source.Key;
            target.Type = source.Type;
            target.Name = source.Name;
            target.Alias = source.Alias;
            target.SortOrder = source.SortOrder;
            target.Properties = context.MapEnumerable<MemberPropertyTypeBasic, MemberPropertyTypeDisplay>(source.Properties);
        }

        // Umbraco.Code.MapAll -Editor -View -Config -ContentTypeId -ContentTypeName -Locked -DataTypeIcon -DataTypeName
        private static void Map(PropertyTypeBasic source, PropertyTypeDisplay target, MapperContext context)
        {
            target.Alias = source.Alias;
            target.AllowCultureVariant = source.AllowCultureVariant;
            target.AllowSegmentVariant = source.AllowSegmentVariant;
            target.DataTypeId = source.DataTypeId;
            target.DataTypeKey = source.DataTypeKey;
            target.Description = source.Description;
            target.GroupId = source.GroupId;
            target.Id = source.Id;
            target.Inherited = source.Inherited;
            target.Label = source.Label;
            target.SortOrder = source.SortOrder;
            target.Validation = source.Validation;
            target.LabelOnTop = source.LabelOnTop;
        }

        // Umbraco.Code.MapAll -Editor -View -Config -ContentTypeId -ContentTypeName -Locked -DataTypeIcon -DataTypeName
        private static void Map(MemberPropertyTypeBasic source, MemberPropertyTypeDisplay target, MapperContext context)
        {
            target.Alias = source.Alias;
            target.AllowCultureVariant = source.AllowCultureVariant;
            target.AllowSegmentVariant = source.AllowSegmentVariant;
            target.DataTypeId = source.DataTypeId;
            target.DataTypeKey = source.DataTypeKey;
            target.Description = source.Description;
            target.GroupId = source.GroupId;
            target.Id = source.Id;
            target.Inherited = source.Inherited;
            target.IsSensitiveData = source.IsSensitiveData;
            target.Label = source.Label;
            target.MemberCanEditProperty = source.MemberCanEditProperty;
            target.MemberCanViewProperty = source.MemberCanViewProperty;
            target.SortOrder = source.SortOrder;
            target.Validation = source.Validation;
            target.LabelOnTop = source.LabelOnTop;
        }

        // Umbraco.Code.MapAll -CreatorId -Level -SortOrder -Variations
        // Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate
        // Umbraco.Code.MapAll -ContentTypeComposition (done by AfterMapSaveToType)
        private static void MapSaveToTypeBase<TSource, TSourcePropertyType>(TSource source, IContentTypeComposition target, MapperContext context)
            where TSource : ContentTypeSave<TSourcePropertyType>
            where TSourcePropertyType : PropertyTypeBasic
        {
            // TODO: not so clean really
            var isPublishing = target is IContentType;

            var id = Convert.ToInt32(source.Id);
            if (id > 0)
                target.Id = id;

            target.Alias = source.Alias;
            target.Description = source.Description;
            target.Icon = source.Icon;
            target.IsContainer = source.IsContainer;
            target.IsElement = source.IsElement;
            target.Key = source.Key;
            target.Name = source.Name;
            target.ParentId = source.ParentId;
            target.Path = source.Path;
            target.Thumbnail = source.Thumbnail;

            target.AllowedAsRoot = source.AllowAsRoot;
            target.AllowedContentTypes = source.AllowedContentTypes.Select((t, i) => new ContentTypeSort(t, i));

            if (!(target is IMemberType))
            {
                target.SetVariesBy(ContentVariation.Culture, source.AllowCultureVariant);
                target.SetVariesBy(ContentVariation.Segment, source.AllowSegmentVariant);
            }

            // handle property groups and property types
            // note that ContentTypeSave has
            // - all groups, inherited and local; only *one* occurrence per group *name*
            // - potentially including the generic properties group
            // - all properties, inherited and local
            //
            // also, see PropertyTypeGroupResolver.ResolveCore:
            // - if a group is local *and* inherited, then Inherited is true
            //   and the identifier is the identifier of the *local* group
            //
            // IContentTypeComposition AddPropertyGroup, AddPropertyType methods do some
            // unique-alias-checking, etc that is *not* compatible with re-mapping everything
            // the way we do it here, so we should exclusively do it by
            // - managing a property group's PropertyTypes collection
            // - managing the content type's PropertyTypes collection (for generic properties)

            // handle actual groups (non-generic-properties)
            var destOrigGroups = target.PropertyGroups.ToArray(); // local groups
            var destOrigProperties = target.PropertyTypes.ToArray(); // all properties, in groups or not
            var destGroups = new List<PropertyGroup>();
            var sourceGroups = source.Groups.Where(x => x.IsGenericProperties == false).ToArray();
            var sourceGroupParentAliases = sourceGroups.Select(x => x.GetParentAlias()).Distinct().ToArray();
            foreach (var sourceGroup in sourceGroups)
            {
                // get the dest group
                var destGroup = MapSaveGroup(sourceGroup, destOrigGroups, context);

                // handle local properties
                var destProperties = sourceGroup.Properties
                    .Where(x => x.Inherited == false)
                    .Select(x => MapSaveProperty(x, destOrigProperties, context))
                    .ToArray();

                // if the group has no local properties and is not used as parent, skip it, ie sort-of garbage-collect
                // local groups which would not have local properties anymore
                if (destProperties.Length == 0 && !sourceGroupParentAliases.Contains(sourceGroup.Alias))
                    continue;

                // ensure no duplicate alias, then assign the group properties collection
                EnsureUniqueAliases(destProperties);

                if (destGroup.PropertyTypes.SupportsPublishing != isPublishing || destGroup.PropertyTypes.SequenceEqual(destProperties) is false)
                {
                    destGroup.PropertyTypes = new PropertyTypeCollection(isPublishing, destProperties);
                }

                destGroups.Add(destGroup);
            }

            // ensure no duplicate name, then assign the groups collection
            EnsureUniqueAliases(destGroups);

            if (target.PropertyGroups.SequenceEqual(destGroups) is false)
            {
                target.PropertyGroups = new PropertyGroupCollection(destGroups);
            }

            // because the property groups collection was rebuilt, there is no need to remove
            // the old groups - they are just gone and will be cleared by the repository

            // handle non-grouped (ie generic) properties
            var genericPropertiesGroup = source.Groups.FirstOrDefault(x => x.IsGenericProperties);
            if (genericPropertiesGroup != null)
            {
                // handle local properties
                var destProperties = genericPropertiesGroup.Properties
                    .Where(x => x.Inherited == false)
                    .Select(x => MapSaveProperty(x, destOrigProperties, context))
                    .ToArray();

                // ensure no duplicate alias, then assign the generic properties collection
                EnsureUniqueAliases(destProperties);
                target.NoGroupPropertyTypes = new PropertyTypeCollection(isPublishing, destProperties);
            }

            // because all property collections were rebuilt, there is no need to remove
            // some old properties, they are just gone and will be cleared by the repository
        }

        // Umbraco.Code.MapAll -Blueprints -Errors -ListViewEditorName -Trashed
        private void MapTypeToDisplayBase(IContentTypeComposition source, ContentTypeCompositionDisplay target)
        {
            target.Alias = source.Alias;
            target.AllowAsRoot = source.AllowedAsRoot;
            target.CreateDate = source.CreateDate;
            target.Description = source.Description;
            target.Icon = source.Icon;
            target.Id = source.Id;
            target.IsContainer = source.IsContainer;
            target.IsElement = source.IsElement;
            target.Key = source.Key;
            target.Name = source.Name;
            target.ParentId = source.ParentId;
            target.Path = source.Path;
            target.Thumbnail = source.Thumbnail;
            target.Udi = MapContentTypeUdi(source);
            target.UpdateDate = source.UpdateDate;

            target.AllowedContentTypes = source.AllowedContentTypes.OrderBy(c => c.SortOrder).Select(x => x.Id.Value);
            target.CompositeContentTypes = source.ContentTypeComposition.Select(x => x.Alias);
            target.LockedCompositeContentTypes = MapLockedCompositions(source);
        }

        // no MapAll - relies on the non-generic method
        private void MapTypeToDisplayBase<TTarget, TTargetPropertyType>(IContentTypeComposition source, TTarget target)
            where TTarget : ContentTypeCompositionDisplay<TTargetPropertyType>
            where TTargetPropertyType : PropertyTypeDisplay, new()
        {
            MapTypeToDisplayBase(source, target);

            var groupsMapper = new PropertyTypeGroupMapper<TTargetPropertyType>(_propertyEditors, _dataTypeService, _logger);
            target.Groups = groupsMapper.Map(source);
        }

        // Umbraco.Code.MapAll -CreateDate -UpdateDate -ListViewEditorName -Errors -LockedCompositeContentTypes
        private void MapTypeToDisplayBase(ContentTypeSave source, ContentTypeCompositionDisplay target)
        {
            target.Alias = source.Alias;
            target.AllowAsRoot = source.AllowAsRoot;
            target.AllowedContentTypes = source.AllowedContentTypes;
            target.Blueprints = source.Blueprints;
            target.CompositeContentTypes = source.CompositeContentTypes;
            target.Description = source.Description;
            target.Icon = source.Icon;
            target.Id = source.Id;
            target.IsContainer = source.IsContainer;
            target.IsElement = source.IsElement;
            target.Key = source.Key;
            target.Name = source.Name;
            target.ParentId = source.ParentId;
            target.Path = source.Path;
            target.Thumbnail = source.Thumbnail;
            target.Trashed = source.Trashed;
            target.Udi = source.Udi;
        }

        // no MapAll - relies on the non-generic method
        private void MapTypeToDisplayBase<TSource, TSourcePropertyType, TTarget, TTargetPropertyType>(TSource source, TTarget target, MapperContext context)
            where TSource : ContentTypeSave<TSourcePropertyType>
            where TSourcePropertyType : PropertyTypeBasic
            where TTarget : ContentTypeCompositionDisplay<TTargetPropertyType>
            where TTargetPropertyType : PropertyTypeDisplay
        {
            MapTypeToDisplayBase(source, target);

            target.Groups = context.MapEnumerable<PropertyGroupBasic<TSourcePropertyType>, PropertyGroupDisplay<TTargetPropertyType>>(source.Groups);
        }

        private IEnumerable<string> MapLockedCompositions(IContentTypeComposition source)
        {
            // get ancestor ids from path of parent if not root
            if (source.ParentId == Constants.System.Root)
                return Enumerable.Empty<string>();

            var parent = _contentTypeService.Get(source.ParentId);
            if (parent == null)
                return Enumerable.Empty<string>();

            var aliases = new List<string>();
            var ancestorIds = parent.Path.Split(Constants.CharArrays.Comma).Select(int.Parse);
            // loop through all content types and return ordered aliases of ancestors
            var allContentTypes = _contentTypeService.GetAll().ToArray();
            foreach (var ancestorId in ancestorIds)
            {
                var ancestor = allContentTypes.FirstOrDefault(x => x.Id == ancestorId);
                if (ancestor != null)
                    aliases.Add(ancestor.Alias);
            }
            return aliases.OrderBy(x => x);
        }

        internal static Udi MapContentTypeUdi(IContentTypeComposition source)
        {
            if (source == null) return null;

            string udiType;
            switch (source)
            {
                case IMemberType _:
                    udiType = Constants.UdiEntityType.MemberType;
                    break;
                case IMediaType _:
                    udiType = Constants.UdiEntityType.MediaType;
                    break;
                case IContentType _:
                    udiType = Constants.UdiEntityType.DocumentType;
                    break;
                default:
                    throw new PanicException($"Source is of type {source.GetType()} which isn't supported here");
            }

            return Udi.Create(udiType, source.Key);
        }

        private static PropertyGroup MapSaveGroup<TPropertyType>(PropertyGroupBasic<TPropertyType> sourceGroup, IEnumerable<PropertyGroup> destOrigGroups, MapperContext context)
            where TPropertyType : PropertyTypeBasic
        {
            PropertyGroup destGroup;
            if (sourceGroup.Id > 0)
            {
                // update an existing group
                // ensure it is still there, then map/update
                destGroup = destOrigGroups.FirstOrDefault(x => x.Id == sourceGroup.Id);
                if (destGroup != null)
                {
                    context.Map(sourceGroup, destGroup);
                    return destGroup;
                }

                // force-clear the ID as it does not match anything
                sourceGroup.Id = 0;
            }

            // insert a new group, or update an existing group that has
            // been deleted in the meantime and we need to re-create
            // map/create
            destGroup = context.Map<PropertyGroup>(sourceGroup);
            return destGroup;
        }

        private static PropertyType MapSaveProperty(PropertyTypeBasic sourceProperty, IEnumerable<PropertyType> destOrigProperties, MapperContext context)
        {
            PropertyType destProperty;
            if (sourceProperty.Id > 0)
            {
                // updating an existing property
                // ensure it is still there, then map/update
                destProperty = destOrigProperties.FirstOrDefault(x => x.Id == sourceProperty.Id);
                if (destProperty != null)
                {
                    context.Map(sourceProperty, destProperty);
                    return destProperty;
                }

                // force-clear the ID as it does not match anything
                sourceProperty.Id = 0;
            }

            // insert a new property, or update an existing property that has
            // been deleted in the meantime and we need to re-create
            // map/create
            destProperty = context.Map<PropertyType>(sourceProperty);
            return destProperty;
        }

        private static void EnsureUniqueAliases(IEnumerable<PropertyType> properties)
        {
            var propertiesA = properties.ToArray();
            var distinctProperties = propertiesA
                .Select(x => x.Alias?.ToUpperInvariant())
                .Distinct()
                .Count();
            if (distinctProperties != propertiesA.Length)
                throw new InvalidOperationException("Cannot map properties due to alias conflict.");
        }

        private static void EnsureUniqueAliases(IEnumerable<PropertyGroup> groups)
        {
            var groupsA = groups.ToArray();
            var distinctProperties = groupsA
                .Select(x => x.Alias)
                .Distinct()
                .Count();
            if (distinctProperties != groupsA.Length)
                throw new InvalidOperationException("Cannot map groups due to alias conflict.");
        }

        private static void MapComposition(ContentTypeSave source, IContentTypeComposition target, Func<string, IContentTypeComposition> getContentType)
        {
            var current = target.CompositionAliases().ToArray();
            var proposed = source.CompositeContentTypes;

            var remove = current.Where(x => !proposed.Contains(x));
            var add = proposed.Where(x => !current.Contains(x));

            foreach (var alias in remove)
                target.RemoveContentType(alias);

            foreach (var alias in add)
            {
                // TODO: Remove N+1 lookup
                var contentType = getContentType(alias);
                if (contentType != null)
                    target.AddContentType(contentType);
            }
        }
    }
}


text:
//  Copyright(c) 2016-2022 Breanna Hansen.

//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

//  The above copyright notice and this permission notice shall be included in all copies or substantial portions
//  of the Software.

//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
//  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//  DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using bkh.ParseTreeLib;

namespace SqlServerQueryTreeViewer
{
    public class VerticalBalancedTreeVisualizer : TreeVisualizer
    {
        private const int _blockWidth = 100;
        private const int _blockHeight = 30;
        private const int _spacingWidth = 30;
        private const int _spacingHeight = 80;
        private const int _interChildSpacingWidth = 30;
        private const int _leftMargin = 110;
        private const int _rightMargin = 110;
        private const int _topMargin = 60;
        private const int _bottomMargin = 60;

        public VerticalBalancedTreeVisualizer()
        {
        }

        public override Bitmap Render(SqlParseTree tree, out List<TreeNodeIcon> nodeIcons)
        {
            if (tree == null)
            {
                throw new ArgumentNullException(nameof(tree));
            }

            int maxDepth;
            int maxWidth;
            List<TreeNodeIcon> icons = FlattenTree(tree, out maxDepth, out maxWidth);

            int width = _leftMargin + _rightMargin + maxWidth * _blockWidth + (maxWidth - 1) * _spacingWidth;
            int height = _topMargin + _bottomMargin + maxDepth * _blockHeight + (maxDepth - 1) * _spacingHeight;
            CenterLevels(icons, width);
            BalanceNodes(icons);
            PositionNodes(icons, out width);

            Font font = new Font("Times New Roman", 10);
            Pen leafNodeBorderPen = new Pen(Color.Red, 2);

            Bitmap bitmap = new Bitmap(width, height);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.FillRectangle(Brushes.White, 0, 0, width, height);
                foreach (TreeNodeIcon icon in icons)
                {
                    Rectangle rectangle = new Rectangle(icon.Left, icon.Top, icon.Width, icon.Height);
                    //graphics.FillRectangle(Brushes.LightSteelBlue, rectangle);
                    Brush brush = GetBrushForOperator(icon.Node.OperationName);
                    Pen borderPen = icon.IsLeafNode ? leafNodeBorderPen : null;
                    DrawFilledRoundedRectangle(graphics, brush, rectangle, borderPen);
                    SizeF textSize = graphics.MeasureString(icon.Node.OperationName, font);
                    RectangleF textRectangle = new RectangleF(
                        icon.Left + icon.Width / 2 - textSize.Width / 2,
                        icon.Top + icon.Height / 2 - textSize.Height / 2,
                        textSize.Width,
                        textSize.Height);
                    graphics.DrawString(icon.Node.OperationName, font, Brushes.Black, textRectangle);

                    if (icon.Parent != null)
                    {
                        graphics.DrawLine(Pens.Black, icon.Left + icon.Width / 2, icon.Top, icon.Parent.Left + icon.Parent.Width / 2, icon.Parent.Top + icon.Parent.Height);
                    }
                }
            }

            nodeIcons = icons;
            ExtractOperators(tree);
            return bitmap;
        }

        private static void DrawFilledRoundedRectangle(Graphics graphics, Brush brush, Rectangle rectangle, Pen optionalBorderPen)
        {
            int radius = 5;

            GraphicsPath path = new GraphicsPath();
            path.StartFigure();

            path.AddArc(rectangle.Left, rectangle.Top, 2 * radius, 2 * radius, 180, 90);
            path.AddLine(rectangle.Left + radius, rectangle.Top, rectangle.Left + rectangle.Width - radius, rectangle.Top);
            path.AddArc(rectangle.Left + rectangle.Width - 2 * radius, rectangle.Top, 2 * radius, 2 * radius, 270, 90);
            path.AddLine(rectangle.Left + rectangle.Width, rectangle.Top + radius, rectangle.Left + rectangle.Width, rectangle.Top + rectangle.Height - radius);
            path.AddArc(rectangle.Left + rectangle.Width - 2 * radius, rectangle.Top + rectangle.Height - 2 * radius, 2 * radius, 2 * radius, 0, 90);
            path.AddLine(rectangle.Left + rectangle.Width - radius, rectangle.Top + rectangle.Height, rectangle.Left + radius, rectangle.Top + rectangle.Height);
            path.AddArc(rectangle.Left, rectangle.Top + rectangle.Height - 2 * radius, 2 * radius, 2 * radius, 90, 90);
            path.AddLine(rectangle.Left, rectangle.Top + rectangle.Height - radius, rectangle.Left, rectangle.Top + radius);

            path.CloseFigure();

            graphics.FillPath(brush, path);
            if (optionalBorderPen != null)
            {
                graphics.DrawPath(optionalBorderPen, path);
            }
        }

        private static List<TreeNodeIcon> FlattenTree(SqlParseTree tree, out int maxDepth, out int maxWidth)
        {
            Dictionary<int, int> levelCounts = new Dictionary<int, int>();
            List<TreeNodeIcon> icons = new List<TreeNodeIcon>();

            MeasureNode(tree.RootNode, 0, levelCounts, icons);

            maxDepth = levelCounts.Keys.Count;
            maxWidth = levelCounts.Values.Max();
            FixupParentLinks(icons);

            return icons;
        }

        private static void MeasureNode(SqlParseTreeNode node, int depth, Dictionary<int, int> levelCounts, List<TreeNodeIcon> icons)
        {
            if (levelCounts.ContainsKey(depth) == false)
            {
                levelCounts.Add(depth, 0);
            }
            levelCounts[depth]++;

            TreeNodeIcon icon = new TreeNodeIcon();

            icon.Node = node;
            icon.X = levelCounts[depth] - 1;
            icon.Y = depth;
            icon.Left = _leftMargin + icon.X * (_blockWidth + _spacingWidth);
            icon.Top = _topMargin + icon.Y * (_blockHeight + _spacingHeight);
            icon.Width = _blockWidth;
            icon.Height = _blockHeight;

            icons.Add(icon);

            foreach (SqlParseTreeNode child in node.Children)
            {
                MeasureNode(child, depth + 1, levelCounts, icons);
            }
        }

        private static void FixupParentLinks(List<TreeNodeIcon> icons)
        {
            Dictionary<SqlParseTreeNode, TreeNodeIcon> iconMap = new Dictionary<SqlParseTreeNode, TreeNodeIcon>();
            icons.ForEach(i => iconMap.Add(i.Node, i));

            foreach (TreeNodeIcon icon in icons)
            {
                SqlParseTreeNode parentNode = icon.Node.Parent;
                if (parentNode != null)
                {
                    icon.Parent = iconMap[parentNode];
                }

                foreach (SqlParseTreeNode childNode in icon.Node.Children)
                {
                    icon.Children.Add(iconMap[childNode]);
                }
            }
        }

        private static void CenterLevels(List<TreeNodeIcon> icons, int overallWidth)
        {
            Dictionary<int, List<TreeNodeIcon>> iconsByLevel = new Dictionary<int, List<TreeNodeIcon>>();
            foreach (TreeNodeIcon icon in icons)
            {
                if (iconsByLevel.ContainsKey(icon.Y) == false)
                {
                    iconsByLevel.Add(icon.Y, new List<TreeNodeIcon>());
                }
                iconsByLevel[icon.Y].Add(icon);
            }

            foreach (int level in iconsByLevel.Keys)
            {
                int left = iconsByLevel[level].Min(i => i.Left);
                int right = iconsByLevel[level].Max(i => i.Left + i.Width);
                int center = (left + right) / 2;
                int shift = overallWidth / 2 - center;

                iconsByLevel[level].ForEach(i => i.Left += shift);
            }
        }

        private static void BalanceNodes(List<TreeNodeIcon> icons)
        {
            int depth = icons.Max(i => i.Y);
            while (depth >= 0)
            {
                List<TreeNodeIcon> iconsAtDepth = icons.Where(i => i.Y == depth).ToList();
                iconsAtDepth.Sort((a, b) => a.X.CompareTo(b.X));

                foreach (TreeNodeIcon icon in iconsAtDepth)
                {
                    icon.DescendantWidth =
                        Math.Max(
                            _blockWidth,
                            icon.Children.Sum(c => c.DescendantWidth) + _interChildSpacingWidth * (icon.Children.Count - 1));
                }

                depth--;
            }
        }

        private static void PositionNodes(List<TreeNodeIcon> icons, out int width)
        {
            // Get the root node
            TreeNodeIcon root = icons.FirstOrDefault(i => i.Parent == null);
            if (root == null)
            {
                throw new ApplicationException("Cannot find root node!");
            }

            width = _leftMargin + root.DescendantWidth + _rightMargin;
            root.Top = _topMargin;
            root.Left = width / 2 - _blockWidth / 2;
            int level = 0;

            List<TreeNodeIcon> nodes = new List<TreeNodeIcon>() { root };
            while (nodes.Count > 0)
            {
                level++;
                foreach (TreeNodeIcon node in nodes)
                {
                    node.Children.Sort((a, b) => a.X.CompareTo(b.X));
                    foreach (TreeNodeIcon child in node.Children)
                    {
                        int previousSiblingWidth = node.Children.Where(n => n.X < child.X).Sum(n => n.DescendantWidth) +
                            node.Children.Count(n => n.X < child.X) * _interChildSpacingWidth;
                        double relativePosition = 1.0 * (previousSiblingWidth + child.DescendantWidth / 2) / node.DescendantWidth;

                        child.Top = _topMargin + (_blockHeight + _spacingHeight) * level;
                        child.Left = (int)(node.Left - node.DescendantWidth / 2 + relativePosition * node.DescendantWidth);
                    }
                }

                nodes = nodes.SelectMany(n => n.Children).ToList();
            }
        }

        private static Brush GetBrushForOperator(string operatorName)
        {
            return new SolidBrush(GetColorForOperator(operatorName));
        }

        private static Color GetColorForOperator(string operatorName)
        {
            if (string.IsNullOrEmpty(operatorName) == false)
            {
                Color? color = ViewerSettings.Instance.GetOperatorColor(operatorName);
                if (color != null)
                {
                    return color.Value;
                }
            }

            switch (operatorName)
            {
                case "LogOp_Project":
                    return Color.LightGreen;

                case "LogOp_Get":
                    return Color.LightBlue;

                case "LogOp_Select":
                    return Color.CornflowerBlue;

                case "ScaOp_Const":
                    return Color.LightSalmon;

                case "ScaOp_Identifier":
                    return Color.LightPink;

                case "ScaOp_Logical":
                    return Color.LightGoldenrodYellow;

                case "ScaOp_Comp":
                    return Color.Goldenrod;

                case "AncOp_PrjList":
                    return Color.LightGreen;

                default:
                    return Color.LightSteelBlue;
            }
        }

        private static void ExtractOperators(SqlParseTree tree)
        {
            List<OperatorColor> operatorColors = new List<OperatorColor>(ViewerSettings.Instance.OperatorColors);
            Dictionary<string, OperatorColor> index = new Dictionary<string, OperatorColor>();
            operatorColors.ForEach(o => { if (index.ContainsKey(o.OperatorName) == false) { index.Add(o.OperatorName, o); } });
            List<SqlParseTreeNode> nodes = new List<SqlParseTreeNode>() { tree.RootNode };
            while (nodes.Count > 0)
            {
                foreach (SqlParseTreeNode node in nodes)
                {
                    if (string.IsNullOrEmpty(node.OperationName) == false &&
                        index.ContainsKey(node.OperationName) == false)
                    {
                        OperatorColor operatorColor = new OperatorColor();
                        operatorColor.OperatorName = node.OperationName;
                        operatorColor.DisplayColor = GetColorForOperator(node.OperationName);
                        index.Add(node.OperationName, operatorColor);
                        ViewerSettings.Instance.AddOperatorColor(node.OperationName, operatorColor.DisplayColor);
                    }
                }

                nodes = nodes.SelectMany(n => n.Children).ToList();
            }
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
package enumerations;

public enum PaymentMethod {

	cash,
	creditCard,
	payPal

}


text:
using System;

namespace Problem_04_Nth_Digit
{
    class Program_04_Nth_Digit
    {
        static void Main()
        {
            string number = Console.ReadLine();
            int position = int.Parse(Console.ReadLine());

            Console.WriteLine(number[number.Length-position]);
        }
    }
}


text:
using System.Collections.Generic;

namespace HyperLibrary.Core.Resources
{
    public class BookCatalogResource : LinkableResource
    {
        public IList<BookResource> Catalog { get; set; }
    }

}


text:
"""Correctness tests for tf.keras DNN model using DistributionStrategy."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from tensorflow.python import keras
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.distribute import combinations
from tensorflow.python.distribute import distribution_strategy_context
from tensorflow.python.eager import context
from tensorflow.python.keras import backend as K
from tensorflow.python.keras import testing_utils
from tensorflow.python.keras.distribute import keras_correctness_test_base
from tensorflow.python.keras.optimizer_v2 import gradient_descent as gradient_descent_keras
from tensorflow.python.platform import test
from tensorflow.python.training import gradient_descent

def all_strategy_combinations_with_eager_and_graph_modes():
  return (combinations.combine(
      distribution=keras_correctness_test_base.all_strategies,
      mode=['graph', 'eager']))

def all_strategy_combinations_with_graph_mode():
  return (combinations.combine(
      distribution=keras_correctness_test_base.all_strategies,
      mode=['graph']))

def is_default_strategy(strategy):
  with strategy.scope():
    return not distribution_strategy_context.has_strategy()

@testing_utils.run_all_without_tensor_float_32(
    'Uses Dense layers, which call matmul')
class TestDistributionStrategyDnnCorrectness(
    keras_correctness_test_base.TestDistributionStrategyCorrectnessBase):

  def get_model(self,
                initial_weights=None,
                distribution=None,
                input_shapes=None):
    with keras_correctness_test_base.MaybeDistributionScope(distribution):
      # We add few non-linear layers to make it non-trivial.
      model = keras.Sequential()
      model.add(keras.layers.Dense(10, activation='relu', input_shape=(1,)))
      model.add(
          keras.layers.Dense(
              10,
              activation='relu',
              kernel_regularizer=keras.regularizers.l2(1e-4)))
      model.add(keras.layers.Dense(10, activation='relu'))
      model.add(keras.layers.Dense(1))

      if initial_weights:
        model.set_weights(initial_weights)

      model.compile(
          loss=keras.losses.mean_squared_error,
          optimizer=gradient_descent_keras.SGD(0.05),
          metrics=['mse'])
      return model

  def get_data(self):
    x_train = np.random.rand(9984, 1).astype('float32')
    y_train = 3 * x_train
    x_predict = np.array([[1.], [2.], [3.], [4.]], dtype=np.float32)
    return x_train, y_train, x_predict

  def get_data_with_partial_last_batch(self):
    x_train = np.random.rand(10000, 1).astype('float32')
    y_train = 3 * x_train
    x_eval = np.random.rand(10000, 1).astype('float32')
    y_eval = 3 * x_eval
    x_predict = np.array([[1.], [2.], [3.], [4.]], dtype=np.float32)
    return x_train, y_train, x_eval, y_eval, x_predict

  def get_data_with_partial_last_batch_eval(self):
    x_train = np.random.rand(9984, 1).astype('float32')
    y_train = 3 * x_train
    x_eval = np.random.rand(10000, 1).astype('float32')
    y_eval = 3 * x_eval
    x_predict = np.array([[1.], [2.], [3.], [4.]], dtype=np.float32)
    return x_train, y_train, x_eval, y_eval, x_predict

  @combinations.generate(
      keras_correctness_test_base.all_strategy_and_input_config_combinations())
  def test_dnn_correctness(self, distribution, use_numpy, use_validation_data):
    self.run_correctness_test(distribution, use_numpy, use_validation_data)

  @combinations.generate(
      keras_correctness_test_base.test_combinations_with_tpu_strategies())
  def test_dnn_correctness_with_partial_last_batch_eval(self, distribution,
                                                        use_numpy,
                                                        use_validation_data):
    self.run_correctness_test(
        distribution, use_numpy, use_validation_data, partial_last_batch='eval')

  @combinations.generate(
      keras_correctness_test_base
      .strategy_minus_tpu_and_input_config_combinations_eager())
  def test_dnn_correctness_with_partial_last_batch(self, distribution,
                                                   use_numpy,
                                                   use_validation_data):
    distribution.extended.experimental_enable_get_next_as_optional = True
    self.run_correctness_test(
        distribution,
        use_numpy,
        use_validation_data,
        partial_last_batch='train_and_eval',
        training_epochs=1)

  @combinations.generate(all_strategy_combinations_with_graph_mode())
  def test_dnn_with_dynamic_learning_rate(self, distribution):
    self.run_dynamic_lr_test(distribution)

class TestDistributionStrategyDnnMetricCorrectness(
    keras_correctness_test_base.TestDistributionStrategyCorrectnessBase):

  def get_model(self,
                distribution=None,
                input_shapes=None):
    with distribution.scope():
      model = keras.Sequential()
      model.add(
          keras.layers.Dense(1, input_shape=(1,), kernel_initializer='ones'))
      model.compile(
          loss=keras.losses.mean_squared_error,
          optimizer=gradient_descent_keras.SGD(0.05),
          metrics=[keras.metrics.BinaryAccuracy()])
    return model

  def run_metric_correctness_test(self, distribution):
    with self.cached_session():
      self.set_up_test_config()

      x_train, y_train, _ = self.get_data()
      model = self.get_model(
          distribution=distribution)

      batch_size = 64
      batch_size = (
          keras_correctness_test_base.get_batch_size(batch_size, distribution))
      train_dataset = dataset_ops.Dataset.from_tensor_slices((x_train, y_train))
      train_dataset = (
          keras_correctness_test_base.batch_wrapper(train_dataset, batch_size))

      history = model.fit(x=train_dataset, epochs=2, steps_per_epoch=10)
      self.assertEqual(history.history['binary_accuracy'], [1.0, 1.0])

  @combinations.generate(all_strategy_combinations_with_eager_and_graph_modes())
  def test_simple_dnn_metric_correctness(self, distribution):
    self.run_metric_correctness_test(distribution)

class TestDistributionStrategyDnnMetricEvalCorrectness(
    keras_correctness_test_base.TestDistributionStrategyCorrectnessBase):

  def get_model(self,
                distribution=None,
                input_shapes=None):
    with distribution.scope():
      model = keras.Sequential()
      model.add(
          keras.layers.Dense(
              3, activation='relu', input_dim=4, kernel_initializer='ones'))
      model.add(
          keras.layers.Dense(
              1, activation='sigmoid', kernel_initializer='ones'))
      model.compile(
          loss='mae',
          metrics=['accuracy', keras.metrics.BinaryAccuracy()],
          optimizer=gradient_descent.GradientDescentOptimizer(0.001))
    return model

  def run_eval_metrics_correctness_test(self, distribution):
    with self.cached_session():
      self.set_up_test_config()

      model = self.get_model(
          distribution=distribution)

      # verify correctness of stateful and stateless metrics.
      x = np.ones((100, 4)).astype('float32')
      y = np.ones((100, 1)).astype('float32')
      dataset = dataset_ops.Dataset.from_tensor_slices((x, y)).repeat()
      dataset = keras_correctness_test_base.batch_wrapper(dataset, 4)
      outs = model.evaluate(dataset, steps=10)
      self.assertEqual(outs[1], 1.)
      self.assertEqual(outs[2], 1.)

      y = np.zeros((100, 1)).astype('float32')
      dataset = dataset_ops.Dataset.from_tensor_slices((x, y)).repeat()
      dataset = keras_correctness_test_base.batch_wrapper(dataset, 4)
      outs = model.evaluate(dataset, steps=10)
      self.assertEqual(outs[1], 0.)
      self.assertEqual(outs[2], 0.)

  @combinations.generate(all_strategy_combinations_with_eager_and_graph_modes())
  def test_identity_model_metric_eval_correctness(self, distribution):
    self.run_eval_metrics_correctness_test(distribution)

class SubclassedModel(keras.Model):

  def __init__(self, initial_weights, input_shapes):
    super(SubclassedModel, self).__init__()
    self.dense1 = keras.layers.Dense(10, activation='relu', input_shape=(1,))
    self.dense2 = keras.layers.Dense(
        10, activation='relu', kernel_regularizer=keras.regularizers.l2(1e-4))
    self.dense3 = keras.layers.Dense(10, activation='relu')
    self.dense4 = keras.layers.Dense(1)
    if input_shapes:
      self.build(input_shapes)
    else:
      # This covers cases when the input is DatasetV1Adapter.
      self.build((None, 1))
    if initial_weights:
      self.set_weights(initial_weights)

  def call(self, inputs):
    x = self.dense1(inputs)
    x = self.dense2(x)
    x = self.dense3(x)
    return self.dense4(x)

@testing_utils.run_all_without_tensor_float_32(
    'Uses Dense layers, which call matmul')
class TestDistributionStrategyDnnCorrectnessWithSubclassedModel(
    TestDistributionStrategyDnnCorrectness):

  def get_model(self,
                initial_weights=None,
                distribution=None,
                input_shapes=None):
    with keras_correctness_test_base.MaybeDistributionScope(distribution):
      model = SubclassedModel(initial_weights, input_shapes)

      model.compile(
          loss=keras.losses.mean_squared_error,
          optimizer=gradient_descent_keras.SGD(0.05),
          metrics=['mse'])
      return model

  @combinations.generate(
      keras_correctness_test_base.all_strategy_and_input_config_combinations())
  def test_dnn_correctness(self, distribution, use_numpy, use_validation_data):
    if (context.executing_eagerly()) or is_default_strategy(distribution):
      self.run_correctness_test(distribution, use_numpy, use_validation_data)
    elif K.is_tpu_strategy(distribution) and not context.executing_eagerly():
      with self.assertRaisesRegex(
          ValueError,
          'Expected `model` argument to be a functional `Model` instance, '
          'but got a subclass model instead.'):
        self.run_correctness_test(distribution, use_numpy, use_validation_data)
    else:
      with self.assertRaisesRegex(
          ValueError,
          'We currently do not support distribution strategy with a '
          '`Sequential` model that is created without `input_shape`/'
          '`input_dim` set in its first layer or a subclassed model.'):
        self.run_correctness_test(distribution, use_numpy, use_validation_data)

  @combinations.generate(all_strategy_combinations_with_graph_mode())
  def test_dnn_with_dynamic_learning_rate(self, distribution):
    if ((context.executing_eagerly() and not K.is_tpu_strategy(distribution)) or
        is_default_strategy(distribution)):
      self.run_dynamic_lr_test(distribution)
    elif K.is_tpu_strategy(distribution):
      with self.assertRaisesRegex(
          ValueError,
          'Expected `model` argument to be a functional `Model` instance, '
          'but got a subclass model instead.'):
        self.run_dynamic_lr_test(distribution)
    else:
      with self.assertRaisesRegex(
          ValueError,
          'We currently do not support distribution strategy with a '
          '`Sequential` model that is created without `input_shape`/'
          '`input_dim` set in its first layer or a subclassed model.'):
        self.run_dynamic_lr_test(distribution)

  @combinations.generate(
      keras_correctness_test_base.test_combinations_with_tpu_strategies())
  def test_dnn_correctness_with_partial_last_batch_eval(self, distribution,
                                                        use_numpy,
                                                        use_validation_data):
    with self.assertRaisesRegex(
        ValueError,
        'Expected `model` argument to be a functional `Model` instance, '
        'but got a subclass model instead.'):
      self.run_correctness_test(
          distribution,
          use_numpy,
          use_validation_data,
          partial_last_batch='eval')

if __name__ == '__main__':
  test.main()


text:
from ronin.events import RoninEventHandler
from ronin.strategies import StrategyFactory
from logging import StreamHandler
from logging.handlers import RotatingFileHandler
from pprint import pprint
import json
import logging
import os
import sys
import time

#: Perform basic logging configuration
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    level=logging.INFO)

#: Configure root level logger.
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
#logger.addHandler(StreamHandler(stream=sys.stdout))

class Manifest(object):
    """
    The Manifest is the representation of the instructions to `ronin` for a
    given directory. It lists the path that files within the `ronin`-enabled
    directory should be syncrhonized to, it lists what files and directories
    should be excluded, provides information on whether the synchronization
    operation should be elevated, etc.

    A manifest file (i.e. `ronin.json`) is a JSON file that contains each of
    the manifest options as a root-level key, e.g.::

    {
      "type": "rsync",
      "path": "/usr/share/mydir",
      "elevate": true,
      "args": ["--verbose"],
      "exclude": [
        ".git"
      ]
    }

    :param manifest_path: Optional. The path to the manifest file. If
        provided it will be parsed when the manifest is instantiated.
    """

    def __init__(self, manifest_path=None):
        #: The individual arguments that should be passed into the handler
        #: for file synchronization.
        self.args = None

        #: The destination directory that the contents of the directory
        #: containing the manifest should be synchronized with.
        self.path = None

        #: Whether or not file synchronization operations should be elevated
        #: and performed by a super user or administrator.
        self.elevate = False

        #: The collection of files and/or directories that should be excluded
        #: from synchronization.
        self.exclude = None

        #: The strategy to use for file syncrhonization, e.g. "rsync"
        self.type = None

        if manifest_path:
            self.parse(manifest_path)

    def __repr__(self):
        return "<Manifest(type='{0}', destination='{1}')>".format(self.type, self.dest)

    def apply(self, **kwargs):
        """
        Assign manifest properties from the passed keyword arguments.

        :param kwargs: the keyword arguments that may be used to assign this
            manifest's properties::

            - args
            - elevate
            - exclude
            - path
            - type
        """
        for key, value in kwargs.iteritems():
            if hasattr(self, key):
                logger.debug("Setting manifest property: {0}={1}".format(key, value))
                setattr(self, key, value)

    @property
    def destination(self):
        """
        Return the destination that files in the manifest directory should
        be synchronized with.
        """
        # TODO: Support remote (i.e. SSH) destinations
        return self.path

    def parse(self, manifest_path):
        """
        Parse the data from the manifest file.

        :param manifest: the manifest file.
        """
        logger.debug("Parsing manifest located at: {0}".format(manifest_path))
        with open(manifest_path) as data_file:
            data = json.load(data_file)
            self.apply(**data)

class Ronin(object):
    """
    The class representing the `ronin` process. Ronin is responsible for
    triggering the file synchronization operation, `ronin` may be invoked
    either as an independent process or as a continuous operation that
    watches and reacts to changes in the file system.

    The `ronin` process can be launched from the command line

    :param source: The directory containing the manifest file.
    :param loglevel: Optional. The level that messages should be logged at.
        Default: `logging.INFO`.
    :param logfile: Optional. The file that messages should be recorded to.
        Default: `/var/log/ronin/ronin.log`
    :param watch: Optional. Whether the `source` directory should be watched
        for changes. Default: `False`.
    :param poll: Optional. If watching a directory, whether polling should
        be used instead of events. Default: `False`.
    """

    #: The maximum number of bytes that a log file should be. (2MB)
    LOG_FILE_BYTE_SIZE = 2097152

    def __init__(self, source, **kwargs):
        #: The level of detail that messages should be logged at, by default
        #: logging.INFO.
        self.loglevel = logging.INFO

        #: The file that persistent log messages should be recorded to.
        self.logfile = self.default_logfile

        #: If watching a directory for changes, this indicates whether or not
        #: ronin should be polling for this changes or relying on file system
        #: events.
        #:
        #: If ronin is being used to synchronize files through a shared folder
        #: polling must be turned on because file system events are not
        #: guaranteed to trigger if a system outside of the directory changes
        #: the files.
        self.poll = False

        #: The source directory from which files will be copied during
        #: the synchronization.
        self.source = source

        #: The strategy that will be invoked when running Ronin.
        self.strategy = None

        #: Whether or not the source directory should be watched for changes.
        #: If the source directory is not being watched, ronin will run once
        #: and then exit.
        self.watch = False

        for key, value in kwargs.iteritems():
            if hasattr(self, key) and value is not None:
                setattr(self, key, value)

        if self.source is None:
            raise TypeError("Source must not be None.")

        #: Initialize
        self.init_logging()

        manifest = self.read_manifest()
        factory = StrategyFactory()
        self.strategy = factory.get_strategy(self.source, manifest)

    def __repr__(self):
        return "<Ronin(source='{0}', watch='{1}', poll='{2}')>".format(self.source, self.watch, self.poll)

    @property
    def default_logfile(self):
        """
        Return the default logfile that messages should be written to.
        """
        path = os.path.abspath(".")
        if os.name == "posix" or os.name == "os2":
            path = os.path.abspath("/var/log/ronin/ronin.log")
        return path

    def init_logging(self):
        """
        Initialize logging for the application.
        """
        path = os.path.dirname(self.logfile)
        try:
            os.makedirs(path)
        except OSError:
            if not os.path.isdir(path):
                raise
        formatter = logging.Formatter(fmt="%(asctime)s - %(levelname)s - %(message)s",
                                      datefmt="%Y-%m-%d %H:%M:%S")
        rfh = RotatingFileHandler(filename=self.logfile,
                                  maxBytes=Ronin.LOG_FILE_BYTE_SIZE,
                                  backupCount=10)
        rfh.setFormatter(formatter)
        logger.addHandler(rfh)
        logger.setLevel(self.loglevel)

    def read_manifest(self):
        """
        Read the manifest located in the `source` directory and, if it exists,
        construct a new Manifest object from it.
        """
        logger.debug("Reading manifest file from source: {0}".format(self.source))
        manifest_path = os.path.join(self.source, "ronin.json")
        if not os.path.exists(manifest_path):
            raise IOError("Source directory: {0} does not contain ronin.json file.".format(self.source))
        manifest = Manifest(manifest_path)
        return manifest

    def run(self):
        """
        Execute the application's logic, invoking the file synchronization
        defined by the strategy. If Ronin is configured to watch the source
        directory for changes, it will react to any changes within the
        file system.
        """
        if self.watch:
            self.run_watch()
        else:
            self.strategy.invoke()
        logger.info("Goodbye!")

    def run_watch(self):
        schedule_kwargs = {"recursive": True}
        if self.poll:
            from ronin.observers.polling import DiscriminatedPollingObserver as Observer
            schedule_kwargs["exclude_paths"] = self.strategy.exclude_paths
        else:
            from watchdog.observers import Observer

        logger.info("Starting file system observer for: {0}".format(self.source))
        event_handler = RoninEventHandler(self)
        observer = Observer()
        observer.schedule(event_handler, self.source, **schedule_kwargs)
        observer.start()

        try:
            logger.info("Watching directory: '{0}' for changes (poll={1})".format(self.source, self.poll))
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            logger.info("Stopping watcher...")
            observer.stop()
        observer.join()


text:
Allows you to create an after/colors/ script for customizing any colorscheme.
This is a bug fixes version for original vim-script AfterColors at
http://www.vim.org/scripts/script.php?script_id=1641

**EXAMPLE**

If you like the 'desert' colorscheme, but you really want comments to be red and functions to be blue, previously you would copy the entire colorscheme into your home directory and customize it.  With this plugin installed, you can create a small script to change just the parts you want for that colorscheme, exactly how you would for an ftplugin or syntax script:

For unix systems you would create:

    # ~/.vim/after/colors/desert.vim:
    highlight Comment guifg=Red ctermfg=Red
    highlight Function guifg=Blue ctermfg=Blue

On windows you would create:

    # C:\Documents and Settings\Peter\vimfiles\after\colors\desert.vim:
    highlight Comment guifg=Red ctermfg=Red
    highlight Function guifg=Blue ctermfg=Blue

## Requirements

- Vim 7.0 or higher.

**VERSION 6 WARNING**

If your Vim is older than version 7, then the after/colors scripts will only be loaded
once when Vim starts.  This will not be a problem if you choose your colorscheme in your
.vimrc file, but if you change your colorscheme after vim has loaded then your after/colors
scripts will be ignored.  This is not an issue in Vim 7.

## Installation

This plugin follows the standard runtime path structure, and as such it can
be installed with a variety of plugin managers:

To install using [Vundle](https://github.com/gmarik/vundle):

    # add this line to your .vimrc file
    Bundle 'exvim/ex-aftercolors'

To install using [Pathogen](https://github.com/tpope/vim-pathogen):

    cd ~/.vim/bundle
    git clone https://github.com/exvim/ex-aftercolors

To install using [NeoBundle](https://github.com/Shougo/neobundle.vim):

    # add this line to your .vimrc file
    NeoBundle 'exvim/ex-aftercolors'

[Download zip file](https://github.com/exvim/ex-aftercolors/archive/master.zip):

    cd ~/.vim
    unzip ex-aftercolors-master.zip
    copy all of the files into your ~/.vim directory

## Reference

* [AfterColors on vim.org](http://www.vim.org/scripts/script.php?script_id=1641)


text:
class UIBarButtonItem

  class << self

    def done(&action)
      sugarcube_barbuttonitem(:done, action)
    end

    def cancel(&action)
      sugarcube_barbuttonitem(:cancel, action)
    end

    def edit(&action)
      sugarcube_barbuttonitem(:edit, action)
    end

    def save(&action)
      sugarcube_barbuttonitem(:save, action)
    end

    def add(&action)
      sugarcube_barbuttonitem(:add, action)
    end

    def flexiblespace(&action)
      sugarcube_barbuttonitem(:flexiblespace, action)
    end

    def flexible_space(&action)
      sugarcube_barbuttonitem(:flexible_space, action)
    end

    def fixedspace(&action)
      sugarcube_barbuttonitem(:fixedspace, action)
    end

    def fixed_space(&action)
      sugarcube_barbuttonitem(:fixed_space, action)
    end

    def compose(&action)
      sugarcube_barbuttonitem(:compose, action)
    end

    def reply(&action)
      sugarcube_barbuttonitem(:reply, action)
    end

    def action(&action)
      sugarcube_barbuttonitem(:action, action)
    end

    def organize(&action)
      sugarcube_barbuttonitem(:organize, action)
    end

    def bookmarks(&action)
      sugarcube_barbuttonitem(:bookmarks, action)
    end

    def search(&action)
      sugarcube_barbuttonitem(:search, action)
    end

    def refresh(&action)
      sugarcube_barbuttonitem(:refresh, action)
    end

    def stop(&action)
      sugarcube_barbuttonitem(:stop, action)
    end

    def camera(&action)
      sugarcube_barbuttonitem(:camera, action)
    end

    def trash(&action)
      sugarcube_barbuttonitem(:trash, action)
    end

    def play(&action)
      sugarcube_barbuttonitem(:play, action)
    end

    def pause(&action)
      sugarcube_barbuttonitem(:pause, action)
    end

    def rewind(&action)
      sugarcube_barbuttonitem(:rewind, action)
    end

    def fastforward(&action)
      sugarcube_barbuttonitem(:fastforward, action)
    end

    def fast_forward(&action)
      sugarcube_barbuttonitem(:fast_forward, action)
    end

    def undo(&action)
      sugarcube_barbuttonitem(:undo, action)
    end

    def redo(&action)
      sugarcube_barbuttonitem(:redo, action)
    end

    def pagecurl(&action)
      sugarcube_barbuttonitem(:pagecurl, action)
    end

    def page_curl(&action)
      sugarcube_barbuttonitem(:page_curl, action)
    end

    def titled(title, style = :bordered, &action)
      sugarcube_barbuttonitem_with_title(title, style, action)
    end

    def imaged(image, style = :bordered, &action)
      sugarcube_barbuttonitem_with_image(image, style, action)
    end

  end

  # Adds the action and keeps a strong reference to the Proc object.
  def set_target_and_action target, action
    self.target = target
    self.action = 'sugarcube_handle_action:'
    @sugarcube_action = action
  end

private

  def self.sugarcube_barbuttonitem(systemitem, action)
    systemitem = systemitem.uibarbuttonitem if systemitem.respond_to?(:uibarbuttonitem)
    b = UIBarButtonItem.alloc.initWithBarButtonSystemItem(systemitem, target:nil, action:nil)
    b.set_target_and_action b, action
    b
  end

  def self.sugarcube_barbuttonitem_with_title(title, style, action)
    style = style.uibarbuttonstyle if style.respond_to?(:uibarbuttonstyle)
    b = UIBarButtonItem.alloc.initWithTitle(title, style:style, target:nil, action:nil)
    b.set_target_and_action b, action
    b
  end

  def self.sugarcube_barbuttonitem_with_image(image, style, action)
    style = style.uibarbuttonstyle if style.respond_to?(:uibarbuttonstyle)
    case image
    when Array
      b = UIBarButtonItem.alloc.initWithImage(image.first.uiimage, landscapeImagePhone:image.last.uiimage, style:style, target:nil, action:nil)
    else
      b = UIBarButtonItem.alloc.initWithImage(image.uiimage, style:style, target:nil, action:nil)
    end
    b.set_target_and_action b, action
    b
  end

  def sugarcube_handle_action(sender)
    handler = @sugarcube_action
    if handler.arity == 0
      handler.call
    else
      handler.call(sender)
    end
  end

end