예제 #1
0
def create_symbolic_links():
    """Create symbolic links for files and dirs, following what's stored on the config file."""
    dot_files_dir = read_config(
        'dirs', 'dotfiles', os.path.realpath(os.path.join(os.path.dirname(__file__), '../dotfiles')))
    if not os.path.exists(dot_files_dir):
        LOGGER.warning("The directory '%s' does not exist", dot_files_dir)
        return
    LOGGER.info("Directory with dot files: '%s'", dot_files_dir)

    LOGGER.info("Creating links for files in [%s]", SECTION_SYMLINKS_FILES)
    links = {}
    cut = len(dot_files_dir) + 1
    for root, _, files in os.walk(dot_files_dir):
        for one_file in files:
            source_file = os.path.join(root, one_file)
            key = source_file[cut:]
            raw_link_name = read_config(SECTION_SYMLINKS_FILES, key, '')
            links[key] = (source_file, raw_link_name)
    # http://stackoverflow.com/questions/9001509/how-can-i-sort-a-python-dictionary-sort-by-key/13990710#13990710
    for key in sorted(links):
        (source_file, raw_link_name) = links[key]
        create_link(key, source_file, raw_link_name, False)

    LOGGER.info("Creating links for dirs in [%s]", SECTION_SYMLINKS_DIRS)
    if CONFIG.has_section(SECTION_SYMLINKS_DIRS):
        for key in CONFIG.options(SECTION_SYMLINKS_DIRS):
            raw_link_name = read_config(SECTION_SYMLINKS_DIRS, key, '')
            create_link(key, key, raw_link_name, True)

    save_config()
예제 #2
0
def window_monitor(save_logs=True):
    """Loop to monitor open windows of the selected applications.

    An app can have multiple windows, each one with its title.

    :param save_logs: True to save logs (default), False to only display what would be saved (dry run).
    :return:
    """
    last = {}
    monitor_start_time = datetime.now()
    LOGGER.info('Starting the window monitor now (%s)...', monitor_start_time.strftime(TIME_FORMAT))
    if not save_logs:
        LOGGER.error('Not saving logs to the database')
    try:
        while True:
            sleep(.2)

            for app, new_titles in list_windows().items():
                assert isinstance(app, str)
                assert isinstance(new_titles, list)

                if app not in last.keys():
                    last[app] = defaultdict(tuple)

                for index, new_title in enumerate(new_titles):
                    if last[app][index] and last[app][index][1] == new_title:
                        continue

                    last_info = last[app][index]
                    # Time since last saved time, or since the beginning of the monitoring
                    start_time = last_info[0] if last_info else monitor_start_time
                    end_time = datetime.now()
                    # Save time info for the next change of window title
                    last[app][index] = (end_time, new_title)

                    # Save logs only after the first change of title
                    old_title = last_info[1] if last_info else ''
                    if old_title:
                        try:
                            video = SESSION_INSTANCE.query(Video).filter(Video.path == old_title).one()
                            video_id = video.video_id
                        except NoResultFound:
                            video_id = None

                        window_log = WindowLog(start_dt=start_time, end_dt=end_time, app_name=app,
                                               title=old_title, video_id=video_id)
                        LOGGER.info(window_log)
                        if save_logs:
                            SESSION_INSTANCE.add(window_log)
                            SESSION_INSTANCE.commit()

                    if new_title:
                        LOGGER.warning("%s Open window in %s: %s", end_time.strftime(TIME_FORMAT), app, new_title)
    except KeyboardInterrupt:
        return