Skip to content

Commit

Permalink
Merge pull request #259 from charmander/files-cleanup
Browse files Browse the repository at this point in the history
Clean up weasyl.files
  • Loading branch information
charmander committed Oct 1, 2017
2 parents 00382e5 + ea4fde7 commit 73f4628
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 105 deletions.
4 changes: 2 additions & 2 deletions weasyl/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ def create(userid, character, friends, tags, thumbfile, submitfile):

# Write temporary thumbnail file
if thumbsize:
files.easyupload(tempthumb, thumbfile, "image")
files.write(tempthumb, thumbfile)
thumbextension = files.get_extension_for_category(
thumbfile, macro.ART_SUBMISSION_CATEGORY)
else:
thumbextension = None

# Write temporary submission file
if submitsize:
files.easyupload(tempsubmit, submitfile, "image")
files.write(tempsubmit, submitfile)
submitextension = files.get_extension_for_category(
submitfile, macro.ART_SUBMISSION_CATEGORY)
else:
Expand Down
121 changes: 22 additions & 99 deletions weasyl/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,105 +8,49 @@

from libweasyl.constants import Category
from libweasyl.exceptions import InvalidFileFormat, UnknownFileFormat
from libweasyl.files import file_type_for_category
from libweasyl.files import file_type_for_category, makedirs_exist_ok
from libweasyl import security
from weasyl.error import WeasylError
import weasyl.define as d
import weasyl.macro as m


PATH_ROOT = m.MACRO_STORAGE_ROOT

PATH_LOG = "log/"
PATH_SAVE = "save/"
PATH_TEMP = "temp/"
PATH_CONFIG = "config/"
PATH_SEARCH = "search/"


def read(filename):
with codecs.open(filename, "r", encoding="utf-8", errors="replace") as f:
return f.read()


def ensure_file_directory(filename):
dirname = os.path.dirname(filename)
try:
os.stat(dirname)
except OSError as e:
if e.errno == errno.ENOENT:
os.makedirs(dirname)
makedirs_exist_ok(dirname)


def write(filename, content, encoding="utf-8", decode=False):
def write(filename, content):
"""
Writes content to the specified file. If the content comes from a readable
uploaded text file, set decode to True, else if it comes from a text input,
use the defaults. If the content comes from an uploaded image file, set
encoding to None.
Writes bytes to the specified file.
"""
if decode and type(content) is str:
content = content.decode("raw_unicode_escape")

ensure_file_directory(filename)

if encoding:
with codecs.open(filename, "w", encoding) as fio:
fio.write(content)
else:
with open(filename, "wb") as fio:
fio.write(content)

with open(filename, "wb") as f:
f.write(content)

def easyupload(filename, content, feature):
if feature == "text":
write(filename, content, decode=True)
elif feature == "image":
write(filename, content, encoding=None)
elif feature == "file":
write(filename, content, encoding=None)
else:
raise ValueError


def append(filename, content, encoding="utf-8", decode=False, formfeed=False):
def append(filename, content):
"""
Appends content to the specified file. If the content comes from a readable
uploaded text file, set decode to True, else use the defaults. Set
formfeed to append a formfeed character before the content.
Appends text to the specified file.
"""
if decode and type(content) is str:
content = content.decode("raw_unicode_escape")

try:
if encoding:
with codecs.open(filename, "a", encoding) as fio:
if formfeed:
fio.write("\n")

fio.write(content)
else:
with open(filename, "a") as fio:
if formfeed:
fio.write("\n")

fio.write(content)
except IOError:
return False
else:
return True
with codecs.open(filename, "a", "utf-8") as f:
f.write(content)


# Copy the specified file.
copy = shutil.copy


def remove(glob_path):
def _remove_glob(glob_path):
"""
Removes the specified file.
Removes files matching the specified pattern.
"""
if not glob_path:
return
for f in glob.glob(glob_path):
try:
os.remove(f)
Expand All @@ -120,33 +64,20 @@ def get_temporary(userid, feature):
Return the full pathname to a temporary file.
Temporary files are named so as to be owned by a user.
"""

return "{root}{temp}{userid}.{feature}.{random}".format(root=PATH_ROOT, temp=PATH_TEMP, userid=userid,
feature=feature, random=security.generate_key(20))
return "{temp}{userid}.{feature}.{random}".format(temp=m.MACRO_SYS_TEMP_PATH, userid=userid,
feature=feature, random=security.generate_key(20))


def clear_temporary(userid):
"""
Remove temporary files owned by a user.
"""

remove("{root}{temp}{userid}.*".format(root=PATH_ROOT, temp=PATH_TEMP, userid=userid))


def makedirs(path):
"""
Make the full hash path to a resource directory on the file system.
"""
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
_remove_glob("{temp}{userid}.*".format(temp=m.MACRO_SYS_TEMP_PATH, userid=userid))


def make_character_directory(target):
path = d.get_character_directory(target)
makedirs(path)
makedirs_exist_ok(path)


def make_resource(userid, target, feature, extension=None):
Expand All @@ -169,30 +100,22 @@ def make_resource(userid, target, feature, extension=None):
return os.path.join(root, filename)


feature_typeflags = {
"thumb": "-",
_feature_typeflags = {
"submit": "=",
"cover": "~",
"avatar": ">",
"banner": "<",
"propic": "#"
}

extension_typeflags = {
_extension_typeflags = {
".jpg": "J",
".png": "P",
".gif": "G",
".txt": "T",
".htm": "H",
".mp3": "M",
".swf": "F",
".pdf": "A"
}


def typeflag(feature, extension):
symbol = feature_typeflags.get(feature, "=")
letter = extension_typeflags.get(extension)
return symbol + letter if letter else ""
symbol = _feature_typeflags[feature]
letter = _extension_typeflags[extension]
return symbol + letter


_categories = {
Expand Down
2 changes: 1 addition & 1 deletion weasyl/journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from weasyl import ignoreuser
from weasyl import macro as m
from weasyl import media
from weasyl import moderation
from weasyl import profile
from weasyl import report
from weasyl import searchtag
Expand Down Expand Up @@ -302,7 +303,6 @@ def edit(userid, journal, friends_only=False):
[journal.title, journal.content, journal.rating.code, settings, journal.journalid])

if userid != query[0]:
from weasyl import moderation
moderation.note_about(
userid, query[0], 'The following journal was edited:',
'- ' + text.markdown_link(journal.title, '/journal/%s?anyway=true' % (journal.journalid,)))
Expand Down
6 changes: 3 additions & 3 deletions weasyl/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def _upload_char(userid, filedata, charid):

filename = d.url_make(charid, "char/.thumb", root=True)

files.write(filename, filedata, encoding=None)
files.write(filename, filedata)

if image.check_type(filename):
image.make_cover(filename)
else:
files.remove(filename)
os.remove(filename)
raise WeasylError("FileType")


Expand Down Expand Up @@ -114,7 +114,7 @@ def _create_char(userid, x1, y1, x2, y2, charid, config=None, remove=True):
thumb = images.make_thumbnail(im, bounds)
thumb.write(dest, format=images.image_file_type(thumb))
if remove:
m.os.remove(filename)
os.remove(filename)


def create(userid, x1, y1, x2, y2, submitid=None, charid=None,
Expand Down

0 comments on commit 73f4628

Please sign in to comment.