text:
from __future__ import unicode_literals

import getpass
import itertools
import threading
import time
import wave
from subprocess import Popen, PIPE

import spotify

from spotify_ripper.color_lines import *
from spotify_ripper.eventloop import EventLoop
from spotify_ripper.post_actions import PostActions
from spotify_ripper.progress import Progress
from spotify_ripper.sync import Sync
from spotify_ripper.tags import set_metadata_tags
from spotify_ripper.utils import *
from spotify_ripper.web import WebAPI

try:
    # Python 3
    import queue
except ImportError:
    # Python 2
    import Queue as queue

class BitRate(spotify.utils.IntEnum):
    BITRATE_160K = 0
    BITRATE_320K = 1
    BITRATE_96K = 2

class Ripper(threading.Thread):
    name = 'SpotifyRipperThread'

    audio_file = None
    pcm_file = None
    wav_file = None
    rip_proc = None
    pipe = None
    current_playlist = None
    current_album = None
    current_chart = None

    login_success = False
    progress = None
    sync = None
    post = None
    web = None
    dev_null = None
    stop_time = None
    track_path_cache = {}

    rip_queue = queue.Queue()

    # threading events
    logged_in = threading.Event()
    logged_out = threading.Event()
    ripper_continue = threading.Event()
    ripping = threading.Event()
    end_of_track = threading.Event()
    finished = threading.Event()
    abort = threading.Event()
    skip = threading.Event()
    play_token_resume = threading.Event()

    def __init__(self, args):
        threading.Thread.__init__(self)

        # initialize progress meter
        self.progress = Progress(args, self)

        self.args = args

        # initially logged-out
        self.logged_out.set()

        config = spotify.Config()
        default_dir = default_settings_dir()

        self.post = PostActions(args, self)
        self.web = WebAPI(args, self)

        proxy = os.environ.get('http_proxy')
        if proxy is not None:
            config.proxy = proxy

        # application key location
        if args.key is not None:
            config.load_application_key_file(args.key[0])
        else:
            if not path_exists(default_dir):
                os.makedirs(enc_str(default_dir))

            app_key_path = os.path.join(default_dir, "spotify_appkey.key")
            if not path_exists(app_key_path):
                print("\n" + yellow(
                    "Please copy your spotify_appkey.key to %s, or use the --key|-k option" % default_dir))
                sys.exit(1)

            config.load_application_key_file(app_key_path)

        # settings directory
        if args.settings is not None:
            settings_dir = norm_path(args.settings[0])
            config.settings_location = settings_dir
            config.cache_location = settings_dir
        else:
            config.settings_location = default_dir
            config.cache_location = default_dir

        self.session = spotify.Session(config=config)
        self.session.volume_normalization = args.normalize

        # disable scrobbling
        self.session.social.set_scrobbling(
            spotify.SocialProvider.SPOTIFY,
            spotify.ScrobblingState.LOCAL_DISABLED)
        self.session.social.set_scrobbling(
            spotify.SocialProvider.FACEBOOK,
            spotify.ScrobblingState.LOCAL_DISABLED)
        self.session.social.set_scrobbling(
            spotify.SocialProvider.LASTFM,
            spotify.ScrobblingState.LOCAL_DISABLED)

        bit_rates = dict([
            ('160', BitRate.BITRATE_160K),
            ('320', BitRate.BITRATE_320K),
            ('96', BitRate.BITRATE_96K)])
        self.session.preferred_bitrate(bit_rates[args.quality])
        self.session.on(spotify.SessionEvent.CONNECTION_STATE_UPDATED,
                        self.on_connection_state_changed)
        self.session.on(spotify.SessionEvent.END_OF_TRACK,
                        self.on_end_of_track)
        self.session.on(spotify.SessionEvent.MUSIC_DELIVERY,
                        self.on_music_delivery)
        self.session.on(spotify.SessionEvent.PLAY_TOKEN_LOST,
                        self.play_token_lost)
        self.session.on(spotify.SessionEvent.LOGGED_IN,
                        self.on_logged_in)

        self.event_loop = EventLoop(self.session, 0.1, self)

    def stop_event_loop(self):
        if self.event_loop.isAlive():
            self.event_loop.stop()
            self.event_loop.join()

    # executes on main thread (not SpotifyRipper thread)
    def login(self):
        args = self.args

        print("Logging in...")
        if args.last:
            self.login_as_last()

        if not self.login_success and args.user is not None:
            # remove old saved password
            self.session.forget_me()

            if args.password is None:
                password = getpass.getpass()
                self.login_as_user(args.user[0], password)
            else:
                self.login_as_user(args.user[0], args.password[0])

        return self.login_success

    def run(self):
        args = self.args

        # start event loop
        self.event_loop.start()

        # wait for main thread to login
        self.ripper_continue.wait()
        if self.abort.is_set():
            return

        # list of spotify URIs
        uris = args.uri

        def get_tracks_from_uri(uri):
            self.current_playlist = None
            self.current_album = None
            self.current_chart = None

            if isinstance(uri, list):
                return uri
            else:
                if (uri.startswith("spotify:artist:") and (
                                args.artist_album_type is not None or args.artist_album_market is not None)):
                    album_uris = self.web.get_albums_with_filter(uri)
                    return itertools.chain(*[self.load_link(album_uri) for album_uri in album_uris])
                elif uri.startswith("spotify:charts:"):
                    charts = self.web.get_charts(uri)
                    if charts is not None:
                        self.current_chart = charts
                        chart_uris = ["spotify:track:" + t["track"]["id"] for t in charts["entries"]["items"]]
                        return itertools.chain(*[self.load_link(chart_uri) for chart_uri in chart_uris])
                    else:
                        return iter([])
                else:
                    return self.load_link(uri)

        # calculate total size and time
        all_tracks = []
        for uri in uris:
            tracks = list(get_tracks_from_uri(uri))

            # TODO: remove dependency on current_album, ...
            for idx, track in enumerate(tracks):

                # ignore local tracks
                if track.is_local:
                    continue

                audio_file = self.format_track_path(idx, track)
                all_tracks.append((track, audio_file))

        self.progress.calc_total(all_tracks)

        if self.progress.total_size > 0:
            print("Total Download Size: " + format_size(self.progress.total_size))

        # create track iterator
        for uri in uris:
            if self.abort.is_set():
                break

            tracks = list(get_tracks_from_uri(uri))

            if args.playlist_sync and self.current_playlist:
                self.sync = Sync(args, self)
                self.sync.sync_playlist(self.current_playlist)

            # ripping loop
            for idx, track in enumerate(tracks):
                try:
                    self.check_stop_time()
                    self.skip.clear()

                    if self.abort.is_set():
                        break

                    print('Loading track %s...' % track.link.uri)
                    track.load()
                    if track.availability != 1:
                        print(red('Track is not available, because spotify, skipping...'))
                        self.post.log_failure(track)
                        continue

                    if track.is_local:
                        print(red('Track is not available, because it is local, skipping...'))
                        self.post.log_failure(track)
                        continue

                    self.audio_file = self.format_track_path(idx, track)

                    if not args.overwrite and path_exists(self.audio_file):
                        if is_partial(self.audio_file, track):
                            print("Overwriting partial file")
                        else:
                            print(yellow("Skipping " + track.link.uri))
                            print(cyan(self.audio_file))
                            self.post.queue_remove_from_playlist(idx)
                            continue

                    self.session.player.load(track)
                    self.prepare_rip(idx, track)
                    self.session.player.play()

                    timeout_count = 0
                    while not self.end_of_track.is_set() or not self.rip_queue.empty():
                        try:
                            if self.abort.is_set() or self.skip.is_set():
                                break

                            rip_item = self.rip_queue.get(timeout=1)

                            if self.abort.is_set() or self.skip.is_set():
                                break

                            self.rip(self.session, rip_item[0], rip_item[1], rip_item[2])
                        except queue.Empty:
                            timeout_count += 1
                            if timeout_count > 60:
                                raise spotify.Error("Timeout while ""ripping track")

                    if self.skip.is_set():
                        extra_line = "" if self.play_token_resume.is_set() else "\n"
                        print(extra_line + yellow("User skipped track... "))
                        self.session.player.play(False)
                        self.post.clean_up_partial()
                        self.post.log_failure(track)
                        self.end_of_track.clear()
                        self.progress.end_track(show_end=False)
                        self.ripping.clear()
                        continue

                    if self.abort.is_set():
                        self.session.player.play(False)
                        self.end_of_track.set()
                        self.post.clean_up_partial()
                        self.post.log_failure(track)
                        break

                    self.end_of_track.clear()

                    self.finish_rip(track)

                    # update id3v2 with metadata and embed front cover image
                    set_metadata_tags(args, self.audio_file, idx, track, self)

                    # make a note of the index and remove all the
                    # tracks from the playlist when everything is done
                    self.post.queue_remove_from_playlist(idx)

                except (spotify.Error, Exception) as e:
                    if isinstance(e, Exception):
                        print(red("Spotify error detected"))
                    print(str(e))
                    print("Skipping to next track...")
                    self.session.player.play(False)
                    self.post.clean_up_partial()
                    self.post.log_failure(track)
                    continue

            # create playlist m3u file if needed
            self.post.create_playlist_m3u(tracks)

            # create playlist wpl file if needed
            self.post.create_playlist_wpl(tracks)

            # actually removing the tracks from playlist
            self.post.remove_tracks_from_playlist()

            # remove libspotify's offline storage cache
            self.post.remove_offline_cache()

        # logout, we are done
        self.post.end_failure_log()
        self.post.print_summary()
        self.logout()
        self.stop_event_loop()
        self.finished.set()

    def check_stop_time(self):
        args = self.args

        def wait_for_resume(resume_time):
            while datetime.now() < resume_time and not self.abort.is_set():
                time.sleep(1)

        def stop_time_triggered():
            print(yellow("Stop time of %s has been triggered, stopping..." % (self.stop_time.strftime("%H:%M"))))

            if args.resume_after is not None:
                resume_time = parse_time_str(args.resume_after)
                print(yellow("Script will resume at " + resume_time.strftime("%H:%M")))
                wait_for_resume(resume_time)
                self.stop_time = None
            else:
                self.abort.set()

        if args.stop_after is not None:
            if self.stop_time is None:
                self.stop_time = parse_time_str(args.stop_after)
                print(yellow("Script will stop after " + self.stop_time.strftime("%H:%M")))

            if self.stop_time < datetime.now():
                stop_time_triggered()

        # we also wait if the "play token" was lost
        elif self.play_token_resume.is_set():
            resume_time = parse_time_str(args.play_token_resume)
            print(yellow("Script will resume at " + resume_time.strftime("%H:%M")))
            wait_for_resume(resume_time)
            self.play_token_resume.clear()

    def load_link(self, uri):
        # ignore if the uri is just blank (e.g. from a file)
        if not uri:
            return iter([])

        link = self.session.get_link(uri)
        if link.type == spotify.LinkType.TRACK:
            track = link.as_track()
            return iter([track])
        elif link.type == spotify.LinkType.PLAYLIST:
            self.current_playlist = link.as_playlist()
            count = 1
            while self.current_playlist is None:
                if count > 3:
                    print(red("Could not load playlist..."))
                    return iter([])
                print("Attempt %d failed: Spotify returned None for playlist, trying in 5 seconds..." % count)
                time.sleep(5.0)
                self.current_playlist = link.as_playlist()
                count += 1

            print('Loading playlist...')
            self.current_playlist.load()
            return iter(self.current_playlist.tracks)
        elif link.type == spotify.LinkType.STARRED:
            link_user = link.as_user()

            def load_starred():
                if link_user is not None:
                    return self.session.get_starred(link_user.canonical_name)
                else:
                    return self.session.get_starred()

            starred = load_starred()

            count = 1
            while starred is None:
                if count > 3:
                    print(red("Could not load starred playlist..."))
                    return iter([])
                print('Attempt %d failed: Spotify returned None for starred playlist, trying in 5 seconds...' % count)
                time.sleep(5.0)
                starred = load_starred()
                count += 1

            print('Loading starred playlist...')
            starred.load()
            return iter(starred.tracks)
        elif link.type == spotify.LinkType.ALBUM:
            album = link.as_album()
            album_browser = album.browse()
            print('Loading album browser...')
            album_browser.load()
            self.current_album = album
            return iter(album_browser.tracks)
        elif link.type == spotify.LinkType.ARTIST:
            artist = link.as_artist()
            artist_browser = artist.browse()
            print('Loading artist browser...')
            artist_browser.load()
            return iter(artist_browser.tracks)
        return iter([])

    def search_query(self, query):
        print("Searching for query: " + query)

        try:
            result = self.session.search(query)
            result.load()
        except spotify.Error as e:
            print(str(e))
            return iter([])

        if len(result.tracks) == 0:
            print(Fore.RED + "No Results" + Fore.RESET)
            return iter([])

        # list tracks
        print(Fore.GREEN + "Results" + Fore.RESET)
        for track_idx, track in enumerate(result.tracks):
            print("  " + Fore.YELLOW + str(track_idx + 1) + Fore.RESET +
                  " [" + to_ascii(track.album.name) + "] " +
                  to_ascii(track.artists[0].name) + " - " +
                  to_ascii(track.name) +
                  " (" + str(track.popularity) + ")")

        pick = raw_input("Pick track(s) (ex 1-3,5): ")

        def get_track(i):
            if 0 <= i < len(result.tracks):
                return iter([result.tracks[i]])
            return iter([])

        pattern = re.compile("^[0-9 ,\-]+$")
        if pick.isdigit():
            pick = int(pick) - 1
            return get_track(pick)
        elif pick.lower() == "a" or pick.lower() == "all":
            return iter(result.tracks)
        elif pattern.match(pick):
            def range_string(comma_string):
                def hyphen_range(hyphen_string):
                    x = [int(x) - 1 for x in hyphen_string.split('-')]
                    return range(x[0], x[-1] + 1)

                return itertools.chain(
                    *[hyphen_range(r) for r in comma_string.split(',')])

            picks = sorted(set(list(range_string(pick))))
            return itertools.chain(*[get_track(p) for p in picks])

        if pick != "":
            print(Fore.RED + "Invalid selection" + Fore.RESET)
        return iter([])

    def on_music_delivery(self, session, audio_format,
                          frame_bytes, num_frames):
        try:
            self.rip_queue.put_nowait((audio_format.sample_rate,
                                       frame_bytes, num_frames))
        except queue.Full:
            print(Fore.RED + "rip_queue is full. dropped music data" +
                  Fore.RESET)
        return num_frames

    def on_connection_state_changed(self, session):
        if session.connection.state is spotify.ConnectionState.LOGGED_IN:
            self.login_success = True
            self.logged_in.set()
            self.logged_out.clear()
        elif session.connection.state is spotify.ConnectionState.LOGGED_OUT:
            self.logged_in.clear()
            self.ripper_continue.clear()
            self.logged_out.set()

    def on_logged_in(self, session, error):
        if error is spotify.ErrorType.OK:
            print("Logged in as " + session.user.display_name)
        else:
            error_map = {
                9: "CLIENT_TOO_OLD",
                8: "UNABLE_TO_CONTACT_SERVER",
                6: "BAD_USERNAME_OR_PASSWORD",
                7: "USER_BANNED",
                15: "USER_NEEDS_PREMIUM",
                16: "OTHER_TRANSIENT",
                10: "OTHER_PERMANENT"
            }
            print("Logged in failed: " + error_map.get(error, "UNKNOWN_ERROR_CODE: " + str(error)))
            self.login_success = False
            self.logged_in.set()

    def play_token_lost(self, session):
        if self.args.play_token_resume is not None:
            print(
                '\n%sPlay token lost, waiting %s to resume...%s' % (Fore.RED, self.args.play_token_resume, Fore.RESET))
            self.play_token_resume.set()
            self.skip.set()
        else:
            print("\n" + Fore.RED + "Play token lost, aborting..." + Fore.RESET)
            self.abort_rip()

    def on_end_of_track(self, session):
        self.session.player.play(False)
        self.end_of_track.set()

    def login_as_user(self, user, password):
        """login into Spotify"""
        self.session.login(user, password, remember_me=True)
        self.logged_in.wait()

    def login_as_last(self):
        """login as the previous logged in user"""
        try:
            self.session.relogin()
            self.logged_in.wait()
        except spotify.Error as e:
            self.login_success = False
            print(str(e))

    def logout(self):
        """logout from Spotify"""
        time.sleep(0.1)
        if self.logged_in.is_set():
            print('Logging out...')
            self.session.logout()
            self.logged_out.wait()

    def format_track_path(self, idx, track):
        args = self.args

        # check if we cached the result already
        track.load()
        if track.link.uri in self.track_path_cache:
            return self.track_path_cache[track.link.uri]

        audio_file = format_track_string(self, args.format[0].strip(), idx, track)

        # in case the file name is too long
        def truncate(_str, max_size):
            return _str[:max_size].strip() if len(_str) > max_size else _str

        def truncate_dir_path(dir_path):
            path_tokens = dir_path.split(os.pathsep)
            path_tokens = [truncate(token, 255) for token in path_tokens]
            return os.pathsep.join(path_tokens)

        def truncate_file_name(file_name):
            tokens = file_name.rsplit(os.extsep, 1)
            if len(tokens) > 1:
                tokens[0] = truncate(tokens[0], 255 - len(tokens[1]) - 1)
            else:
                tokens[0] = truncate(tokens[0], 255)
            return os.extsep.join(tokens)

        # ensure each component in path is no more than 255 chars long
        tokens = audio_file.rsplit(os.pathsep, 1)
        if len(tokens) > 1:
            audio_file = os.path.join(
                truncate_dir_path(tokens[0]), truncate_file_name(tokens[1]))
        else:
            audio_file = truncate_file_name(tokens[0])

        # replace filename
        if args.replace is not None:
            audio_file = self.replace_filename(audio_file, args.replace)

        # remove not allowed characters in filename and encode utf-8
        audio_file = audio_file.replace('*."/\[]:;|=,', '')

        # prepend base_dir
        audio_file = to_ascii(os.path.join(base_dir(), audio_file))

        if args.normalized_ascii:
            audio_file = to_normalized_ascii(audio_file)

        # create directory if it doesn't exist
        audio_path = os.path.dirname(audio_file)
        if not path_exists(audio_path):
            os.makedirs(enc_str(audio_path))

        self.track_path_cache[track.link.uri] = audio_file
        return audio_file

    def replace_filename(self, filename, pattern_list):
        for pattern in pattern_list:
            repl = pattern.split('/')
            filename = re.sub(repl[0], repl[1], filename)
        return filename

    def prepare_rip(self, idx, track):
        args = self.args

        # reset progress
        self.progress.prepare_track(track)

        if self.progress.total_tracks > 1:
            skipped_str = ''
            if self.progress.skipped_tracks > 0:
                skipped_str = '(%d skipped) ' % self.progress.skipped_tracks
            print("%s[ %d / %d %s] Ripping %s %s\t(ESC to skip)%s" % (
                Fore.GREEN, self.progress.track_idx, self.progress.total_tracks, skipped_str,
                track.link.uri, Fore.WHITE, Fore.RESET))
        else:
            print(Fore.GREEN + "Ripping " + track.link.uri + Fore.RESET)
        print(Fore.CYAN + self.audio_file + Fore.RESET)

        file_size = calc_file_size(track)
        print("Track Download Size: " + format_size(file_size))

        audio_file_enc = enc_str(self.audio_file)

        if args.output_type == "wav":
            self.wav_file = wave.open(audio_file_enc, "wb")
            self.wav_file.setparams((2, 2, 44100, 0, 'NONE', 'not compressed'))
        elif args.output_type == "pcm":
            self.pcm_file = open(audio_file_enc, 'wb')
        elif args.output_type == "flac":
            self.rip_proc = Popen(
                ["flac", "-f", ("-" + str(args.comp)), "--silent", "--endian",
                 "little", "--channels", "2", "--bps", "16", "--sample-rate",
                 "44100", "--sign", "signed", "-o", audio_file_enc, "-"],
                stdin=PIPE)
        elif args.output_type == "alac.m4a":
            self.rip_proc = Popen(
                ["avconv", "-nostats", "-loglevel", "0", "-f", "s16le", "-ar",
                 "44100", "-ac", "2", "-channel_layout", "stereo", "-i", "-",
                 "-acodec", "alac", audio_file_enc],
                stdin=PIPE)
        elif args.output_type == "ogg":
            if args.cbr:
                self.rip_proc = Popen(
                    ["oggenc", "--quiet", "--raw", "-b", args.bitrate, "-o",
                     audio_file_enc, "-"], stdin=PIPE)
            else:
                self.rip_proc = Popen(
                    ["oggenc", "--quiet", "--raw", "-q", args.vbr, "-o",
                     audio_file_enc, "-"], stdin=PIPE)
        elif args.output_type == "opus":
            if args.cbr:
                self.rip_proc = Popen(
                    ["opusenc", "--quiet", "--comp", args.comp, "--cvbr",
                     "--bitrate", str(int(args.bitrate) / 2), "--raw",
                     "--raw-rate", "44100", "-", audio_file_enc], stdin=PIPE)
            else:
                self.rip_proc = Popen(
                    ["opusenc", "--quiet", "--comp", args.comp, "--vbr",
                     "--bitrate", args.vbr, "--raw", "--raw-rate", "44100",
                     "-", audio_file_enc], stdin=PIPE)
        elif args.output_type == "aac":
            if self.dev_null is None:
                self.dev_null = open(os.devnull, 'wb')
            if args.cbr:
                self.rip_proc = Popen(
                    ["faac", "-P", "-X", "-b", args.bitrate, "-o",
                     audio_file_enc, "-"], stdin=PIPE,
                    stdout=self.dev_null, stderr=self.dev_null)
            else:
                self.rip_proc = Popen(
                    ["faac", "-P", "-X", "-q", args.vbr, "-o",
                     audio_file_enc, "-"], stdin=PIPE,
                    stdout=self.dev_null, stderr=self.dev_null)
        elif args.output_type == "m4a":
            if args.cbr:
                self.rip_proc = Popen(
                    ["fdkaac", "-S", "-R", "-b",
                     args.bitrate, "-o", audio_file_enc, "-"], stdin=PIPE)
            else:
                self.rip_proc = Popen(
                    ["fdkaac", "-S", "-R", "-m", args.vbr,
                     "-o", audio_file_enc, "-"], stdin=PIPE)
        elif args.output_type == "mp3":
            lame_args = ["lame", "--silent"]

            if args.stereo_mode is not None:
                lame_args.extend(["-m", args.stereo_mode])

            if args.cbr:
                lame_args.extend(["-cbr", "-b", args.bitrate])
            else:
                lame_args.extend(["-V", args.vbr])

            lame_args.extend(["-h", "-r", "-", audio_file_enc])
            self.rip_proc = Popen(lame_args, stdin=PIPE)

        if self.rip_proc is not None:
            self.pipe = self.rip_proc.stdin

        self.ripping.set()

    def finish_rip(self, track):
        self.progress.end_track()
        if self.pipe is not None:
            ripping_time = format_timedelta(self.progress.last_track_time)
            print(self.progress.song_duration)
            print(self.progress.last_track_time.microseconds)
            speed = int(self.progress.last_track_time.microseconds * 100.0 / track.duration)
            # speed = 120
            print(Fore.GREEN + ('Rip complete, it took %s, speed %d%%' % (ripping_time, speed)) + Fore.RESET)
            self.pipe.flush()
            self.pipe.close()

            # wait for process to end before continuing
            ret_code = self.rip_proc.wait()
            if ret_code != 0:
                print(
                    Fore.YELLOW + "Warning: encoder returned non-zero "
                                  "error code " + str(ret_code) + Fore.RESET)
            self.rip_proc = None
            self.pipe = None

        if self.wav_file is not None:
            self.wav_file.close()
            self.wav_file = None

        if self.pcm_file is not None:
            self.pcm_file.flush()
            os.fsync(self.pcm_file.fileno())
            self.pcm_file.close()
            self.pcm_file = None

        self.ripping.clear()
        self.post.log_success(track)

    def rip(self, session, sample_rate, frame_bytes, num_frames):
        if self.ripping.is_set():
            self.progress.update_progress(num_frames, sample_rate)
            if self.pipe is not None:
                self.pipe.write(frame_bytes)

            if self.wav_file is not None:
                self.wav_file.writeframes(frame_bytes)

            if self.pcm_file is not None:
                self.pcm_file.write(frame_bytes)

    def abort_rip(self):
        self.ripping.clear()
        self.abort.set()


text:
	#include <sys/types.h>
	#include <sys/stat.h>
#endif

#if OE_ANDROID
	#include "OE/Platform/Android/Android.hpp"
#endif

#include <string>

namespace OrbitEngine {	namespace System {

#if OE_ANDROID
	typedef AAsset* IOHandle;
#else
	typedef FILE* IOHandle;
#endif

	enum class AccessMode {
		READ,
		WRITE,
		READ_AND_WRITE
	};

	class IOStream {
	public:
		IOStream(const std::string& file, AccessMode access = AccessMode::READ);
		~IOStream();

		bool isOpen();
		size_t getFilesize();
		size_t read(void* buffer, size_t size, size_t count);
		size_t write(const void* buffer, size_t size, size_t count);
		bool seek(size_t offset, int origin);
		size_t tell();
		void flush();

	private:
		IOHandle m_Data;
		std::string m_File;

		size_t m_CachedFilesize;
	};
} }

#endif

redpajama_set_name: RedPajamaGithub


text:
'use strict';

var EventEmitter = require('EventEmitter');
var NavigationEvent = require('NavigationEvent');

type ExtraInfo = {
  defaultPrevented: ?boolean,
  eventPhase: ?number,
  propagationStopped: ?boolean,
  target: ?Object,
};

class NavigationEventEmitter extends EventEmitter {
  _emitQueue: Array<any>;
  _emitting: boolean;
  _target: Object;

