Skip to content

Instantly share code, notes, and snippets.

@evilwk
Created January 20, 2021 09:06
Show Gist options
  • Save evilwk/758d6359541a96a8de852e680d5eed4a to your computer and use it in GitHub Desktop.
Save evilwk/758d6359541a96a8de852e680d5eed4a to your computer and use it in GitHub Desktop.
[Android]生成最小宽度限定符适配文件
# -*- coding: utf-8 -*-
import xml.sax
import argparse
import os
import re
import configparser
dimens = []
source_file = ""
xml_head = """<?xml version="1.0" encoding="UTF-8"?>
<resources>
"""
class LayoutHandler(xml.sax.ContentHandler):
_config = None
def __init__(self):
self.curr_tag = ""
self.curr_name = ""
self.init_config()
def init_config(self):
self._config = configparser.ConfigParser()
config_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.ini")
if os.path.exists(config_file):
self._config.read(config_file)
else:
print("can not find config.ini")
exit(-1)
def get_match(self):
return [int(n) for n in self._config["config"]["match_dp"].split(",")]
def get_base_dp(self):
return self._config["config"].getint("base_dp")
def startElement(self, tag, attributes):
self.curr_tag = tag
self.curr_name = attributes.get("name")
def endElement(self, tag):
self.curr_tag = ""
self.curr_name = ""
def characters(self, content):
if self.curr_tag == "":
return
m = re.match(r"(-?\d+\.?\d*)([ds]i?p)", content)
if m:
dimens.append((self.curr_name, float(m.group(1)), m.group(2)))
def endDocument(self):
if not dimens:
return
base_dp = self.get_base_dp()
matchs = self.get_match()
for match_dp in matchs:
factor = match_dp / base_dp
items = []
for dimen in dimens:
items.append((dimen[0], dimen[1] * factor, dimen[2]))
create_xml_file("values-sw%sdp/dimens.xml" % match_dp, items)
@staticmethod
def camelCase(res_id):
return "".join([name.title() for name in res_id.split("_")])
def parse_xml_file(file_path):
global source_file
source_file = file_path
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
handler = LayoutHandler()
parser.setContentHandler(handler)
parser.parse(file_path)
def is_xml_dimens(file_path):
parent_dir = os.path.basename(os.path.dirname(file_path))
return os.path.basename(file_path) == "dimens.xml" and parent_dir == "values"
def create_xml_file(file_path, items):
source_dir_parent = os.path.dirname(os.path.dirname(source_file))
out_file = os.path.join(source_dir_parent, file_path)
dir_path = os.path.dirname(out_file)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
with open(file_path, "w+") as file:
file.write(xml_head)
for dimen in items:
file.write(" <dimen name=\"%s\">%.4f%s</dimen>\n" % (dimen[0], dimen[1], dimen[2]))
file.write("</resources>")
def main():
parse = argparse.ArgumentParser()
parse.add_argument("file", nargs=1)
args = parse.parse_args()
file_path = args.file[0]
if os.path.isfile(file_path) and is_xml_dimens(file_path):
parse_xml_file(file_path)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment