def __init__(self, path): self._path = path self._unread = [] self.unsent = [] mkdir(os.path.dirname(path)) self.connection = None self.socket = None self._created_at = 0 self.reset()
def migrate_path(): """Migrate the folder structure from < 0.4.0. Move existing presets into the new subfolder "presets" """ new_preset_folder = os.path.join(CONFIG_PATH, 'presets') if not os.path.exists(get_preset_path()) and os.path.exists(CONFIG_PATH): logger.info('Migrating presets from < 0.4.0...') devices = os.listdir(CONFIG_PATH) mkdir(get_preset_path()) for device in devices: path = os.path.join(CONFIG_PATH, device) if os.path.isdir(path): target = path.replace(CONFIG_PATH, new_preset_folder) logger.info('Moving "%s" to "%s"', path, target) os.rename(path, target) logger.info('done')
def get_presets(device): """Get all presets for the device and user, starting with the newest. Parameters ---------- device : string """ device_folder = get_preset_path(device) mkdir(device_folder) paths = glob.glob(os.path.join(device_folder, '*.json')) presets = [ os.path.splitext(os.path.basename(path))[0] for path in sorted(paths, key=os.path.getmtime) ] # the highest timestamp to the front presets.reverse() return presets
def test_doesnt_migrate(self): if os.path.exists(tmp): shutil.rmtree(tmp) touch(os.path.join(tmp, 'foo1', 'bar1.json')) touch(os.path.join(tmp, 'foo2', 'bar2.json')) # already migrated mkdir(os.path.join(tmp, 'presets')) migrate_path() self.assertTrue(os.path.exists(os.path.join(tmp, 'foo1', 'bar1.json'))) self.assertTrue(os.path.exists(os.path.join(tmp, 'foo2', 'bar2.json'))) self.assertFalse( os.path.exists(os.path.join(tmp, 'presets', 'foo1', 'bar1.json'))) self.assertFalse( os.path.exists(os.path.join(tmp, 'presets', 'foo2', 'bar2.json')))
def __init__(self, path): """Create a pipe, or open it if it already exists.""" self._path = path self._unread = [] self._created_at = time.time() paths = (f'{path}r', f'{path}w') mkdir(os.path.dirname(path)) if not os.path.exists(paths[0]): logger.spam('Creating new pipe for "%s"', path) # The fd the link points to is closed, or none ever existed # If there is a link, remove it. if os.path.islink(paths[0]): os.remove(paths[0]) if os.path.islink(paths[1]): os.remove(paths[1]) self._fds = os.pipe() fds_dir = f'/proc/{os.getpid()}/fd/' chown(f'{fds_dir}{self._fds[0]}') chown(f'{fds_dir}{self._fds[1]}') # to make it accessible by path constants, create symlinks os.symlink(f'{fds_dir}{self._fds[0]}', paths[0]) os.symlink(f'{fds_dir}{self._fds[1]}', paths[1]) else: logger.spam('Using existing pipe for "%s"', path) # thanks to os.O_NONBLOCK, readline will return b'' when there # is nothing to read self._fds = (os.open(paths[0], os.O_RDONLY | os.O_NONBLOCK), os.open(paths[1], os.O_WRONLY | os.O_NONBLOCK)) self._handles = (open(self._fds[0], 'r'), open(self._fds[1], 'w'))
def setUp(self): self.grab = evdev.InputDevice.grab self.daemon = None mkdir(get_config_path()) config.save_config()
def test_mkdir(self): mkdir('/tmp/b/c/d/e') self.assertTrue(os.path.exists('/tmp/b/c/d/e')) self.assertTrue(os.path.isdir('/tmp/b/c/d/e'))