  constructor(target: Object) {
    super();
    this._emitting = false;
    this._emitQueue = [];
    this._target = target;
  }

  emit(
    eventType: string,
    data: any,
    didEmitCallback: ?Function,
    extraInfo: ?ExtraInfo
  ): void {
    if (this._emitting) {
      // An event cycle that was previously created hasn't finished yet.
      // Put this event cycle into the queue and will finish them later.
      var args: any = Array.prototype.slice.call(arguments);
      this._emitQueue.push(args);
      return;
    }

    this._emitting = true;

    var event = NavigationEvent.pool(eventType, this._target, data);

    if (extraInfo) {
      if (extraInfo.target) {
        event.target = extraInfo.target;
      }

      if (extraInfo.eventPhase) {
        event.eventPhase = extraInfo.eventPhase;
      }

      if (extraInfo.defaultPrevented) {
        event.preventDefault();
      }

      if (extraInfo.propagationStopped) {
        event.stopPropagation();
      }
    }

    // EventEmitter#emit only takes `eventType` as `String`. Casting `eventType`
    // to `String` to make @flow happy.
    super.emit(String(eventType), event);

    if (typeof didEmitCallback === 'function') {
      didEmitCallback.call(this._target, event);
    }
    event.dispose();

    this._emitting = false;

    while (this._emitQueue.length) {
      var args: any = this._emitQueue.shift();
      this.emit.apply(this, args);
    }
  }
}

module.exports = NavigationEventEmitter;


text:
package day11;

import common.IntcodeComputer;
import common.Point;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class EmergencyHullPaintingRobot {

    public static final int COLOR_BLACK = 0;
    public static final int COLOR_WHITE = 1;

    public static final int DIRECTION_UP = 0;
    public static final int DIRECTION_RIGHT = 1;
    public static final int DIRECTION_DOWN = 2;
    public static final int DIRECTION_LEFT = 3;

    protected IntcodeComputer brain;

    protected Point location;
    protected int direction;

    protected HashMap<Point, Integer> panelColors;
    protected Set<Point> panelsPainted;

    protected int xMin = 0;
    protected int xMax = 0;
    protected int yMin = 0;
    protected int yMax = 0;

    public EmergencyHullPaintingRobot(BigInteger[] paintingInstructions) {
        brain = new IntcodeComputer(paintingInstructions);
//        brain.setVerbose(true);
        brain.setInterruptOnOutput(true);
        location = new Point(0, 0);
        direction = DIRECTION_UP;
        panelsPainted = new HashSet<>();
        panelColors = new HashMap<>();
    }

    public void paint() throws Exception {

        Integer color;
        int paintColor, turnDirection;
        while (true) {
            color = panelColors.containsKey(location) ? panelColors.get(location) : COLOR_BLACK;
            brain.pushOntoInputs(color);
            try {
                brain.process();
                return;
            } catch (IntcodeComputer.OutputInterrupt exception) {
                paintColor = brain.getLastOutput().intValue();
            }
            try {
                brain.process();
                throw new Exception("Process with no output");
            } catch (IntcodeComputer.OutputInterrupt exception) {
                turnDirection = brain.getLastOutput().intValue();
            }
            panelColors.put(location, paintColor);
            panelsPainted.add(location);
            location = location.clone();
            turn(turnDirection);
            move();
        }
    }

    public int findNumPanelsPaintedAtLeastOnce() {
        return panelsPainted.size();
    }

    protected void turn(int turnDirection) throws Exception {
        switch (turnDirection) {
            case 0:
                direction--;
                if (direction == -1) {
                    direction = 3;
                }
                break;
            case 1:
                direction++;
                if (direction == 4) {
                    direction = 0;
                }
                break;
            default:
                throw new Exception("Unknown direction!");
        }
    }

    protected void move() {
        switch (direction) {
            case DIRECTION_UP:
                location.y--;
                if (location.y < yMin) {
                    yMin = location.y;
                }
                break;
            case DIRECTION_RIGHT:
                location.x++;
                if (location.x > xMax) {
                    xMax = location.x;
                }
                break;
            case DIRECTION_DOWN:
                location.y++;
                if (location.y > yMax) {
                    yMax = location.y;
                }
                break;
            case DIRECTION_LEFT:
                location.x--;
                if (location.x < xMin) {
                    xMin = location.x;
                }
                break;
        }
    }

    public void setStartingPanelWhite() {
        panelColors.put(new Point(0, 0), COLOR_WHITE);
    }

    public void printHull() {
        Point point;
        int color;
        for (int y = yMin; y <= yMax; y++) {
            for (int x = xMin; x <= xMax; x++) {
                point = new Point(x, y);
                color = panelColors.containsKey(point) ? panelColors.get(point) : COLOR_BLACK;
                System.out.print(color);
            }
            System.out.println();
        }
    }
}


text:
import requests, csv

# This class will be handling the api and the data
class DataHandler:
	def data(self, portfolio=False, company=None):
		if portfolio is False:
			company = company
			self.yahoo = 'http://ichart.finance.yahoo.com/table.csv?s=%s&c=1962' % company
			with requests.Session() as sess:
				download = sess.get(self.yahoo)
				decoded_content = download.content.decode('utf-8')
				decoded_data = csv.reader(decoded_content.splitlines(), delimiter=',')
				return list(decoded_data) #
		elif portfolio is True:
			# Todo
			pass
		else:
			raise Exception("Problem with portfolio in data")

	def urls(self):
		# TODO
		pass


text:
from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'servicezen.views.home', name='home'),
    # url(r'^servicezen/', include('servicezen.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    url(r'^auth/', include('accounts.urls', namespace='accounts')),
    url(r'', include('tokenapi.urls')),
    url(r'^', include('services.urls', namespace='services')),
)


text:
This project is an exploration of skrollr.js and paralax in general. I'm using an article that my wife wrote as a content.

http://www.dilettantearmy.com/facts/beyond-detail


text:
@interface KTGViewController ()

@property (nonatomic, strong) NSArray* exampleEvents;

@end

@implementation KTGViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray* retVal = [NSMutableArray array];

    self.exampleEvents = retVal;

//    self.dayView.delegate = self;
//    self.dayView.dataSource = self;

    [[EKEventStore alloc] requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if(!granted){
            //WHAT DID YOU THINK WOULD HAPPEN?
        } else {
            [self fetchLocalEvents];
        }
    }];
}

