Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_simple_cdata(self):
obj = {'a': 'b'}
self.assertEqual(obj, parse(unparse(obj)))
self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
def assertXMLEqual(self, expected, xml):
expected = xmltodict.unparse(xmltodict.parse(expected))
xml = xmltodict.unparse(xmltodict.parse(xml))
self.assertEqual(expected, xml)
def _generate_uvmpw_file(self):
uvmpw_dic = xmltodict.parse(open(self.uvmpw_file, "rb"))
uvmpw_dic['ProjectWorkspace']['project'] = []
for project in self.workspace['projects']:
# We check how far is project from root and workspace. IF they dont match,
# get relpath for project and inject it into workspace
path_project = os.path.dirname(project['files']['uvproj'])
path_workspace = os.path.dirname(self.workspace['settings']['path'] + '\\')
destination = os.path.join(os.path.relpath(self.env_settings.root, path_project), project['files']['uvproj'])
if path_project != path_workspace:
destination = os.path.join(os.path.relpath(self.env_settings.root, path_workspace), project['files']['uvproj'])
uvmpw_dic['ProjectWorkspace']['project'].append({'PathAndName': destination})
# generate the file
uvmpw_xml = xmltodict.unparse(uvmpw_dic, pretty=True)
project_path, uvmpw = self.gen_file_raw(uvmpw_xml, '%s.uvmpw' % self.workspace['settings']['name'], self.workspace['settings']['path'])
return project_path, uvmpw
def get_all_checks(self, request):
"""
Return the current configuration and state of all checks.
"""
request.setHeader(b"content-type", b"application/xml")
return xmltodict.unparse(get_all_checks())
def write_advancedsettings(self, loc, dictionary):
''' Writes the supplied dictionary back to the advancedsettings.xml file '''
if not dictionary.get('advancedsettings', None):
self.log('Empty dictionary passed to advancedsettings file writer. Preventing write, backing up and removing file.')
subprocess.call(['sudo', 'cp', loc, loc.replace('advancedsettings.xml', 'advancedsettings_backup.xml')])
subprocess.call(['sudo', 'rm', '-f', loc])
return
with open(loc, 'w') as f:
xmltodict.unparse( input_dict = dictionary,
output = f,
pretty = True)
def _winrm_send_input(self, protocol, shell_id, command_id, stdin, eof=False):
rq = {'env:Envelope': protocol._get_soap_header(
resource_uri='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd',
action='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send',
shell_id=shell_id)}
stream = rq['env:Envelope'].setdefault('env:Body', {}).setdefault('rsp:Send', {})\
.setdefault('rsp:Stream', {})
stream['@Name'] = 'stdin'
stream['@CommandId'] = command_id
stream['#text'] = base64.b64encode(to_bytes(stdin))
if eof:
stream['@End'] = 'true'
protocol.send_message(xmltodict.unparse(rq))
for i, item in enumerate(items, 1):
root['person' + str(i)] = item
my_dict = {
'root': root
}
# my_dict = {
# 'root': {
# 'person1': {"first_name": "Ivan", "last_name": "Ivanov", "city": "Moscow"},
# 'person2': {"first_name": "Sergey", "last_name": "Sidorov", "city": "Sochi"},
# }
# }
# Параметр full_document=False убирает из XML строку "
def __init__(self, obj='', mimetype="application/xml", *args, **kwargs):
content = xmltodict.unparse(obj)
super(XMLResponse, self).__init__(content, mimetype, *args, **kwargs)
def json2xml(j):
x = xmltodict.unparse({'json': j})
logger.debug('json2xml: %s', x.encode('utf-8'))
return x
def tofile(results, logfile, format='JSON'):
"""
dump results to file (specifiled by file handle logfile)
"""
if format == 'JSON':
json.dump(results, logfile, sort_keys=True, indent=8, separators=(',',': '))
elif format == 'XML':
newresults = replace_keys(results)
res = xmltodict.unparse(newresults, pretty=True)
logfile.write(res)
else:
raise ValueError('Format has to be JSON or XML')