|
# coding utf-8 |
|
|
|
# Copyright 2016 Farbod Salamat-Zadeh |
|
# |
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
# you may not use this file except in compliance with the License. |
|
# You may obtain a copy of the License at |
|
# |
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
# |
|
# Unless required by applicable law or agreed to in writing, software |
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
# See the License for the specific language governing permissions and |
|
# limitations under the License. |
|
|
|
|
|
"""Reads a color xml file and replaces Material Design colors with references to colors from the MaterialDesignUtils |
|
library for Android. |
|
The color xml file is located by its full path from user input, and the new file is created and written in the same |
|
directory, except with the name 'colors_new.xml' |
|
""" |
|
|
|
import os.path |
|
|
|
|
|
def log(message): |
|
"""Prints a log message""" |
|
# print("LOG : " + message) |
|
pass |
|
|
|
|
|
def convert(path): |
|
"""Reads the specified color XML file and writes a new one, replacing Material Design colors with references to |
|
colors from the MaterialDesignUtils library for Android. |
|
:param path: The full path of the file to be 'converted' |
|
""" |
|
list_file = open("colors_list.csv", "r") |
|
color_list = {} |
|
|
|
for line in list_file: |
|
values = line.split(",") |
|
val_hex = "#" + str(values[2].strip("\n")) # 3rd column of csv |
|
new_name = "@color/mdu_" + values[0] + "_" + values[1] |
|
color_list[val_hex] = new_name |
|
log("Created dictionary from CSV file") |
|
|
|
old_file = open(path, "r+") |
|
path_pair = os.path.split(path) |
|
new_file = open(path_pair[0] + "\colors_new.xml", "a") |
|
|
|
small_tab = " " * 2 |
|
tab = " " * 4 |
|
description = "Generated by the MaterialDesignUtils File Converter library, from your previous color XML file" |
|
|
|
new_file.write('<?xml version="1.0" encoding="utf-8"?>\n') |
|
new_file.write("<!--\n" + small_tab + description + "\n" + small_tab + "-->") |
|
new_file.write("\n<resources>\n") |
|
log("Generated start tags") |
|
|
|
for line in old_file: |
|
has_written = False |
|
log("Processing: " + line.strip("\n")) |
|
if "<color name=" in line: # checks for color tag |
|
for key in color_list: # iterates through all colors |
|
if key in line: # if line contains a hex from the dictionary |
|
parts = line.split("#") |
|
new_file.write(parts[0] + color_list[key] + "</color>\n") |
|
log(tab + "Replaced color with MDU ref: " + color_list[key]) |
|
has_written = True |
|
if not has_written: # if contains color, but is not material design color |
|
new_file.write(line) |
|
else: |
|
if "<?xml" in line \ |
|
or "<resources>" in line \ |
|
or "</resources>" in line: |
|
log(tab + "Cannot rewrite this line") |
|
else: |
|
new_file.write(line) |
|
|
|
new_file.write("</resources>") |
|
log("Generated end tag") |
|
|
|
print("\nThe new file has been created!") |
|
|
|
|
|
while True: |
|
f_path = input("\nEnter the full path and name of the XML file to be converted: ") |
|
if not f_path.endswith(".xml"): |
|
f_path += ".xml" |
|
if os.path.isfile(f_path): |
|
print("\nConverting...") |
|
convert(f_path) |
|
break |
|
else: |
|
print("This file does not exist!") |