def test_normalize(self): tests = [ ('HELLO', 'hello'), ('Hello.World', 'hello.world'), ('HELLO_wOrLd', 'hello_world'), ('Hello, World!', 'hello-world'), (' hello,world!!', 'hello-world'), ('helloWorld', 'helloworld'), (' hello,,,,, world!!! ', 'hello-world'), ('### ! Hello World @ Large ! ###', 'hello-world-large') ] for tset in tests: self.assertEqual(Tag.normalize(tset[0]), tset[1], tset[0])
def convert(self): print( F"--- Converting Metiisto/{self.m_cfg['name']} to Cartaro/{self.c_cfg['name']} ---" ) obj_cursor = self.metiisto.cursor() # Fetch Metiisto data fld_str = ",".join(self.m_cfg['fields']) obj_sql = F"select id,{fld_str} from {self.m_cfg['name']} order by id" if self.opts.get('limit', None): obj_sql += F" limit {self.opts['limit']}" print(F" => {obj_sql}") tags = {} if self.opts.get("has_tags", False): tag_class = self.opts['tag_class'] tag_sql = F"select tagged_object.obj_id, tags.name from tags, tagged_object where obj_class='{tag_class}' and tags.id = tagged_object.tag_id order by obj_id" print(F" => {tag_sql}") tag_cursor = self.metiisto.cursor() tag_cursor.execute(tag_sql) for row in tag_cursor: note_id = row[0] if note_id not in tags: tags[note_id] = [] tags[note_id].append(Tag.normalize(row[1])) count = 0 obj_cursor.execute(obj_sql) for row in obj_cursor: record = {} obj_id = row[0] if self.opts.get('interactive', False): print( "-------------------------------------------------------") pprint.pprint(row, indent=2) print( "-------------------------------------------------------") # row[0] == DB `id`, skip for mapping mapping = zip(self.c_cfg['fields'], row[1:]) for (name, raw_value) in mapping: value = raw_value if raw_value and name.endswith('_at'): date = arrow.get(raw_value, Base.TIMEZONE) value = date.timestamp elif name.startswith('is_'): value = True if raw_value else False transformer = self.opts.get(F"{name}_transformer", None) if transformer: value = transformer( value, record=record, interactive=self.opts.get('interactive')) # print(F"{name} == {value} - {type(value)}") record[name] = value if self.opts.get("has_tags", False): record['tags'] = tags.get(obj_id, []) for tag in self.opts.get("additional_tags", []): if not 'tags' in record: record['tags'] = [] record['tags'].append(Tag.normalize(tag)) # Metiisto side does not have TS, Added `created_at` to Cartaro data if not self.opts.get('has_datestamps', True): record['created_at'] = record[ 'created_at'] if 'created_at' in record else arrow.now( Base.TIMEZONE).timestamp record['updated_at'] = None record['deleted_at'] = None self.cartaro.insert(record) count += 1 print(F"{self.m_cfg['name']} - {count}", end="\r") print(F" => Converted {count} records.")
'preserve_data': True, 'additional_tags': ['sticky note'] } }, "tags": { 'metiisto': { 'name': 'tags', 'fields': ['name'] }, 'cartaro': { 'name': 'Tags', 'fields': ['name'] }, 'options': { 'has_datestamps': False, 'name_transformer': lambda name, **kwargs: Tag.normalize(name) } }, "work_days": { 'metiisto': { 'name': 'work_days', 'fields': [ 'work_date', 'time_in', 'time_out', 'note', 'is_vacation*100+is_holiday*10+is_sick_day as type' ], }, 'cartaro': { 'name': 'WorkDays', 'fields': ['date', 'time_in', 'time_out', 'note', 'type'], },