# Path: dpa/app/entity.py
# class EntityRegistry(Singleton):
# 
#     # -------------------------------------------------------------------------
#     def init(self):
#         self._registry = defaultdict(list)
# 
#     # -------------------------------------------------------------------------
#     def get_entity_classes(self, app_name):
# 
#         # XXX should be filtered based on current ptask. 
#         # for example, anim stages may want to expose different entity types
#         # than modeling or lighting
# 
#         return self._registry.get(app_name, [])
# 
#     # -------------------------------------------------------------------------
#     def register(self, app_name, cls):
#         self._registry[app_name].append(cls)
# 
# class EntityError(Exception):
#     pass
# 
# Path: dpa/maya/entity/base.py
# class SetBasedWorkfileEntity(SetBasedEntity):
# 
#     # -------------------------------------------------------------------------
#     @classmethod
#     def import_product_representation(cls, session, representation, *args,
#         **kwargs):
# 
#         product = representation.product_version.product
# 
#         if representation.type != "ma":
#             raise EntityError(
#                 "Don't know how to import {cat} of type {typ}".format(
#                     cat=cls.category, type=representation.type)
#             )
# 
#         repr_path = cls.get_import_file(session, product.name, 
#             product.category, representation)
# 
#         name = product.name
#         instances = kwargs.get('instances', 1)
#         instance_start = kwargs.get('instance_start', 0)
#         exportable = kwargs.get('exportable', True)
#         group_reference = kwargs.get('group_reference', False)
# 
#         entities_to_create = []        
# 
#         if instances == 1 and instance_start == 0:
#             session.cmds.file(
#                 repr_path,
#                 reference=True,
#                 groupReference=group_reference,
#                 groupName=name,
#                 mergeNamespacesOnClash=True, 
#                 namespace=":"
#             )
#             if exportable:
#                 if group_reference:
#                     entities_to_create.append((name, name, None))
#                 else:
#                     ref_node = session.cmds.file(
#                         repr_path, referenceNode=True, query=True)
#                     if not ref_node:
#                         Logger.get().warning(
#                             "No reference node found for " + repr_path)
#                     else:
#                         entities_to_create.append(
#                             (cls._get_top_level_ref_objs(session, ref_node),
#                                 name, None))
#         else:
#             for inst in range(instance_start, instance_start + instances):
#                 inst_name = name + "_" + str(inst)
#                 session.cmds.file(
#                     repr_path,
#                     reference=True,
#                     groupReference=group_reference,
#                     groupName=inst_name,
#                     mergeNamespacesOnClash=True, 
#                     namespace=":"
#                 )
#                 if exportable:
#                     if group_reference:
#                         entities_to_create.append((inst_name, name, inst))
#                     else:
#                         ref_node = session.cmds.file(
#                             repr_path, referenceNode=True, query=True)
#                         if not ref_node:
#                             Logger.get().warning(
#                                 "No reference node found for " + repr_path)
#                         else:
#                             entities_to_create.append(
#                                 (cls._get_top_level_ref_objs(session, ref_node), 
#                                     name, inst))
# 
#         entities = []
#         
#         if exportable:
#             for (obj_name, name, inst) in entities_to_create:
#                 set_name = cls.get_set_name(name, inst)
#                 session.cmds.sets(obj_name, name=set_name)
# 
#                 entities.append(cls.get(name, session, instance=inst))
#             
#         return entities
# 
#     # -------------------------------------------------------------------------
#     @classmethod
#     def _get_top_level_ref_objs(cls, session, ref_node):
# 
#         ref_objs = session.cmds.referenceQuery(ref_node, nodes=True)
#         top_level_objs = session.cmds.ls(references=True, assemblies=True)
#         top_level_ref_objs= []
#         for top_level_obj in top_level_objs:
#             if top_level_obj in ref_objs:
#                 top_level_ref_objs.append(top_level_obj)
#         
#         return top_level_ref_objs


import os
import re
from dpa.app.entity import EntityRegistry, EntityError
from dpa.maya.entity.base import SetBasedWorkfileEntity

        product_repr_dir = product_repr.directory

        product_repr_file = os.path.join(
            product_repr_dir, self.display_name + "." + file_type)

        if self.display_name == self.__class__._name_from_context(self.session):
            self.session.cmds.file(
                product_repr_file, 
                type='mayaAscii', 
                exportAll=True, 
                force=True, 
                preserveReferences=(not bake_references),
            )
        else:
            export_objs = self.get_export_objects()
            with self.session.selected(export_objs):
                self.session.cmds.file(
                    product_repr_file, 
                    type='mayaAscii', 
                    exportSelected=True,
                    force=True, 
                    preserveReferences=(not bake_references),
                )

        product_repr.area.set_permissions(0660)

        return [product_repr]

# -----------------------------------------------------------------------------

