Exemplo n.º 1
0
def GetTextSizeFromPainter(painter: QG.QPainter, text: str):

    try:

        text_size = painter.fontMetrics().size(QC.Qt.TextSingleLine, text)

    except ValueError:

        from hydrus.client.metadata import ClientTags

        if not ClientTags.have_shown_invalid_tag_warning:

            from hydrus.core import HydrusData

            HydrusData.ShowText(
                'Hey, I think hydrus stumbled across an invalid tag! Please run _database->check and repair->fix invalid tags_ immediately, or you may get errors!'
            )

            bad_text = repr(text)
            bad_text = HydrusText.ElideText(bad_text, 24)

            HydrusData.ShowText('The bad text was: {}'.format(bad_text))

            ClientTags.have_shown_invalid_tag_warning = True

        text = '*****INVALID, UNDISPLAYABLE TAG, RUN DATABASE REPAIR NOW*****'

        text_size = painter.fontMetrics().size(QC.Qt.TextSingleLine, text)

    return (text_size, text)
Exemplo n.º 2
0
def AppendMenuLabel(menu, label, description=''):

    original_label_text = label
    label = SanitiseLabel(label)

    if description is None:

        description = ''

    menu_item = QW.QAction(menu)

    if HC.PLATFORM_MACOS:

        menu_item.setMenuRole(QW.QAction.ApplicationSpecificRole)

    menu_item.setText(HydrusText.ElideText(label, 128, elide_center=True))

    menu_item.setStatusTip(description)
    menu_item.setToolTip(description)
    menu_item.setWhatsThis(description)

    menu.addAction(menu_item)

    BindMenuItem(menu_item, HG.client_controller.pub, 'clipboard', 'text',
                 original_label_text)

    return menu_item
Exemplo n.º 3
0
def AppendMenuCheckItem(menu, label, description, initial_value, callable,
                        *args, **kwargs):

    label = SanitiseLabel(label)

    menu_item = QW.QAction(menu)

    if HC.PLATFORM_MACOS:

        menu_item.setMenuRole(QW.QAction.ApplicationSpecificRole)

    menu_item.setText(HydrusText.ElideText(label, 128, elide_center=True))

    menu_item.setStatusTip(description)
    menu_item.setToolTip(description)
    menu_item.setWhatsThis(description)

    menu_item.setCheckable(True)
    menu_item.setChecked(initial_value)

    menu.addAction(menu_item)

    BindMenuItem(menu_item, callable, *args, **kwargs)

    return menu_item
    def _Paste(self):

        try:

            raw_text = HG.client_controller.GetClipboardText()

        except HydrusExceptions.DataMissing as e:

            QW.QMessageBox.critical(self, 'Error', str(e))

            return

        try:

            texts = [
                text for text in HydrusText.DeserialiseNewlinedTexts(raw_text)
            ]

            if not self._allow_empty_input:

                texts = [text for text in texts if text != '']

            if len(texts) > 0:

                self._add_callable(texts)

        except:

            QW.QMessageBox.critical(
                self, 'Error',
                'I could not understand what was in the clipboard')
Exemplo n.º 5
0
def AppendMenuItem(menu, label, description, callable, *args, **kwargs):

    label = SanitiseLabel(label)

    menu_item = QW.QAction(menu)

    if HC.PLATFORM_MACOS:

        menu_item.setMenuRole(QW.QAction.ApplicationSpecificRole)

    elided_label = HydrusText.ElideText(label, 128, elide_center=True)

    menu_item.setText(elided_label)

    if elided_label != label:

        menu_item.setToolTip(label)

    else:

        menu_item.setToolTip(description)

    menu_item.setStatusTip(description)
    menu_item.setWhatsThis(description)

    menu.addAction(menu_item)

    BindMenuItem(menu_item, callable, *args, **kwargs)

    return menu_item
