def __parse_data(self): """ Parse data header and extract columns indexes for type, id and languages. """ # find index of type column if self.TYPE in self.header: self.__type_column = self.header.index(self.TYPE) else: raise MstException("Can't fine %s column in header: %s" % (self.TYPE, self.header)) # find index of resource id column if self.__resource_column_name in self.header: self.__id_column = self.header.index(self.__resource_column_name) else: raise MstException("Can't find %s column in header: %s" % (self.__resource_column_name, self.header)) # find indexes of all language columns for language in self.__languages: if language in self.header: col = self.header.index(language) self.__language_column[language] = col else: raise MstException("Can't find %s column in header: %s" % (language, self.header)) # find index of options column if Spreadsheet.OPTIONS in self.header: self.__options_column = self.header.index(Spreadsheet.OPTIONS) else: raise MstException("Can't find %s column in header: %s" % (Spreadsheet.OPTIONS, self.header))
def merge(self, string): if( self.key != string.key ): raise MstException('Trying to merge String resources with different keys: %s != %s', (self.key, string.key) ) intersection = self.languages.intersection(string.languages) if( len( intersection ) > 0 ): raise MstException('Trying to merge Strings containing common languages: %s' % intersection ) for lang in string.languages: self.languages.add(lang) self.strings[lang] = string.get(lang)
def merge(self, array): if( self.key != array.key ): raise MstException('Trying to merge %s resources with different keys: %s != %s', (self.name, self.key, array.key) ) intersection = self.languages.intersection(array.languages) if( len( intersection ) > 0 ): raise MstException('Trying to merge %s containing common languages: %s' % (self.name, intersection) ) for lang in array.languages: self.__string_arrays[lang] = array.array(lang)
def merge(self, resource): if( self.key != resource.key ): raise MstException('Trying to merge %s resources with different keys: %s != %s', (self.name, self.key, resource.key) ) intersection = self.languages.intersection(resource.languages) if( len( intersection ) > 0 ): raise MstException('Trying to merge %s containing common languages: %s' % (self.name, intersection) ) for lang in resource.languages: for quantity in self.QUANTITIES: self.quantity_string[lang][quantity] = resource.get(lang, quantity)
def add_quantity_string(self, language, quantity, string): self.__validate_quantity(quantity) if language in self.__quantity_strings: quantities = self.__quantity_strings[ language ] quantities[quantity] = unicodedata.normalize('NFC', string) else: raise MstException("Can't add string to quantities dictionary. Requested language '%s' not existing" % language)
def add(self, language, index, string): if not isinstance(language,str) or not isinstance(index, int) or not isinstance(string, str): raise TypeError('Wrong argument types: %s, %s, %s' % (type(language), type(index), type(string)) ) if language in self.string_arrays: array = self.string_arrays[ language ] array[ int(index) ] = unicodedata.normalize('NFC', string) # fixme: index should be int else: raise MstException("Can't add string to array. Requested language '%s' not existing" % language)
def get_array(self, language): if language in self.string_arrays: array = self.string_arrays[ language ] else: raise MstException("Can't get string from array. Requested language '%s' not existing" % language) items = [] for index in sorted( array.keys() ): items.append( array[index] ) return items
def __load_data(self): try: gc = gspread.login(self.username, self.password) spreadsheet = gc.open(self.spreadsheet) worksheet = spreadsheet.worksheet(self.worksheet) self.data = worksheet.get_all_values() except: raise MstException("Cannot load Google Spreadsheet: %s" % str(self.__params))
def create_key_id(generator_type): d = { Factory.GENERATOR_ANDROID: "android_id", Factory.GENERATOR_ANDROID_XML: "android_id", Factory.GENERATOR_IOS: "ios_id" } try: return d[generator_type] except KeyError: msg = "Unknown generator requested: %s. I don't know how to choose key. Allowed: %s" % ( str(generator_type), sorted(list(d.keys()))) raise MstException(msg)
def create_generator(generator_type): d = { Factory.GENERATOR_ANDROID: generator.AndroidGenerator, Factory.GENERATOR_ANDROID_XML: generator.AndroidXmlGenerator, Factory.GENERATOR_IOS: generator.AppleGenerator } try: return d[generator_type]() # get type and call object constructor except KeyError: msg = "Unknown generator requested: %s. Allowed: %s" % ( str(generator_type), sorted(list(d.keys()))) raise MstException(msg)
def __init__(self, project_root, verbose=False, config_file='mst.cfg'): try: self.__root = os.path.abspath(project_root) self.__verbose = verbose file_path = os.path.join(self.root, config_file) file = open(file_path, 'r') data = json.load(file) self.__generator = data['generator'] self.__paths = data['paths'] self.__sorted = data.get('sorted', True) except FileNotFoundError: raise MstException("Cannot open config file: %s" % os.path.join(project_root, config_file))
def __init__(self, file): Loader.__init__(self) self.__file = file try: csv_file = open(file, 'r') except: raise MstException("Cannot open CSV file with resources: %s" % file) csv_reader = csv.reader(csv_file) rows = [] for row in csv_reader: rows.append(row) self.data = rows
parser.add_argument('-G', '--google-loader', nargs=4, metavar=('EMAIL', 'PASSWORD', 'SPREADSHEET', 'WORKSHEET'), help='Load data from Google Docs spreadsheet') parser.add_argument('-c', '--config', nargs=1, default=['mst.cfg'], help='Configuration file') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose mode') args = parser.parse_args() # load configuration from project root config = Config( args.project_root[0], args.verbose, args.config[0] ) Log.init(args.verbose) # create loader - this will also load resources data if args.csv_loader == None and args.google_loader != None: loader = Factory.create_loader( Factory.LOADER_GOOGLEDOCS, args.google_loader ) elif args.csv_loader != None and args.google_loader == None: loader = Factory.create_loader( Factory.LOADER_CSV, args.csv_loader ) else: raise MstException("""No loader defined in command line. I don't know how to load translations. RTF(riendly)M.""") key_id = Factory.create_key_id(config.generator) # create a spreadsheet with loaded data sheet = Spreadsheet(key_id, loader.data, config.languages) # extract resources from the spreadsheet strings = sheet.get_strings() string_arrays = sheet.get_string_arrays() quantity_strings = sheet.get_quantity_strings() Log.print( 'Project root: %s' % config.root ) Log.print( 'Generator: %s' % config.generator ) Log.print( 'Loader: %s' % loader ) Log.print( 'Languages: %s' % ', '.join(config.languages) )
def get(self, language, index): if language in self.string_arrays: array = self.string_arrays[ language ] return array[index] else: raise MstException("Can't get string from array. Requested language '%s' not existing" % language)
def get(self, language): if language in self.strings: return self.strings[ language ] else: raise MstException("Can't get string. Requested language '%s' not existing" % language)
def add(self, language, string): if language in self.strings: self.strings[ language ] = unicodedata.normalize('NFC', string) else: raise MstException("Can't add string. Requested language '%s' not existing" % language)
def size(self, language): if language in self.string_arrays: array = self.string_arrays[ language ] return len( array ) else: raise MstException("Can't get string array. Requested language '%s' not existing" % language)
def get_quantity_string(self, language, quantity): if language in self.__quantity_strings: quantities = self.__quantity_strings[ language ] return quantities[quantity] else: raise MstException("Can't get string from quantities dictionary. Requested language '%s' not existing" % language)