# Path: dpa/app/entity.py
# class EntityRegistry(Singleton):
# 
#     # -------------------------------------------------------------------------
#     def init(self):
#         self._registry = defaultdict(list)
# 
#     # -------------------------------------------------------------------------
#     def get_entity_classes(self, app_name):
# 
#         # XXX should be filtered based on current ptask. 
#         # for example, anim stages may want to expose different entity types
#         # than modeling or lighting
# 
#         return self._registry.get(app_name, [])
# 
#     # -------------------------------------------------------------------------
#     def register(self, app_name, cls):
#         self._registry[app_name].append(cls)
# 
# class EntityError(Exception):
#     pass
# 
# Path: dpa/maya/entity/base.py
# class SetBasedWorkfileEntity(SetBasedEntity):
# 
#     # -------------------------------------------------------------------------
#     @classmethod
#     def import_product_representation(cls, session, representation, *args,
#         **kwargs):
# 
#         product = representation.product_version.product
# 
#         if representation.type != "ma":
#             raise EntityError(
#                 "Don't know how to import {cat} of type {typ}".format(
#                     cat=cls.category, type=representation.type)
#             )
# 
#         repr_path = cls.get_import_file(session, product.name, 
#             product.category, representation)
# 
#         name = product.name
#         instances = kwargs.get('instances', 1)
#         instance_start = kwargs.get('instance_start', 0)
#         exportable = kwargs.get('exportable', True)
#         group_reference = kwargs.get('group_reference', False)
# 
#         entities_to_create = []        
# 
#         if instances == 1 and instance_start == 0:
#             session.cmds.file(
#                 repr_path,
#                 reference=True,
#                 groupReference=group_reference,
#                 groupName=name,
#                 mergeNamespacesOnClash=True, 
#                 namespace=":"
#             )
#             if exportable:
#                 if group_reference:
#                     entities_to_create.append((name, name, None))
#                 else:
#                     ref_node = session.cmds.file(
#                         repr_path, referenceNode=True, query=True)
#                     if not ref_node:
#                         Logger.get().warning(
#                             "No reference node found for " + repr_path)
#                     else:
#                         entities_to_create.append(
#                             (cls._get_top_level_ref_objs(session, ref_node),
#                                 name, None))
#         else:
#             for inst in range(instance_start, instance_start + instances):
#                 inst_name = name + "_" + str(inst)
#                 session.cmds.file(
#                     repr_path,
#                     reference=True,
#                     groupReference=group_reference,
#                     groupName=inst_name,
#                     mergeNamespacesOnClash=True, 
#                     namespace=":"
#                 )
#                 if exportable:
#                     if group_reference:
#                         entities_to_create.append((inst_name, name, inst))
#                     else:
#                         ref_node = session.cmds.file(
#                             repr_path, referenceNode=True, query=True)
#                         if not ref_node:
#                             Logger.get().warning(
#                                 "No reference node found for " + repr_path)
#                         else:
#                             entities_to_create.append(
#                                 (cls._get_top_level_ref_objs(session, ref_node), 
#                                     name, inst))
# 
#         entities = []
#         
#         if exportable:
#             for (obj_name, name, inst) in entities_to_create:
#                 set_name = cls.get_set_name(name, inst)
#                 session.cmds.sets(obj_name, name=set_name)
# 
#                 entities.append(cls.get(name, session, instance=inst))
#             
#         return entities
# 
#     # -------------------------------------------------------------------------
#     @classmethod
#     def _get_top_level_ref_objs(cls, session, ref_node):
# 
#         ref_objs = session.cmds.referenceQuery(ref_node, nodes=True)
#         top_level_objs = session.cmds.ls(references=True, assemblies=True)
#         top_level_ref_objs= []
#         for top_level_obj in top_level_objs:
#             if top_level_obj in ref_objs:
#                 top_level_ref_objs.append(top_level_obj)
#         
#         return top_level_ref_objs


# Path: dpa/maya/entity/workfile.py
import os
import re
from dpa.app.entity import EntityRegistry, EntityError
from dpa.maya.entity.base import SetBasedWorkfileEntity


        product_repr_dir = product_repr.directory

        product_repr_file = os.path.join(
            product_repr_dir, self.display_name + "." + file_type)

        if self.display_name == self.__class__._name_from_context(self.session):
            self.session.cmds.file(
                product_repr_file, 
                type='mayaAscii', 
                exportAll=True, 
                force=True, 
                preserveReferences=(not bake_references),
            )
        else:
            export_objs = self.get_export_objects()
            with self.session.selected(export_objs):
                self.session.cmds.file(
                    product_repr_file, 
                    type='mayaAscii', 
                    exportSelected=True,
                    force=True, 
                    preserveReferences=(not bake_references),
                )

        product_repr.area.set_permissions(0660)

        return [product_repr]

# -----------------------------------------------------------------------------

EntityRegistry().register('maya', WorkfileEntity)