Exemplo n.º 6
0
def GetExternalIP():

    if HydrusData.TimeHasPassed(EXTERNAL_IP['time'] + (3600 * 24)):

        cmd = [UPNPC_PATH, '-l']

        sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

        HydrusData.CheckProgramIsNotShuttingDown()

        try:

            p = subprocess.Popen(cmd,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 **sbp_kwargs)

        except FileNotFoundError:

            RaiseMissingUPnPcError('fetch external IP')

        HydrusData.WaitForProcessToFinish(p, 30)

        (stdout, stderr) = HydrusThreading.SubprocessCommunicate(p)

        if stderr is not None and len(stderr) > 0:

            raise Exception(
                'Problem while trying to fetch External IP (if it says No IGD UPnP Device, you are either on a VPN or your router does not seem to support UPnP):'
                + os.linesep * 2 + str(stderr))

        else:

            try:

                lines = HydrusText.DeserialiseNewlinedTexts(stdout)

                i = lines.index(
                    'i protocol exPort->inAddr:inPort description remoteHost leaseTime'
                )

                # ExternalIPAddress = ip
                (gumpf, external_ip_address) = lines[i - 1].split(' = ')

            except ValueError:

                raise Exception('Could not parse external IP!')

            if external_ip_address == '0.0.0.0':

                raise Exception(
                    'Your UPnP device returned your external IP as 0.0.0.0! Try rebooting it, or overwrite it in options!'
                )

            EXTERNAL_IP['ip'] = external_ip_address
            EXTERNAL_IP['time'] = HydrusData.GetNow()

    return EXTERNAL_IP['ip']
Exemplo n.º 7
0
 def _UpdateLabel( self ):
     
     label = self._string_converter.ToString()
     
     self.setToolTip( label )
     
     elided_label = HydrusText.ElideText( label, 64 )
     
     self.setText( elided_label )
Exemplo n.º 8
0
def ParseHashesFromRawHexText(hash_type, hex_hashes_raw):

    hash_type_to_hex_length = {
        'md5': 32,
        'sha1': 40,
        'sha256': 64,
        'sha512': 128
    }

    hex_hashes = HydrusText.DeserialiseNewlinedTexts(hex_hashes_raw)

    # convert md5:abcd to abcd
    hex_hashes = [hex_hash.split(':')[-1] for hex_hash in hex_hashes]

    hex_hashes = [HydrusText.HexFilter(hex_hash) for hex_hash in hex_hashes]

    expected_hex_length = hash_type_to_hex_length[hash_type]

    bad_hex_hashes = [
        hex_hash for hex_hash in hex_hashes
        if len(hex_hash) != expected_hex_length
    ]

    if len(bad_hex_hashes):

        m = 'Sorry, {} hashes should have {} hex characters! These did not:'.format(
            hash_type, expected_hex_length)
        m += os.linesep * 2
        m += os.linesep.join(
            ('{} ({} characters)'.format(bad_hex_hash, len(bad_hex_hash))
             for bad_hex_hash in bad_hex_hashes))

        raise Exception(m)

    hex_hashes = [
        hex_hash for hex_hash in hex_hashes if len(hex_hash) % 2 == 0
    ]

    hex_hashes = DedupeList(hex_hashes)

    hashes = tuple([bytes.fromhex(hex_hash) for hex_hash in hex_hashes])

    return hashes
Exemplo n.º 9
0
def IsAlreadyRunning( db_path, instance ):
    
    path = os.path.join( db_path, instance + '_running' )
    
    if os.path.exists( path ):
        
        with open( path, 'r', encoding = 'utf-8' ) as f:
            
            file_text = f.read()
            
            try:
                
                ( pid, create_time ) = HydrusText.DeserialiseNewlinedTexts( file_text )
                
                pid = int( pid )
                create_time = float( create_time )
                
            except ValueError:
                
                return False
                
            
            try:
                
                me = psutil.Process()
                
                if me.pid == pid and me.create_time() == create_time:
                    
                    # this is me! there is no conflict, lol!
                    # this happens when a linux process restarts with os.execl(), for instance (unlike Windows, it keeps its pid)
                    
                    return False
                    
                
                if psutil.pid_exists( pid ):
                    
                    p = psutil.Process( pid )
                    
                    if p.create_time() == create_time and p.is_running():
                        
                        return True
                        
                    
                
            except psutil.Error:
                
                return False
                
            
        
    
    return False