- (void)fetchLocalEvents {
    EKEventStore* store = [[EKEventStore alloc] init];
    const double secondsInAYear = (60.0*60.0*24.0)*365.0;
    NSPredicate* predicate = [store predicateForEventsWithStartDate:[NSDate dateWithTimeIntervalSinceNow:-secondsInAYear] endDate:[NSDate dateWithTimeIntervalSinceNow:secondsInAYear] calendars:nil];

    NSArray* calendarEvents = [store eventsMatchingPredicate:predicate];

    if(calendarEvents.count == 0){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[[UIAlertView alloc] initWithTitle:@"No events found" message:@"Add some events in Calendar.app" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
        });
        return;
    }

    NSMutableArray* retVal = [NSMutableArray array];

    for(EKEvent* ev in calendarEvents){
        KTGExampleEvent* event = [[KTGExampleEvent alloc] init];
        event.eventName = ev.title;
        event.location = ev.location;
        event.start = ev.startDate;
        event.end = ev.endDate;
        [retVal addObject:event];
    }
    self.exampleEvents = retVal;

    //Must be called on the main thread
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.currentDate = nil;
        self.currentDate = [NSDate date];
//        [self.dayView reloadData];
//        [self.dayView scrollToEvent:[self.exampleEvents firstObject] position:UITableViewScrollPositionMiddle animated:YES];
    });

}

///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark KTGCalendarDayViewDataSource
///////////////////////////////////////////////////////////////////////////////////////////////////

- (NSArray *)eventsForDayView:(KTGCalendarDayView*) dayView {
    NSArray* retVal = [self.exampleEvents filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        id<KTGCalendarEvent> ev = evaluatedObject;
        return [ev.startTime ktg_isSameDay:dayView.date] || [ev.endTime ktg_isSameDay:dayView.date];
    }]];
    NSLog(@"Returning %d dates for %@", retVal.count, dayView.date);
    return retVal;
}

- (id<KTGCalendarDayViewDataSource>)datasource {
    return self;
}

@end


text:
/**
 * Class CoefficientEditSlider
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 * @link http://github.com/kizi/easyminer-miningui
 *
 * @type Class
 */
var CoefficientEditSlider = new Class({
	Extends: Slider,

	numSteps: 100,
	precision: 0,
	inversePrecision: 0,

	elementSlider: null,
	elementValue: null,
    previousSlider: null,
    nextSlider: null,
	numberNormalizer: null,
	inverseNumberNormalizer: null,

	initialize: function (elementSlider, elementValue, minValue, minValueInclusive, maxValue, maxValueInclusive) {
		this.numSteps = Math.abs(minValue - maxValue) + 1;
		this.elementSlider = elementSlider;
		this.elementValue = elementValue;
		this.numberNormalizer = new NumberNormalizer(minValue, maxValue, this.inversePrecision, minValue, maxValue, this.precision, this.numSteps, minValueInclusive, maxValueInclusive);
		this.inverseNumberNormalizer = new NumberNormalizer(minValue, maxValue, this.precision, minValue, maxValue, this.inversePrecision, this.numSteps, minValueInclusive, maxValueInclusive);

		this.parent(this.elementSlider, this.elementSlider.getElement('.knob'), {
	        range: [minValue, maxValue],
	        initialStep: elementValue.value,
	        onChange: function(value) {
	        	this.handleChange(value);
	        }
	    });
	},

    setPreviousSlider: function(previousSlider) {
        this.previousSlider = previousSlider;
    },

    setNextSlider: function(nextSlider) {
        this.nextSlider = nextSlider;
    },

	handleChange: function (value) {
    	var number = this.inverseNumberNormalizer.validate(value);
    	number = this.inverseNumberNormalizer.normalize(number);
    	var string = this.inverseNumberNormalizer.format(number);

    	this.elementValue.value = string;

    	if (this.previousSlider && (this.previousSlider.elementValue.value > number)) {
    		this.previousSlider.set(number);
    	}

        if (this.nextSlider && (this.nextSlider.elementValue.value < number)) {
            this.nextSlider.set(number);
        }
	}
});


text:
CREATE TABLE users (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR,
  email VARCHAR,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
  updated_at TIMESTAMP NOT nULL DEFAULt CURRENT_TIMESTAMP
)


text:
// This file contains structures for describing SSRCs from a media source such
// as a MediaStreamTrack when it is sent across an RTP session. Multiple media
// sources may be sent across the same RTP session, each of them will be
// described by one StreamParams object
// SsrcGroup is used to describe the relationship between the SSRCs that
// are used for this media source.
// E.x: Consider a source that is sent as 3 simulcast streams
// Let the simulcast elements have SSRC 10, 20, 30.
// Let each simulcast element use FEC and let the protection packets have
// SSRC 11,21,31.
// To describe this 4 SsrcGroups are needed,
// StreamParams would then contain ssrc = {10,11,20,21,30,31} and
// ssrc_groups = {{SIM,{10,20,30}, {FEC,{10,11}, {FEC, {20,21}, {FEC {30,31}}}
// Please see RFC 5576.

#ifndef TALK_SESSION_PHONE_STREAMPARAMS_H_
#define TALK_SESSION_PHONE_STREAMPARAMS_H_

#include <algorithm>
#include <string>
#include <vector>

#include "talk/base/basictypes.h"

namespace cricket {

extern const char kFecSsrcGroupSemantics[];
extern const char kFidSsrcGroupSemantics[];
extern const char kSimSsrcGroupSemantics[];

struct SsrcGroup {
  SsrcGroup(const std::string& usage, const std::vector<uint32>& ssrcs)
      : semantics(usage), ssrcs(ssrcs) {
  }

  bool operator==(const SsrcGroup& other) const {
    return (semantics == other.semantics && ssrcs == other.ssrcs);
  }
  bool operator!=(const SsrcGroup &other) const {
    return !(*this == other);
  }

  bool has_semantics(const std::string& semantics) const;

  std::string ToString() const;

  std::string semantics;  // e.g FIX, FEC, SIM.
  std::vector<uint32> ssrcs;  // SSRCs of this type.
};

struct StreamParams {
  static StreamParams CreateLegacy(uint32 ssrc) {
    StreamParams stream;
    stream.ssrcs.push_back(ssrc);
    return stream;
  }
  bool operator==(const StreamParams& other) const {
    return (nick == other.nick &&
            name == other.name &&
            ssrcs == other.ssrcs &&
            ssrc_groups == other.ssrc_groups &&
            type == other.type &&
            display == other.display &&
            cname == other.cname &&
            sync_label == sync_label);
  }
  bool operator!=(const StreamParams &other) const {
    return !(*this == other);
  }

  uint32 first_ssrc() const {
    if (ssrcs.empty()) {
      return 0;
    }

    return ssrcs[0];
  }
  bool has_ssrcs() const {
    return !ssrcs.empty();
  }
  bool has_ssrc(uint32 ssrc) const {
    return std::find(ssrcs.begin(), ssrcs.end(), ssrc) != ssrcs.end();
  }
  void add_ssrc(uint32 ssrc) {
    ssrcs.push_back(ssrc);
  }
  bool has_ssrc_groups() const {
    return !ssrc_groups.empty();
  }
  bool has_ssrc_group(const std::string& semantics) const {
    return (get_ssrc_group(semantics) != NULL);
  }
  const SsrcGroup* get_ssrc_group(const std::string& semantics) const {
    for (std::vector<SsrcGroup>::const_iterator it = ssrc_groups.begin();
         it != ssrc_groups.end(); ++it) {
      if (it->has_semantics(semantics)) {
        return &(*it);
      }
    }
    return NULL;
  }

  std::string ToString() const;

  // Resource of the MUC jid of the participant of with this stream.
  // For 1:1 calls, should be left empty (which means remote streams
  // and local streams should not be mixed together).
  std::string nick;
  // Unique name of this source (unique per-nick, not for all nicks)
  std::string name;
  std::vector<uint32> ssrcs;  // All SSRCs for this source
  std::vector<SsrcGroup> ssrc_groups;  // e.g. FID, FEC, SIM
  // Examples: "camera", "screencast"
  std::string type;
  // Friendly name describing stream
  std::string display;
  std::string cname;  // RTCP CNAME
  std::string sync_label;  // Friendly name of cname.
};

typedef std::vector<StreamParams> StreamParamsVec;

// Finds the stream in streams with the specified ssrc.
// If you are only interested in the stream exist it is ok to call this function
// stream_out = NULL.
bool GetStreamBySsrc(const StreamParamsVec& streams, uint32 ssrc,
                     StreamParams* stream_out);

// Finds the stream in streams with the specified nick and name.
// If you are only interested in the stream exist it is ok to call this function
// stream_out = NULL.
bool GetStreamByNickAndName(const StreamParamsVec& streams,
                            const std::string& nick,
                            const std::string& name,
                            StreamParams* stream_out);

// Removes the stream with ssrc from streams. Returns true if a stream is
// removed, false otherwise.
bool RemoveStreamBySsrc(StreamParamsVec* streams, uint32 ssrc);
bool RemoveStreamByNickAndName(StreamParamsVec* streams,
                               const std::string& nick,
                               const std::string& name);

}  // namespace cricket

#endif  // TALK_SESSION_PHONE_STREAMPARAMS_H_


text:
package version

import (
	"errors"
	"time"

	"github.com/ava-labs/avalanchego/utils/timer/mockable"
)

var (
	errIncompatible = errors.New("peers version is incompatible")

	_ Compatibility = (*compatibility)(nil)
)

// Compatibility a utility for checking the compatibility of peer versions
type Compatibility interface {
	// Returns the local version
	Version() *Application

	// Returns nil if the provided version is compatible with the local version.
	// This means that the version is connectable and that consensus messages
	// can be made to them.
	Compatible(*Application) error
}

type compatibility struct {
	version *Application

	minCompatable     *Application
	minCompatableTime time.Time
	prevMinCompatable *Application

	clock mockable.Clock
}

// NewCompatibility returns a compatibility checker with the provided options
func NewCompatibility(
	version *Application,
	minCompatable *Application,
	minCompatableTime time.Time,
	prevMinCompatable *Application,
) Compatibility {
	return &compatibility{
		version:           version,
		minCompatable:     minCompatable,
		minCompatableTime: minCompatableTime,
		prevMinCompatable: prevMinCompatable,
	}
}

func (c *compatibility) Version() *Application {
	return c.version
}

func (c *compatibility) Compatible(peer *Application) error {
	if err := c.version.Compatible(peer); err != nil {
		return err
	}

	if !peer.Before(c.minCompatable) {
		// The peer is at least the minimum compatible version.
		return nil
	}

	// The peer is going to be marked as incompatible at [c.minCompatableTime].
	now := c.clock.Time()
	if !now.Before(c.minCompatableTime) {
		return errIncompatible
	}

	// The minCompatable check isn't being enforced yet.
	if !peer.Before(c.prevMinCompatable) {
		// The peer is at least the previous minimum compatible version.
		return nil
	}
	return errIncompatible
}


