Пример #1
0
 def nano_command(self, arg):
     oldday = self.day
     if arg == '?':
         self.print_('xs to init nanomode, xd to show day, xd+ to increase day')
     elif arg == 'd':
         self.print_('Day is {}'.format(self.day))
     elif arg == 'd+':
         self.day += 1
         self.print_('Day changed to {}'.format(self.day))
     elif re.match(r'd\d\d?$', arg):
         day = int(arg[1:])
         if day not in range(1,31):
             self.error('Invalid day')
             return
         self.day = day
         self.print_('Day changed to {}'.format(self.day))
     elif arg == 's':
         if not self.activated:
             logfile_path = self.get_logfile_path()
             if os.path.exists(logfile_path):
                 log = read_json(logfile_path)
                 self.offset = log.get('offset', 0)
             self.print_('Nano mode initiated.')
             self.activated = True
             if self.settings['override title wordcount']:
                 self.textarea.wordcount_changed.emit(self.get_wordcount()[0])
         else:
             self.error('Nano mode already running!')
     # Update the config just in case
     if self.day != oldday:
         self.settings = read_json(self.configfile)
         self.settings['day'] = self.day
         write_json(self.configfile, self.settings)
Пример #2
0
 def start_logging(self, arg):
     arg = arg.lower().strip()
     filepath = self.textarea.file_path
     if not filepath:
         self.error('Can\'t log an unnamed file! Save and try again (without offset)')
         return
     # Paths
     logdir, indexpath, relative_filepath = self.get_logpaths()
     index = get_index(indexpath)
     if not arg:
         if relative_filepath in index:
             self.print_('File is being logged.')
         else:
             self.print_('File is not being logged.')
         return
     if arg not in ('y','n'):
         self.error('Argument should be y/n! (use offset/no offset)')
         return
     if relative_filepath in index:
         self.error('File already logged!')
         return
     # Offsets
     if arg == 'y':
         offset = len(re.findall(r'\S+', read_file(self.textarea.file_path)))
     else:
         offset = 0
     # Init
     logfname = generate_logfilename(logdir, relative_filepath)
     index[relative_filepath] = logfname
     write_file(join(logdir, logfname), str(offset) + '\n')
     write_json(indexpath, index)
     self.print_('Started logging file!')
Пример #3
0
def write_metadata(entries):
    for entry in entries:
        metadata = {
            'title': entry.title,
            'description': entry.description,
            'tags': list(entry.tags)
        }
        write_json(entry.metadatafile, metadata)
Пример #4
0
def create_directory(root_path, metadata):
    """ Create directory and a default metadata file """
    default_path = os.path.join(root_path, metadata['title'])
    path = default_path
    counter = itertools.count(2)
    while os.path.exists(path):
        path = default_path + '-' + str(next(counter))
    os.mkdir(path, mode=0o755)
    write_json(os.path.join(path, 'metadata.json'), metadata)
    return path
Пример #5
0
def create_directory(root_path, metadata):
    """ Create directory and a default metadata file """
    default_path = os.path.join(root_path, metadata['title'])
    path = default_path
    counter = itertools.count(2)
    while os.path.exists(path):
        path = default_path + '-' + str(next(counter))
    os.mkdir(path, mode=0o755)
    write_json(os.path.join(path, 'metadata.json'), metadata)
    return path
Пример #6
0
def write_metadata(entries):
    for entry in entries:
        metadata = {
            'title': entry.title,
            'description': entry.description,
            'tags': list(entry.tags),
            'length': entry.length,
            'current_page': entry.current_page
        }
        write_json(entry.metadatafile, metadata)
Пример #7
0
def run_update(data, name):
    try:
        entryname = get_entry_name(data.keys(), name)
    except KeyError:
        print('Error: Can\'t find entry')
    else:
        today = date.today().strftime('%Y-%m-%d')
        data[entryname]['stats'].append(today)
        write_json(local_path('medusadata.json'), data)
        print('Updated today: {}'.format(entryname))
Пример #8
0
def run_update(data, name):
    try:
        entryname = get_entry_name(data.keys(), name)
    except KeyError:
        print('Error: Can\'t find entry')
    else:
        today = date.today().strftime('%Y-%m-%d')
        data[entryname]['stats'].append(today)
        write_json(local_path('medusadata.json'), data)
        print('Updated today: {}'.format(entryname))