Exemplo n.º 10
0
    def EnterText(self):

        text = self._text_input.text()

        text = HydrusText.StripIOInputLine(text)

        if text == '' and not self._allow_empty_input:

            return

        self._add_callable((text, ))

        self._text_input.clear()
Exemplo n.º 11
0
def GetExternalIP():

    if HydrusData.TimeHasPassed(EXTERNAL_IP['time'] + (3600 * 24)):

        cmd = [upnpc_path, '-l']

        sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

        p = subprocess.Popen(cmd,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             **sbp_kwargs)

        HydrusData.WaitForProcessToFinish(p, 30)

        (stdout, stderr) = p.communicate()

        if stderr is not None and len(stderr) > 0:

            raise Exception('Problem while trying to fetch External IP:' +
                            os.linesep * 2 + str(stderr))

        else:

            try:

                lines = HydrusText.DeserialiseNewlinedTexts(stdout)

                i = lines.index(
                    'i protocol exPort->inAddr:inPort description remoteHost leaseTime'
                )

                # ExternalIPAddress = ip
                (gumpf, external_ip_address) = lines[i - 1].split(' = ')

            except ValueError:

                raise Exception('Could not parse external IP!')

            if external_ip_address == '0.0.0.0':

                raise Exception(
                    'Your UPnP device returned your external IP as 0.0.0.0! Try rebooting it, or overwrite it in options!'
                )

            EXTERNAL_IP['ip'] = external_ip_address
            EXTERNAL_IP['time'] = HydrusData.GetNow()

    return EXTERNAL_IP['ip']
Exemplo n.º 12
0
def GetSiblingProcessPorts( db_path, instance ):
    
    path = os.path.join( db_path, instance + '_running' )
    
    if os.path.exists( path ):
        
        with open( path, 'r', encoding = 'utf-8' ) as f:
            
            file_text = f.read()
            
            try:
                
                ( pid, create_time ) = HydrusText.DeserialiseNewlinedTexts( file_text )
                
                pid = int( pid )
                create_time = float( create_time )
                
            except ValueError:
                
                return None
                
            
            try:
                
                if psutil.pid_exists( pid ):
                    
                    ports = []
                    
                    p = psutil.Process( pid )
                    
                    for conn in p.connections():
                        
                        if conn.status == 'LISTEN':
                            
                            ports.append( int( conn.laddr[1] ) )
                            
                        
                    
                    return ports
                    
                
            except psutil.Error:
                
                return None
                
            
        
    
    return None
Exemplo n.º 13
0
 def _UpdateLabel( self ):
     
     statements = self._string_processor.GetProcessingStrings()
     
     if len( statements ) == 0:
         
         label = self._string_processor.ToString()
         
     else:
         
         statements = [ HydrusText.ElideText( statement, 64 ) for statement in statements ]
         
         label = os.linesep.join( statements )
         
     
     self.setText( label )
Exemplo n.º 14
0
def AppendMenuBitmapItem(menu, label, description, bitmap, callable, *args,
                         **kwargs):

    label = SanitiseLabel(label)

    menu_item = QW.QAction(menu)

    if HC.PLATFORM_MACOS:

        menu_item.setMenuRole(QW.QAction.ApplicationSpecificRole)

    menu_item.setText(HydrusText.ElideText(label, 64, elide_center=True))

    menu_item.setStatusTip(description)
    menu_item.setToolTip(description)
    menu_item.setWhatsThis(description)

    menu_item.setIcon(QG.QIcon(bitmap))

    menu.addAction(menu_item)

    BindMenuItem(menu_item, callable, *args, **kwargs)

    return menu_item
