def sequence_id_from_path(path): """Return the `sequence_id` from the given path or uri. >>> from panoptes.utils import sequence_id_from_path >>> path = 'gs://panoptes-raw-images/PAN012/ee04d1/20190820T111638/20190820T122447.fits' >>> sequence_id_from_path(path) 'PAN012_ee04d1_20190820T111638' >>> path = 'nothing/to/match' >>> sequence_id_from_path(path) Args: path (str): A path or uri for a file. Returns: str: The image id in the form "<unit_id>_<camera_id>_<sequence_id>" """ result = PATH_MATCHER.match(unquote_url(path)) with contextlib.suppress(AttributeError): match = '{}_{}_{}'.format( result.group('unit_id'), result.group('camera_id'), result.group('sequence_id') ) return match return None
def information_as_link(self): ''' Formats the .information into an IRC or HTTP(S) <a> if possible, otherwise escapes it. ''' irc_match = re.match(r'^#([a-zA-Z0-9-_]+)@([a-zA-Z0-9-_.:]+)$', self.information) if irc_match: # Return a formatted IRC uri return '<a href="irc://{1}/{0}">#{0}@{1}</a>'.format(*irc_match.groups()) url_match = re.match(r'^(https?:\/\/.+?)$', self.information) if url_match: url = url_match.group(1) invalid_url_characters = '<>"' # Check if url contains invalid characters if not any(c in url for c in invalid_url_characters): return '<a href="{0}">{1}</a>'.format(url, escape_markup(unquote_url(url))) # Escaped return escape_markup(self.information)
def organize_single_entry(self, entry): """We're needed because RhythmDB doesn't support the iteration protocol.""" s = _EscapedRDBEntry(self.rdb, entry) uri = entry.get_playback_uri() if not uri or not uri.startswith("file://"): return src = unquote_url( uri.partition("://")[2] ) dst = os.path.join(rb.music_dir(), self.new_path.format(s)) try: if src != dst: print("%s -> %s" % (src, dst)) super_rename(src, dst) except OSError as err: print( str(err) )