def validate_worker_host(self, worker_name, caps_type=None, required_caps=None, current_caps=None): """ Checks if current host (with current caps) is acceptable for the job. Caps type can be a function that returns caps object. Caps object should be comparable and str-able. Minimum required caps can be specified. """ import socket current = WorkerStats(socket.gethostname(), current_caps, logger=self._logger) self._logger.debug('Current stats: {0}'.format(current)) from clckwrkbdgr import xdg worker_host_file = xdg.save_state_path( 'dailyupdate') / '{0}.worker'.format(worker_name) stored = WorkerStats.load(worker_host_file, caps_type=caps_type, logger=self._logger) self._logger.debug('Stored stats: {0}'.format(stored)) if not current.is_preferred(stored, required_caps): self._logger.debug('Current stats are not acceptable.') return False self._logger.debug('Current stats are acceptable.') current.save(worker_host_file) return True
def setUp(self): self.setUpPyfakefs(modules_to_reload=[clckwrkbdgr.taskwarrior, clckwrkbdgr.taskwarrior._base]) taskwarrior_dir = xdg.save_data_path('taskwarrior') if not taskwarrior_dir.exists(): self.fs.create_dir(str(taskwarrior_dir)) taskwarrior_state_dir = xdg.save_state_path('taskwarrior') if not taskwarrior_state_dir.exists(): self.fs.create_dir(six.text_type(taskwarrior_state_dir))
def rewrite_history(self, start_datetime, stop_datetime, new_titles, fill_value=None): """ Rewrites part of task history in given time frame (including boundaries). Replaces all tasks within time frame with corresponding title from given list. If title is None, a stop is created instead. Number of titles should match number of actual tasks in the time frame, otherwise RuntimeError is raised. Unless fill_value is specified, which is used in case when there were not enough titles to cover the time frame. Creates backup version in XDG_STATE_HOME/taskwarrior/taskfile.bak """ new_taskfile = tempfile.NamedTemporaryFile(mode='w', delete=False) new_titles_count = 0 try: with new_taskfile as fobj: for entry_datetime, entry_title in self._parse_history( self.config.taskfile, self.config.separator): if start_datetime <= entry_datetime <= stop_datetime: if not new_titles: if fill_value: entry_title = fill_value else: raise RuntimeError( 'Not enough titles specified for the time frame {0}..{1}: got only {2}' .format( start_datetime, stop_datetime, new_titles_count, )) else: entry_title = new_titles.pop(0) new_titles_count += 1 if entry_title: line = '{0}{2}{1}\n'.format(entry_datetime.isoformat(), entry_title, self.config.separator) else: line = '{0}\n'.format(entry_datetime.isoformat()) try: fobj.write(line) except UnicodeError: # pragma: no cover fobj.write(line.encode('ascii', 'replace').decode()) if new_titles: raise RuntimeError( '{0} unused titles left after replacing entries in the time frame {1}..{2}' .format(len(new_titles), start_datetime, stop_datetime)) shutil.copy( str(self.config.taskfile), str(xdg.save_state_path('taskwarrior') / 'taskfile.bak')) shutil.move(str(new_taskfile.name), str(self.config.taskfile)) finally: if os.path.exists(str(new_taskfile.name)): os.unlink(str(new_taskfile.name))
def __init__(self, _providers=None): force_load_task_providers(providers=_providers) self._filename = xdg.save_state_path('todo') / 'tasklist.lst'
from __future__ import print_function, unicode_literals import os, sys, re try: from pathlib2 import Path except ImportError: # pragma: no cover from pathlib import Path import clckwrkbdgr.xdg as xdg userstate_dir = xdg.save_state_path("userstate") known_userstate_markers = [] def read_config_file(configfile): # pragma: no cover -- TODO reads FS """ Config file is a plain-test file with markers defined on separate lines. Marker should be a correct C identificator: - only letters, digits and underscore; - cannot start with digit. Comments (starting with #) are supported: first_marker # Comment. second_marker # Inlined comment. Returns number of successfully read markers. """ if not configfile.is_file(): return 0 id_pattern = re.compile("^[a-zA-Z_][a-zA-Z_0-9]*$") valid_markers = 0 for line in configfile.read_text().splitlines(): if not line.strip(): continue
import functools from collections import namedtuple import clckwrkbdgr.collections from clckwrkbdgr.collections import dotdict from clckwrkbdgr.utils import get_type_by_name, classfield from clckwrkbdgr.math import Point, Size, Rect, Matrix import clckwrkbdgr.math import clckwrkbdgr.logging from clckwrkbdgr import xdg trace = clckwrkbdgr.logging.getFileLogger('rogue', xdg.save_state_path('dotrogue')/'rogue.log') def is_diagonal_movement(point_from, point_to): shift = abs(point_to - point_from) return shift.x + shift.y > 1 class Version(clckwrkbdgr.collections.Enum): auto = clckwrkbdgr.collections.Enum.auto() INITIAL = auto() ENTER_EXIT = auto() LEVELS = auto() ITEMS = auto() INVENTORY = auto() MONSTERS = auto() HITPOINTS = auto() ITEM_CLASSES = auto() WIELDING = auto() WEARING = auto() JSONPICKLE = auto() class Event(object): @classmethod
def should_create_subdir_for_state_path(self, mkdir_mock): self.assertEqual(xdg.save_state_path('test_xdg'), xdg.XDG_STATE_HOME / 'test_xdg') mkdir_mock.assert_called_once_with(parents=True, exist_ok=True)
#!/usr/bin/env python import os, sys, subprocess import platform from clckwrkbdgr import xdg, fs import clckwrkbdgr.jobsequence.context context = clckwrkbdgr.jobsequence.context.init( working_dir=xdg.XDG_CONFIG_HOME / 'lib', ) lock = fs.CrossHostFSMutex( xdg.save_state_path('dailyupdate') / 'dotfiles_lib_unittest.lock') lock.wait_lock(timeout=60) with lock: context | subprocess.call(['unittest'] + context.quiet_arg("-q"), shell=(platform.system() == 'Windows')) context.done()