Exemplo n.º 15
0
def GetMime(path, ok_to_look_for_hydrus_updates=False):

    size = os.path.getsize(path)

    if size == 0:

        raise HydrusExceptions.FileSizeException('File is of zero length!')

    with open(path, 'rb') as f:

        bit_to_check = f.read(256)

    for (offset, header, mime) in header_and_mime:

        offset_bit_to_check = bit_to_check[offset:]

        if offset_bit_to_check.startswith(header):

            if mime == HC.UNDETERMINED_WM:

                if HydrusVideoHandling.HasVideoStream(path):

                    return HC.VIDEO_WMV

                # we'll catch and verify wma later

            elif mime == HC.UNDETERMINED_PNG:

                if HydrusVideoHandling.HasVideoStream(path):

                    return HC.IMAGE_APNG

                else:

                    return HC.IMAGE_PNG

            else:

                return mime

    try:

        mime = HydrusVideoHandling.GetMime(path)

        if mime != HC.APPLICATION_UNKNOWN:

            return mime

    except HydrusExceptions.UnsupportedFileException:

        pass

    except Exception as e:

        HydrusData.Print('FFMPEG had trouble with: ' + path)
        HydrusData.PrintException(e, do_wait=False)

    if HydrusText.LooksLikeHTML(bit_to_check):

        return HC.TEXT_HTML

    if ok_to_look_for_hydrus_updates:

        with open(path, 'rb') as f:

            update_network_bytes = f.read()

        try:

            update = HydrusSerialisable.CreateFromNetworkBytes(
                update_network_bytes)

            if isinstance(update, HydrusNetwork.ContentUpdate):

                return HC.APPLICATION_HYDRUS_UPDATE_CONTENT

            elif isinstance(update, HydrusNetwork.DefinitionsUpdate):

                return HC.APPLICATION_HYDRUS_UPDATE_DEFINITIONS

        except:

            pass

    return HC.APPLICATION_UNKNOWN
Exemplo n.º 16
0
def GetFFMPEGInfoLines( path, count_frames_manually = False, only_first_second = False ):
    
    # open the file in a pipe, provoke an error, read output
    
    cmd = [ FFMPEG_PATH, "-i", path ]
    
    if only_first_second:
        
        cmd.insert( 1, '-t' )
        cmd.insert( 2, '1' )
        
    
    if count_frames_manually:
        
        # added -an here to remove audio component, which was sometimes causing convert fails on single-frame music webms
        
        if HC.PLATFORM_WINDOWS:
            
            cmd += [ "-vf", "scale=-2:120", "-an", "-f", "null", "NUL" ]
            
        else:
            
            cmd += [ "-vf", "scale=-2:120", "-an", "-f", "null", "/dev/null" ]
            
        
    
    sbp_kwargs = HydrusData.GetSubprocessKWArgs()
    
    HydrusData.CheckProgramIsNotShuttingDown()
    
    try:
        
        process = subprocess.Popen( cmd, bufsize = 10**5, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, **sbp_kwargs )
        
    except FileNotFoundError as e:
        
        global FFMPEG_MISSING_ERROR_PUBBED
        
        if not FFMPEG_MISSING_ERROR_PUBBED:
            
            message = 'FFMPEG, which hydrus uses to parse and render video, was not found! This may be due to it not being available on your system, or hydrus being unable to find it.'
            message += os.linesep * 2
            
            if HC.PLATFORM_WINDOWS:
                
                message += 'You are on Windows, so there should be a copy of ffmpeg.exe in your install_dir/bin folder. If not, please check if your anti-virus has removed it and restore it through a new install.'
                
            else:
                
                message += 'If you are certain that FFMPEG is installed on your OS and accessible in your PATH, please let hydrus_dev know, as this problem is likely due to an environment problem. You may be able to solve this problem immediately by putting a static build of the ffmpeg executable in your install_dir/bin folder.'
                
            
            message += os.linesep * 2
            message += 'You can check your current FFMPEG status through help->about.'
            
            HydrusData.ShowText( message )
            
            FFMPEG_MISSING_ERROR_PUBBED = True
            
        
        raise FileNotFoundError( 'Cannot interact with video because FFMPEG not found--are you sure it is installed? Full error: ' + str( e ) )
        
    
    ( stdout, stderr ) = HydrusThreading.SubprocessCommunicate( process )
    
    data_bytes = stderr
    
    if len( data_bytes ) == 0:
        
        global FFMPEG_NO_CONTENT_ERROR_PUBBED
        
        if not FFMPEG_NO_CONTENT_ERROR_PUBBED:
            
            message = 'FFMPEG, which hydrus uses to parse and render video, did not return any data on a recent file metadata check! More debug info has been written to the log.'
            message += os.linesep * 2
            message += 'You can check this info again through help->about.'
            
            HydrusData.ShowText( message )
            
            message += os.linesep * 2
            message += str( sbp_kwargs )
            message += os.linesep * 2
            message += str( os.environ )
            message += os.linesep * 2
            message += 'STDOUT Response: {}'.format( stdout )
            message += os.linesep * 2
            message += 'STDERR Response: {}'.format( stderr )
            
            HydrusData.DebugPrint( message )
            
            FFMPEG_NO_CONTENT_ERROR_PUBBED = True
            
        
        raise HydrusExceptions.DataMissing( 'Cannot interact with video because FFMPEG did not return any content.' )
        
    
    del process
    
    ( text, encoding ) = HydrusText.NonFailingUnicodeDecode( data_bytes, 'utf-8' )
    
    lines = text.splitlines()
    
    CheckFFMPEGError( lines )
    
    return lines
