Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def do_text_fill(self, filename, fill):
"""
Module to change color of badge's details
:param `filename` - svg file to modify.
:param `fill` - color to be applied on text
"""
tree = etree.parse(open(filename, 'r'))
element = tree.getroot()
for _id in self.ids:
path = element.xpath(("//*[@id='{}']").format(_id))[0]
style_detail = path.get("style")
style_detail = style_detail.split(";")
if style_detail[7].split(':')[0] == 'fill':
style_detail[7] = "fill:" + str(fill)
print(style_detail[7])
elif style_detail[6].split(':')[0] == 'fill':
style_detail[6] = "fill:" + str(fill)
print(style_detail[6])
else:
for ind, i in enumerate(style_detail):
if i.split(':')[0] == 'fill':
style_detail[ind] = "fill:" + str(fill)
style_detail = ';'.join(style_detail)
filename = os.path.join(self.APP_ROOT, 'svg', 'user_defined.svg')
tree = parse(open(filename, 'r'))
element = tree.getroot()
# changing style using XPath.
path = element.xpath('//*[@id="rect4504"]')[0]
style_detail = path.get("style")
style_detail = style_detail.split(";")
style_detail[0] = "opacity:" + str(opacity)
style_detail[1] = "fill:" + str(fill)
style_detail = ';'.join(style_detail)
path.set("style", style_detail)
# changing text using XPath.
path = element.xpath('//*[@id="tspan932"]')[0]
# Saving in the original XML tree
etree.ElementTree(element).write(filename, pretty_print=True)
print("done")
png_name = os.path.join(self.APP_ROOT, 'static', 'uploads', 'image', str(uuid.uuid4())) + ".png"
svg2png(url=filename, write_to=png_name)
return png_name
:param `badge_page` - Single Badge Sheet
:param `options` - Options for the page
"""
if options.get('width') and options.get('height'):
paper_width = options.get('width')
paper_height = options.get('height')
else:
paper_size_format = options.get('paper_size_format')
paper_width = self.paper_sizes[paper_size_format][0]
paper_height = self.paper_sizes[paper_size_format][1]
tree = parse(open(badge_page, 'r'))
root = tree.getroot()
path = root.xpath('//*[@id="svg2"]')[0]
path.set('width', paper_width)
path.set('height', paper_height)
etree.ElementTree(root).write(badge_page, pretty_print=True)
def remove_extra(badge_page, offset):
tree = etree.parse(open(badge_page, 'r'))
root = tree.getroot()
children = root.findall('.//{http://www.w3.org/2000/svg}g')
for i in range(offset, len(children)):
children[i].getparent().remove(children[i])
etree.ElementTree(root).write(badge_page, pretty_print=True)
def remove_extra(badge_page, offset):
tree = etree.parse(open(badge_page, 'r'))
root = tree.getroot()
children = root.findall('.//{http://www.w3.org/2000/svg}g')
for i in range(offset, len(children)):
children[i].getparent().remove(children[i])
etree.ElementTree(root).write(badge_page, pretty_print=True)
def make_sub_element(parent, tag, nsmap=None):
"""Wrapper for etree.SubElement, that takes care of unsupported nsmap option."""
if use_lxml:
return etree.SubElement(parent, tag, nsmap=nsmap)
return etree.SubElement(parent, tag)
def parse_errata(data, force):
""" Parse CentOS errata from https://cefs.steve-meier.de/
"""
result = etree.XML(data)
errata_xml = result.findall('*')
elen = len(errata_xml)
ptext = 'Processing {0!s} Errata:'.format(elen)
progress_info_s.send(sender=None, ptext=ptext, plen=elen)
for i, child in enumerate(errata_xml):
progress_update_s.send(sender=None, index=i + 1)
if not check_centos_release(child.findall('os_release')):
continue
e = parse_errata_tag(child.tag, child.attrib, force)
if e is not None:
parse_errata_children(e, child.getchildren())
def get_metalink_urls(url):
""" Parses a metalink and returns a list of mirrors
"""
res = get_url(url)
if response_is_valid(res):
if 'content-type' in res.headers and \
res.headers['content-type'] == 'application/metalink+xml':
data = download_url(res, 'Downloading repo info:')
ns = 'http://www.metalinker.org/'
try:
context = etree.parse(BytesIO(data), etree.XMLParser())
except etree.XMLSyntaxError:
context = etree.parse(BytesIO(extract(data, 'gz')),
etree.XMLParser())
xpath = "//ns:files/ns:file[@name='repomd.xml']/ns:resources/ns:url[@protocol='https']" # noqa
metalink_urls = context.xpath(xpath, namespaces={'ns': ns})
return [x.text for x in metalink_urls]
:param `badge_page` - Single Badge Sheet
:param `options` - Options for the page
"""
if options.get('width') and options.get('height'):
paper_width = options.get('width')
paper_height = options.get('height')
else:
paper_size_format = options.get('paper_size_format')
paper_width = self.paper_sizes[paper_size_format][0]
paper_height = self.paper_sizes[paper_size_format][1]
tree = parse(open(badge_page, 'r'))
root = tree.getroot()
path = root.xpath('//*[@id="svg2"]')[0]
path.set('width', paper_width)
path.set('height', paper_height)
etree.ElementTree(root).write(badge_page, pretty_print=True)
def extract_yum_packages(data, url):
""" Extract package metadata from a yum primary.xml file
"""
extracted = extract(data, url)
ns = 'http://linux.duke.edu/metadata/common'
m_context = etree.iterparse(BytesIO(extracted),
tag='{{{0!s}}}metadata'.format(ns))
plen = int(next(m_context)[1].get('packages'))
p_context = etree.iterparse(BytesIO(extracted),
tag='{{{0!s}}}package'.format(ns))
packages = set()
if plen > 0:
ptext = 'Extracting packages: '
progress_info_s.send(sender=None, ptext=ptext, plen=plen)
for i, p_data in enumerate(p_context):
elem = p_data[1]
progress_update_s.send(sender=None, index=i + 1)
name = elem.xpath('//ns:name',
namespaces={'ns': ns})[0].text.lower()
arch = elem.xpath('//ns:arch',
namespaces={'ns': ns})[0].text
fullversion = elem.xpath('//ns:version',
namespaces={'ns': ns})[0]