def add_site(cls, session, domain, site_type, name=None, site_tags=[], tag_source=None, alternate_domains=[], ignore_inactive=False, force_inactive=False, ignore_redirected=False): """Add one site.""" site, status = parse_domain(domain, site_type) if name is not None: site['name'] = name error_flag = False if status == 'invalid': logger.error('Not a valid domain %s!', domain) error_flag = True elif status == 'inactive': if ignore_inactive is True: logger.warning('Domain %s is inactive now, ignored', domain) else: logger.error( """Domain %r is inactive now, cannot determine \ the base URL. Try `--force-inactive` to force insert.""", domain) error_flag = True elif status == 'redirected': if 'ignore_redirected' is True: logger.warning('Domain %r is redirected to %s, ignored', domain, site['base_url']) else: error_flag = True logger.error( """Domain %r is redirected to %r \ Use the redirected domain as primary domain and add %r as secondary domain""", domain, site['base_url'], domain) else: site_tags = [dict(source=tag_source, name=t) for t in site_tags] get_or_create_msite( session, site, site_tags=site_tags, alternate_domains=alternate_domains, onduplicate='ignore') if error_flag: raise SystemExit(1) else: logger.info('Done!')
def load_sites(cls, session, fn, force_inactive, ignore_inactive, ignore_redirected): """Load site.yaml into database.""" with open(fn, 'r') as f: sites = yaml.load(f) invalid_flag = False inactive_flag = False redirected_flag = False site_tuples = [parse_site(site) for site in sites] for site, status in site_tuples: if status == 'invalid': invalid_flag = True logger.error('Site %s absence of required fields (%s)!', site, REQ_FIELDS) elif status == 'inactive': inactive_flag = True logger.warning('Site %s is inactive now!', site['name']) elif status == 'redirected': redirected_flag = True logger.warning('Site %s redirects to %s!', site['name'], site['base_url']) if invalid_flag is True or \ (inactive_flag is True and (ignore_inactive is False and force_inactive is False)) or \ (redirected_flag is True and ignore_redirected is False): logger.error("""Please fix the warnings or errors above! \ Edit domains, or use --ignore-redirected to handle redirected domains', \ or Use --ignore-inactive or --force-inactive to handle inactive domains""") raise SystemExit(2) for site, status in site_tuples: if status == 'inactive' and ignore_inactive is True: continue elif status == 'redirected' and ignore_redirected is True: continue else: alternate_domains = site.pop('alternate_domains', []) site_tags = site.pop('site_tags', []) get_or_create_msite( session, site, alternate_domains=alternate_domains, site_tags=site_tags, onduplicate='update') logger.debug('Insert or update site %s', site['domain'])