Exemplo n.º 17
0
    def OnData(self, mime_data, result):

        media_dnd = isinstance(mime_data, QMimeDataHydrusFiles)
        urls_dnd = mime_data.hasUrls()
        text_dnd = mime_data.hasText()

        if media_dnd and self._media_callable is not None:

            result = mime_data.hydrusFiles()

            if result is not None:

                (page_key, hashes) = result

                if page_key is not None:

                    QP.CallAfter(
                        self._media_callable, page_key,
                        hashes)  # callafter so we can terminate dnd event now

            result = QC.Qt.MoveAction

            # old way of doing it that messed up discord et al
            '''
        elif mime_data.formats().count( 'application/hydrus-media' ) and self._media_callable is not None:
            
            mview = mime_data.data( 'application/hydrus-media' )

            data_bytes = mview.data()

            data_str = str( data_bytes, 'utf-8' )

            (encoded_page_key, encoded_hashes) = json.loads( data_str )

            if encoded_page_key is not None:
                
                page_key = bytes.fromhex( encoded_page_key )
                hashes = [ bytes.fromhex( encoded_hash ) for encoded_hash in encoded_hashes ]

                QP.CallAfter( self._media_callable, page_key, hashes )  # callafter so we can terminate dnd event now
                

            result = QC.Qt.MoveAction
            '''
        elif urls_dnd or text_dnd:

            paths = []
            urls = []

            if urls_dnd:

                dnd_items = mime_data.urls()

                for dnd_item in dnd_items:

                    if dnd_item.isLocalFile():

                        paths.append(os.path.normpath(dnd_item.toLocalFile()))

                    else:

                        urls.append(dnd_item.url())

            else:

                text = mime_data.text()

                text_lines = HydrusText.DeserialiseNewlinedTexts(text)

                for text_line in text_lines:

                    if text_line.startswith('http'):

                        urls.append(text_line)

                        # ignore 'paths'

            if self._filenames_callable is not None:

                if len(paths) > 0:

                    QP.CallAfter(self._filenames_callable,
                                 paths)  # callafter to terminate dnd event now

            if self._url_callable is not None:

                if len(urls) > 0:

                    for url in urls:

                        QP.CallAfter(
                            self._url_callable,
                            url)  # callafter to terminate dnd event now

            result = QC.Qt.IgnoreAction

        else:

            result = QC.Qt.IgnoreAction

        return result
