-
Notifications
You must be signed in to change notification settings - Fork 3
/
build-obj-c-classes.py
executable file
·790 lines (638 loc) · 24.7 KB
/
build-obj-c-classes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
#
# Creates Objective-C classes from our ontology.
#
# Pass in the "-f" flag if you want to overwrite existing classes
#
### config ###
_obj_c_class_prefix = 'SM'
_template_dir = 'Generator'
_generated_classes_dir = 'GeneratedClasses'
_class_examples_dir = 'SMARTFrameworkTests/RDF'
_class_unittests_dir = 'SMARTFrameworkTests/ClassTests'
### there's probably no need to edit anything beyond this line ###
### ---------------------------------------------------------- ###
_classes_to_ignore = [
'http://www.w3.org/2001/XMLSchema#anyURI',
'http://www.w3.org/2000/01/rdf-schema#Literal',
'http://www.w3.org/2006/vcard/ns#Cell',
'http://www.w3.org/2006/vcard/ns#Home',
'http://www.w3.org/2006/vcard/ns#Pref',
'http://www.w3.org/2006/vcard/ns#Work',
'http://smartplatforms.org/terms/api#Call',
'http://smartplatforms.org/terms/api#Filter',
'http://smartplatforms.org/terms/api#Parameter',
'http://smartplatforms.org/terms/api#ParameterSet',
'http://smartplatforms.org/terms#AppManifest',
'http://smartplatforms.org/terms#Component',
'http://smartplatforms.org/terms#ContainerManifest',
'http://smartplatforms.org/terms#Ontology',
'http://smartplatforms.org/terms#ScratchpadData', # curated by hand
'http://smartplatforms.org/terms#SMARTAPI',
'http://smartplatforms.org/terms#UserPreferences',
'http://smartplatforms.org/terms#VCardLabels',
]
_templates = {}
_templates['property_header'] = """/// Representing {{ uri }} as {{ itemClass }}
{{ add_comment }}@property (nonatomic, {{ strength }}) {{ useClass }} *{{ name }};"""
_templates['literal_getter'] = """@synthesize {{ name }} = _{{ name }};
- ({{ itemClass }} *){{ name }}
{
if (!_{{ name }}) {
RedlandNode *predicate = [RedlandNode nodeWithURIString:@"{{ uri }}"];
RedlandStatement *statement = [RedlandStatement statementWithSubject:self.subject predicate:predicate object:nil];
RedlandStreamEnumerator *query = [self.inModel enumeratorOfStatementsLike:statement];
RedlandStatement *rslt = [query nextObject];
_{{ name }} = [rslt.object literalValue];
}
return _{{ name }};
}
- (void){{ setName }}:({{ itemClass }} *){{ name }}
{
if ({{ name }} != _{{ name }}) {
RedlandNode *predicate = ({{ name }} || _{{ name }}) ? [RedlandNode nodeWithURIString:@"{{ uri }}"] : nil;
if (_{{ name }}) {
[self.inModel removeStatementsLike:[RedlandStatement statementWithSubject:self.subject predicate:predicate object:nil]];
}
_{{ name }} = [{{ name }} copy];
if (_{{ name }}) {
[self.inModel addStatement:[RedlandStatement statementWithSubject:self.subject predicate:predicate object:[_{{ name }} nodeValue]]];
}
}
}"""
_templates['multi_literal_getter'] = """@synthesize {{ name }} = _{{ name }};
- (NSArray *){{ name }}
{
if (!_{{ name }}) {
RedlandNode *predicate = [RedlandNode nodeWithURIString:@"{{ uri }}"];
RedlandStatement *statement = [RedlandStatement statementWithSubject:self.subject predicate:predicate object:nil];
RedlandStreamEnumerator *query = [self.inModel enumeratorOfStatementsLike:statement];
// loop results
NSMutableArray *arr = [NSMutableArray array];
RedlandStatement *rslt = nil;
while ((rslt = [query nextObject])) {
{{ itemClass }} *newItem = [rslt.object literalValue]; // only works for NSString for now
if (newItem) {
[arr addObject:newItem];
}
}
_{{ name }} = [arr copy];
}
return _{{ name }};
}
- (void){{ setName }}:(NSArray *){{ name }}
{
if ({{ name }} != _{{ name }}) {
RedlandNode *predicate = ({{ name }} || _{{ name }}) ? [RedlandNode nodeWithURIString:@"{{ uri }}"] : nil;
if (_{{ name }}) {
[self.inModel removeStatementsLike:[RedlandStatement statementWithSubject:self.subject predicate:predicate object:nil]];
}
_{{ name }} = [{{ name }} copy];
if (_{{ name }}) {
for ({{ itemClass }} *newItem in _{{ name }}) {
[self.inModel addStatement:[RedlandStatement statementWithSubject:self.subject predicate:predicate object:[newItem nodeValue]]];
}
}
}
}"""
_templates['model_getter'] = """@synthesize {{ name }} = _{{ name }};
- ({{ itemClass }} *){{ name }}
{
if (!_{{ name }}) {
// get the "{{ name }}" element
RedlandNode *predicate = [RedlandNode nodeWithURIString:@"{{ uri }}"];
RedlandStatement *statement = [RedlandStatement statementWithSubject:self.subject predicate:predicate object:nil];
RedlandStreamEnumerator *query = [self.inModel enumeratorOfStatementsLike:statement];
RedlandStatement *rslt = [query nextObject];
// create an item for this object
{{ itemClass }} *obj = [{{ itemClass }} newWithSubject:rslt.object inModel:self.inModel];
_{{ name }} = obj ? obj : (id)[NSNull null];
}
// we use NSNull as a placeholder in case we already searched the graph and haven't found the object. This should help with performance.
if ((id)[NSNull null] == _{{ name }}) {
return nil;
}
return _{{ name }};
}
- (void){{ setName }}:({{ itemClass }} *){{ name }}
{
if ({{ name }} != _{{ name }}) {
RedlandNode *predicate = ({{ name }} || _{{ name }}) ? [RedlandNode nodeWithURIString:@"{{ uri }}"] : nil;
if (_{{ name }}) {
[self.inModel removeSubmodel:_{{ name }}.model];
[self.inModel removeStatementsLike:[RedlandStatement statementWithSubject:self.subject predicate:predicate object:nil]];
}
_{{ name }} = {{ name }};
if (_{{ name }}) {
[self.inModel addStatement:[RedlandStatement statementWithSubject:self.subject predicate:predicate object:_{{ name }}.subject]];
[self.inModel addSubmodel:_{{ name }}.model];
}
}
}"""
_templates['multi_model_getter'] = """@synthesize {{ name }} = _{{ name }};
- (NSArray *){{ name }}
{
if (!_{{ name }}) {
// get the "{{ name }}" elements
RedlandNode *predicate = [RedlandNode nodeWithURIString:@"{{ uri }}"];
RedlandStatement *statement = [RedlandStatement statementWithSubject:self.subject predicate:predicate object:nil];
RedlandStreamEnumerator *query = [self.inModel enumeratorOfStatementsLike:statement];
// loop through the results
NSMutableArray *arr = [NSMutableArray array];
RedlandStatement *rslt = nil;
while ((rslt = [query nextObject])) {
// instantiate an item for each object
{{ itemClass }} *newItem = [{{ itemClass }} newWithSubject:rslt.object inModel:self.inModel];
if (newItem) {
[arr addObject:newItem];
}
}
_{{ name }} = [arr copy];
}
return _{{ name }};
}
- (void){{ setName }}:(NSArray *){{ name }}
{
if ({{ name }} != _{{ name }}) {
RedlandNode *predicate = ({{ name }} || _{{ name }}) ? [RedlandNode nodeWithURIString:@"{{ uri }}"] : nil;
if (_{{ name }}) {
for ({{ itemClass }} *item in _{{ name }}) {
[self.inModel removeSubmodel:item.model];
}
[self.inModel removeStatementsLike:[RedlandStatement statementWithSubject:self.subject predicate:predicate object:nil]];
}
_{{ name }} = [{{ name }} copy];
if (_{{ name }}) {
for ({{ itemClass }} *item in _{{ name }}) {
[self.inModel addStatement:[RedlandStatement statementWithSubject:self.subject predicate:predicate object:item.subject]];
[self.inModel addSubmodel:item.model];
}
}
}
}"""
_templates['class_base_path_getter'] = """+ (NSString *)basePath
{
return @"{{ base_path }}";
}"""
_templates['record_multi_item_getter'] = """/**
* {{ description }}.
*
* Makes a call to {{ path }}, originally named "{{ orig_name }}".
* @param callback A SMSuccessRetvalueBlock block that will have a success flag and a user info dictionary containing
* the desired objects (key: SMARTResponseArrayKey) if successful.
*/
{{ method_signature }}
{
NSString *path = [NSString stringWithFormat:@"{{ nsstring_path }}", self.record_id];
[self getObjectsOfClass:[{{ item_class }} class] from:path callback:callback];
}"""
_templates['class_unit_test'] = """/**
* Testing {{ CLASS_NAME }}
*/
- (void)test{{ CLASS_NAME }}
{
NSString *rdfPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"{{ CLASS_NAME }}" ofType:@"rdf"];
STAssertNotNil(rdfPath, @"Expecting an RDF file for the class {{ CLASS_NAME }}");
NSString *rdf = [NSString stringWithContentsOfFile:rdfPath encoding:NSUTF8StringEncoding error:nil];
{{ CLASS_NAME }} *item = [{{ CLASS_NAME }} newWithRDFXML:rdf];
STAssertNotNil(item, @"Failed to parse RDF");
if (item) {
{{ ITEM_TESTS }};
}
}
"""
### ---------------------------------------------------------- ###
import os
import sys
import re
import urllib2
import datetime
print 'Use -v for verbose or -f to force overriding existing classes'
print '--> Parsing ontology'
from smart_common.rdf_tools import rdf_ontology
_arguments = sys.argv[1:] if len(sys.argv) > 1 else []
_overwrite = '-f' in _arguments
_verbose = '-v' in _arguments
_known_classes = {} # will be { SMART name: Obj-C class name }
_valid_values = {} # will be { SMART name: { key: val } }
_record_calls = []
_single_item_calls = []
_classes_written = []
def toObjCClassName(name):
"""Converts any name into a hopefully acceptable Objective-C class name,
prepending the prefix defined in "_obj_c_class_prefix".
"""
basename = None
if name and len(name) > 1:
parts = re.split(r'[-_\W]', name)
real_parts = []
for p in parts:
if p and len(p) > 1:
real_parts.append(p)
basename = ''.join(['%s%s' % (p[0].upper(), p[1:]) for p in real_parts])
elif name:
basename = name.upper()
if basename:
return '%s%s' % (_obj_c_class_prefix, basename)
return None;
def toObjCPropertyName(name):
"""Converts any property name into a hopefully acceptable Objective-C
property name.
"""
if name and len(name) > 1:
parts = re.split(r'[-_\W]', name)
real_parts = []
for p in parts:
if p and len(p) > 1:
real_parts.append(p)
cap = ''.join(['%s%s' % (p[0].upper(), p[1:]) for p in real_parts])
return '%s%s' % (cap[0].lower(), cap[1:])
return name.lower() if name else None
def handle_class(a_class):
""" Returns a tuple with the Objective-C class name to use with the given class, or None if we don't want this,
class, and a dictionary of valid coded values for the class, if those are defined and is applicable to the class.
Feed it a SMART_Class for which it should create an Objective-C class for, this class then fills a dictionary with
the values for template keys. The dictionary can then be used to substitute placeholders in the class template
files and contains:
- CLASS_NAME
- CLASS_SUPERCLASS
- CLASS_FORWARDS
- CLASS_PROPERTIES
- CLASS_GETTERS
- BASE_PATH
- RDF_TYPE
- AUTHOR
- DATE
- YEAR
- EXAMPLE
"""
global _known_classes
global _valid_values
identifier = str(a_class.uri)
# already handled?
if identifier in _known_classes:
valid = _valid_values[identifier] if identifier in _valid_values else None
return _known_classes[identifier], valid
# is this an item we want a class for?
if identifier in _classes_to_ignore:
if _verbose:
print 'IGNORING %s [%s]' % (a_class.name, a_class.uri)
return None, None
# start the dictionary
now = datetime.date.today()
base_path = a_class.base_path
class_name = toObjCClassName(a_class.name)
myDict = {
'CLASS_NAME': class_name,
'CLASS_SUPERCLASS': 'SMBaseDocument' if base_path else 'SMObject',
'BASE_PATH': None,
'RDF_TYPE': unicode(a_class.uri),
'AUTHOR': __file__,
'DATE': str(now),
'YEAR': str(now.year),
'EXAMPLE': a_class.example,
}
# special case: all our code types we simplify to coded value
if identifier.startswith('http://smartplatforms.org/terms/codes/'):
if _verbose:
print 'CODED %s describes a SMART code and will become Coded Value' % a_class.name
class_name = 'SMCodedValue'
_known_classes[identifier] = class_name
# equivalents are our "valid values"
equiv = None
if a_class.equivalent_classes is not None and len(a_class.equivalent_classes) > 0:
equiv = {}
for e in a_class.equivalent_classes:
for eq in e.one_of:
equiv[unicode(eq.uri)] = unicode(eq.title)
_valid_values[identifier] = equiv
return class_name, equiv
# add our class to the known classes dict
_known_classes[identifier] = class_name
c_forwards = set()
prop_statements = []
prop_getter = []
# get properties that represent other classes (OWL_ObjectProperty instances)
my_properties = []
for o_prop in a_class.object_properties:
# o_prop.multiple_cardinality -> Bool whether the property can have multiple items
# o_prop.to_class -> SMART_Class represented by the property
# o_prop.to_class.name -> The name of the class
# o_prop.to_class.uri -> Class URI
# handle the property class
itemClass, valid = handle_class(o_prop.to_class)
prop_class_id = str(o_prop.to_class.uri)
c_forwards.add(itemClass)
prop_name = toObjCPropertyName(o_prop.name);
prop = {
'name': prop_name,
'setName': "set%s%s" % (prop_name[0].upper(), prop_name[1:]),
'uri': o_prop.uri,
'itemClass': itemClass,
'useClass': 'NSArray' if o_prop.multiple_cardinality else itemClass,
'strength': 'copy' if o_prop.multiple_cardinality else 'strong',
'_template': 'multi_model_getter' if o_prop.multiple_cardinality else 'model_getter'
}
# if we have valid values, write a nice comment
if prop_class_id in _valid_values:
comment = "///\n/// The codes in this property should be:\n///\n"
for key, val in _valid_values[prop_class_id].iteritems():
comment += "/// - \"%s\": \t%s\n" % (key, val)
prop['add_comment'] = comment
my_properties.append(prop)
# get data properties (OWL_DataProperty instances)
for d_prop in a_class.data_properties:
primitive = 'NSString' # TODO: When to use NSNumber or NSDate?
prop_name = toObjCPropertyName(d_prop.name)
prop = {
'name': prop_name,
'setName': "set%s%s" % (prop_name[0].upper(), prop_name[1:]),
'uri': d_prop.uri,
'itemClass': primitive,
'useClass': 'NSArray' if d_prop.multiple_cardinality else primitive,
'strength': 'copy',
'_template': 'multi_literal_getter' if d_prop.multiple_cardinality else 'literal_getter'
}
my_properties.append(prop)
# sort and apply property dicts to the templates
my_properties = sorted(my_properties, key=lambda k: k['name'])
my_property_tests = []
for prop in my_properties:
stmt = apply_template(_templates['property_header'], prop)
prop_statements.append(stmt)
getter = apply_template(_templates[prop['_template']], prop)
prop_getter.append(getter)
# prepare unit test syntax
item_prop = 'item.%s' % prop['name']
item_comment = '@"Expecting \\\"%s\\\" to be of class %s, but is %%@"' % (prop['name'], prop['useClass'])
item_class = 'NSStringFromClass([%s class])' % item_prop
item_test = 'STAssertTrue(!%s || [%s isKindOfClass:[%s class]], %s, %s)' % (item_prop, item_prop, prop['useClass'], item_comment, item_class)
my_property_tests.append(item_test)
# base path
if base_path:
# we need to convert "allergy_id", "medication_id" and the like to "uuid"
base_path = re.sub(r'(\{record_id\}/\w+/\{\s*)[a-z]+_id(\})', '\g<1>uuid\g<2>', base_path)
t_base = apply_template(_templates['class_base_path_getter'], {'base_path': base_path})
myDict['BASE_PATH'] = t_base
# add properties to our dict
myDict['CLASS_FORWARDS'] = '@class %s;' % ', '.join(sorted(c_forwards)) if len(c_forwards) > 0 else ''
myDict['CLASS_PROPERTIES'] = "\n\n".join(prop_statements)
myDict['CLASS_GETTERS'] = "\n\n".join(prop_getter)
if len(my_property_tests) > 0:
myDict['ITEM_TESTS'] = ";\n\t\t".join(my_property_tests)
# calls for this class (SMART_API_Call instances)
if a_class.calls and len(a_class.calls) > 0:
for api in a_class.calls:
handle_class_method(class_name, api)
# output the RDF example for the class
if write_class_example(myDict, _overwrite):
print '--> Wrote %s example' % class_name
# collect unit tests
global class_tests
unit_test = synthesize_class_tests(myDict)
if unit_test is not None:
class_tests.append(unit_test)
# write class files
if write_class(myDict, _overwrite):
print '--> Wrote class %s' % class_name
return class_name, None
def write_class(class_dict, overwrite=False):
""" Writes the .h and .m file for the given class
"""
written = False
# write the header
filename_h = '%s.h' % class_dict['CLASS_NAME']
path_h = os.path.join(_generated_classes_dir, filename_h)
if overwrite or not os.path.exists(path_h):
header = apply_template(_templates['ClassTemplate.h'], class_dict)
if header:
handle = open(path_h, 'w')
handle.write(header)
written = True
# write the implementation
filename_m = '%s.m' % class_dict['CLASS_NAME']
path_m = os.path.join(_generated_classes_dir, filename_m)
if overwrite or not os.path.exists(path_m):
implem = apply_template(_templates['ClassTemplate.m'], class_dict)
if implem:
handle = open(path_m, 'w')
handle.write(implem)
written = True
_classes_written.append(class_dict['CLASS_NAME'])
return written
def write_class_example(class_dict, overwrite=False):
""" Checks if the class has an RDF example and if yes, writes the RDF to file,
returns True if stuff was written.
"""
# bail out if there is no example
if class_dict is None \
or 'EXAMPLE' not in class_dict \
or not class_dict['EXAMPLE'] \
or len(class_dict['EXAMPLE']) < 1:
return False
# write the RDF
path_e = os.path.join(_class_examples_dir, '%s.%s' % (class_dict['CLASS_NAME'], 'rdf'))
if overwrite or not os.path.exists(path_e):
handle = open(path_e, 'w')
handle.write(class_dict['EXAMPLE'])
return True
return False
def synthesize_class_tests(class_dict):
""" Generates an Objective-C method that can be run against SenTestKit
"""
test_method = None
# bail out if there is no example
if class_dict is None \
or 'EXAMPLE' not in class_dict \
or not class_dict['EXAMPLE'] \
or len(class_dict['EXAMPLE']) < 1:
return test_method
# generate unit tests
if 'ITEM_TESTS' in class_dict and len(class_dict['ITEM_TESTS']) > 0:
test_method = apply_template(_templates['class_unit_test'], class_dict)
#print "----\n%s\n----" % test_method
return test_method
def handle_class_method(class_name, api):
""" Handles API calls defined on a class.
This method just puts them into globals and lets the main exect handle them.
class_name: A string with the Objective-C class name
api: SMART_API_Call instance
"""
# we can only use record-scoped calls
if 'record' == api.category:
orig_name = api.guess_name()
method_name = toObjCPropertyName(orig_name)
cDict = {
'orig_name': orig_name,
'http_method': api.http_method,
'item_class': class_name,
'path': str(api.path),
'nsstring_path': str(re.sub(r'(\{\s*\w+\s*\})', '%@', api.path)),
'description': str(api.description),
}
# synthesize the method name:
# getX:(block)callback
# getXForY:(NSString *)y callback:(block)callback
usable = []
prefix = '- (void)'
block_arg = '(SMSuccessRetvalueBlock)callback'
# extract placeholders from the path
placeholders = re.findall(r'\{\s*(\w+)\s*\}', api.path)
guessed_item_id_name = orig_name.replace('get_', '')
# put record-level getters in the array
if 1 == len(placeholders) and 'record_id' == placeholders[0]:
global _record_calls
if 'GET' == api.http_method:
cDict['method_signature'] = '%s%s:%s' % (prefix, method_name, block_arg)
_record_calls.append(cDict)
# find the "get one item" methods (first param 'record_id', second 'item_id')
elif 2 == len(placeholders) and 'record_id' == placeholders[0]:
global _single_item_calls
_single_item_calls.append(orig_name)
# other class related calls: there currently are none and we're not doing anything with these
else:
for p in placeholders:
if 'record_id' != p:
argname = toObjCPropertyName(p)
pname = argname if len(usable) > 0 else toObjCPropertyName('%s_for_%s' % (orig_name, p))
usable.append('%s:(NSString *)%s' % (pname, argname))
if len(usable) > 0:
usable.append('callback:%s' % block_arg)
cDict['method_name'] = ' '.join(usable)
else:
cDict['method_name'] = '%s:%s' % (toObjCPropertyName(orig_name), block_arg)
def read_template(template_name):
"""Looks for a template with the given filename and returns its contents"""
template_path = '%s/%s' % (_template_dir, template_name)
if not os.path.exists(template_path):
print 'xx> No template could be found at %s' % template_path
return None
return open(template_path).read()
def apply_template(template, subst):
"""Substitutes all values of the "subst" dictionary in the template with its
values
"""
if not template:
print 'xx> No template given'
return None
# apply the known items
applied = template
for k, v in subst.iteritems():
applied = re.sub('\{\{\s*' + k + '\s*\}\}', v if v else '', applied)
# remove unknown items
matches = re.findall('\{\{[^\}]+\}\}', applied)
if matches is not None:
for match in matches:
applied = applied.replace(match, '')
return applied
if __name__ == "__main__":
"""Outputs Objective-C classes to be used in our iOS framework
"""
# grab the template files
for f in ['ClassTemplate.h', 'ClassTemplate.m', 'CategoryTemplate.h', 'CategoryTemplate.m', 'UnitTestTemplate.h', 'UnitTestTemplate.m']:
template = read_template(f)
if template is None:
print 'xx> Failed to load template %s' % f
sys.exit(1)
_templates[f] = template
# prepare to grab classes
if not os.path.exists(_generated_classes_dir):
os.mkdir(_generated_classes_dir)
if not os.access(_generated_classes_dir, os.W_OK):
print "xx> Can't write classes to %s" % _generated_classes_dir
sys.exit(1)
if not os.path.exists(_class_examples_dir):
os.mkdir(_class_examples_dir)
if not os.access(_class_examples_dir, os.W_OK):
print "xx> Can't write test RDF to %s" % _class_examples_dir
sys.exit(1)
if not os.path.exists(_class_unittests_dir):
os.mkdir(_class_unittests_dir)
if not os.access(_class_unittests_dir, os.W_OK):
print "xx> Can't write unit tests to %s" % _class_unittests_dir
sys.exit(1)
print '--> Processing classes'
class_tests = []
num_classes = 0
num_calls = 0
# loop all SMART_Class instances
for a_class in rdf_ontology.api_types:
if handle_class(a_class)[0] is not None:
num_classes += 1
# write unit tests
class_tests = sorted(class_tests)
complete_tests = "\n\n".join(class_tests) if len(class_tests) > 0 else None
if complete_tests is not None:
now = datetime.date.today()
test_dict = {
'COMPLETE_TEST_METHODS': complete_tests,
'AUTHOR': __file__,
'DATE': str(now),
'YEAR': str(now.year)
}
path_h = os.path.join(_class_unittests_dir, 'TestGeneratedClasses.h')
path_m = os.path.join(_class_unittests_dir, 'TestGeneratedClasses.m')
header = apply_template(_templates['UnitTestTemplate.h'], test_dict)
handle = open(path_h, 'w')
handle.write(header)
implem = apply_template(_templates['UnitTestTemplate.m'], test_dict)
handle = open(path_m, 'w')
handle.write(implem)
print '--> Wrote %d class unit tests' % len(class_tests)
# put record-scoped calls into a record category (only GET needs synthesized methods)
used_call_names = _single_item_calls
record_sigs = []
record_calls = []
for api in _record_calls:
if 'GET' == api['http_method']:
used_call_names.append(api['orig_name'])
call = apply_template(_templates['record_multi_item_getter'], api)
record_sigs.append('%s;' % api['method_signature'])
record_calls.append(call)
# warn about the api calls that we did ignore
if _verbose:
for api in rdf_ontology.api_calls:
orig_name = api.guess_name()
if orig_name not in used_call_names:
print 'IGNORED API call: %s (level: %s)' % (orig_name, api.category)
# write to SMRecord category
if len(record_sigs) > 0:
written = False
record_sigs = sorted(record_sigs)
record_calls = sorted(record_calls)
now = datetime.date.today()
d = {
'CATEGORY_CLASS': 'SMRecord',
'CATEGORY_NAME': 'Calls',
'METHOD_SIGNATURES': "\n".join(record_sigs),
'FULL_METHODS': "\n\n".join(record_calls),
'AUTHOR': __file__,
'DATE': str(now),
'YEAR': str(now.year),
}
# write the header
filename_h = '%s+%s.h' % (d['CATEGORY_CLASS'], d['CATEGORY_NAME'])
path_h = os.path.join(_generated_classes_dir, filename_h)
if _overwrite or not os.path.exists(path_h):
header = apply_template(_templates['CategoryTemplate.h'], d)
handle = open(path_h, 'w')
handle.write(header)
written = True
# finish the implementation
filename_m = '%s+%s.m' % (d['CATEGORY_CLASS'], d['CATEGORY_NAME'])
path_m = os.path.join(_generated_classes_dir, filename_m)
if _overwrite or not os.path.exists(path_m):
implem = apply_template(_templates['CategoryTemplate.m'], d)
handle = open(path_m, 'w')
handle.write(implem)
written = True
if written:
print '--> Wrote category %s on %s' % (d['CATEGORY_NAME'], d['CATEGORY_CLASS'])
num_calls += 1
# all classes are done
print '--> %d classes and %d categories processed, %d classes written.' % (num_classes, num_calls, len(_classes_written))
print '--> SMARTObjects.h'
# output class imports
classnames = sorted(set(_known_classes.values()), key=lambda s: s.lower())
for name in classnames:
print '#import "%s.h"' % name
print '-> Done'