Package: procamora-utils
python3-utilsThis repository contains a set of libraries that I use in other projectsInstallationInstallation can be done through thepip3command:pip3installprocamora-utils--userYou can also update the library with:python3-mpipinstall--user--upgradeprocamora-utilsLibrariesloggerinterface_sqliteippingclient_sshloggerLibrary to run the logging library with colors according to the type of log.Basic UsageTo use this class the first thing to do is import the library:importloggingfromprocamora_utils.loggerimportget_logginglogger:logging=get_logging(verbose=False,name='test')logger.debug('hi')logger.info('hi')logger.warning('hi')logger.error('hi')logger.critical('hi')interface_sqliteThis library provides an easy way to manage a sqlite database. To do this, use thesqlite3library to connect to the database and thelogginglibrary to display information about errors and debugging.Basic UsageTo use this class the first thing to do is import the library:fromprocamora_utils.loggerimportget_loggingfromprocamora_utils.interface_sqliteimport*Theinterface_sqlitefile when doing animport *we are importing three functions, these are:__all__ = ['conection_sqlite', 'execute_script_sqlite', 'dump_database']conection_sqliteThis function is responsible for carrying out the main SQL operations, such as:SELECT,INSERT,UPDATEorDELETE.An example of some of these functions would be:frompathlibimportPathfromtypingimportList,Dict,Text,Anyfromprocamora_utils.interface_sqliteimport*db:Path=Path('database.db')defselect_all_hosts()->List[Dict[Text,Any]]:query:Text="SELECT * FROM Hosts"response_query:List[Dict[Text,Any]]=conection_sqlite(db,query,is_dict=True)returnresponse_querydefupdate_host_offline(date:Text):query:Text=f"UPDATE Hosts SET active=0 WHERE date <> '{date}';"conection_sqlite(db,query)Parameterized queryThis library allows the Parameterized of sql queries, here is an example of how to use itfrompathlibimportPathfromtypingimportList,Dict,Text,Any,Tuplefromprocamora_utils.interface_sqliteimport*db:Path=Path('database.db')query:Text="SELECT * FROM table1 WHERE value=?"params:Tuple=('Python',)response_query:List[Dict[Text,Any]]=conection_sqlite(db,query,query_params=params,is_dict=True)execute_script_sqliteThis function allows you to run a script or dump that you receive in string format. With this function, databases could be created.frompathlibimportPathfromtypingimportTextfromprocamora_utils.interface_sqliteimport*db:Path=Path('database.db')dump:Text='''INSERT INTO() VALUES();INSERT INTO() VALUES();'''execute_script_sqlite(db,dump)dump_databaseThis function allows you to perform a complete dump of the database.frompathlibimportPathfromprocamora_utils.interface_sqliteimport*db:Path=Path('database.db')response=dump_database(db)ipThis library provides a high-level abstraction for storing an IP address or FQDN. In case of entering a wrong value it will raise an exception.Basic UsageimportloggingfromtypingimportTextfromprocamora_utils.ipimportIPfromprocamora_utils.loggerimportget_logginglogger:logging=get_logging(False,'ip')fqdn:Text='google.es'ip:IP=IP(fqdn=fqdn)logger.info(ip.get_addr())ipv4:Text='192.168.1.1'ip:IP=IP(ip=ipv4)logger.info(ip.get_addr())pingThis library provides an easy way to ping using thepingcommand provided by the operating system. The reason for using the operating system command is so that you do not need to be root to send an ICMP packet.Basic UsageTo use this class the first thing to do is import the library:importloggingfromtypingimportListfromprocamora_utils.ipimportIPfromprocamora_utils.pingimportpingfromprocamora_utils.loggerimportget_logginglogger:logging=get_logging(False,'ping')ips:List[IP]=[IP(ip="127.0.0.1"),IP(ip="192.168.0.103"),IP(fqdn="google.es"),IP(fqdn="google.false")]foripinips:texto:bool=ping(ip)iftexto:logger.info("{}up".format(ip.get_addr()))else:logger.info("{}down".format(ip.get_addr()))client_sshThis library provides a high-level abstraction for storing an IP address or FQDN. In case of entering a wrong value it will raise an exception.Basic UsageimportloggingfromtypingimportTextfromprocamora_utils.ipimportIPfromprocamora_utils.loggerimportget_logginglogger:logging=get_logging(False,'ip')fqdn:Text='google.es'ip:IP=IP(fqdn=fqdn)logger.info(ip.get_addr())ipv4:Text='192.168.1.1'ip:IP=IP(ip=ipv4)logger.info(ip.get_addr())pingThis library provides an easy way to use thesshcommand, allowing you to connect unattended over SSH and thus facilitating process automation tasks.Basic UsageTo use this class the first thing to do is import the library:fromprocamora_utils.loggerimportget_logging,loggingfromprocamora_utils.client_sshimportcreate_arg_parser,ClientSSHlogger:logging=get_logging(False,'ping')args=create_arg_parser()ssh:ClientSSH=ClientSSH(args.ip,args.port,args.verbose)output=ssh.execute_command(user=args.user,password=args.pwd,cert=args.key,sudo=args.sudo)print(output)