Exemplo n.º 18
0
def GetUPnPMappings():

    cmd = [upnpc_path, '-l']

    sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

    HydrusData.CheckProgramIsNotShuttingDown()

    p = subprocess.Popen(cmd,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         **sbp_kwargs)

    HydrusData.WaitForProcessToFinish(p, 30)

    (stdout, stderr) = HydrusThreading.SubprocessCommunicate(p)

    if stderr is not None and len(stderr) > 0:

        raise Exception('Problem while trying to fetch UPnP mappings:' +
                        os.linesep * 2 + stderr)

    else:

        try:

            lines = HydrusText.DeserialiseNewlinedTexts(stdout)

            i = lines.index(
                'i protocol exPort->inAddr:inPort description remoteHost leaseTime'
            )

            data_lines = []

            i += 1

            while i < len(lines):

                if not lines[i][0] in (' ', '0', '1', '2', '3', '4', '5', '6',
                                       '7', '8', '9'):
                    break

                data_lines.append(lines[i])

                i += 1

            processed_data = []

            for line in data_lines:

                # 0 UDP 65533->192.168.0.197:65533 'Skype UDP at 192.168.0.197:65533 (2665)' '' 0

                while '  ' in line:

                    line = line.replace('  ', ' ')

                if line.startswith(' '):

                    (empty, number, protocol, mapping_data,
                     rest_of_line) = line.split(' ', 4)

                else:

                    (number, protocol, mapping_data,
                     rest_of_line) = line.split(' ', 3)

                (external_port,
                 rest_of_mapping_data) = mapping_data.split('->')

                external_port = int(external_port)

                if rest_of_mapping_data.count(':') == 1:

                    (internal_client,
                     internal_port) = rest_of_mapping_data.split(':')

                else:

                    parts = rest_of_mapping_data.split(':')

                    internal_port = parts.pop(-1)

                    internal_client = ':'.join(parts)

                internal_port = int(internal_port)

                (empty, description, space, remote_host,
                 rest_of_line) = rest_of_line.split('\'', 4)

                lease_time = int(rest_of_line[1:])

                processed_data.append(
                    (description, internal_client, internal_port,
                     external_port, protocol, lease_time))

            return processed_data

        except Exception as e:

            HydrusData.Print('UPnP problem:')
            HydrusData.Print(traceback.format_exc())
            HydrusData.Print('Full response follows:')
            HydrusData.Print(stdout)

            raise Exception('Problem while trying to parse UPnP mappings:' +
                            os.linesep * 2 + str(e))
Exemplo n.º 19
0
def GetUPnPMappingsParseResponse(stdout):

    try:

        lines = HydrusText.DeserialiseNewlinedTexts(stdout)

        i = lines.index(
            'i protocol exPort->inAddr:inPort description remoteHost leaseTime'
        )

        data_lines = []

        i += 1

        while i < len(lines):

            if not lines[i][0] in (' ', '0', '1', '2', '3', '4', '5', '6', '7',
                                   '8', '9'):
                break

            data_lines.append(lines[i])

            i += 1

        processed_data = []

        for line in data_lines:

            # 0 UDP 65533->192.168.0.197:65533 'Skype UDP at 192.168.0.197:65533 (2665)' '' 0

            while '  ' in line:

                line = line.replace('  ', ' ')

            if line.startswith(' '):

                (empty, number, protocol, mapping_data,
                 rest_of_line) = line.split(' ', 4)

            else:

                (number, protocol, mapping_data,
                 rest_of_line) = line.split(' ', 3)

            (external_port, rest_of_mapping_data) = mapping_data.split('->')

            external_port = int(external_port)

            if rest_of_mapping_data.count(':') == 1:

                (internal_client,
                 internal_port) = rest_of_mapping_data.split(':')

            else:

                parts = rest_of_mapping_data.split(':')

                internal_port = parts.pop(-1)

                internal_client = ':'.join(parts)

            internal_port = int(internal_port)

            (empty, description, space, remote_host,
             rest_of_line) = rest_of_line.split('\'', 4)

            lease_time = int(rest_of_line[1:])

            processed_data.append((description, internal_client, internal_port,
                                   external_port, protocol, lease_time))

        return processed_data

    except Exception as e:

        HydrusData.Print('UPnP problem:')
        HydrusData.Print(traceback.format_exc())
        HydrusData.Print('Full response follows:')
        HydrusData.Print(stdout)

        raise Exception('Problem while trying to parse UPnP mappings:' +
                        os.linesep * 2 + str(e))
