def open_config(self): # Open config file if exists self.config = POProjectConfig() self.config.open(self.args.config) configdatas = self.config.get_datas() # Merge config in arguments for empty argument only for item in POProjectConfig.options: if item in configdatas: val = getattr(self.args, item, None) or configdatas[item] setattr(self.args, item, val) self.root_logger.debug("Set config value '%s' to: %s", item, val)
class CliInterfaceBase(object): """ The common base CLI interface to use within a command function It takes care of the logging, timer, config and service connection, also embed some common args validate """ def __init__(self, args): self.args = args self.config = None self.con = None self.starttime = datetime.datetime.now() # Init, load and builds self.root_logger = logging_handler.init_logging(self.args.loglevel.upper(), logfile=self.args.logfile) def open_config(self): # Open config file if exists self.config = POProjectConfig() self.config.open(self.args.config) configdatas = self.config.get_datas() # Merge config in arguments for empty argument only for item in POProjectConfig.options: if item in configdatas: val = getattr(self.args, item, None) or configdatas[item] setattr(self.args, item, val) self.root_logger.debug("Set config value '%s' to: %s", item, val) def save_config(self): # Save config with current values if self.config and not self.args.passive: self.root_logger.debug("Saving config") values = {} for item in POProjectConfig.options: if hasattr(self.args, item): values[item] = getattr(self.args, item) self.config.set_datas(values) self.config.save() def connect(self): """ Connect to the PO Project API service """ # Open client self.con = POProjectClient(self.args.host, (self.args.user, self.args.password)) # Connect to the service try: self.con.connect() except (HTTPError, ConnectionError, InvalidSchema) as e: import traceback top = traceback.extract_stack()[-1] self.root_logger.error("%s: %s", type(e).__name__, e) raise CommandError('Error exit') def close(self): if self.args.timer: endtime = datetime.datetime.now() self.root_logger.info('Done in %s', str(endtime-self.starttime)) def validate_authentication_args(self): # Validate required arguments to connect if not self.args.user or not self.args.password or not self.args.host: self.root_logger.error("'user', 'password' and 'hostname' are required arguments to connect to the service") raise CommandError('Error exit') def validate_slug_args(self): # Validate required argument to reach the project if not self.args.project_slug: self.root_logger.error("Project 'slug' name is a required argument") raise CommandError('Error exit') def validate_locale_path_args(self): # Validate locale dir path argument if self.args.locale_path and not os.path.exists(self.args.locale_path): self.root_logger.error("The given locale directory path does not exists : %s"%self.args.locale_path) raise CommandError('Error exit') if self.args.locale_path and not os.path.isdir(self.args.locale_path): self.root_logger.error("The given locale directory path is not a directory") raise CommandError('Error exit') # Validate the required argument when kind is 'django', needed to do a trick for a POT file if self.args.kind == 'django': if not self.args.django_default_locale: self.root_logger.error("For 'django' kind you have to give a default locale directory name (relative to 'locale_path') with '--django_default_locale'") raise CommandError('Error exit') default_locale_path = os.path.join(self.args.locale_path, self.args.django_default_locale) if not os.path.exists(default_locale_path) or not os.path.isdir(default_locale_path): self.root_logger.error("The default locale path does not exists or is not a directory: %s"%default_locale_path) raise CommandError('Error exit')
class CliInterfaceBase(object): """ The common base CLI interface to use within a command function It takes care of the logging, timer, config and service connection, also embed some common args validate """ def __init__(self, args): self.args = args self.config = None self.con = None self.starttime = datetime.datetime.now() # Init, load and builds self.root_logger = logging_handler.init_logging( self.args.loglevel.upper(), logfile=self.args.logfile) def open_config(self): # Open config file if exists self.config = POProjectConfig() self.config.open(self.args.config) configdatas = self.config.get_datas() # Merge config in arguments for empty argument only for item in POProjectConfig.options: if item in configdatas: val = getattr(self.args, item, None) or configdatas[item] setattr(self.args, item, val) self.root_logger.debug("Set config value '%s' to: %s", item, val) def save_config(self): # Save config with current values if self.config and not self.args.passive: self.root_logger.debug("Saving config") values = {} for item in POProjectConfig.options: if hasattr(self.args, item): values[item] = getattr(self.args, item) self.config.set_datas(values) self.config.save() def connect(self): """ Connect to the PO Project API service """ # Open client self.con = POProjectClient(self.args.host, (self.args.user, self.args.password)) # Connect to the service try: self.con.connect() except (HTTPError, ConnectionError, InvalidSchema) as e: import traceback top = traceback.extract_stack()[-1] self.root_logger.error("%s: %s", type(e).__name__, e) raise CommandError('Error exit') def close(self): if self.args.timer: endtime = datetime.datetime.now() self.root_logger.info('Done in %s', str(endtime - self.starttime)) def validate_authentication_args(self): # Validate required arguments to connect if not self.args.user or not self.args.password or not self.args.host: self.root_logger.error( "'user', 'password' and 'hostname' are required arguments to connect to the service" ) raise CommandError('Error exit') def validate_slug_args(self): # Validate required argument to reach the project if not self.args.project_slug: self.root_logger.error( "Project 'slug' name is a required argument") raise CommandError('Error exit') def validate_locale_path_args(self): # Validate locale dir path argument if self.args.locale_path and not os.path.exists(self.args.locale_path): self.root_logger.error( "The given locale directory path does not exists : %s" % self.args.locale_path) raise CommandError('Error exit') if self.args.locale_path and not os.path.isdir(self.args.locale_path): self.root_logger.error( "The given locale directory path is not a directory") raise CommandError('Error exit') # Validate the required argument when kind is 'django', needed to do a trick for a POT file if self.args.kind == 'django': if not self.args.django_default_locale: self.root_logger.error( "For 'django' kind you have to give a default locale directory name (relative to 'locale_path') with '--django_default_locale'" ) raise CommandError('Error exit') default_locale_path = os.path.join(self.args.locale_path, self.args.django_default_locale) if not os.path.exists(default_locale_path) or not os.path.isdir( default_locale_path): self.root_logger.error( "The default locale path does not exists or is not a directory: %s" % default_locale_path) raise CommandError('Error exit')