diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2022-12-12 19:00:42 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2022-12-12 19:00:42 -0600 |
commit | 585ebb88138ccf9d72decd897e0ad5fa00f36479 (patch) | |
tree | 9c766a4f32567012656dc593e28ddf35ec4fdb6c | |
parent | 35ff363e779149b6364901a53dd1eefe89ca5ddb (diff) |
remove force feature, unnecessary complexity
-rw-r--r-- | src/pyssg/arg_parser.py | 4 | ||||
-rw-r--r-- | src/pyssg/builder.py | 26 | ||||
-rw-r--r-- | src/pyssg/database.py | 34 | ||||
-rw-r--r-- | src/pyssg/database_entry.py | 7 | ||||
-rw-r--r-- | src/pyssg/md_parser.py | 24 | ||||
-rw-r--r-- | src/pyssg/page.py | 2 | ||||
-rw-r--r-- | src/pyssg/pyssg.py | 1 |
7 files changed, 24 insertions, 74 deletions
diff --git a/src/pyssg/arg_parser.py b/src/pyssg/arg_parser.py index b5d47b2..5b66697 100644 --- a/src/pyssg/arg_parser.py +++ b/src/pyssg/arg_parser.py @@ -30,10 +30,6 @@ def get_parser() -> ArgumentParser: help='''generates all HTML files by parsing MD files present in source directory and copies over manually written HTML files''') - parser.add_argument('-f', '--force', - action='store_true', - help='''force building all pages and not only the - updated ones''') parser.add_argument('--debug', action='store_true', help='''change logging level from info to debug''') diff --git a/src/pyssg/builder.py b/src/pyssg/builder.py index f7537d6..582eb74 100644 --- a/src/pyssg/builder.py +++ b/src/pyssg/builder.py @@ -58,9 +58,7 @@ class Builder: self.html_files: list[str] # files and pages are synoyms - # TODO: include updated_tags when when implemented self.all_files: list[Page] - self.updated_files: list[Page] self.all_tags: list[tuple[str, str]] self.common_vars: dict @@ -94,7 +92,6 @@ class Builder: # just so i don't have to pass these vars to all the functions self.all_files = parser.all_files - self.updated_files = parser.updated_files self.all_tags = parser.all_tags # TODO: check if need to pass dirs.dir_path.files @@ -147,31 +144,14 @@ class Builder: for file in self.html_files: src_file = os.path.join(self.dir_cfg['src'], file) dst_file = os.path.join(self.dir_cfg['dst'], file) - # always copy on force - if self.config['info']['force']: - log.debug('copying "%s"; forced', file) - copy_file(src_file, dst_file) - continue - # only copy files if they have been modified (or are new) - if self.db.update(src_file, remove=f'{self.dir_cfg["src"]}/'): - log.debug('copying "%s"; has been modified or is new', file) - copy_file(src_file, dst_file) - continue - log.debug('ignoring "%s"; hasn\'t been modified, not forced', file) + log.debug('copying "%s"', file) + copy_file(src_file, dst_file) def __render_pages(self, template_name: str) -> None: log.debug('rendering pages with template "%s"', template_name) page_vars: dict = deepcopy(self.common_vars) - temp_pages: list[Page] - # check if only updated should be created - if self.config['info']['force']: - log.debug('all html will be rendered, force is set to true') - temp_pages = self.all_files - else: - log.debug('only updated or new html will be rendered') - temp_pages = self.updated_files - for p in temp_pages: + for p in self.all_files: p_fname: str = p.name.replace('.md', '.html') log.debug('adding page "%s" to exposed vars for jinja', p_fname) page_vars['page'] = p diff --git a/src/pyssg/database.py b/src/pyssg/database.py index d9c6467..40065ef 100644 --- a/src/pyssg/database.py +++ b/src/pyssg/database.py @@ -19,31 +19,25 @@ class Database: self.db_path: str = db_path self.e: dict[str, DatabaseEntry] = dict() - # updates the tags for a specific entry (file) - # file_name only contains the entry name (not an absolute path) - # TODO: make the function return true/false if updated def update_tags(self, file_name: str, new_tags: list[str]) -> None: # technically, I should ensure this function can only run if self.e is populated if file_name in self.e: log.debug('updating tags for entry "%s"', file_name) - log.debug('entry "%s" old content: %s', + log.debug('entry "%s" old tags: %s', file_name, self.e[file_name]) self.e[file_name].update_tags(new_tags) - log.debug('entry "%s" new content: %s', + log.debug('entry "%s" new tags: %s', file_name, self.e[file_name]) else: log.error('can\'t update tags for entry "%s",' ' as it is not present in db', file_name) sys.exit(1) - # returns a bool that indicates if the entry - # was (includes new entries) or wasn't updated def update(self, file_name: str, - remove: str='') -> bool: + remove: str='') -> None: log.debug('updating entry for file "%s"', file_name) - # initial default values f: str = file_name tags: list[str] = [] if remove != '': @@ -52,20 +46,20 @@ class Database: # get current time, needs actual file name time: float = os.stat(file_name).st_mtime - log.debug('modified time for "%s": %s', file_name, time) + log.debug('time for "%s": %s', file_name, time) # calculate current checksum, also needs actual file name checksum: str = get_checksum(file_name) - log.debug('current checksum for "%s": "%s"', file_name, checksum) + log.debug('checksum for "%s": "%s"', file_name, checksum) - # two cases, 1) entry didn't exist, + # three cases, 1) entry didn't exist, # 2) entry has been mod and, # 3) entry hasn't been mod #1) if f not in self.e: log.debug('entry "%s" didn\'t exist, adding with defaults', f) self.e[f] = DatabaseEntry([f, time, 0.0, checksum, tags]) - return True + return # old_e is old entity old_e: DatabaseEntry = self.e[f] @@ -73,25 +67,21 @@ class Database: # 2) if checksum != old_e.checksum: - if old_e.mtimestamp == 0.0: - log.debug('entry "%s" has been modified for the first' - ' time, updating', f) - else: - log.debug('entry "%s" has been modified, updating', f) + log.debug('entry "%s" has been modified, updating', f) self.e[f] = DatabaseEntry([f, old_e.ctimestamp, time, checksum, tags]) log.debug('entry "%s" new content: (%s, %s, %s, (%s))', f, self.e[f]) - return True + return # 3) else: log.debug('entry "%s" hasn\'t been modified', f) - return False + return def write(self) -> None: log.debug('writing db') with open(self.db_path, 'w') as file: + csv_writer = csv.writer(file, delimiter=self.__COLUMN_DELIMITER) for _, v in self.e.items(): log.debug('writing row: %s', v) - csv_writer = csv.writer(file, delimiter=self.__COLUMN_DELIMITER) csv_writer.writerow(v.get_raw_entry()) def _db_path_exists(self) -> bool: @@ -101,7 +91,6 @@ class Database: ' created once process finishes,' ' ignore if it\'s the first run', self.db_path) return False - if not os.path.isfile(self.db_path): log.error('"%s" is not a file"', self.db_path) sys.exit(1) @@ -115,6 +104,7 @@ class Database: log.debug('db contains %d rows', len(rows)) return rows + # TODO: don't include files that are not in the db anymore def read(self) -> None: log.debug('reading db') if not self._db_path_exists(): diff --git a/src/pyssg/database_entry.py b/src/pyssg/database_entry.py index 90c3f5f..1674a7c 100644 --- a/src/pyssg/database_entry.py +++ b/src/pyssg/database_entry.py @@ -20,15 +20,15 @@ class DatabaseEntry: if entry[4] != '-': self.tags = entry[4].split(',') - log.debug('"%s" tag: [%s]', self.fname, ', '.join(self.tags)) + log.debug('"%s" tags: %s', self.fname, self.tags) def __str__(self) -> str: - _return_str: str = '[{}, {}, {}, {}, [{}]]'\ + _return_str: str = '[{}, {}, {}, {}, {}]'\ .format(self.fname, self.ctimestamp, self.mtimestamp, self.checksum, - ', '.join(self.tags)) + self.tags) return _return_str # used for csv writing @@ -39,6 +39,5 @@ class DatabaseEntry: self.checksum, ','.join(self.tags) if self.tags else '-'] - # TODO: make the function return true/false if updated def update_tags(self, new_tags: list[str]) -> None: self.tags = new_tags diff --git a/src/pyssg/md_parser.py b/src/pyssg/md_parser.py index 14544a3..8751035 100644 --- a/src/pyssg/md_parser.py +++ b/src/pyssg/md_parser.py @@ -53,21 +53,16 @@ class MDParser: self.db: Database = db self.md: Markdown = _get_md_obj() - # TODO: include updated_tags when when implemented self.all_files: list[Page] = [] - # updated and modified are synonyms here - self.updated_files: list[Page] = [] self.all_tags: list[tuple[str, str]] = [] def parse_files(self) -> None: log.debug('parsing all files') - for f in self.files: + for i, f in enumerate(self.files): log.debug('parsing file "%s"', f) src_file: str = os.path.join(self.dir_config['src'], f) log.debug('path "%s"', src_file) - # get flag if update is successful - # update is only used to get a separate list of only updated files - file_updated: bool = self.db.update(src_file, remove=f'{self.dir_config["src"]}/') + self.db.update(src_file, remove=f'{self.dir_config["src"]}/') log.debug('parsing md into html') content: str = self.md.reset().convert(open(src_file).read()) @@ -81,19 +76,11 @@ class MDParser: self.dir_config) page.parse_metadata() - # keep a separated list for all and updated pages - if file_updated: - log.debug('has been modified, adding to mod file list') - self.updated_files.append(page) log.debug('adding to file list') self.all_files.append(page) - # parse tags - # TODO: only parse tags if set in config - # TODO: separate all tags and only updated tags - if page.tags is not None: - log.debug('parsing tags') - # add its tag to corresponding db entry if existent + if self.dir_config['tags'] and page.tags is not None: + log.debug('parsing tags for "%s"', f) self.db.update_tags(f, list(map(itemgetter(0), page.tags))) log.debug('add all tags to tag list') @@ -107,9 +94,8 @@ class MDParser: log.debug('no tags to parse') log.debug('sorting all lists for consistency') - self.all_tags.sort(key=itemgetter(0)) - self.updated_files.sort(reverse=True) self.all_files.sort(reverse=True) + self.all_tags.sort(key=itemgetter(0)) pages_amount: int = len(self.all_files) # note that prev and next are switched because of the diff --git a/src/pyssg/page.py b/src/pyssg/page.py index 2a6fce7..7f8c542 100644 --- a/src/pyssg/page.py +++ b/src/pyssg/page.py @@ -143,7 +143,7 @@ class Page: ' "default_image" set in the config file') # if contains open graph elements - # TODO: better handle thsi part + # TODO: better handle this part try: # og_e = object graph entry og_elements: list[str] = self.meta['og'] diff --git a/src/pyssg/pyssg.py b/src/pyssg/pyssg.py index 94584aa..d158caa 100644 --- a/src/pyssg/pyssg.py +++ b/src/pyssg/pyssg.py @@ -80,7 +80,6 @@ def main() -> None: config['info'] = dict() config['info']['version'] = static_config['info']['version'] config['info']['debug'] = str(args['debug']) - config['info']['force'] = str(args['force']) if args['init']: log.info('initializing the directory structure and copying over templates') |