Exemplo n.º 20
0
    def OnData( self, mime_data, result ):
        
        media_dnd = isinstance( mime_data, QMimeDataHydrusFiles )
        urls_dnd = mime_data.hasUrls()
        text_dnd = mime_data.hasText()
        
        if media_dnd and self._media_callable is not None:
            
            result = mime_data.hydrusFiles()
            
            if result is not None:
                
                ( page_key, hashes ) = result
                
                if page_key is not None:
                    
                    QP.CallAfter( self._media_callable, page_key, hashes )  # callafter so we can terminate dnd event now
                    
                
            
            result = QC.Qt.MoveAction
            
            # old way of doing it that messed up discord et al
            '''
        elif mime_data.formats().count( 'application/hydrus-media' ) and self._media_callable is not None:
            
            mview = mime_data.data( 'application/hydrus-media' )

            data_bytes = mview.data()

            data_str = str( data_bytes, 'utf-8' )

            (encoded_page_key, encoded_hashes) = json.loads( data_str )

            if encoded_page_key is not None:
                
                page_key = bytes.fromhex( encoded_page_key )
                hashes = [ bytes.fromhex( encoded_hash ) for encoded_hash in encoded_hashes ]

                QP.CallAfter( self._media_callable, page_key, hashes )  # callafter so we can terminate dnd event now
                

            result = QC.Qt.MoveAction
            '''
        elif urls_dnd or text_dnd:
            
            paths = []
            urls = []
            
            if urls_dnd:
                
                dnd_items = mime_data.urls()
                
                for dnd_item in dnd_items:
                    
                    if dnd_item.isLocalFile():
                        
                        paths.append( os.path.normpath( dnd_item.toLocalFile() ) )
                        
                    else:
                        
                        urls.append( dnd_item.url() )
                        
                    
                
            else:
                
                text = mime_data.text()
                
                text_lines = HydrusText.DeserialiseNewlinedTexts( text )
                
                for text_line in text_lines:
                    
                    if text_line.startswith( 'http' ):
                        
                        urls.append( text_line )
                        
                        # ignore 'paths'
                        
                    
                
            
            if self._filenames_callable is not None:
                
                if len( paths ) > 0:
                    
                    QP.CallAfter( self._filenames_callable, paths ) # callafter to terminate dnd event now
                    
                
            
            if self._url_callable is not None:
                
                if len( urls ) > 0:
                    
                    for url in urls:
                        
                        # https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
                        # data:image/png;base64,(data)
                        # so what I prob have to do here is parse the file, decode from base64 or whatever, and then write to a fresh temp location and call self._filenames_callable
                        # but I need to figure out a way to reproduce this on my own. Chrome is supposed to do it on image DnD, but didn't for me
                        if url.startswith( 'data:' ) or len( url ) > 8 * 1024:
                            
                            HydrusData.ShowText( 'This drag and drop was in the unsupported \'Data URL\' format. hydev would like to know more about this so he can fix it.' )
                            
                            continue
                            
                        
                        QP.CallAfter( self._url_callable, url ) # callafter to terminate dnd event now
                        
                    
                
            
            result = QC.Qt.IgnoreAction
            
        else:
            
            result = QC.Qt.IgnoreAction
            
        
        return result
Exemplo n.º 21
0
def GetMime(path, ok_to_look_for_hydrus_updates=False):

    size = os.path.getsize(path)

    if size == 0:

        raise HydrusExceptions.ZeroSizeFileException('File is of zero length!')

    if ok_to_look_for_hydrus_updates and size < 64 * 1024 * 1024:

        with open(path, 'rb') as f:

            update_network_bytes = f.read()

        try:

            update = HydrusSerialisable.CreateFromNetworkBytes(
                update_network_bytes)

            if isinstance(update, HydrusNetwork.ContentUpdate):

                return HC.APPLICATION_HYDRUS_UPDATE_CONTENT

            elif isinstance(update, HydrusNetwork.DefinitionsUpdate):

                return HC.APPLICATION_HYDRUS_UPDATE_DEFINITIONS

        except:

            pass

    with open(path, 'rb') as f:

        bit_to_check = f.read(256)

    for (offsets_and_headers, mime) in headers_and_mime:

        it_passes = False not in (bit_to_check[offset:].startswith(header)
                                  for (offset, header) in offsets_and_headers)

        if it_passes:

            if mime in (HC.UNDETERMINED_WM, HC.UNDETERMINED_MP4):

                return HydrusVideoHandling.GetMime(path)

            elif mime == HC.UNDETERMINED_PNG:

                if IsPNGAnimated(bit_to_check):

                    return HC.IMAGE_APNG

                else:

                    return HC.IMAGE_PNG

            else:

                return mime

    if HydrusText.LooksLikeHTML(bit_to_check):

        return HC.TEXT_HTML

    # it is important this goes at the end, because ffmpeg has a billion false positives!
    # for instance, it once thought some hydrus update files were mpegs
    try:

        mime = HydrusVideoHandling.GetMime(path)

        if mime != HC.APPLICATION_UNKNOWN:

            return mime

    except HydrusExceptions.UnsupportedFileException:

        pass

    except Exception as e:

        HydrusData.Print('FFMPEG had trouble with: ' + path)
        HydrusData.PrintException(e, do_wait=False)

    return HC.APPLICATION_UNKNOWN
