def installVim(args): print("Checking for Vim directory") vimDir = PosixPath("~/.vim").expanduser() if not vimDir.exists(): print("Vim not found, need to install...") else: print("Vim exists, continuing with process") print("Checking for Vundle") vundleDir = PosixPath("~/.vim/bundle/Vundle.vim").expanduser() if not vundleDir.exists(): print("Vundle not found, cloning...") os.system( 'git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim' ) else: print("Vundle Found! Continuing with install...") print("Insalling OhMyZsh") installZsh(args=args) print("Installing Zsh Syntax Highlighting") installZshSyntaxHighlighting(args=args) print("Overwriting vimrc") copyfile(PosixPath("./shared/vimrc"), PosixPath("~/.vimrc").expanduser()) print("Overwriting input") copyfile(PosixPath("./shared/inputrc"), PosixPath("~/.inputrc").expanduser()) print("Overwriting tmux") copyfile(PosixPath("./shared/tmux.conf"), PosixPath("~/.tmux.conf").expanduser()) print("Overwriting gitignore") copyfile(PosixPath("./shared/gitignore"), PosixPath("~/.gitignore").expanduser())
class BMP280Sensor(AbstractTemperatureSensor): """ Class to read the sensor BMP280 on the I2C bus To find the sensor id, make sure the i2c bus is enabled, the device is connected, and mp3115a2 module is loaded. Additionally, you have to tell the i2c bus to read the chip with the "echo bmp280 0x76" > /sys/bus/i2c/devices/i2c-1/new_device ...command. The i2c-1 bus number may change if the system has more than one i2c bus loadded. Then look in /sys/bus/i2c/devices directory for the 1-0076 directory. The 0x76 above and the 1-0076 represents the i2c bus id. The bus id can be determined with the i2cdetect command is needed. Some bmp280 sensors have ids of 0x77. The bme280 should also be supported but the humidity value will not be read """ def __init__(self, temperature_config): super().__init__(temperature_config) self.property_bus = "i2c" devicepath = PosixPath("/sys/bus/i2c/devices").joinpath( temperature_config.device).joinpath("iio:device0") self.temperature_path = PosixPath(devicepath.joinpath("in_temp_input")) self.pressure_path = PosixPath( devicepath.joinpath("in_pressure_input")) # Make sure they exist if (not self.temperature_path.exists() and not self.temperature_path.is_file()): raise DeviceError(self.temperature_path) if (not self.pressure_path.exists() and not self.pressure_path.is_file()): raise DeviceError(self.pressure_path) @property def temperature(self): with self.temperature_path.open() as f: data = f.readline() data = str.strip(data) result = int(data) / 1000 if (self.property_sensor_config.rounding != -1): result = round(result, self.property_sensor_config.rounding) return (result) @property def pressure(self): with self.pressure_path.open() as f: data = f.readline() data = str.strip(data) result = float(data) * 10 if (self.property_sensor_config.rounding != -1): result = round(result, self.property_sensor_config.rounding) return result @property def bus(self): return self.property_bus
class BH1750Sensor(AbstractLightSensor): """ Class to read the sensor BH1750 on the I2C bus To find the sensor id, make sure the i2c bus is enabled, the device is connected, and BH1750 module is loaded. Additionally, you have to tell the i2c bus to read the chip with the "echo bh1750 0x23" > /sys/bus/i2c/devices/i2c-1/new_device ...command. The i2c-1 bus number may change if the system has more than one i2c bus loadded. Then look in /sys/bus/i2c/devices directory for the 1-0023 directory. The 0x23 above and the 1-0023 represents the i2c bus id. The bus id can be determined with the i2cdetect command is needed. """ def __init__(self, lightsensor_config): super().__init__(lightsensor_config) self.property_bus = "i2c" devicepath = PosixPath("/sys/bus/i2c/devices").joinpath( lightsensor_config.device).joinpath("iio:device0") self.lightsensor_path_raw = PosixPath( devicepath.joinpath("in_illuminance_raw")) self.lightsensor_path_scale = PosixPath( devicepath.joinpath("in_illuminance_scale")) # Make sure they exist if (not self.lightsensor_path_raw.exists() and not self.lightsensor_path_raw.is_file()): raise DeviceError(self.lightsensor_path_raw) if (not self.lightsensor_path_scale.exists() and not self.lightsensor_path_scale.is_file()): raise DeviceError(self.lightsensor_path_scale) def dispose(self): pass @property def lightlevel(self): with self.lightsensor_path_raw.open() as f: data_raw = f.readline() with self.lightsensor_path_scale.open() as f: data_scale = f.readline() result = int(data_raw) * float(data_scale) if (self.property_sensor_config.rounding != -1): result = round(result, self.property_sensor_config.rounding) return result @property def bus(self): return self.property_bus
def load_component(gdspath: PosixPath) -> Component: """Returns Component from gdspath, with ports (CSV) and metadata (JSON) info (if any)""" if not gdspath.exists(): raise FileNotFoundError(f"No such file '{gdspath}'") ports_filepath = gdspath.with_suffix(".ports") metadata_filepath = gdspath.with_suffix(".json") c = pp.import_gds(gdspath) if ports_filepath.exists(): with open(str(ports_filepath), newline="") as csvfile: reader = csv.reader(csvfile, delimiter=",", quotechar="|") for r in reader: layer_type = int(r[5].strip().strip("(")) data_type = int(r[6].strip().strip(")")) c.add_port( name=r[0], midpoint=[float(r[1]), float(r[2])], orientation=int(r[3]), width=float(r[4]), layer=(layer_type, data_type), ) if metadata_filepath.exists(): with open(metadata_filepath) as f: data = json.load(f) cell_settings = data["cells"][c.name] c.settings.update(cell_settings) return c
def vcopy(source, destination): '''Copy files to the specified destination.''' path_source = PosixPath(source) path_destination = PosixPath(destination) # Creating the destination folder if it does not exist if not path_destination.exists() or not path_destination.is_dir(): path_destination.mkdir(mode=0o755) chown(path_destination, user=0, group=0) # Getting the name of the files to copy files = list() for object in path_source.iterdir(): if object.is_file(): filename = str(object.relative_to(path_source)) if filename[0] != '.': files.append(filename) # Copying files for file in files: path_file_source = path_source / file path_file_destination = path_destination / file if not path_file_destination.exists( ) or not path_file_destination.is_file(): copy(path_file_source, path_file_destination) path_file_destination.chmod(0o755) chown(path_file_destination, user=0, group=0)
def process_list_files_request(self, request): """ Process LIST_FILES request. The request is expected to contain the directory for which the server must list files for. The possible responses are. * ACCEPTED with FILES_LISTED and the list of files, if the listing was successful * REFUSED with NOT_A_DIRECTORY if the list files directory doesn't exists Other responsed include ERROR with BAD_REQUEST if the request is imporperly formatted, or ERROR with UNKNOWN ERROR if any other error occured during the reading of the directory. """ # extract list directory from request, raise bad request error # if something goes wrong try: assert isinstance(request, tuple) assert len(request) == 2 directory = PosixPath(request[1]) except Exception: response = make_bad_request_error() self.socket.send_pyobj(response) return # normalize the directory (later it can be combined with the # root directory) directory = normalize_directory(directory) # combine the list directory with the root directory directory = self.root_directory / directory # if the directory doesn't refer to an actual directory, send # not a directory refused resonse if not directory.exists() or directory.is_file(): response = make_not_a_directory_response() self.socket.send_pyobj(response) return # build the list of files of the given directory, with files # properties files_list = {} for _file in directory.iterdir(): name = _file.name is_directory = _file.is_dir() size = _file.stat().st_size if not is_directory else 0 last_accessed = _file.stat().st_atime files_list[name] = (is_directory, size, last_accessed) # send list file accepted response with list of files response = make_files_listed_response(files_list) self.socket.send_pyobj(response)
def MainProcess(text: str, number_processes: int, number_iterations: int, log: io) -> None: child_pids: List[int] = [] for i in range(1, number_processes + 1): pid = os.fork() if (pid != 0): child_pids.append(pid) child, _ = os.waitpid(0, os.WNOHANG) if child != 0: print(f'child with pid={child} exited with code={code}. parent with pid id={os.getpid()} exited') exit(2) else: parent_pid = os.getppid() parent_path = PosixPath(f'/proc/{parent_pid}') for j in range(number_iterations * i): if not parent_path.exists(): print(f'parent with pid={parent_pid} exited. child with pid={os.getpid()} exited') exit(1) print(f'process = {i}\tnumber_iteration = {j}\ttext = {text}', file=log) sleep(1) exit(0) while True: child, code = os.waitpid(0, os.WNOHANG) if child != 0: print(f'child with pid={child} exited with code={code}. parent with pid id={os.getpid()} exited') exit(2)
def _check_dir_existence(path: PosixPath): """ Validates the existence of a given directory """ if not path.exists(): msg = "Provided directory cannot be found." logging.error(msg) raise Exception(msg)
def read_raw_words( words_path: pathlib.PosixPath = DEFAULT_WORDS_PATH) -> List[str]: if not words_path.exists(): raise FileNotFoundError( f'File at path {words_path} doesn\'t seem to exist.') with words_path.open() as f: lines = [x.strip() for x in f.readlines()] return lines
def _load_bare_config(self): log.debug("Loading configuration file: %s", self.__resolved_config_file) config_file_path = PosixPath(self.__resolved_config_file) if config_file_path.exists(): with config_file_path.open() as f: self.__bare_config_dict = json.load(f) else: self.__bare_config_dict = {}
def init_ssh_key(path: PosixPath): if path.exists(): key = RSAKey.from_private_key_file(str(path)) log.info('[⚙] Using existing SSH key in {}'.format(path)) else: key = generate_cert(path) log.info('[⚙] Generated SSH key in {}'.format(path)) return key
def pngTable(self, df: DataFrame, colwidth_factor: float = 0.20, fontsize: int = 12, formatFloats: bool = True, save: bool = False, in_folder: PosixPath = None): ''' Displays or saves a table as png. Uses matplotlib => pandas plotting table. parameters: df: dataframe or pivot table colwidth_factor: float, default 0.20, defines the width of columns fontsize: int, default 12 formatFloats: bool, default True, formats as two digit prettiy floats save: saves the png file as table.png in_folder: posixpath, default None, folder to save the png file returns: png file in Downloads folder ''' if not isinstance(in_folder, PosixPath) or not in_folder.exists(): in_folder = core.folder.Downloads # file name and path path = in_folder.joinpath(f"table-{core.now_prefix()}.png") # format floats - two digits if formatFloats: df.applymap(lambda x: '{:,.2f}'.format(x) if isinstance(x, float) else x) # get pandas.plotting.table table = pd.plotting.table fig, ax = plt.subplots(figsize=(1.9 * df.shape[1], 0.3 * df.shape[0])) # set size frame ax.xaxis.set_visible(False) # hide the x axis ax.yaxis.set_visible(False) # hide the y axis ax.set_frame_on(False) # no visible frame, uncomment if size is ok tabla = table(ax, df, loc='upper left', colWidths=[colwidth_factor] * len(df.columns)) # where df is your data frame tabla.auto_set_font_size(False) # Activate set fontsize manually tabla.set_fontsize(fontsize) # if ++fontsize is necessary ++colWidths tabla.scale(1.2, 1.2) # change size table if save: plt.savefig(fname=path, bbox_inches="tight", pad_inches=1) # save # https://stackoverflow.com/questions/56328353/matplotlib-savefig-cuts-off-pyplot-table plt.close() print(f"saved in Downloads folder as {path.stem}.png") else: plt.show() # show the result plt.close()
def try_read(fn): try: path = PosixPath(fn) if path.exists(): with open(path, "r") as fp: q = fp.read() except OSError: q = fn return q
def __load_bare_config(self): log.debug("Loading configuration file: %s", self.__resolved_config_file) config_file_path = PosixPath(self.__resolved_config_file) if config_file_path.exists(): self.__bare_config_dict = self.load_bare_config(config_file_path) else: self.__bare_config_dict = {}
def copy_if_not_exists(source: PosixPath, to_copy: PosixPath): '''if source file does not exist — copy to_copy file on its place. source (PosixPath) — path to source file to_copy (PosixPath) - path to the file to be copied ''' if source.exists(): return else: copyfile(to_copy, source)
def __get_img_from_(self, path: PosixPath): if not path.exists(): raise Errors().FileNotFound(path=path) # img = Image.open(str(path)) import cv2 img = cv2.imread(str(path)) # if self.transforms: # img = self.transforms(img) return img
def __init__(self, root_directory, token, file_size_limit=FILE_SIZE_LIMIT, min_chunk_size=MINIMUM_CHUNK_SIZE, max_chunk_size=MAXIMUM_CHUNK_SIZE): """ Construct a :py:class:`Server` instance. Long description. :param root_directory: foobar. :param token: foobar. :param file_size_limit: foobar. :param min_chunk_size: foobar. :param max_chunk_size: foobar. """ root_directory = PosixPath(root_directory) if not root_directory.is_absolute(): root_directory = PosixPath(os.getcwd(), root_directory) if not root_directory.exists() or root_directory.is_file(): raise NotADirectoryError( "The root directory must be an existing directory") self.root_directory = root_directory self.token = bytes(token, 'utf-8') if file_size_limit <= 0: raise ValueError("The file size limit must be greater than 0") self.file_size_limit = file_size_limit if min_chunk_size <= 0: raise ValueError("The minimum chunk size must be greater than 0") self.min_chunk_size = min_chunk_size self.max_chunk_size = max_chunk_size self.state = ServerState.IDLE # server UPLOAD and DOWNLOAD state related attributes self.chunk_size = 0 self.temporary_file = None # file open in 'wb' mode OR file open in 'rb' mode self.file_description = None self.remaining_bytes = 0 self.file_destination = None # upload only attribute # socket relate attributes self.router = None self.dealer = None self.socket = None self.is_running = False
def setup_exit(): '''Program stopping procedure.''' path_setup_dir = PosixPath(SETUP_EXTRACT_DIR) # Deleting installation files if path_setup_dir.exists() and path_setup_dir.is_dir(): try: rmtree(path_setup_dir) except IOError: pass
def _find_frontend_data(): installed_data_frontend = pkg_resources.resource_filename(__name__, 'frontend') if PosixPath(installed_data_frontend).exists(): log.debug("Found local frontend path: %s", installed_data_frontend) return installed_data_frontend setup_py = pkg_resources.resource_filename(__name__, "main.py") dev_env_frontend_dist = PosixPath(setup_py).parent.parent / "frontend" / "dist" if dev_env_frontend_dist.exists(): log.debug("Found dev local frontend path: %s", dev_env_frontend_dist) return str(dev_env_frontend_dist) return None
def read_config(cfg_path: PosixPath) -> Dict: if cfg_path.exists(): with cfg_path.open('r') as fp: yaml = YAML(typ="safe") cfg = yaml.load(fp) else: raise FileNotFoundError(cfg_path) cfg = parse_config(cfg) return cfg
def main(): LOGGER.info("=" * 60) LOGGER.info("= Spojitr installer") LOGGER.info("=" * 60) bashrc_filepath = PosixPath("~/.bashrc").expanduser() if not bashrc_filepath.exists(): LOGGER.info("Creating file %s", bashrc_filepath) bashrc_filepath.touch(mode=0o644, exist_ok=True) install(bashrc_filepath)
def init_hci(iface: str = 'hci0'): # hciconfig <hci> up 的前提是 rfkill 先 unblock subprocess.check_output('rfkill unblock %d' % find_rfkill_devid(iface), stderr=STDOUT, timeout=5, shell=True) subprocess.check_output('hciconfig {} up'.format(iface), stderr=STDOUT, timeout=5, shell=True) subprocess.check_output('systemctl restart bluetooth.service', stderr=STDOUT, timeout=5, shell=True) hci = HCI(iface) # 下面在发送各种 HCI command 时,如果出现如下异常: # BlockingIOError: [Errno 11] Resource temporarily unavailable # 那么可能是 hci socket 被设为了 non-blocking mode。 hci.inquiry_cancel() hci.exit_periodic_inquiry_mode() hci.write_scan_enable() # No scan enabled event_params = hci.le_set_advertising_enable() # Advertising is disabled if event_params['Status'] != 0x00: #print(WARNING, 'Status of HCI_LE_Set_Advertising_Enable command: 0x%02x'%event_params['Status']) pass try: hci.le_set_scan_enable({ 'LE_Scan_Enable': 0x00, # Scanning disabled 'Filter_Duplicates': 0x01 # Ignored }) except RuntimeError as e: #print(WARNING, e) pass hci.set_event_filter({'Filter_Type': 0x00}) # Clear All Filters event_params = hci.read_bdaddr() if event_params['Status'] != 0: raise RuntimeError else: local_bd_addr = event_params['BD_ADDR'].upper() # Clear bluetoothd cache cache_path = PosixPath('/var/lib/bluetooth/') / local_bd_addr / 'cache' if cache_path.exists(): for file in cache_path.iterdir(): os.remove(file) hci.close()
def unzip(url:str, dest:PosixPath, chunk_size:int=1024*1024, remove_zip: bool=False): """ Downloads and unzips a zip file parameters: url: str, uri to zip file dest: PosixPath, destination folder chunk_size: int, default 1 MB remove_zip: bool, default False, unlinks zip file after unzip operation returns: tqdm progress bar and typer echo messages """ stream = requests.get(url, stream=True, verify=False, allow_redirects=True) filename = stream.url.split(sep="/")[-1] length = int(stream.headers.get("content-length", -1)) if length < 1: raise Exception(f"content length is less than 1 bytes") if not dest.exists(): raise Exception(f"destination folder does not exist: {dest}") if dest.is_file(): dest = dest.parent dest = dest.resolve() typer.echo("Downloading zip file...") with tqdm.wrapattr( open(dest.joinpath(filename), "wb"), "write", unit='B', unit_scale=True, unit_divisor=1024, miniters=1, desc=filename, total=length) as f: for chunk in stream.iter_content(chunk_size=chunk_size): if chunk: f.write(chunk) f.flush() typer.echo("Extracting zip file...") with zipfile.ZipFile(dest.joinpath(filename)) as zippo: for member in tqdm(zippo.infolist(), desc="Extracting zip file..."): zippo.extract(member, dest) if remove_zip: dest.joinpath(filename).unlink() typer.secho(f"{filename} is removed.", bold=True, fg="red") else: typer.secho(f"{filename} is unzipped in {dest}.", bold=True, fg="green")
def copySnippets(args): print("Checking for the snippets configuration directory") vimDir = PosixPath("~/.config/nvim/").expanduser() if not vimDir.exists(): print("Neovim config dir not found, need to install...") return snippetsDir = PosixPath("~/.config/nvim/UltiSnips").expanduser() if not snippetsDir.exists(): print("Directory not created, creating...") snippetsDir.mkdir(parents=True, exist_ok=True) try: copyfile( PosixPath("./snippets/html.snippets"), PosixPath("~/.config/nvim/UltiSnips/html.snippets").expanduser()) copyfile( PosixPath("./snippets/typescript.snippets"), PosixPath( "~/.config/nvim/UltiSnips/typescript.snippets").expanduser()) copyfile( PosixPath("./snippets/scss.snippets"), PosixPath("~/.config/nvim/UltiSnips/scss.snippets").expanduser()) except SameFileError: print("Same files detected, snips may be symlinked. Ignoring...")
def convert(self, destination_file: PosixPath, subset_numbered_keys: Optional[str] = None): ktx_dict = self.getter.get_dict() destination_file = Path(destination_file) getter_tag = self.getter.get_getter_tag() format_tag = self.get_format_tag() # - Initialise file md_file = mdutils.MdUtils(file_name=str(destination_file)) # - Write header if any: for hdr_key in self.getter.get_headers_keys(): prefix, suffix, add_key = keys_to_decorations( getter_tag, format_tag, hdr_key) if add_key: prefix += f"{hdr_key}. " md_file.write(prefix + ktx_dict[hdr_key] + suffix) # - Write numbered keys if any: n_keys = self.getter.get_quantity_numbered_keys() numbered_keys = self.getter.get_numbered_keys() if isinstance(numbered_keys, dict): numbered_keys = numbered_keys[subset_numbered_keys] num_numbered_keys_found = 0 for n in range(n_keys[0], n_keys[1] + 1): for key in numbered_keys: prefix, suffix, add_key = keys_to_decorations( getter_tag, format_tag, key) nmb_key = f"{key}{n}" if add_key: prefix += f"{n}. " if nmb_key in ktx_dict.keys(): num_numbered_keys_found += 1 md_file.write(prefix + ktx_dict[nmb_key] + suffix) # Delete file if one with the same name is found if destination_file.exists(): destination_file.unlink() # Write sequence to file md_file.create_md_file() print( f"File {destination_file} created with {num_numbered_keys_found} numbered keys." )
def init_hci(iface='hci0'): hci = HCI(iface) exitcode, output = subprocess.getstatusoutput('rfkill unblock %d' % find_rfkill_devid(iface)) if exitcode != 0: logger.error('rfkill: ' + output) sys.exit(exitcode) exitcode, output = subprocess.getstatusoutput("hciconfig up " + iface) if exitcode != 0: logger.error("Failed to up " + iface) sys.exit(exitcode) else: time.sleep(0.5) # hci.reset() hci.inquiry_cancel() hci.exit_periodic_inquiry_mode() hci.write_scan_enable() # No scan enabled event_params = hci.le_set_advertising_enable() # Advertising is disabled if event_params['Status'] != 0x00: #print(WARNING, 'Status of HCI_LE_Set_Advertising_Enable command: 0x%02x'%event_params['Status']) pass try: hci.le_set_scan_enable({ 'LE_Scan_Enable': 0x00, # Scanning disabled 'Filter_Duplicates': 0x01 # Ignored }) except RuntimeError as e: #print(WARNING, e) pass hci.set_event_filter({'Filter_Type': 0x00}) # Clear All Filters event_params = hci.read_bdaddr() if event_params['Status'] != 0: raise RuntimeError else: local_bd_addr = event_params['BD_ADDR'].upper() # Clear bluetoothd cache cache_path = PosixPath('/var/lib/bluetooth/') / local_bd_addr / 'cache' if cache_path.exists(): for file in cache_path.iterdir(): os.remove(file)
def __gen_filename(self, key, version_id=None, filename=None): if filename is not None: filename = PosixPath(filename).resolve() if filename.is_file(): return filename.as_posix() elif filename.is_dir() and filename.exists(): basepath = filename else: return None else: basepath = PosixPath.cwd() leaf = key if version_id is not None: leaf = '%s-%s'%(leaf, version_id) return basepath.joinpath(leaf).as_posix()
def highest_seat_ID(path_to_passes: pathlib.PosixPath): if not path_to_passes.exists(): raise FileExistsError('No file exists at the path provided.') with path_to_passes.open('r') as f: passes = f.read().split('\n') highest_id = 0 for boarding_pass in passes: data = decode_binary_boarding(boarding_pass) highest_id = max(data[-1], highest_id) print(f'The highest seat ID in the data is: {highest_id}') return highest_id
def look_for_file(filename, paths): ''' Tries to smartly find the absolute path of a config file. If the given path is absolute and exists return it unmodified, otherwise do usual leaf based lookup If the given path contains only a file name check for existence in _SEARCH_DIRS returning if found If the given path contains a relative filepath check for existence in _SEARCH_DIRS joining each with the fragement ''' filename = PosixPath(filename) if filename.is_absolute(): if filename.exists(): return filename return None for confDir in paths: if confDir.joinpath(filename).exists(): return confDir.joinpath(filename).resolve() return None
def setup( folder_path: str, help="Path where the common foler is located", ): """ Install fcust service for current user. """ fpath = PosixPath(folder_path) if not fpath.exists(): raise FileNotFoundError( f"Specified folder {folder_path} does not exist!") click.echo(f"Installing fcust service for {folder_path}") unit_path = create_user_unit_path(create_folder=True) create_fcust_service_unit(folder_path=fpath, unit_path=unit_path) click.echo("fcust service installed.")
def broken_image_file(): img = Path(__file__).parent / 'fixtureimage.png.broken' assert img.exists(), 'image not found' return img
def png_file(): img = Path(__file__).parent / 'fixtureimage.png' assert img.exists(), 'image not found' return img