text:
#include "hb-ot-shape-complex-private.hh"

/* TODO Add kana, hangul, and other small sahpers here */

/* When adding trivial shapers, eg. kana, hangul, etc, we can either
 * add a full shaper enum value for them, or switch on the script in
 * the default complex shaper.  The former is faster, so I think that's
 * what we would do, and hence the default complex shaper shall remain
 * empty.
 */

void
_hb_ot_shape_complex_collect_features_default (hb_ot_map_builder_t *map, const hb_segment_properties_t  *props)
{
}

bool
_hb_ot_shape_complex_prefer_decomposed_default (void)
{
  return FALSE;
}

void
_hb_ot_shape_complex_setup_masks_default (hb_ot_map_t *map, hb_buffer_t *buffer)
{
}


text:
using System;

namespace Bazam.Modules
{
    public class EnuMasterException : Exception
    {
        public EnuMasterException(string message) : base(message) { }
        public EnuMasterException(string message, Exception innerEx) : base(message, innerEx) { }
    }
}


text:
from __future__ import (
    print_function, division, absolute_import, unicode_literals)
from collections import OrderedDict
import fontTools.voltLib.ast as ast
from fontTools.voltLib.lexer import Lexer
from fontTools.voltLib.error import VoltLibError
from io import open

PARSE_FUNCS = {
    "DEF_GLYPH": "parse_def_glyph_",
    "DEF_GROUP": "parse_def_group_",
    "DEF_SCRIPT": "parse_def_script_",
    "DEF_LOOKUP": "parse_def_lookup_",
    "DEF_ANCHOR": "parse_def_anchor_",
    "GRID_PPEM": "parse_ppem_",
    "PRESENTATION_PPEM": "parse_ppem_",
    "PPOSITIONING_PPEM": "parse_ppem_",
    "COMPILER_USEEXTENSIONLOOKUPS": "parse_compiler_flag_",
    "COMPILER_USEPAIRPOSFORMAT2": "parse_compiler_flag_",
    "CMAP_FORMAT": "parse_cmap_format",
}

