<commit_before><commit_msg>A class that starts and passes commands to xflux<commit_after>import pexpect
import sys
import time

class XfluxController(object):

    def __init__(self, color='3400', pause_color='6500', **kwargs):
        if 'zipcode' not in kwargs and 'latitude' not in kwargs:
            # one of these should be passed in, error otherwise
            raise Exception("Required key not found (either zipcode or latitude)")
        args=self._create_startup_arg_list(color,**kwargs)
        self.state='INIT' # TODO: replace state with better design
        self._start(args)
        self.state='RUNNING'
        self._current_color=str(color)
        self._pause_color=str(pause_color)

    def __del__(self):
        if self.state not in ["INIT","TERMINATED"]:
            self._stop()

    def set_xflux_latitude(self,latitude):
        self._xflux.sendline("l="+str(latitude))

    def set_xflux_longitude(self,longitude):
        self._xflux.sendline("g="+str(longitude))

    def set_xflux_zipcode(self,zipcode):
        self._xflux.sendline("z="+str(zipcode))

    def preview_color(self,preview_color):
        # could probably be implemented better

        preview_color=str(preview_color)
        self._set_xflux_screen_color(preview_color)
        self._c()
        while self.color != preview_color:
            time.sleep(.5)
        time.sleep(2)
        if self.state=="RUNNING":
            self._set_xflux_screen_color(self._current_color)
        elif self.state=="PAUSED":
            self._set_xflux_screen_color(self._pause_color)
        self._c()

    def toggle_pause(self):
        if self.state=='RUNNING':
            self.state='PAUSED'
            self._set_xflux_screen_color(self._pause_color)
            self._c()
        elif self.state=='PAUSED':
            self.state='RUNNING'
            self._set_xflux_screen_color(self._current_color)
            self._c()

    def _get_xflux_color(self):
        self._c()
        index = self._xflux.expect("Color.*")
        color=-1
        if index == 0:
            color = self._xflux.after[10:14]
        return color

    def _set_xflux_color(self,color):
        self._set_xflux_screen_color(color)
        self._current_color=str(color)
        if self.state=="PAUSED":
            self.state=="RUNNING"

    color=property(_get_xflux_color,_set_xflux_color)



    def _create_startup_arg_list(self, color='3400', **kwargs):
        startup_args=[]
        if "zipcode" in kwargs:
            startup_args+=["-z",str(kwargs["zipcode"])]
        if "latitude" in kwargs:
            # by default xflux seems to use latitude even if zipcode is given
            startup_args+=["-l",str(kwargs["latitude"])]
            if "longitude" in kwargs:
                startup_args+=["-g",str(kwargs["longitude"])]
        startup_args+=["-k",str(color),"-nofork"] # nofork is vital

        return startup_args

    def _start(self,startup_args):
        if self.state=="RUNNING":
            raise Exception("xflux is already running.")
        try:
            self._xflux = pexpect.spawn("/usr/bin/xflux", startup_args,\
                    logfile=file("tmp/xfluxout.txt",'w'))

            # TODO: remove xflux logging
            self._xflux.logfile_read=file("tmp/xfluxout_read.txt","w")
            self._xflux.logfile_send=file("tmp/xfluxout_send.txt","w")

        except pexpect.ExceptionPexpect:
            print "\nError: Please install xflux in /usr/bin/ \n"

            sys.exit(2)

    def _stop(self):
        try:
            if self._xflux.terminate(force=True):
                self.state="TERMINATED"
                return True
            else:
                return False
        except Exception, e:
            # xflux has crashed in the meantime?
            return True

    def _p(self):
        # seems to bring color up to "off" then transitions back down
        # not sure what this is supposed to be used for
        self._xflux.sendline("p")

    def _c(self):
        # prints Colortemp=####
        # Also: When called after a color change (sendline(k=#)) makes changes immediate
        #   (see use in toggle_pause() and preview_color())
        self._xflux.sendline("c")

    def _set_xflux_screen_color(self,color):
        # use _set_xflux_color unless keeping self._current_color the same is necessary
        self._xflux.sendline("k="+str(color))


{'fraction_non_alphanumeric': 0.11250284673195173, 'fraction_numerical': 0.004782509678888636, 'mean_word_length': 2.3680981595092025, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5807144', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1321, 'n_words': 282}