import os
import re

from PyQt4 import QtCore, QtGui
Qt = QtCore.Qt

from maya import cmds, mel

from sgfs.ui import product_select
import sgfs.ui.scene_name.widget as scene_name


class CameraSelector(product_select.Layout):
    
    def _setup_sections(self):
        super(CameraSelector, self)._setup_sections()
        self.register_section('Camera', self._iter_cameras)
    
    def _iter_cameras(self, step_path):
        if step_path is None:
            return
        camera_dir = os.path.join(step_path, 'maya', 'scenes', 'camera')
        if os.path.exists(camera_dir):
            for name in os.listdir(camera_dir):
                
                if name.startswith('.'):
                    continue
                if not name.endswith('.ma'):
                    continue
                if re.search(r'\.20\d{2}\.ma$', name):
                    continue
                
                m = re.search(r'v(\d+)(?:_r(\d+))?', name)
                if m:
                    priority = tuple(int(x or 0) for x in m.groups())
                else:
                    priority = (0, 0)
                cam_path = os.path.join(camera_dir, name)
                try:
                    ref_node = cmds.referenceQuery(cam_path, referenceNode=True)
                except RuntimeError:
                    pass
                else:
                    name += ' (already referenced)'
                    priority = (-1, 0)
                
                yield name, cam_path, priority


class Dialog(QtGui.QDialog):
    
    def __init__(self):
        super(Dialog, self).__init__()
        self._setup_ui()
    
    def _setup_ui(self):
        
        self.setWindowTitle("Camera Import")
        self.setLayout(QtGui.QVBoxLayout())
        
        self._selector = CameraSelector(parent=self)
        self.layout().addLayout(self._selector)
        
        button = QtGui.QPushButton("Reference")
        button.clicked.connect(self._on_reference)
        self.layout().addWidget(button)
    
    def _on_reference(self, *args):
        path = self._selector.path()
        if path:
            try:
                ref_node = cmds.referenceQuery(path, referenceNode=True)
            except RuntimeError:
                pass
            else:
                cmds.warning('Already referenced')
                return
            cmds.file(path, reference=True)
        self.close()



def __before_reload__():
    if dialog:
        dialog.close()

dialog = None

def run():
    
    global dialog
    
    if dialog:
        dialog.close()
    
    dialog = Dialog()    
    dialog.show()

@@ -176,203 +176,1003 @@
 ame%0A
-%0A%0Aclass CameraSelector(product_select.Layout):%0A    %0A    def _setup_sections(self):%0A        super(CameraSelector, self)._setup_sections()%0A        self.register_section('Camera', self._iter_cameras
+from sgpublish.importer.generic import Importer%0Afrom sgpublish.importer.ui.dialog import ImportDialog%0Afrom sgpublish.importer.ui.workarea import WorkAreaImporter%0Afrom sgpublish.importer.ui.publish import PublishImporter%0A%0A%0Aclass CameraImporter(Importer):%0A%0A    def import_(self, path):%0A        try:%0A            ref_node = cmds.referenceQuery(path, referenceNode=True)%0A        except RuntimeError:%0A            pass%0A        else:%0A            cmds.warning('Already referenced')%0A            return%0A        cmds.file(path, reference=True)%0A%0A%0Aclass Dialog(ImportDialog):%0A    %0A    importer_class = CameraImporter%0A%0A    def __init__(self, **kwargs):%0A        super(Dialog, self).__init__(**kwargs)%0A        %0A        workarea = WorkAreaImporter(self.importer)%0A        workarea.picker.register_section('Camera', self._iter_cameras)%0A        self.tabs.addTab(workarea, %22From Work Area%22)%0A%0A        publish = PublishImporter(self.importer, publish_type='maya_camera')%0A        self.tabs.addTab(publish, %22From Publish%22
 )%0A  
@@ -1432,32 +1432,61 @@
                %0A
+                # Tempfiles.%0A
                 
@@ -1530,32 +1530,86 @@
         continue
+%0A%0A                # WesternX deals with ascii cameras.
 %0A               
@@ -1658,32 +1658,76 @@
         continue
+%0A%0A                # Skip the versioned ones.
 %0A               
@@ -2019,24 +2019,25 @@
 ty = (0, 0)%0A
+%0A
             
@@ -2074,32 +2074,33 @@
 mera_dir, name)%0A
+%0A
                 
@@ -2359,16 +2359,41 @@
  (-1, 0)
+ # Drop it to the bottom.
 %0A       
@@ -2455,913 +2455,8 @@
 y%0A%0A%0A
-class Dialog(QtGui.QDialog):%0A    %0A    def __init__(self):%0A        super(Dialog, self).__init__()%0A        self._setup_ui()%0A    %0A    def _setup_ui(self):%0A        %0A        self.setWindowTitle(%22Camera Import%22)%0A        self.setLayout(QtGui.QVBoxLayout())%0A        %0A        self._selector = CameraSelector(parent=self)%0A        self.layout().addLayout(self._selector)%0A        %0A        button = QtGui.QPushButton(%22Reference%22)%0A        button.clicked.connect(self._on_reference)%0A        self.layout().addWidget(button)%0A    %0A    def _on_reference(self, *args):%0A        path = self._selector.path()%0A        if path:%0A            try:%0A                ref_node = cmds.referenceQuery(path, referenceNode=True)%0A            except RuntimeError:%0A                pass%0A            else:%0A                cmds.warning('Already referenced')%0A                return%0A            cmds.file(path, reference=True)%0A        self.close()%0A%0A
 %0A%0Ade

Move camcache importer to new UI