Esempio n. 1
0
    def testVoidDataTypeOverride(self):
        class FakeLM(language_model.LanguageModel):
            def CodeTypeForVoid(self):
                return 'the absence of all'

        api = template_objects.CodeObject({}, None)
        void = data_types.Void(api)
        api.SetLanguageModel(FakeLM())
        self.assertEquals('the absence of all', void.code_type)
Esempio n. 2
0
    def __init__(self, discovery_doc, language=None):
        super(Api, self).__init__(discovery_doc, self)
        name = self.values['name']
        self._validator.ValidateApiName(name)
        self._validator.ValidateApiVersion(self.values['version'])
        self._class_name = utilities.CamelCase(name)
        self._language = language
        self._template_dir = None
        self._surface_features = {}
        self._schemas = {}
        self.void_type = data_types.Void(self)

        self.SetTemplateValue('className', self._class_name)
        self.SetTemplateValue('versionNoDots',
                              self.values['version'].replace('.', '_'))
        self.SetTemplateValue(
            'dataWrapper', 'dataWrapper' in discovery_doc.get('features', []))

        self._BuildSchemaDefinitions()
        self._BuildResourceDefinitions()
        self.SetTemplateValue('resources', self._resources)

        # Make data models part of the api dictionary
        self.SetTemplateValue('models', self.ModelClasses())

        # Replace methods dict with Methods
        self._methods = []
        for name, method_dict in self.values.get('methods', {}).iteritems():
            self._methods.append(Method(self, name, method_dict))
        self.SetTemplateValue('methods', self._methods)

        # Global parameters
        self._parameters = []
        for name, param_dict in self.values.get('parameters', {}).iteritems():
            self._parameters.append(Parameter(self, name, param_dict, self))
        self.SetTemplateValue('parameters', self._parameters)

        # Auth scopes
        self._authscopes = []
        if (self.values.get('auth') and self.values['auth'].get('oauth2')
                and self.values['auth']['oauth2'].get('scopes')):
            for value, auth_dict in sorted(
                    self.values['auth']['oauth2']['scopes'].iteritems()):
                self._authscopes.append(AuthScope(self, value, auth_dict))
            self.SetTemplateValue('authscopes', self._authscopes)
Esempio n. 3
0
    def __init__(self, discovery_doc, language=None):
        super(Api, self).__init__(discovery_doc,
                                  self,
                                  wire_name=discovery_doc['name'])
        name = self.values['name']
        self._validator.ValidateApiName(name)
        if name != 'freebase':
            self._validator.ValidateApiVersion(self.values['version'])
        canonical_name = self.values.get('canonicalName') or name
        if not self.values.get('canonicalName'):
            self.values['canonicalName'] = canonical_name
        self._class_name = self.ToClassName(canonical_name, self)
        # Guard against language implementor not taking care of spaces
        self._class_name = self._class_name.replace(' ', '')
        self._NormalizeOwnerInformation()
        self._language = language
        self._template_dir = None
        self._surface_features = {}
        self._schemas = {}
        self._methods_by_name = {}
        self._all_methods = []

        self.SetTemplateValue('className', self._class_name)
        self.SetTemplateValue('versionNoDots',
                              self.values['version'].replace('.', '_'))
        self.SetTemplateValue('versionNoDash',
                              self.values['version'].replace('-', '_'))
        self.SetTemplateValue(
            'dataWrapper', 'dataWrapper' in discovery_doc.get('features', []))
        self.values.setdefault('title', name)
        self.values.setdefault('exponentialBackoffDefault', False)
        if not self.values.get('revision'):
            self.values['revision'] = 'snapshot'

        self._NormalizeUrlComponents()

        # Information for variant subtypes, a dictionary of the format:
        #
        #  { 'wireName': {'discriminant': discriminant, 'value': value,
        #                 'schema': schema},
        #    ... }
        #
        # ... where wireName is the name of variant subtypes, discriminant
        # the field name of the discriminant, value the discriminant value
        # for this variant, and schema the base schema.
        #
        # This information cannot be stored in the referred schema at
        # reading time because at the time we read it from the base
        # schema, the referenced variant schemas may not yet be loaded. So
        # we first store it here, and after all schemas have been loaded,
        # update the schema template properties.
        self._variant_info = {}

        # Build data types and methods
        self._SetupModules()
        self.void_type = data_types.Void(self)
        self._BuildSchemaDefinitions()
        self._BuildResourceDefinitions()
        self.SetTemplateValue('resources', self._resources)

        # Make data models part of the api dictionary
        self.SetTemplateValue('models', self.ModelClasses())

        # Replace methods dict with Methods
        self._top_level_methods = []
        method_dict = self.values.get('methods') or {}
        for name in sorted(method_dict):
            self._top_level_methods.append(
                Method(self, name, method_dict[name]))
        self.SetTemplateValue('methods', self._top_level_methods)

        # Global parameters
        self._parameters = []
        param_dict = self.values.get('parameters') or {}
        for name in sorted(param_dict):
            parameter = Parameter(self, name, param_dict[name], self)
            self._parameters.append(parameter)
            if name == 'alt':
                self.SetTemplateValue('alt', parameter)
        self.SetTemplateValue('parameters', self._parameters)

        # Auth scopes
        self._authscopes = []
        if (self.values.get('auth') and self.values['auth'].get('oauth2')
                and self.values['auth']['oauth2'].get('scopes')):
            for value, auth_dict in sorted(
                    self.values['auth']['oauth2']['scopes'].iteritems()):
                self._authscopes.append(AuthScope(self, value, auth_dict))
            self.SetTemplateValue('authscopes', self._authscopes)
Esempio n. 4
0
 def testVoidDataTypeDefault(self):
     api = template_objects.CodeObject({}, None)
     void = data_types.Void(api)
     api.SetLanguageModel(language_model.LanguageModel())
     self.assertEquals('void', void.code_type)