def reject(): ''' Rejects the license. ''' store.dump('accepted', False) store.flush() gramex.console('Gramex license rejected')
def mail(args, kwargs): # Get config file location default_dir = os.path.join(variables['GRAMEXDATA'], 'mail') _mkdir(default_dir) if 'conf' in kwargs: confpath = kwargs.conf elif os.path.exists('gramex.yaml'): confpath = os.path.abspath('gramex.yaml') else: confpath = os.path.join(default_dir, 'gramexmail.yaml') if not os.path.exists(confpath): if 'init' in kwargs: with io.open(confpath, 'w', encoding='utf-8') as handle: handle.write(default_mail_config.format(confpath=confpath)) app_log.info('Initialized %s', confpath) elif not args and not kwargs: app_log.error(show_usage('mail')) else: app_log.error('Missing config %s. Use --init to generate skeleton', confpath) return conf = PathConfig(confpath) if 'list' in kwargs: for key, alert in conf.get('alert', {}).items(): to = alert.get('to', '') if isinstance(to, list): to = ', '.join(to) gramex.console('{:15}\t"{}" to {}'.format(key, alert.get('subject'), to)) return if 'init' in kwargs: app_log.error('Config already exists at %s', confpath) return if len(args) < 1: app_log.error(show_usage('mail')) return from gramex.services import email as setup_email, create_alert alert_conf = conf.get('alert', {}) email_conf = conf.get('email', {}) setup_email(email_conf) sys.path += os.path.dirname(confpath) for key in args: if key not in alert_conf: app_log.error('Missing key %s in %s', key, confpath) continue alert = create_alert(key, alert_conf[key]) alert()
def accept(force=False): ''' Prints the license. Allows users to accept the license by prompting. If force=True is passed, accepts the license without prompting the user. ''' if is_accepted(): return gramex.console(EULA) result = 'y' if force else '' while not result: result = six.moves.input('Do you accept the license (Y/N): ').strip() if result.lower().startswith('y'): store.dump('accepted', time.time()) store.flush() gramex.console('Gramex license accepted') else: raise RuntimeError('Gramex license not accepted')
def accept(force=False): ''' Prints the license. Allows users to accept the license by prompting. If force=True is passed, accepts the license with no prompt. If no stdin is available, e.g. when running as a service, accept license with no prompt. ''' if is_accepted(): return gramex.console(EULA) result = 'y' if force or not sys.stdin else '' while not result: # bandit: input() is safe in Python 3 result = input('Do you accept the license (Y/N): ').strip() # nosec if result.lower().startswith('y'): store.dump('accepted', time.time()) store.flush() gramex.console('Gramex license accepted') else: raise RuntimeError('Gramex license not accepted')
def commandline(args=None): ''' usage: slidesense [config.yaml] [url-name] [--source=...] [--target=...] [--data=...] Generates target PPTX from a source PPTX, applying rules in config file and opens it. If no config file is specified, uses `gramex.yaml` in the current directory. The config file can have a pptgen configuration like {source: ..., target: ..., rules: ...} or be a `gramex.yaml` with `url: {url-name: {handler: PPTXHandler, kwargs: {source: ...}}}` Rules are picked up from the first PPTXHandler URL that matches `url-name`, or the first PPTXHandler in `gramex.yaml`. --source=... overrides source PPTX path in config file --target=... overrides target PPTX path in config file (defaults to output.pptx) --data=... overrides data file in config path --no-open don't open target PPTX after generating it ''' args = gramex.parse_command_line(sys.argv[1:] if args is None else args) if 'help' in args or (not args['_'] and not os.path.exists('gramex.yaml')): return gramex.console(dedent(commandline.__doc__).strip()) config_file, *urls = args.pop('_') or ['gramex.yaml'] conf = gramex.cache.open(config_file, 'config') if 'url' in conf: for key, spec in conf.url.items(): if spec.handler == 'PPTXHandler': if not urls or any(url in key for url in urls): rules = spec.kwargs break else: return app_log.error( f'No PPTXHandler matched in file: {config_file}') elif any(key in conf for key in ('source', 'target', 'data', 'rules')): rules = conf else: return app_log.error(f'No rules found in file: {config_file}') gramex.config.merge(rules, args) rules.setdefault('target', 'output.pptx') rules.setdefault('mode', 'expr') # Allow importing python files in current directory sys.path.append('.') # Generate output gramex.pptgen2.pptgen(**rules) # If --no-open is specified, or the OS doesn't have startfile (e.g. Linux), stop here. # Otherwise, open the output PPTX created if not rules.get('no-open', False) and hasattr(os, 'startfile'): # os.startfile() is safe since the target is an explicit file we've created os.startfile(rules['target']) # nosec
def license(cmd, args): if len(cmd) == 0: gramex.console(gramex.license.EULA) if gramex.license.is_accepted(): gramex.console('License already ACCEPTED. Run "gramex license reject" to reject') else: gramex.console('License NOT YET accepted. Run "gramex license accept" to accept') elif cmd[0] == 'accept': gramex.license.accept(force=True) elif cmd[0] == 'reject': gramex.license.reject() else: app_log.error('Invalid command license %s', cmd[0])
def quit(self): self.info.update(quit=True) if self.options == 'log': console('From: %s' % self.info['from_addr']) console('To: %s' % self.info['to_addrs']) console(self.info['msg'])