class Parser(object):
    def __init__(self, path):
        self.doc_ = ast.VoltFile()
        self.glyphs_ = OrderedSymbolTable()
        self.groups_ = SymbolTable()
        self.anchors_ = {}  # dictionary of SymbolTable() keyed by glyph
        self.scripts_ = SymbolTable()
        self.langs_ = SymbolTable()
        self.lookups_ = SymbolTable()
        self.next_token_type_, self.next_token_ = (None, None)
        self.next_token_location_ = None
        with open(path, "r") as f:
            self.lexer_ = Lexer(f.read(), path)
        self.advance_lexer_()

    def parse(self):
        statements = self.doc_.statements
        while self.next_token_type_ is not None:
            self.advance_lexer_()
            if self.cur_token_ in PARSE_FUNCS.keys():
                func = getattr(self, PARSE_FUNCS[self.cur_token_])
                statements.append(func())
            elif self.is_cur_keyword_("END"):
                if self.next_token_type_ is not None:
                    raise VoltLibError("Expected the end of the file",
                                       self.cur_token_location_)
                return self.doc_
            else:
                raise VoltLibError(
                    "Expected " + ", ".join(sorted(PARSE_FUNCS.keys())),
                    self.cur_token_location_)
        return self.doc_

    def parse_def_glyph_(self):
        assert self.is_cur_keyword_("DEF_GLYPH")
        location = self.cur_token_location_
        name = self.expect_string_()
        self.expect_keyword_("ID")
        gid = self.expect_number_()
        if gid < 0:
            raise VoltLibError("Invalid glyph ID", self.cur_token_location_)
        gunicode = None
        if self.next_token_ == "UNICODE":
            self.expect_keyword_("UNICODE")
            gunicode = [self.expect_number_()]
            if gunicode[0] < 0:
                raise VoltLibError("Invalid glyph UNICODE",
                                   self.cur_token_location_)
        elif self.next_token_ == "UNICODEVALUES":
            self.expect_keyword_("UNICODEVALUES")
            gunicode = self.parse_unicode_values_()
        gtype = None
        if self.next_token_ == "TYPE":
            self.expect_keyword_("TYPE")
            gtype = self.expect_name_()
            assert gtype in ("BASE", "LIGATURE", "MARK")
        components = None
        if self.next_token_ == "COMPONENTS":
            self.expect_keyword_("COMPONENTS")
            components = self.expect_number_()
        self.expect_keyword_("END_GLYPH")
        if self.glyphs_.resolve(name) is not None:
            raise VoltLibError(
                'Glyph "%s" (gid %i) already defined' % (name, gid),
                location
            )
        def_glyph = ast.GlyphDefinition(location, name, gid,
                                        gunicode, gtype, components)
        self.glyphs_.define(name, def_glyph)
        return def_glyph

    def parse_def_group_(self):
        assert self.is_cur_keyword_("DEF_GROUP")
        location = self.cur_token_location_
        name = self.expect_string_()
        enum = None
        if self.next_token_ == "ENUM":
            self.expect_keyword_("ENUM")
            enum = self.parse_enum_()
        self.expect_keyword_("END_GROUP")
        if self.groups_.resolve(name) is not None:
            raise VoltLibError(
                'Glyph group "%s" already defined, '
                'group names are case insensitive' % name,
                location
            )
        def_group = ast.GroupDefinition(location, name, enum)
        self.groups_.define(name, def_group)
        return def_group

    def parse_def_script_(self):
        assert self.is_cur_keyword_("DEF_SCRIPT")
        location = self.cur_token_location_
        name = None
        if self.next_token_ == "NAME":
            self.expect_keyword_("NAME")
            name = self.expect_string_()
        self.expect_keyword_("TAG")
        tag = self.expect_string_()
        if self.scripts_.resolve(tag) is not None:
            raise VoltLibError(
                'Script "%s" already defined, '
                'script tags are case insensitive' % tag,
                location
            )
        self.langs_.enter_scope()
        langs = []
        while self.next_token_ != "END_SCRIPT":
            self.advance_lexer_()
            lang = self.parse_langsys_()
            self.expect_keyword_("END_LANGSYS")
            if self.langs_.resolve(lang.tag) is not None:
                raise VoltLibError(
                    'Language "%s" already defined in script "%s", '
                    'language tags are case insensitive' % (lang.tag, tag),
                    location
                )
            self.langs_.define(lang.tag, lang)
            langs.append(lang)
        self.expect_keyword_("END_SCRIPT")
        self.langs_.exit_scope()
        def_script = ast.ScriptDefinition(location, name, tag, langs)
        self.scripts_.define(tag, def_script)
        return def_script

    def parse_langsys_(self):
        assert self.is_cur_keyword_("DEF_LANGSYS")
        location = self.cur_token_location_
        name = None
        if self.next_token_ == "NAME":
            self.expect_keyword_("NAME")
            name = self.expect_string_()
        self.expect_keyword_("TAG")
        tag = self.expect_string_()
        features = []
        while self.next_token_ != "END_LANGSYS":
            self.advance_lexer_()
            feature = self.parse_feature_()
            self.expect_keyword_("END_FEATURE")
            features.append(feature)
        def_langsys = ast.LangSysDefinition(location, name, tag, features)
        return def_langsys

    def parse_feature_(self):
        assert self.is_cur_keyword_("DEF_FEATURE")
        location = self.cur_token_location_
        self.expect_keyword_("NAME")
        name = self.expect_string_()
        self.expect_keyword_("TAG")
        tag = self.expect_string_()
        lookups = []
        while self.next_token_ != "END_FEATURE":
            # self.advance_lexer_()
            self.expect_keyword_("LOOKUP")
            lookup = self.expect_string_()
            lookups.append(lookup)
        feature = ast.FeatureDefinition(location, name, tag, lookups)
        return feature

    def parse_def_lookup_(self):
        assert self.is_cur_keyword_("DEF_LOOKUP")
        location = self.cur_token_location_
        name = self.expect_string_()
        if not name[0].isalpha():
            raise VoltLibError(
                'Lookup name "%s" must start with a letter' % name,
                location
            )
        if self.lookups_.resolve(name) is not None:
            raise VoltLibError(
                'Lookup "%s" already defined, '
                'lookup names are case insensitive' % name,
                location
            )
        process_base = True
        if self.next_token_ == "PROCESS_BASE":
            self.advance_lexer_()
        elif self.next_token_ == "SKIP_BASE":
            self.advance_lexer_()
            process_base = False
        process_marks = True
        if self.next_token_ == "PROCESS_MARKS":
            self.advance_lexer_()
            if self.next_token_ == "MARK_GLYPH_SET":
                self.advance_lexer_()
                process_marks = self.expect_string_()
            elif self.next_token_type_ == Lexer.STRING:
                process_marks = self.expect_string_()
            elif self.next_token_ == "ALL":
                self.advance_lexer_()
            else:
                raise VoltLibError(
                    "Expected ALL, MARK_GLYPH_SET or an ID. "
                    "Got %s" % (self.next_token_type_),
                    location)
        elif self.next_token_ == "SKIP_MARKS":
            self.advance_lexer_()
            process_marks = False
        direction = None
        if self.next_token_ == "DIRECTION":
            self.expect_keyword_("DIRECTION")
            direction = self.expect_name_()
            assert direction in ("LTR", "RTL")
        reversal = None
        if self.next_token_ == "REVERSAL":
            self.expect_keyword_("REVERSAL")
            reversal = True
        comments = None
        if self.next_token_ == "COMMENTS":
            self.expect_keyword_("COMMENTS")
            comments = self.expect_string_()
        context = []
        while self.next_token_ in ("EXCEPT_CONTEXT", "IN_CONTEXT"):
            context = self.parse_context_()
        as_pos_or_sub = self.expect_name_()
        sub = None
        pos = None
        if as_pos_or_sub == "AS_SUBSTITUTION":
            sub = self.parse_substitution_(reversal)
        elif as_pos_or_sub == "AS_POSITION":
            pos = self.parse_position_()
        else:
            raise VoltLibError(
                "Expected AS_SUBSTITUTION or AS_POSITION. "
                "Got %s" % (as_pos_or_sub),
                location)
        def_lookup = ast.LookupDefinition(
            location, name, process_base, process_marks, direction, reversal,
            comments, context, sub, pos)
        self.lookups_.define(name, def_lookup)
        return def_lookup

    def parse_context_(self):
        location = self.cur_token_location_
        contexts = []
        while self.next_token_ in ("EXCEPT_CONTEXT", "IN_CONTEXT"):
            side = None
            coverage = None
            ex_or_in = self.expect_name_()
            # side_contexts = [] # XXX
            if self.next_token_ != "END_CONTEXT":
                left = []
                right = []
                while self.next_token_ in ("LEFT", "RIGHT"):
                    side = self.expect_name_()
                    coverage = self.parse_coverage_()
                    if side == "LEFT":
                        left.append(coverage)
                    else:
                        right.append(coverage)
                self.expect_keyword_("END_CONTEXT")
                context = ast.ContextDefinition(location, ex_or_in, left,
                                                right)
                contexts.append(context)
            else:
                self.expect_keyword_("END_CONTEXT")
        return contexts

    def parse_substitution_(self, reversal):
        assert self.is_cur_keyword_("AS_SUBSTITUTION")
        location = self.cur_token_location_
        src = []
        dest = []
        if self.next_token_ != "SUB":
            raise VoltLibError("Expected SUB", location)
        while self.next_token_ == "SUB":
            self.expect_keyword_("SUB")
            src.append(self.parse_coverage_())
            self.expect_keyword_("WITH")
            dest.append(self.parse_coverage_())
            self.expect_keyword_("END_SUB")
        self.expect_keyword_("END_SUBSTITUTION")
        max_src = max([len(cov) for cov in src])
        max_dest = max([len(cov) for cov in dest])
        # many to many or mixed is invalid
        if ((max_src > 1 and max_dest > 1) or
                (reversal and (max_src > 1 or max_dest > 1))):
            raise VoltLibError(
                "Invalid substitution type",
                location)
        mapping = OrderedDict(zip(tuple(src), tuple(dest)))
        if max_src == 1 and max_dest == 1:
            if reversal:
                sub = ast.SubstitutionReverseChainingSingleDefinition(
                    location, mapping)
            else:
                sub = ast.SubstitutionSingleDefinition(location, mapping)
        elif max_src == 1 and max_dest > 1:
            sub = ast.SubstitutionMultipleDefinition(location, mapping)
        elif max_src > 1 and max_dest == 1:
            sub = ast.SubstitutionLigatureDefinition(location, mapping)
        return sub

    def parse_position_(self):
        assert self.is_cur_keyword_("AS_POSITION")
        location = self.cur_token_location_
        pos_type = self.expect_name_()
        if pos_type not in (
                "ATTACH", "ATTACH_CURSIVE", "ADJUST_PAIR", "ADJUST_SINGLE"):
            raise VoltLibError(
                "Expected ATTACH, ATTACH_CURSIVE, ADJUST_PAIR, ADJUST_SINGLE",
                location)
        if pos_type == "ATTACH":
            position = self.parse_attach_()
        elif pos_type == "ATTACH_CURSIVE":
            position = self.parse_attach_cursive_()
        elif pos_type == "ADJUST_PAIR":
            position = self.parse_adjust_pair_()
        elif pos_type == "ADJUST_SINGLE":
            position = self.parse_adjust_single_()
        self.expect_keyword_("END_POSITION")
        return position

    def parse_attach_(self):
        assert self.is_cur_keyword_("ATTACH")
        location = self.cur_token_location_
        coverage = self.parse_coverage_()
        coverage_to = []
        self.expect_keyword_("TO")
        while self.next_token_ != "END_ATTACH":
            cov = self.parse_coverage_()
            self.expect_keyword_("AT")
            self.expect_keyword_("ANCHOR")
            anchor_name = self.expect_string_()
            coverage_to.append((cov, anchor_name))
        self.expect_keyword_("END_ATTACH")
        position = ast.PositionAttachDefinition(
            location, coverage, coverage_to)
        return position

    def parse_attach_cursive_(self):
        assert self.is_cur_keyword_("ATTACH_CURSIVE")
        location = self.cur_token_location_
        coverages_exit = []
        coverages_enter = []
        while self.next_token_ != "ENTER":
            self.expect_keyword_("EXIT")
            coverages_exit.append(self.parse_coverage_())
        while self.next_token_ != "END_ATTACH":
            self.expect_keyword_("ENTER")
            coverages_enter.append(self.parse_coverage_())
        self.expect_keyword_("END_ATTACH")
        position = ast.PositionAttachCursiveDefinition(
            location, coverages_exit, coverages_enter)
        return position

    def parse_adjust_pair_(self):
        assert self.is_cur_keyword_("ADJUST_PAIR")
        location = self.cur_token_location_
        coverages_1 = []
        coverages_2 = []
        adjust_pair = {}
        while self.next_token_ == "FIRST":
            self.advance_lexer_()
            coverage_1 = self.parse_coverage_()
            coverages_1.append(coverage_1)
        while self.next_token_ == "SECOND":
            self.advance_lexer_()
            coverage_2 = self.parse_coverage_()
            coverages_2.append(coverage_2)
        while self.next_token_ != "END_ADJUST":
            id_1 = self.expect_number_()
            id_2 = self.expect_number_()
            self.expect_keyword_("BY")
            pos_1 = self.parse_pos_()
            pos_2 = self.parse_pos_()
            adjust_pair[(id_1, id_2)] = (pos_1, pos_2)
        self.expect_keyword_("END_ADJUST")
        position = ast.PositionAdjustPairDefinition(
            location, coverages_1, coverages_2, adjust_pair)
        return position

    def parse_adjust_single_(self):
        assert self.is_cur_keyword_("ADJUST_SINGLE")
        location = self.cur_token_location_
        adjust_single = []
        while self.next_token_ != "END_ADJUST":
            coverages = self.parse_coverage_()
            self.expect_keyword_("BY")
            pos = self.parse_pos_()
            adjust_single.append((coverages, pos))
        self.expect_keyword_("END_ADJUST")
        position = ast.PositionAdjustSingleDefinition(
            location, adjust_single)
        return position

    def parse_def_anchor_(self):
        assert self.is_cur_keyword_("DEF_ANCHOR")
        location = self.cur_token_location_
        name = self.expect_string_()
        self.expect_keyword_("ON")
        gid = self.expect_number_()
        self.expect_keyword_("GLYPH")
        glyph_name = self.expect_name_()
        # check for duplicate anchor names on this glyph
        if (glyph_name in self.anchors_
                and self.anchors_[glyph_name].resolve(name) is not None):
            raise VoltLibError(
                'Anchor "%s" already defined, '
                'anchor names are case insensitive' % name,
                location
            )
        self.expect_keyword_("COMPONENT")
        component = self.expect_number_()
        if self.next_token_ == "LOCKED":
            locked = True
            self.advance_lexer_()
        else:
            locked = False
        self.expect_keyword_("AT")
        pos = self.parse_pos_()
        self.expect_keyword_("END_ANCHOR")
        anchor = ast.AnchorDefinition(location, name, gid, glyph_name,
                                      component, locked, pos)
        if glyph_name not in self.anchors_:
            self.anchors_[glyph_name] = SymbolTable()
        self.anchors_[glyph_name].define(name, anchor)
        return anchor

    def parse_adjust_by_(self):
        self.advance_lexer_()
        assert self.is_cur_keyword_("ADJUST_BY")
        adjustment = self.expect_number_()
        self.expect_keyword_("AT")
        size = self.expect_number_()
        return adjustment, size

    def parse_pos_(self):
        # VOLT syntax doesn't seem to take device Y advance
        self.advance_lexer_()
        location = self.cur_token_location_
        assert self.is_cur_keyword_("POS"), location
        adv = None
        dx = None
        dy = None
        adv_adjust_by = {}
        dx_adjust_by = {}
        dy_adjust_by = {}
        if self.next_token_ == "ADV":
            self.advance_lexer_()
            adv = self.expect_number_()
            while self.next_token_ == "ADJUST_BY":
                adjustment, size = self.parse_adjust_by_()
                adv_adjust_by[size] = adjustment
        if self.next_token_ == "DX":
            self.advance_lexer_()
            dx = self.expect_number_()
            while self.next_token_ == "ADJUST_BY":
                adjustment, size = self.parse_adjust_by_()
                dx_adjust_by[size] = adjustment
        if self.next_token_ == "DY":
            self.advance_lexer_()
            dy = self.expect_number_()
            while self.next_token_ == "ADJUST_BY":
                adjustment, size = self.parse_adjust_by_()
                dy_adjust_by[size] = adjustment
        self.expect_keyword_("END_POS")
        return (adv, dx, dy, adv_adjust_by, dx_adjust_by, dy_adjust_by)

    def parse_unicode_values_(self):
        location = self.cur_token_location_
        try:
            unicode_values = self.expect_string_().split(",")
            unicode_values = [
                int(uni[2:], 16)
                for uni in unicode_values if uni != ""]
        except ValueError as err:
            raise VoltLibError(str(err), location)
        return unicode_values if unicode_values != [] else None

    def parse_enum_(self):
        assert self.is_cur_keyword_("ENUM")
        location = self.cur_token_location_
        enum = self.parse_coverage_()
        self.expect_keyword_("END_ENUM")
        return enum

    def parse_coverage_(self):
        coverage = []
        location = self.cur_token_location_
        while self.next_token_ in ("GLYPH", "GROUP", "RANGE", "ENUM"):
            if self.next_token_ == "ENUM":
                self.advance_lexer_()
                enum = self.parse_enum_()
                coverage.append(enum)
            elif self.next_token_ == "GLYPH":
                self.expect_keyword_("GLYPH")
                name = self.expect_string_()
                coverage.append(name)
            elif self.next_token_ == "GROUP":
                self.expect_keyword_("GROUP")
                name = self.expect_string_()
                # resolved_group = self.groups_.resolve(name)
                group = (name,)
                coverage.append(group)
                # if resolved_group is not None:
                #     coverage.extend(resolved_group.enum)
                # # TODO: check that group exists after all groups are defined
                # else:
                #     group = (name,)
                #     coverage.append(group)
                #     # raise VoltLibError(
                #     #     'Glyph group "%s" is not defined' % name,
                #     #     location)
            elif self.next_token_ == "RANGE":
                self.expect_keyword_("RANGE")
                start = self.expect_string_()
                self.expect_keyword_("TO")
                end = self.expect_string_()
                coverage.append((start, end))
        return tuple(coverage)

    def resolve_group(self, group_name):
        return self.groups_.resolve(group_name)

    def glyph_range(self, start, end):
        rng = self.glyphs_.range(start, end)
        return frozenset(rng)

    def parse_ppem_(self):
        location = self.cur_token_location_
        ppem_name = self.cur_token_
        value = self.expect_number_()
        setting = ast.SettingDefinition(location, ppem_name, value)
        return setting

    def parse_compiler_flag_(self):
        location = self.cur_token_location_
        flag_name = self.cur_token_
        value = True
        setting = ast.SettingDefinition(location, flag_name, value)
        return setting

    def parse_cmap_format(self):
        location = self.cur_token_location_
        name = self.cur_token_
        value = (self.expect_number_(), self.expect_number_(),
                 self.expect_number_())
        setting = ast.SettingDefinition(location, name, value)
        return setting

    def is_cur_keyword_(self, k):
        return (self.cur_token_type_ is Lexer.NAME) and (self.cur_token_ == k)

    def expect_string_(self):
        self.advance_lexer_()
        if self.cur_token_type_ is not Lexer.STRING:
            raise VoltLibError("Expected a string", self.cur_token_location_)
        return self.cur_token_

    def expect_keyword_(self, keyword):
        self.advance_lexer_()
        if self.cur_token_type_ is Lexer.NAME and self.cur_token_ == keyword:
            return self.cur_token_
        raise VoltLibError("Expected \"%s\"" % keyword,
                           self.cur_token_location_)

    def expect_name_(self):
        self.advance_lexer_()
        if self.cur_token_type_ is Lexer.NAME:
            return self.cur_token_
        raise VoltLibError("Expected a name", self.cur_token_location_)

    def expect_number_(self):
        self.advance_lexer_()
        if self.cur_token_type_ is not Lexer.NUMBER:
            raise VoltLibError("Expected a number", self.cur_token_location_)
        return self.cur_token_

    def advance_lexer_(self):
        self.cur_token_type_, self.cur_token_, self.cur_token_location_ = (
            self.next_token_type_, self.next_token_, self.next_token_location_)
        try:
            (self.next_token_type_, self.next_token_,
             self.next_token_location_) = self.lexer_.next()
        except StopIteration:
            self.next_token_type_, self.next_token_ = (None, None)

