def main(): """ Main entry point for the FunPDBe client :return: None """ logger = FunPDBeClientLogger(name="main", write_mode="w") try: opts, args = getopt.getopt(sys.argv[1:], "u:p:m:i:r:f:a:oh", [ "user="******"pwd=", "mode=", "pdb_id=", "resource=", "path=", "api=", "overwrite", "help" ]) except getopt.GetoptError as err: generic_error() logger.log().error(err) sys.exit(2) schema = Schema() user = User() client = Client(schema, user) if opts: Control(opts, client=client).run() else: Control([('--help', '')], client=client).run()
def run(self): self.configure() if self.help: print(self.client) elif not self.mode: generic_error() self.logger.log().error(CONTROL_ERRORS["no_mode"]) else: return self.action() return None
def check_resource(self, resource): """ Check if resource name exists and if it is a known (registered) resource :param resource: String :return: Boolean """ if not self.check_exists(resource, "no_resource"): return False if resource in RESOURCES: return True self.logger.log().error(CLIENT_ERRORS["unknown_resource"]) generic_error() return False
def validate_json(self): """ Validate JSON against schema :return: Boolean """ if not self.schema.json_schema: self.schema.get_schema() if self.schema.validate_json(self.json_data): self.logger.log().info("JSON complies with FunPDBe schema") self.json_data = self.schema.clean_json(self.json_data) return True self.logger.log().error(CLIENT_ERRORS["bad_json"]) generic_error() return False
def check_pdb_id(self, pdb_id): """ Check if PDB id exists and if it matches the regular expression pattern of a valid PDB identifier :param pdb_id: String :return: Boolean """ if not self.check_exists(pdb_id, "no_pdb"): return False if re.match(PDB_ID_PATTERN, pdb_id): return True generic_error() self.logger.log().error(CLIENT_ERRORS["bad_pdb"]) return False
def check_status(response, expected, component_logger): """ Check if status code is what is expected and log message accordingly :param response: Response :param expected: Int :param component_logger: Logger instance :return: None """ if response.status_code == expected: component_logger.log().info("[%i] SUCCESS" % response.status_code) else: generic_error() component_logger.log().error("[%i] FAIL - %s" % (response.status_code, response.text))
def run(self): """ Main entry point :return: Response.text or None """ self.configure() self.set_help_and_overwrite_flags() if self.help: print(self.client) elif not self.mode: generic_error() self.logger.log().error(CONTROL_ERRORS["no_mode"]) else: return self.action() return None
def parse_json(self, path): """ Parse user JSON file :param path: String, path to JSON :return: Boolean """ if not self.check_exists(path, "no_path"): return None try: with open(path) as json_file: try: self.json_data = json.load(json_file) self.logger.log().info("JSON parsed") return True except ValueError as valerr: self.logger.log().error(valerr) generic_error() return False except IOError as ioerr: self.logger.log().error(ioerr) generic_error() return False