Exemplo n.º 22
0
    def GetTags(self, service_key, path):

        tags = set()

        tags.update(self._tags_for_all)

        if self._load_from_neighbouring_txt_files:

            txt_path = path + '.txt'

            if os.path.exists(txt_path):

                try:

                    with open(txt_path, 'r', encoding='utf-8') as f:

                        txt_tags_string = f.read()

                except:

                    HydrusData.ShowText('Could not parse the tags from ' +
                                        txt_path + '!')

                    tags.add(
                        '___had problem reading .txt file--is it not in utf-8?'
                    )

                try:

                    txt_tags = [
                        tag for tag in HydrusText.DeserialiseNewlinedTexts(
                            txt_tags_string)
                    ]

                    if True in (len(txt_tag) > 1024 for txt_tag in txt_tags):

                        HydrusData.ShowText(
                            'Tags were too long--I think this was not a regular text file!'
                        )

                        raise Exception()

                    tags.update(txt_tags)

                except:

                    HydrusData.ShowText('Could not parse the tags from ' +
                                        txt_path + '!')

                    tags.add('___had problem parsing .txt file')

        (base, filename) = os.path.split(path)

        (filename, any_ext_gumpf) = os.path.splitext(filename)

        (filename_boolean, filename_namespace) = self._add_filename

        if filename_boolean:

            if filename_namespace != '':

                tag = filename_namespace + ':' + filename

            else:

                tag = filename

            tags.add(tag)

        (drive, directories) = os.path.splitdrive(base)

        while directories.startswith(os.path.sep):

            directories = directories[1:]

        directories = directories.split(os.path.sep)

        for (index, (dir_boolean,
                     dir_namespace)) in list(self._directories_dict.items()):

            # we are talking -3 through 2 here

            if not dir_boolean:

                continue

            try:

                directory = directories[index]

            except IndexError:

                continue

            if dir_namespace != '':

                tag = dir_namespace + ':' + directory

            else:

                tag = directory

            tags.add(tag)

        #

        for regex in self._regexes:

            try:

                result = re.findall(regex, path)

                for match in result:

                    if isinstance(match, tuple):

                        for submatch in match:

                            tags.add(submatch)

                    else:

                        tags.add(match)

            except:

                pass

        for (namespace, regex) in self._quick_namespaces:

            try:

                result = re.findall(regex, path)

                for match in result:

                    if isinstance(match, tuple):

                        for submatch in match:

                            tags.add(namespace + ':' + submatch)

                    else:

                        tags.add(namespace + ':' + match)

            except:

                pass

        #

        tags = HydrusTags.CleanTags(tags)

        tags = HG.client_controller.tag_display_manager.FilterTags(
            ClientTags.TAG_DISPLAY_STORAGE, service_key, tags)

        return tags
Exemplo n.º 23
0
    def _GetURLsFromURLsString(self, urls_string):

        urls = HydrusText.DeserialiseNewlinedTexts(urls_string)

        return urls
    def _GetSourcesFromSourcesString(self, sources_string):

        sources = HydrusText.DeserialiseNewlinedTexts(sources_string)

        return sources