def setLabelProps(self): self.title = my(self.props.attrib).text self.numberOfLines = my(self.props.attrib).numberOfLines if my( self.props.attrib).numberOfLines else 0 for node in self.props.iter(): if node.tag == 'fontDescription': self.fontSize = my(node.attrib).pointSize
def setButtonProps(self): for node in self.props.iter(): if node.tag == 'state': self.title = my(node.attrib).title self.image = my(node.attrib).image self.backgroundImage = my(node.attrib).backgroundImage for color in node: if color.tag == 'color': self.titleColor = 'kColor<##>'
def parse(root): ui_list, ui_trees = find_uis(root) set_view_name(root, ui_list) allconstraints = [] allconstraints = filter( lambda constraint: (constraint.tag == 'constraint'), root.iter()) for node in ui_trees: if node is None: continue #fist treavel print node.tag print '*' * 100 v = find_ui_byid(node.attrib.get('id'), ui_list) for subnode in node: if subnode is None: continue # print subnode.tag if subnode.tag == 'rect': rect = my(subnode.attrib) v.frame = 'CGRectMake(' + ','.join( [rect.x, rect.y, rect.width, rect.height]) + ')' print v.frame if subnode.tag == 'color': color = my(subnode.attrib) v.backgroundColor = [ color.green, color.red, color.blue, color.alpha ] print v.backgroundColor # subs if subnode.tag == 'subviews': for subv in subnode: uiobject = find_ui_byid(subv.attrib.get('id'), ui_list) v.subviews.append(uiobject) uiobject.parent = v # cons if subnode.tag == 'constraints': for constraint in subnode.iter(): if constraint.tag == 'constraint': v.constraints.append(constraint) for c in allconstraints: if c.attrib.get('firstItem') == v.id: v.constraints.append(c) print '*' * 100 print '\n' print_obj(ui_list) return ui_list
def gen_mas(v): if len(v.constraints) == 0:return '' layout = '' for constraint in set(v.constraints): cons_obj = my(constraint.attrib) layout += mas_method(cons_obj.firstItem, cons_obj.firstAttribute, cons_obj.secondItem, cons_obj.secondAttribute, cons_obj.multiplier, cons_obj.constant, cons_obj.relation) s = ''' [self.${name} mas_makeConstraints: ^ (MASConstraintMaker * make){ ${layout} }]; ''' return string.Template(s).safe_substitute({'name':v.name,'layout':layout})
def genfile(path): viewlist = [] namelist = [] filetypelist = ['Cell', 'View', 'ViewController'] xibName = os.path.basename(path).split('.')[0] fileType = filter(lambda x: xibName.endswith(x), filetypelist)[0] vList = [ 'activityIndicatorView', 'pageControl', 'tableViewCellContentView', "view", "label", "textField", "textView", "button", "imageView", "switch", "tableView", "scrollView", 'segmentedControl' ] tree = etree.parse(path) root = tree.getroot() #find all views for child in root.iter(): if child.tag in vList: child.props = my(child.attrib) child.subviews = [] viewlist.append(child) #parse outlets names if child.tag == 'placeholder': if child.attrib['placeholderIdentifier'] == 'IBFilesOwner': for outLets in child.iter(): if outLets.attrib.get('property'): namelist.append(outLets) rootView = viewlist[0] rootView.isRoot = True for view in viewlist: view.constraints = [] for name in namelist: if view.props.id == name.attrib.get('destination'): view.name = name.attrib.get('property') if view.tag in vList: for child in view.iter(): # subviews if child.tag in vList: if child.attrib['id'] != view.props.id: view.subviews.append(child) child.parent = view elif child.tag == 'rect': view.rect = my(child.attrib) elif child.tag == 'connections': view.event = my(child.attrib) for sun in child: view.event = my(sun.attrib) elif child.tag == 'constraints': for constraint in child: constraint.father = view view.constraints.append(constraint) print 'begin temple render'.center(100, '*') subview_list = viewlist[1:] def layoutSubViews(v): if not hasattr(v, 'isRoot'): parentView = 'self.' + getname(v.parent) if hasattr(v.parent, 'isRoot'): parentView = 'self' if fileType == 'cell': parentView = 'self.contentView' parentView = 'self.view' if fileType == 'ViewController' else 'self' return '[' + parentView + ' addSubview:' + 'self' + '.' + getname( v) + ']' else: return '' def findConstraintsById(mid): return filter( lambda constraint: get_constraint_value(constraint.attrib, 'firstItem') == mid, viewlist[0].constraints) for view in subview_list: view.constraints.extend(findConstraintsById(view.attrib['id'])) def findViewById(id): for view in subview_list: if view.props.id == id: return getname(view) return '' masonrys = reduce( lambda x, y: x + y, map(lambda view: gen_mas(findViewById, view, fileType), subview_list)) buttons, getters = gen_events(map(lambda v: getname(v), subview_list), subview_list) html = render_template( 'result' + '.m', { 'props': '\n'.join(map(getPops, subview_list)), 'layoutSubViews': '\n'.join(map(layoutSubViews, viewlist)), 'touches': '\n'.join([x for x in map(buttonTouchs, viewlist) if len(x)]) + '\n', 'masonrys': masonrys, 'sums': getters }) openTemple(html, path)
def setImageViewProps(self): self.image = my(self.props.attrib).image if my( self.props.attrib).image else ''
def getProp_set(self): for node in self.props.iter(): if node.tag == 'userDefinedRuntimeAttributes': for mynode in node: try: self.mylayout += self.name + '.' + my( node.attrib).keyPath + '=' + my( mynode.attrib).value + ';\n' except: try: self.mylayout += self.name + '.' + my( node.attrib).keyPath + '=' + my( node.attrib).value + ';\n' except: if my(node.attrib).keyPath == 'mySize': self.mylayout += self.name + '.' + my( node.attrib).keyPath + 'CGSizeMake(' + my( mynode.attrib).width + my( mynode.attrib).height + ');\n' if my(node.attrib).keyPath == 'myCenter': self.mylayout += self.name + '.' + my( node.attrib ).keyPath + ' = CGPointMake(' + my( mynode.attrib).x + ',' + my( mynode.attrib).y + ');\n' else: print '' print self.mylayout if self.type == 'button': self.setButtonProps() if self.type == 'label': self.setLabelProps() if self.type == 'imageView': self.setImageViewProps() if self.type == 'tableView': print self.props try: return render_template(self.type + '.html', { 'name': self.name, 'ui': self }) except: with open('./uis/' + self.type + '.html', 'w') as f: f.write('_{{name}}.frame = {{ ui.frame }};') return render_template('normal_ui.html', { 'name': self.name, 'ui': self })