class SymbolTable(object):
    def __init__(self):
        self.scopes_ = [{}]

    def enter_scope(self):
        self.scopes_.append({})

    def exit_scope(self):
        self.scopes_.pop()

    def define(self, name, item):
        self.scopes_[-1][name] = item

    def resolve(self, name, case_insensitive=True):
        for scope in reversed(self.scopes_):
            item = scope.get(name)
            if item:
                return item
        if case_insensitive:
            for key in scope:
                if key.lower() == name.lower():
                    return scope[key]
        return None

class OrderedSymbolTable(SymbolTable):
    def __init__(self):
        self.scopes_ = [OrderedDict()]

    def enter_scope(self):
        self.scopes_.append(OrderedDict())

    def resolve(self, name, case_insensitive=False):
        SymbolTable.resolve(self, name, case_insensitive=case_insensitive)

    def range(self, start, end):
        for scope in reversed(self.scopes_):
            if start in scope and end in scope:
                start_idx = list(scope.keys()).index(start)
                end_idx = list(scope.keys()).index(end)
                return list(scope.keys())[start_idx:end_idx + 1]
        return None


text:
namespace Microsoft.Skype.UCWA.Enums
{
    /// <summary>
    /// Represents when call forwarding settings are active.
    /// </summary>
    public enum ActivePeriod
    {
        /// <summary>
        /// Call forwarding settings are always active.
        /// </summary>
        Always,
        /// <summary>
        /// Call forwarding settings are active only during workhours.
        /// </summary>
        Workhours
    }
}


text:
package org.jboss.weld.tests.decorators.ordering.global;

import jakarta.decorator.Decorator;

@Decorator
public class LegacyDecorator1 extends AbstractDecorator {
}


text:
package org.apache.ignite.internal.processors.cache;

import java.util.Collections;
import java.util.concurrent.atomic.AtomicReference;
import javax.cache.CacheException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.CachePartialUpdateException;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.cluster.ClusterTopologyCheckedException;
import org.apache.ignite.internal.util.lang.GridAbsPredicate;
import org.apache.ignite.internal.util.typedef.CA;
import org.apache.ignite.internal.util.typedef.CIX1;
import org.apache.ignite.internal.util.typedef.G;
import org.apache.ignite.internal.util.typedef.X;
import org.apache.ignite.internal.util.typedef.internal.CU;
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.testframework.GridTestUtils;
import org.apache.ignite.transactions.TransactionConcurrency;
import org.apache.ignite.transactions.TransactionIsolation;
import org.jetbrains.annotations.Nullable;

import static org.apache.ignite.cache.CacheRebalanceMode.SYNC;

/**
 * Failover tests for cache.
 */
public abstract class GridCacheAbstractFailoverSelfTest extends GridCacheAbstractSelfTest {
    /** */
    private static final long TEST_TIMEOUT = 3 * 60 * 1000;

    /** */
    private static final String NEW_IGNITE_INSTANCE_NAME = "newGrid";

    /** */
    private static final int ENTRY_CNT = 100;

    /** */
    private static final int TOP_CHANGE_CNT = 10;

    /** */
    private static final int TOP_CHANGE_THREAD_CNT = 3;

    /** {@inheritDoc} */
    @Override protected long getTestTimeout() {
        return TEST_TIMEOUT;
    }

    /** {@inheritDoc} */
    @Override protected int gridCount() {
        return 3;
    }

    /** {@inheritDoc} */
    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);

        cfg.setNetworkTimeout(60_000);
        cfg.setMetricsUpdateFrequency(5_000);

        TcpDiscoverySpi discoSpi = (TcpDiscoverySpi)cfg.getDiscoverySpi();

        discoSpi.setSocketTimeout(30_000);
        discoSpi.setAckTimeout(30_000);
        discoSpi.setNetworkTimeout(60_000);
        discoSpi.setReconnectCount(2);

        ((TcpCommunicationSpi)cfg.getCommunicationSpi()).setSharedMemoryPort(-1);

        return cfg;
    }

    /** {@inheritDoc} */
    @Override protected CacheConfiguration cacheConfiguration(String igniteInstanceName) throws Exception {
        CacheConfiguration cfg = super.cacheConfiguration(igniteInstanceName);

        cfg.setRebalanceMode(SYNC);

        if (cfg.getCacheMode() == CacheMode.PARTITIONED)
            cfg.setBackups(TOP_CHANGE_THREAD_CNT);

        return cfg;
    }

    /** {@inheritDoc} */
    @Override protected void beforeTestsStarted() throws Exception {
        // No-op
    }

    /** {@inheritDoc} */
    @Override protected void afterTestsStopped() throws Exception {
        // No-op
    }

    /** {@inheritDoc} */
    @Override protected void beforeTest() throws Exception {
        startGridsMultiThreaded(gridCount());

        super.beforeTest();
    }

    /** {@inheritDoc} */
    @Override protected void afterTest() throws Exception {
        stopAllGrids();
    }

    /**
     * @throws Exception If failed.
     */
    public void testTopologyChange() throws Exception {
        testTopologyChange(null, null);
    }

    /**
     * @throws Exception If failed.
     */
    public void testConstantTopologyChange() throws Exception {
        testConstantTopologyChange(null, null);
    }

    /**
     * @param concurrency Concurrency control.
     * @param isolation Isolation level.
     * @throws Exception If failed.
     */
    protected void testTopologyChange(@Nullable TransactionConcurrency concurrency,
        @Nullable TransactionIsolation isolation) throws Exception {
        boolean tx = concurrency != null && isolation != null;

        if (tx)
            put(ignite(0), jcache(), ENTRY_CNT, concurrency, isolation);
        else
            put(jcache(), ENTRY_CNT);

        Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME);

        check(cache(g), ENTRY_CNT);

        int half = ENTRY_CNT / 2;

        if (tx) {
            remove(g, cache(g), half, concurrency, isolation);
            put(g, cache(g), half, concurrency, isolation);
        }
        else {
            remove(cache(g), half);
            put(cache(g), half);
        }

        stopGrid(NEW_IGNITE_INSTANCE_NAME);

        check(jcache(), ENTRY_CNT);
    }

    /**
     * @param concurrency Concurrency control.
     * @param isolation Isolation level.
     * @throws Exception If failed.
     */
    protected void testConstantTopologyChange(@Nullable final TransactionConcurrency concurrency,
        @Nullable final TransactionIsolation isolation) throws Exception {
        final boolean tx = concurrency != null && isolation != null;

        if (tx)
            put(ignite(0), jcache(), ENTRY_CNT, concurrency, isolation);
        else
            put(jcache(), ENTRY_CNT);

        check(jcache(), ENTRY_CNT);

        final int half = ENTRY_CNT / 2;

        final AtomicReference<Exception> err = new AtomicReference<>();

        IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() {
            @Override public void apply() {
                info("Run topology change.");

                try {
                    String name = "new-node-" + Thread.currentThread().getName();

                    for (int i = 0; i < TOP_CHANGE_CNT && err.get() == null; i++) {
                        info("Topology change " + i);

                        try {
                            final Ignite g = startGrid(name);

                            IgniteCache<String, Object> cache = g.cache(DEFAULT_CACHE_NAME);

                            for (int k = half; k < ENTRY_CNT; k++) {
                                String key = "key" + k;

                                assertNotNull("Failed to get key: 'key" + k + "'", cache.getAsync(key).get(30_000));
                            }
                        }
                        finally {
                            G.stop(name, false);
                        }
                    }
                }
                catch (Exception e) {
                    err.set(e);

                    log.error("Unexpected exception in topology-change-thread: " + e, e);
                }
            }
        }, TOP_CHANGE_THREAD_CNT, "topology-change-thread");

        try {
            while (!fut.isDone()) {
                if (tx) {
                    remove(grid(0), jcache(), half, concurrency, isolation);
                    put(grid(0), jcache(), half, concurrency, isolation);
                }
                else {
                    remove(jcache(), half);
                    put(jcache(), half);
                }
            }
        }
        catch (Exception e) {
            err.set(e);

            log.error("Unexpected exception: " + e, e);

            throw e;
        }

        fut.get();

        Exception err0 = err.get();

        if (err0 != null)
            throw err0;
    }

    /**
     * @param cache Cache.
     * @param cnt Entry count.
     * @throws IgniteCheckedException If failed.
     */
    private void put(IgniteCache<String, Integer> cache, int cnt) throws Exception {
        try {
            for (int i = 0; i < cnt; i++)
                cache.put("key" + i, i);
        }
        catch (CacheException e) {
            if (!X.hasCause(e, ClusterTopologyCheckedException.class) && !(e instanceof CachePartialUpdateException))
                throw e;
        }
    }

    /**
     * @param ignite Ignite.
     * @param cache Cache.
     * @param cnt Entry count.
     * @param concurrency Concurrency control.
     * @param isolation Isolation level.
     * @throws IgniteCheckedException If failed.
     */
    private void put(Ignite ignite,
        IgniteCache<String, Integer> cache,
        final int cnt,
        TransactionConcurrency concurrency,
        TransactionIsolation isolation)
        throws Exception
    {
        try {
            info("Putting values to cache [0," + cnt + ')');

            CU.inTx(ignite, cache, concurrency, isolation, new CIX1<IgniteCache<String, Integer>>() {
                @Override public void applyx(IgniteCache<String, Integer> cache) {
                    for (int i = 0; i < cnt; i++)
                        cache.put("key" + i, i);
                }
            });
        }
        catch (Exception e) {
            // It is ok to fail with topology exception.
            if (!X.hasCause(e, ClusterTopologyCheckedException.class))
                throw e;
            else
                info("Failed to put values to cache due to topology exception [0," + cnt + ')');
        }
    }

    /**
     * @param cache Cache.
     * @param cnt Entry count.
     * @throws IgniteCheckedException If failed.
     */
    private void remove(IgniteCache<String, Integer> cache, int cnt) throws Exception {
        try {
            for (int i = 0; i < cnt; i++)
                cache.remove("key" + i);
        }
        catch (CacheException e) {
            if (!X.hasCause(e, ClusterTopologyCheckedException.class) && !(e instanceof CachePartialUpdateException))
                throw e;
        }
    }

    /**
     * @param ignite Ignite.
     * @param cache Cache.
     * @param cnt Entry count.
     * @param concurrency Concurrency control.
     * @param isolation Isolation level.
     * @throws IgniteCheckedException If failed.
     */
    private void remove(Ignite ignite, IgniteCache<String, Integer> cache, final int cnt,
        TransactionConcurrency concurrency, final TransactionIsolation isolation) throws Exception {
        try {
            info("Removing values form cache [0," + cnt + ')');

            CU.inTx(ignite, cache, concurrency, isolation, new CIX1<IgniteCache<String, Integer>>() {
                @Override public void applyx(IgniteCache<String, Integer> cache) {
                    for (int i = 0; i < cnt; i++) {
                        String key = "key" + i;

                        // Use removeAll for serializable tx to avoid version check.
                        if (isolation == TransactionIsolation.SERIALIZABLE)
                            cache.removeAll(Collections.singleton(key));
                        else
                            cache.remove(key);
                    }
                }
            });
        }
        catch (Exception e) {
            // It is ok to fail with topology exception.
            if (!X.hasCause(e, ClusterTopologyCheckedException.class))
                throw e;
            else
                info("Failed to remove values from cache due to topology exception [0," + cnt + ')');
        }
    }

    /**
     * @param cache Cache.
     * @param expSize Minimum expected cache size.
     * @throws Exception If failed.
     */
    private void check(final IgniteCache<String, Integer> cache, final int expSize) throws Exception {
        GridTestUtils.waitForCondition(new GridAbsPredicate() {
            @Override public boolean apply() {
                return cache.size() >= expSize;
            }
        }, 5000);

        int size = cache.size();

        assertTrue("Key set size is lesser then the expected size [size=" + size + ", expSize=" + expSize + ']',
            size >= expSize);

        for (int i = 0; i < expSize; i++)
            assertNotNull("Failed to get value for key: 'key" + i + "'", cache.get("key" + i));
    }

    /**
     * @param g Grid.
     * @return Cache.
     */
    private IgniteCache<String,Integer> cache(Ignite g) {
        return g.cache(DEFAULT_CACHE_NAME);
    }
}


text:
module Pipes.Difference (SequenceDifference(..), diff) where

import           Pipes

data SequenceDifference a b = LeftOnly a
                            | RightOnly b
                            | Common a b

diff :: (Monad m)
     => (a -> b -> Ordering) -- ^ ordering on elements
     -> Producer a m ()      -- ^ source producer of elements
     -> Producer b m ()      -- ^ compare to producer of elements
     -> Producer (SequenceDifference a b) m ()
diff compare' = go
  where
    consumeLeft p = lift (next p) >>= either return
                                        (\(a, p') -> yield (LeftOnly a) >> consumeLeft p')
    consumeRight p = lift (next p) >>= either return
                                         (\(a, p') -> yield (RightOnly a) >> consumeRight p')
    go l r = lift (next l) >>= either (\_ -> consumeRight r) (goRight r)
    goLeft l (rval, r) = lift (next l) >>= either (\_ -> yield (RightOnly rval) >> consumeRight r)
                                             (\(lval, l') -> emit lval rval l' r)
    goRight r (lval, l) = lift (next r) >>= either (\_ -> yield (LeftOnly lval) >> consumeLeft l)
                                              (\(rval, r') -> emit lval rval l r')
    emit lval rval l r =
      case compare' lval rval of
        LT -> yield (LeftOnly lval) >> goLeft l (rval, r)
        GT -> yield (RightOnly rval) >> goRight r (lval, l)
        EQ -> yield (Common lval rval) >> go l r


text:
layout: post
title: March Tweets
categories: [social]
tags: [social, tweets]
---

> 'We are in the "oops" phase, let's act quickly before being in the "fuck!" one!
>
> ![]({{ site.url }}/media/1103757447964094467-D1FVihDXcAckalT.jpg)

 [Thu Mar 07 20:40:57 +0000 2019](https://twitter.com/bruncedric/status/1103757447964094467)

----

Replying to [@svenefftinge, @gatsbyjs and @gitpodio](https://twitter.com/svenefftinge/status/1103748654744117248)

> Nice! Any @gitpodio enabled git repo I could start from in one click ? ;-)

 [Thu Mar 07 20:48:07 +0000 2019](https://twitter.com/bruncedric/status/1103759253007986688)

----

> RT @Obeo_Fr: Palmarès des éditeurs de logiciels dans le Grand Ouest par @EYFrance @adnouest et @syntecnumerique : Obeo listé dans le top 25…

 [Tue Mar 12 22:15:32 +0000 2019](https://twitter.com/bruncedric/status/1105593191905017857)

----

> RT @CoMarchand: Cherche désespérément un texte (discours, lettre,...) d'un écrivain ou philosophe qui proposerait un plaidoyer pour la prot…

 [Thu Mar 14 00:10:51 +0000 2019](https://twitter.com/bruncedric/status/1105984601699291136)

----

Replying to [@jfbrazeau and @Obeo_Fr](https://twitter.com/jfbrazeau/status/1107020084109807616)

> 😨😨😨😨😨😨😨

 [Sat Mar 16 21:01:10 +0000 2019](https://twitter.com/bruncedric/status/1107024029729333249)

----

> I'll have the honor next Monday to share a panel with the Canadian Ambassador in France, I'm so excited and looking forward to this moment! https://twitter.com/Obeo_Fr/status/1110480833532649472

 [Wed Mar 27 09:01:51 +0000 2019](https://twitter.com/bruncedric/status/1110829271268974599)

----

> Well, I knew it had to happen at some point, following @MaudiMm 's advice I ended up "investing" in a tie!
>
> ![]({{ site.url }}/media/1111678048095690754-D215SNDX4AEXCYx.jpg)

 [Fri Mar 29 17:14:35 +0000 2019](https://twitter.com/bruncedric/status/1111678048095690754)

----

Replying to [@emmaperaldi and @MaudiMm](https://twitter.com/emmaperaldi/status/1111691182483083266)

> Il aura fallu une rencontre avec la représentante directe du gouvernement d'une des plus grande nation de ce monde donc c'est à la hauteur, ça va ;-)

 [Fri Mar 29 18:44:59 +0000 2019](https://twitter.com/bruncedric/status/1111700800433278976)

----

Replying to [@glefur_, @sliard and @MaudiMm](https://twitter.com/glefur_/status/1111945972295389184)

> Si vous saviez les macro de ouf que j'ai déjà fais pour faire des calculs avec incertitude.... :-D

 [Sat Mar 30 11:05:24 +0000 2019](https://twitter.com/bruncedric/status/1111947528222752768)

----

Replying to [@glefur_, @sliard and @MaudiMm](https://twitter.com/glefur_/status/1111945972295389184)

> Il y'a un petit challenge supplémentaire a faire des macro qui passent aussi bien dans Excel que Libre Office, on rentre vite dans des différences de fonctionnement internes et assez profondes... 😈

 [Sat Mar 30 13:42:26 +0000 2019](https://twitter.com/bruncedric/status/1111987047558979584)

----

Replying to [@glefur_, @sliard and @MaudiMm](https://twitter.com/bruncedric/status/1111987047558979584)

> Thread on the crazy macros I'm already guilty of: https://twitter.com/bruncedric/status/885431120204005376

 [Sat Mar 30 13:52:28 +0000 2019](https://twitter.com/bruncedric/status/1111989570860736512)

----

Replying to [@LibreOfficeFR](https://twitter.com/LibreOfficeFR/status/1112039431240892418)

> C'est la version pour google sheet celle -là, j'ai la version VBA sous le coude ;-)

 [Sat Mar 30 17:13:40 +0000 2019](https://twitter.com/bruncedric/status/1112040204280434688)

----

Replying to [@LibreOfficeFR](https://twitter.com/LibreOfficeFR/status/1112051081918537730)

> Ah non, ce serait insupportable, une fois, il y'a un an, et je suis encore en train de m'en remettre !

 [Sat Mar 30 18:19:15 +0000 2019](https://twitter.com/bruncedric/status/1112056711223934982)


text:
"""
:author: Konrad Podlawski <kpodlawski@kretstudios.pl>
"""
from bot import Bot
from chatterbot.conversation import Statement
from unlearn_adapter import UnlearnAdapter
from response_selection import get_weighted_random_response
import logging

class Brain(object):
    def __init__(self, name="Olga", storage_adapter="chatterbot.storage.JsonFileStorageAdapter", db="./database.json"):
        self._chatbot = Bot(
            name=name,
            storage_adapter=storage_adapter,
            database=db,
            logic_adapters=[
                {
                    "import_path": 'chatterbot.logic.BestMatch',
                    "response_selection_method": get_weighted_random_response,
                    #"statement_comparison_function": 'chatterbot.comparisons.synset_distance'
                },
            ],
            filters=["chatterbot.filters.RepetitiveResponseFilter"],
            input_adapter='chatterbot.input.TerminalAdapter',  # Change this to voice
            output_adapter='chatterbot.output.TerminalAdapter'  # Change this to voice
        )

    def process_input(self):
        return self._chatbot.input.process_input_statement()

    def get_response(self, input_statement):
        input_statement = Statement(input_statement)
        previous_statement = self._chatbot.conversation_sessions.get(
            self._chatbot.default_session.uuid
        ).conversation.get_last_response_statement()
        previous_input = self._chatbot.conversation_sessions.get(
            self._chatbot.default_session.uuid
        ).conversation.get_last_input_statement()
        self._chatbot.learn_response(input_statement, previous_statement)
        statement, response, confidence = self._chatbot.generate_response(input_statement)
        if confidence > 0.3:
            self._chatbot.learn_response(response, input_statement)
            self._chatbot.conversation_sessions.update_default(
                (statement, response,)
            )
            return self._chatbot.output.process_response(response, confidence)
        else:
            return self._chatbot.output.process_response(self.ask_for_help(statement, response, confidence), 1.0)

    def ask_for_help(self, statement, response, confidence):
        print(u'\nThe closest matching reply to "{}" I could come up with is '
              u'"{}".\nWhat would be a better response?'.format(
            statement, response
        ).encode('utf-8'))
        new_response = self._chatbot.input.process_input_statement()
        self._chatbot.learn_response(new_response, statement)
        self._chatbot.conversation_sessions.update_default(
            (statement, new_response,)
        )
        return new_response

if __name__ == '__main__':
    brain = Brain()
    while True:
        brain.get_response(brain.process_input())