Пример #9
0
 def reload_settings(self):
     settings, style, stylepath = read_config(self.configdir, self.defaultstyle)
     # TODO: FIX THIS UGLY ASS SHIT
     # Something somewhere f***s up and changes the settings dict,
     # therefor the deepcopy(). Fix pls.
     if settings != self.settings:
         if settings['title']:
             self.setWindowTitle(settings['title'])
         else:
             self.setWindowTitle('Mneme')
         self.settings = copy.deepcopy(settings)
         self.index_viewer.update_settings(settings)
         self.popuphomekey.setKey(QtGui.QKeySequence(settings['hotkeys']['home']))
     if style != self.style:
         self.style = style.copy()
         self.update_style(style)
         write_json(stylepath, style)
Пример #10
0
 def on_save(self):
     if not self.activated:
         return
     if self.settings['override title wordcount']:
         self.textarea.wordcount_changed.emit(self.get_wordcount()[0])
     logfile_path = self.get_logfile_path()
     if os.path.exists(logfile_path):
         log = read_json(logfile_path)
     else:
         log = {'days':[], 'chapters':[]}
     wc, chapters = self.get_wordcount()
     log['chapters'] = chapters
     try:
         lastwc = int(log['days'][-1].split(';')[2])
     except IndexError:
         lastwc = 0
     if wc == lastwc:
         return
     log['days'].append('{};{};{}'.format(datetime.now(), self.day, wc))
     write_json(logfile_path, log)
Пример #11
0
    def reload_settings(self):
        self.defaultstyle = read_json(local_path('defaultstyle.json'))
        self.css_template = read_file(local_path(join('templates','template.css')))
        self.index_css_template = read_file(local_path(join('templates','index_page.css')))

        settings, style, stylepath = read_config(self.configdir, self.defaultstyle)
        # TODO: FIX THIS UGLY ASS SHIT
        # Something somewhere f***s up and changes the settings dict,
        # therefor the deepcopy(). Fix pls.
        #if settings != self.settings:
        if settings['title']:
            self.setWindowTitle(settings['title'])
        else:
            self.setWindowTitle('Nomia')
        self.settings = copy.deepcopy(settings)
        self.index_viewer.update_settings(settings)
        self.popuphomekey.setKey(QtGui.QKeySequence(settings['hotkey home']))
        #if style != self.style:
        self.style = style.copy()
        self.update_style(style)
        write_json(stylepath, style)
Пример #12
0
 def new_entry(self, arg):
     """
     Main new entry method, called by the terminal.
     """
     def metadatafile(path):
         dirname, fname = os.path.split(path)
         return join(dirname, '.' + fname + '.metadata')
     file_exists = False
     tags = []
     new_entry_rx = re.match(r'\s*\(([^\(]*?)\)\s*(.+)\s*', arg)
     if not new_entry_rx:
         self.error('Invalid new entry command')
         return
     tagstr, path = new_entry_rx.groups()
     fullpath = os.path.expanduser(join(self.settings['path'], path))
     dirname, fname = os.path.split(fullpath)
     metadatafile = join(dirname, '.' + fname + '.metadata')
     if tagstr:
         tags = list({tag.strip() for tag in tagstr.split(',')})
     if exists(metadatafile):
         self.error('Metadata already exists for that file')
         return
     if exists(fullpath):
         file_exists = True
     # Fix the capitalization
     title = re.sub(r"\w[\w']*",
                    lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(),
                    os.path.splitext(fname)[0].replace('-', ' '))
     try:
         open(fullpath, 'a').close()
         write_json(metadatafile, {'title': title, 'description': '', 'tags': tags, 'length': '1', 'current_page': '0'})
     except Exception as e:
         self.error('Couldn\'t create the files: {}'.format(str(e)))
     else:
         self.reload_view()
         if file_exists:
             self.print_('New entry created, metadatafile added to existing file')
         else:
             self.print_('New entry created')
Пример #13
0
 def write_data(self):
     # TODO: attribute-specific shit here
     write_json(self._datapath, self._entries)
Пример #14
0
 def save_settings(self):
     """ Save the settings to the config file """
     config_file_path = self.paths['config_file']
     settings = {'automatic': self.auto_settings, 'manual': self.manual_settings}
     common.write_json(config_file_path, settings)
     self.write_plugin_config.emit()
Пример #15
0
def main():
    template = read_json(os.path.join(local_path('templates'), 'defaultentry-meta.json'))
    malanimelist = ET.parse(local_path('animelist-2016-05-26.xml'))
    htmldata = extract_html_data(local_path('malanimetempcache'))
    entries = parse_animelist_xml(malanimelist.getroot(), htmldata, template)
    write_json(local_path('animelisttest.json'), entries)
Пример #16
0
 def write_data(self, datapath):
     if self.dryrun:
         return
     write_json(datapath, self.entries,
                default=self.nonstandard_data_to_json)