def get_content_item(key): """ Gets deployed content for the specified key. """ cfg = get_cfg() resource = get_content_resource() return resource.get(params={'content_type_slug':slugify(cfg.get('Connection','content_type')),'content_key':key})
def _get_resource(app,resource,library=True): """ Gets a resource client. """ cfg = get_cfg() apikey_setting = 'library_key' if library else 'api_key' return ResourceClient('%s/api/resource' % cfg.get('Connection','endpoint'),app,'beta3',resource,auth_user=cfg.get('Connection',apikey_setting))
def ping_library(): """ Pings the library. """ cfg = get_cfg() lib = get_library_client() lib.ping(project=cfg.get('Connection','project'),content_type=cfg.get('Connection','content_type'))
def _get_client(app,library=True): """ Gets a regular API client. """ cfg = get_cfg() apikey_setting = 'library_key' if library else 'api_key' return HttpClient('%s/api' % cfg.get('Connection','endpoint'),app,'beta3',auth_user=cfg.get('Connection',apikey_setting))
def get_content_keys(): """ Gets the keys for the conten type. """ cfg = get_cfg() api = get_content_api() keys = api.getcontentkeys(content_type_slug=slugify(cfg.get('Connection','content_type'))) return keys
def extract_keyfield(path): """ Extracts the keyfield value from the document. """ cfg = get_cfg() keyfield_name = cfg.get('Connection','key_field') md = markdown.Markdown(extensions=['meta','tables','attr_list']) with open(path) as docfile: md.convert(docfile.read()) try: keyfield = md.Meta[keyfield_name][0] return keyfield except KeyError: return None
def extract_keyfield(path): """ Extracts the keyfield value from the document. """ cfg = get_cfg() keyfield_name = cfg.get('Connection', 'key_field') md = markdown.Markdown(extensions=['meta', 'tables', 'attr_list']) with open(path) as docfile: md.convert(docfile.read()) try: keyfield = md.Meta[keyfield_name][0] return keyfield except KeyError: return None
def keyfields(args): """ Synchronizes the local cache of keyfield / key data. """ print 'Synchronizing keyfield cache.' cfg = get_cfg() keyfield_name = cfg.get('Connection','key_field') keyfield_data = {} keys = get_content_keys() for key in keys: content_item = get_content_item(key) keyfield_data[content_item['data'][keyfield_name]] = key print 'Mapping',content_item['data'][keyfield_name],'to',key write_keyfields(keyfield_data) print 'Keyfield cache synchronized.'
def keyfields(args): """ Synchronizes the local cache of keyfield / key data. """ print('Synchronizing keyfield cache.') cfg = get_cfg() keyfield_name = cfg.get('Connection', 'key_field') keyfield_data = {} keys = get_content_keys() for key in keys: content_item = get_content_item(key) keyfield_data[content_item['data'][keyfield_name]] = key print('Mapping', content_item['data'][keyfield_name], 'to', key) write_keyfields(keyfield_data) print('Keyfield cache synchronized.')
def _tagging(md,content_key,content_type): """ Apply tagging """ tagger = get_library_client() cfg = get_cfg() project = cfg.get('Connection','project') # tagging tags = md.Meta['tags'] for tag in tags: if tag: tagger.tagcontent(project=project, content_type=content_type, content_key=content_key, tag=tag)
def _tagging(md, content_key, content_type): """ Apply tagging """ tagger = get_library_client() cfg = get_cfg() project = cfg.get('Connection', 'project') # tagging tags = md.Meta['tags'] for tag in tags: if tag: tagger.tagcontent(project=project, content_type=content_type, content_key=content_key, tag=tag)
def upload_document(path, key=None): """ Uploads the specified document, returns the key of the uploaded document. """ # 1. Prepare data for upload cfg = get_cfg() body_field_name = cfg.get('Connection', 'body_field') data = {} md = markdown.Markdown(extensions=['meta', 'tables', 'attr_list']) with open(path) as docfile: body = md.convert(docfile.read()) for field_name, field_value in list(md.Meta.items()): data[field_name] = field_value[0] data[body_field_name] = body # Upload Content return_key = None resource = get_content_library_resource() if key: resource.put( data={ 'content': data, 'content_type': cfg.get('Connection', 'content_type'), 'project': cfg.get('Connection', 'project'), 'key': key }) _tagging(md, key, cfg.get('Connection', 'content_type')) return (key, False) # no new document created else: response = resource.post( data={ 'content': data, 'project': cfg.get('Connection', 'project'), 'content_type': cfg.get('Connection', 'content_type') }) created_content_type, created_key = response['created_content'].split( ':') _tagging(md, created_key, cfg.get('Connection', 'content_type')) return (created_key, True) # new document created
def upload_document(path,key=None): """ Uploads the specified document, returns the key of the uploaded document. """ # 1. Prepare data for upload cfg = get_cfg() body_field_name = cfg.get('Connection','body_field') data = {} md = markdown.Markdown(extensions=['meta','tables','attr_list']) with open(path) as docfile: body = md.convert(docfile.read()) for field_name, field_value in md.Meta.items(): data[field_name] = field_value[0] data[body_field_name] = body # Upload Content return_key = None resource = get_content_library_resource() if key: resource.put(data={'content':data, 'content_type':cfg.get('Connection','content_type'), 'project':cfg.get('Connection','project'), 'key':key}) _tagging(md,key,cfg.get('Connection','content_type')) return (key,False) # no new document created else: response = resource.post(data={'content':data, 'project':cfg.get('Connection','project'), 'content_type':cfg.get('Connection','content_type')}) created_content_type, created_key = response['created_content'].split(':') _tagging(md,created_key,cfg.get('Connection','content_type')) return (created_key,True) # new document created
def find_key(path,keymap,keyfields): """ Finds a key for the specified document if it exists. """ # First try to pull key from the keymap try: return keymap[path] except KeyError: pass # Try to find it from a keyfield try: cfg = get_cfg() keyfield_name = cfg.get('Connection','key_field') md = markdown.Markdown(extensions=['meta','tables','attr_list']) with open(path) as docfile: md.convert(docfile.read()) keyfield = md.Meta[keyfield_name][0] return keyfields[keyfield] except KeyError: pass # Give up return None
def find_key(path, keymap, keyfields): """ Finds a key for the specified document if it exists. """ # First try to pull key from the keymap try: return keymap[path] except KeyError: pass # Try to find it from a keyfield try: cfg = get_cfg() keyfield_name = cfg.get('Connection', 'key_field') md = markdown.Markdown(extensions=['meta', 'tables', 'attr_list']) with open(path) as docfile: md.convert(docfile.read()) keyfield = md.Meta[keyfield_name][0] return keyfields[keyfield] except KeyError: pass # Give up return None